Paypal-checkout-components: Can smart buttons create a subscription and the server activate it?

Created on 26 Feb 2020  路  15Comments  路  Source: paypal/paypal-checkout-components

I am using smart buttons for subscriptions on the client. I send the subscription id from the onApprove block to the server and the server calls the paypal api to check if the subscription is active and update the db.

The issue is that once the user approves the subscription on the client, the payment has already been captured before I get the subscription id to send to the backend.

I was wondering if there is a way to use smart button on the client but capture the subscription on the server?

Thank you

Most helpful comment

@ahmadabdolsaheb You can use below code snippet to avoid inline activate.
Docs https://developer.paypal.com/docs/api/subscriptions/v1/#definition-application_context

paypal.Buttons({

    createSubscription: function (data, actions) {
        return actions.subscription.create({
            'plan_id': 'P-XXXXXXXXXXXXXXXXXX',
            'application_context': {
                'user_action': 'CONTINUE' // By default its SUBSCRIBE_NOW
            }
        });
    },

    onApprove: function (data, actions) {
        alert('Approved By Buyer')
        // use below code to activate/make first payments (if any)
        // or call your server using fetch / axios / XHR etc. 
        // Must return a promise. 
        return actions.subscription.activate().then(res => alert('Activated by Merchant'))
    }
}).render('#paypal-buttons');

All 15 comments

@blakejoy, you are the domain expert. I was wondering if you know a solution to this query? thank you

Well you can look into webhooks or IPN to capture the specific event needed to get the data you need

Thanks for the quick reply,@blakejoy.
Correct, I can get notifications after creation and capture of a subscription, but I would like to add a logic ( lets say check my db) and capture subscriptions selectively after the user's approval.
do you think that is possible using smart buttons?

@ahmadabdolsaheb You can use below code snippet to avoid inline activate.
Docs https://developer.paypal.com/docs/api/subscriptions/v1/#definition-application_context

paypal.Buttons({

    createSubscription: function (data, actions) {
        return actions.subscription.create({
            'plan_id': 'P-XXXXXXXXXXXXXXXXXX',
            'application_context': {
                'user_action': 'CONTINUE' // By default its SUBSCRIBE_NOW
            }
        });
    },

    onApprove: function (data, actions) {
        alert('Approved By Buyer')
        // use below code to activate/make first payments (if any)
        // or call your server using fetch / axios / XHR etc. 
        // Must return a promise. 
        return actions.subscription.activate().then(res => alert('Activated by Merchant'))
    }
}).render('#paypal-buttons');

@ravishekhar oh this is beautiful. Thank you :)

I should post to
https://api.sandbox.paypal.com/v1/billing/subscriptions/sub-id/activate
from the server to activate it, correct?

Yes you can use the activate api, details here https://developer.paypal.com/docs/api/subscriptions/v1/#subscriptions_activate

On the same note, can you please help us understand the use case why you do not want to activate the subscription while buyer is still on paypal.com

I just needed to make sure the user has not donated and is signed in.

This is the best thread I have found on the internet about how to make smart buttons work with subscriptions.

@ravishekhar

Here's why: unless I'm missing something, if PayPal activates a subscription on Paypal.com, but the users connection goes out before the onApprove callback makes a request to our server to connect the subscription ID with our user ID, you have an orphaned subscription.

This happens to use several times a week, and we have to manually find and remove these.

It looks like it doesn't work now. At least for sandbox.
Even when I set

'application_context': {
     'user_action': 'CONTINUE'
}

it still shows "Subscribe now" button and the subscription is automatically captured.

But what should I do with situations when the user loses connection or closes the window right after completing steps on Paypal but before saving subscription id in my DB?

@blakejoy I think it would be interesting to answer the above question. I think a lot of us are trying to implement a robust application.

@blakejoy I think it would be interesting to answer the above question. I think a lot of us are trying to implement a robust application.

I haven鈥檛 worked with the api for quite some time but I am definitely interested in the discussion @sebRomeo

@blakejoy maybe you could point out some staff that could answer ?
Here is a stackoverflow question about this case, if anyone is interrested in answering :)

https://stackoverflow.com/questions/67270378/how-to-confirm-billing-of-a-monthly-subscription-in-paypal-api-via-webhook

Let me take a look. I don鈥檛 know any staff. I was working with this as a side project before lol @sebRomeo

Was this page helpful?
0 / 5 - 0 ratings

Related issues

stevedeighton picture stevedeighton  路  6Comments

bluepnume picture bluepnume  路  3Comments

MrMooky picture MrMooky  路  3Comments

stephen-last picture stephen-last  路  6Comments

webdeb picture webdeb  路  3Comments