- Create a PayPal REST API Application
- Configure Webhooks
- Use the Webhooks Simulator
Visit the PayPal Developer Dashboard and click “create app”.
Back in a terminal, you’ll need to add the paypal client id and secret obtained in the above step:
EDITOR=vim rails credentials:edit
and populate the file with:
paypal: client_id: abc123 secret: xyz987
Open a rails console and create a product:
product = PayPal::Product.create!(name: 'Example product', description: 'example product description')
In the same rails console session, create a plan:
plan = PayPal::Plan.create!(product_id: product.id, name: 'Example plan', description: 'example plan description')
This application will modify a user’s subscription status when that user cancels a plan on paypal.com.
Only the first webhook is used for verification. See
for more details about how incoming webhooks are verified. Notably,
webhooks.first is present.
Visit the developer dashboard
and click on the application you created earlier. Click “add webhook”. Set the
webhook url to
At the time of this writing (2023-03), only the
type is used to modify a user’s subscription status in the application.
The PayPal Webhooks Simulator
allows you to send test events like
BILLING.SUBSCRIPTION.CANCELLED to a
staging or local environment to simulate buyer behaviors on paypal.com.
You can use
ngrok to generate a public webhook url:
ngrok http 3000 # Forwarding https://123a-456-789-012-345.ngrok.io -> http://localhost:3000
For convenience, there is a commented out line in
config/environments/development.rb that allows incoming requests from
# config/environments/development.rb config.hosts << /[a-z0-9\-]+\.ngrok\.io/
It might be useful to click around the application UI and see which events are triggered by paypal. Use the following code to write a mock for each received webhook.
File.write( "spec/mocks/paypal-webhook-" + params["event_type"] + ".json", JSON.pretty_generate(params.to_unsafe_hash) )