skip to Main Content

I need integrate paypal subscriptions to my project.
I use new paypal subscriptions api https://developer.paypal.com/docs/api/subscriptions/v1/#subscriptions and need to know how i can identify payer and my own db user.

Here is scenario in my mind:

  1. I create my own db user with signup form.
  2. I set my own user id to paypal api call
  3. After user confirm subscription paypal return me subscriptions id, then i call paypal api to get details of returned subscription and see my own db user id

But looks like paypal cant provide it.

here is my smart checkout button code

    paypal.Buttons({
        style: {
            layout: 'horizontal',
            size: 'small',
            color:  'blue',
            label:  'pay',
            height: 55,
            tagline: 'false'
        },
        createSubscription: function(data, actions) {
            return actions.subscription.create({
                'plan_id': 'P-5GS67390M7258253CLUHXAHQ',
                'metadata' : {
                    'user_id' : 'myuserid'
                }
            });

        },
        onApprove: function(data, actions) {
            console.log(data);
            alert('You have successfully created subscription ' + data.subscriptionID);

        }

    }).render('#paypal-button-container');
</script>

I found only one way how i can do it

  1. User insert all data to signup form and then click on paypal button
  2. After success paypal payment on

        onApprove: function(data, actions) {
            $('#myform').append('<input name="subscriptions_id" value="data.subscriptionID"')
            $('#myform').submit()
            alert('You have successfully created subscription ' + data.subscriptionID);
    
        }
    

What is the best way to add subscriptions id for mysql db user?

2

Answers


  1. The solution in the question is correct, the subscription-id should be stored and associated with the internal user-id at creation time, which for subscriptions occurs right after approval (in the onApprove function)

    The fetch API can be used instead of a form post to communicate this subscription-id to the server, along with the user-id to associate it with — similar to the examples in https://developer.paypal.com/demo/checkout/#/pattern/server

    Login or Signup to reply.
  2. The solution in the question is one possible way. The problem with it is that if the client goes down before sending the data to the server, the association between user_id and subscription_id is lost.

    The way around it is to send the user_id to paypal as custom_id. That custom_id is then a part of the subscription data, retained on the payapl server, shown in the paypal billing UI for the subscriptiion details. I do not yet know it for sure, but strongly suspect it is also sent to the webhook.
    see PayPal API documentation for creating a subscription

    So ideally combine both. Have a webhook to associate the custom_id (your user_id) with the subscription. Have the API call (form post) like you do. If either way fails, you have the fallback.

    UPDATE 2021-09-02: I myself use that approach in our web billing solution. One thing I noticed, statistically, it is a very rare event that the web client goes down before getting a response back. I know it because webhook calls come from hours to days after the event. After several hundreds of subscriptions that I analyzed, I have seen not a single case where the webhook came to patch up a missing transaction.

    I do not know why it is so. It certainly exceeded my expectations.
    My guesses are 1. PayPal server replies fast 2. my PWA is written well enough to warn user to wait a bit not close the browser window until the confirmation screen.

    Still I recommend to implement webhook. It would be needed for all the other cases where user cancels subscription directly on the paypal, or such other events.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search