0

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?

Andrew Johns
  • 705
  • 1
  • 6
  • 26

1 Answers1

1

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.

Preston PHX
  • 27,642
  • 4
  • 24
  • 44
  • The only downside I see for this is that there is a delay in the webhook receiving the details and updating the local database. So any reload of the page would still show the account in its original state prior to the local account status being changed. – Andrew Johns Jul 08 '22 at 12:08
  • 1
    You can also have the client side onApprove event do fetch to a route on your server that then queries the status of the subscription with an API call https://developer.paypal.com/docs/api/subscriptions/v1/#subscriptions_get , updating your status record if it returns active. So either that or the webhook, whichever occurs first, will change the state. – Preston PHX Jul 08 '22 at 14:32