如何使用Paypal客户端休息API创建订阅付款?

时间:2017-06-17 13:54:57

标签: paypal payment-gateway paypal-rest-sdk

Client-side REST integration文档介绍了如何为一个或多个项目创建快速结帐。

我如何使用相同的方式创建订阅或定期付款?如何修改以下内容?

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

我找到了类似的Rest api for Node。不知道JS会怎么样。

1 个答案:

答案 0 :(得分:1)

首先,您需要制定结算方案:

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")

这里使用的属性具有字面含义。现在,既然您已经创建了一个计费计划,那么您需要为用户提供一些界面,以便他们可以订阅它。以下是此示例代码:

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

在最后一行,您将获得可以提供给用户的批准链接。 接下来,您必须设置一个端点,如果用户批准付款,该端点将成为回调网址。

billing_agreement_response = BillingAgreement.execute(payment_token)

payment_token由paypal发送到您的回拨网址。