Search code examples
node.jspaypalpaypal-subscriptionspaypal-webhooks

paypal subscription button and storing to db flow


I'm attempting a Paypal subscription flow in Node, bug all of the various SDKs seem to be deprecated now, and I'm struggling to find a current example of implementing a subscription button and then validating the subscription and updating the user's status in local db.

I think the expected flow in 2022 is to implement a standard button and use a webhook to listen out for the event, at which point you verify the payload, and update a local DB to reflect the new subscription status.

I guess this means that in the OnApprove clientside method of the button, we could run some sort of polling against the local DB to check that the subscription status has changed?


Solution

  • The onApprove callback already receives data indicating the subscription has been approved. This is sufficient for client-side presentment of the result (not to be used for database operations, as it is client-side).

    For storing the result in a database, listen for the webhook PAYMENT.SALE.COMPLETED. This webhook will trigger for the first payment, as well as every future payment on the subscription. It is the only webhook you need to be concerned about, all subscription logic can be driven solely by PAYMENT.SALE.COMPLETED.

    To aid in reconciliation, add a custom_id parameter when creating the subscription. Its value will be returned in webhooks events for the subscription.