Search code examples
paypalpayment-gatewaypaypal-rest-sdk

How to create a Subscription payment using Paypal client-side rest api?


The Client-side REST integration documentaion describes about creating a express checkout for one or more items.

How can i use the same for creating a subscription or Recurring payment? How should the following be modified?

 payment: function(data, actions) {
        return actions.payment.create({
            transactions: [
                {
                    amount: { total: '1.00', currency: 'USD' }
                }
            ]
        });
    },

I found a similar Rest api for Node. Not sure how it would be on JS.


Solution

  • First you need to create a billing plan:

    billing_plan_attributes = {
                    "name": PLAN_NAME_HERE,
                    "description": PLAN_DESCRIPTION,
                    "merchant_preferences": {
                        "auto_bill_amount": "yes", # yes if you want auto bill 
                        "cancel_url": "http://www.cancel.com", # redirect uri if user cancels payment
                        "initial_fail_amount_action": "continue",
                        "max_fail_attempts": "1",
                        "return_url": RETURN_URL,
                        "setup_fee": {
                            "currency": CURRENCY,
                            "value": VALUE # how much do you want to charge
                        }
                    },
                    "payment_definitions": [
                        {
                            "amount": {
                                "currency": request.form['currency'],
                                "value": request.form['amount']
                            },
    
                            "cycles": CYCLES, # how much time this subscription will charge user
                            "frequency": FREQ, # month, day
                            "frequency_interval": INTERVAL, # per month or per three month or so on
                            "name": NAME,
                            "type": TYPE
                        }
                    ],
                    "type": TYPE
                }
                billing_plan = BillingPlan(billing_plan_attributes)
                if billing_plan.create():
                    print("success")
    

    The attributes used have literal meaning here. Now since you have created a billing plan you need to give users some interface so that they can subscribe with it. Below is a sample code for this:

    billing_agreement = BillingAgreement({
                "name": "Organization plan name",
                "description": "Agreement for " + request.args.get('name', ''),
                "start_date": (datetime.now() + timedelta(hours=1)).strftime('%Y-%m-%dT%H:%M:%SZ'),
                "plan": {
                    "id": request.args.get('id', '')
                },
                "payer": {
                    "payment_method": "paypal"
                },
                "shipping_address": {
                    "line1": "StayBr111idge Suites",
                    "line2": "Cro12ok Street",
                    "city": "San Jose",
                    "state": "CA",
                    "postal_code": "95112",
                    "country_code": "US"
                }
            })
            if billing_agreement.create():
                for link in billing_agreement.links:
                    if link.rel == "approval_url":
                        approval_url = link.href
    

    In the last line you get the approval link which can be given to user. Next you have to setup an endpoint which will be the callback url if user approves the payment.

    billing_agreement_response = BillingAgreement.execute(payment_token)
    

    payment_token is sent by paypal to your callback url.