Skip to main content Link Menu Expand (external link) Copy Copied

Payments Configuration

Create a PayPal REST API Application

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

Create a Product

Open a rails console and create a product:

product = PayPal::Product.create!(name: 'Example product', description: 'example product description')

Create a Plan

In the same rails console session, create a plan:

plan = PayPal::Plan.create!(product_id: product.id, name: 'Example plan', description: 'example plan description')

Configure Webhooks

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 WebhooksController#create for more details about how incoming webhooks are verified. Notably, webhooks.first is present.

Using the Rails Console

PayPal::Webhook.create("https://saas-starter.app/webhooks")

Using the PayPal Developer Dashboard

Visit the developer dashboard and click on the application you created earlier. Click “add webhook”. Set the webhook url to https://saas-starter.app/webhooks.

At the time of this writing (2023-03), only the BILLING.SUBSCRIPTION.CANCELLED event type is used to modify a user’s subscription status in the application.

Use the Webhooks Simulator

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.

Using ngrok for Local Webhook Testing

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 ngrok:

# config/environments/development.rb
config.hosts << /[a-z0-9\-]+\.ngrok\.io/

Generate More Webhook Mocks

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)
)