Search code examples
paypalpaypal-sandboxpaypal-rest-sdkpaypal-subscriptions

PayPal subscription API onApprove


I'm trying to implement the PayPal Subscription API but I'm not sure about the process flow.
I use the react-paypal-js package to generate the subscription button.
The way the users are created on my website is only through the Paypal subscription.

The process is as follow:

  1. The user choose a plan and click on the subscribe button
  2. The PayPal window open and the user follows the subscription process on Paypal
  3. The user is redirected to my website
  4. Using webhooks or with the onApprove callback, I receive the newly created subscription, create a user on my website with the Paypal email address associated with the subscription.

It all works. My problem is that I need to check, before accepting the subscription, if I already have a local user with given email and in this case, I don't want to accept the subscription (as I would then have 2 subscriptions for the same user) and display a message saying that their is already an active (or suspended) subscription associated with the given email.

As far as I can tell, when the onApprove is called or the webhook is called, the subscription has already been processed and activated.

Thanks!


Solution

  • You don't have to design a system in which the PayPal email used to make the payment, and the email of the user used to log into your site, have a 1:1 relationship. Instead you can have the user log and create their account with you first (as they will always need that to manage something subscription-based) and then you will know there is no subscription associated with that user before displaying the PayPal Subscribe button. Then they can pay with any PayPal account (might be the same email, might be different)


    As far as I can tell, when the onApprove is called or the webhook is called, the subscription has already been processed and activated.

    That is the default behavior, yes. Per the above it shouldn't be ncessary, but you can change the subscription to start in an inactive state, and show a final review step on your site that will activate it via API. This is controlled by setting application_context -> user_action -> CONTINUE (vs default SUBSCRIBE_NOW). See the API reference for subscription create, and you would of course need to use the subscription activate API call as a final step after confirmation.