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

How do you know if a user has paid for a subscription


I am looking at this tutorial to create my own paypal subscription gateway and I have never been so confused and lost about an API before.

So when we create the plan we set merchant preferences, specifically the return URL

$merchantPreferences->setReturnUrl("mysite.com/subscription?success=true")

Then at the end of the tutorial, when a user has paid for the service it redirects the user to mysite.com/subscription?success=true.

Then there is code which grabs the MOST important thing, the token from the return URL $token = $_GET['token']; which allows us to get the agreement ID using $agreement->execute($token, $apiContext);. My question is, there is a huge possibility that the user pays for the subscription and then closes their tab and doesn't get redirected. At that point, my system hasn't recognized their payment and activated their subscription on my end even though they paid for their subscription already.

Am I wrong here and misunderstanding whats happening in the Paypal API or is the paypal system flawed?


Solution

  • That tutorial uses a deprecated SDK that only works with the previous version of PayPal Subscriptions. Do not follow anything in that tutorial, it is no good.


    For the current version of PayPal Subscriptions, the button described there will activate the subscription on the client side.

    You can instead set up your button and use a REST API call to activate a subscription from your server immediately after approval. Activating from the server gives you a reliable server-side notification that a subscription is started. There is no SDK for these calls, you must first get the API access token and perform the call yourself, via HTTPS.

    However, to be notified of actual payments for the subscription -- which typically start the following day and repeat on its cycle -- you need a webhook listener for the event PAYMENT.SALE.COMPLETED. There is no supported SDK for webhooks, so you must set up your own listening code and also perform any desired verification of webhooks messages received yourself (verification can be done by checking the signature, or an API call back to PayPal)

    If you need to receive a payment the moment a subscription starts (instead of the following day), configure it with a setup_fee and a one cycle trial period before the regular billing cycle begins.