【问题标题】:How to create a Subscription payment using Paypal client-side rest api?如何使用 Paypal 客户端 rest api 创建订阅付款?
【发布时间】:2017-11-20 04:29:27
【问题描述】:

Client-side REST integration 文档描述了如何为一件或多件商品创建快速结账。

我如何使用它来创建订阅或定期付款?下面应该如何修改?

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

我发现了一个类似的Rest api for Node。不知道在 JS 上会怎样。

【问题讨论】:

  • 客户端集成不用于订阅目的。您必须使用服务器端实现才能创建订阅计划。服务器端也很有意义,因为您总是希望存储您收到的金额或到期金额的详细信息以及更多详细信息
  • 仅供参考:您提供的链接具有服务器端实现。
  • 你是对的。但我无法弄清楚如何更改 Server side request 中使用的 JSON 以便创建订阅。
  • 我不知道有任何文档。
  • 你想只使用rest api还是可以使用客户端库?

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


【解决方案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

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

billing_agreement_response = BillingAgreement.execute(payment_token)

payment_token 由 paypal 发送到您的回调 url。

【讨论】:

  • PayPal 设置费用和 json 中的金额有什么区别。
  • 这适用于您想收取一次性注册费或会员费等一次性设置费的情况。
猜你喜欢
  • 2014-05-24
  • 2016-04-28
  • 2013-09-03
  • 2017-07-13
  • 1970-01-01
  • 2013-08-29
  • 2017-10-04
  • 2013-07-04
  • 2014-04-27
相关资源
最近更新 更多