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

How to activate a PayPal subscription?


I've implemented a recurring payments system using PayPal and the SmartButtons. The customer is able to create the subscription through my system, at the end of the checkout I get the subscription created:

{
    "orderID": "3JR7411152833961C",
    "subscriptionID": "I-J8CHJ9UKB8JU",
    "facilitatorAccessToken": "A21AAElq71jrRrKHzaxxOeA4o7cOie83F_N-LKMZoAe2mhmaANy-a784yj9DUbSlQPIxtu_O-7XyzHWab23gKVgwhqK9Hjaow"
} 

in order to activate the subscription and get the payment I need to execute it, so I wrote this method:

let executeAgreement = (paymentToken) => {

    paypal.billingAgreement.execute(paymentToken, {}, function (error, billingAgreement) {
        if (error) {
            console.log(error);
            throw error;
        }
        else {
            console.log('Billing Agreement Execute Response');
            console.log(JSON.stringify(billingAgreement));
        }
    });
}

the problem's that I got:

response: { name: 'BUSINESS_VALIDATION_ERROR', debug_id: '82426af46aee4', message: 'Validation Error.', information_link: 'https://developer.paypal.com/docs/api/payments.billing-agreements#errors', details: [ [Object] ], httpStatusCode: 400 }, httpStatusCode: 400 }

I send to executeAgreement the subscriptionId, but I guess the problem it's just that, in the created subscription I only reiceve the id of the subscription not the paymentToken, how can I fix?

Essentially: how can I execute/activate the subscription if I've only the subscription id returned by the following method:

 opts.createSubscription = function (data, actions) {
     that.step = that.steps.PAYPAL_EXTERNAL_WINDOW;
          return actions.subscription.create({
              plan_id: that.paymentData.plan.id,
              application_context: {
              user_action: "CONTINUE",
              shipping_preference: 'NO_SHIPPING'
          }
     });
}

the method above return orderId - subscriptionId - facilitatorAccessToken, seems I'm not able to activate the subscription id after the user as approved the recurring payment through the smart payment buttons.


Solution

  • Actually, I-J8CHJ9UKB8JU is already a created and active subscription and you don't need to execute anything.

    The notion of executing a billing agreement is something from old documentation that predates the Subscriptions API.

    In the current Sbscriptions API, you would simply create and activate a subscription:

    https://developer.paypal.com/docs/api/subscriptions/v1/#subscriptions_create

    Followed by either a redirection to an approval URL, or use of the Smart Payment Button (which is much better, since it opens up an "in context" approval -- no need for any redirect)

    So anyway, you have your active Subscription ID: I-J8CHJ9UKB8JU

    Now just save this object and associate it with your customer object.