通过 Paypal Express Checkout REST API 定期付款
Posted
技术标签:
【中文标题】通过 Paypal Express Checkout REST API 定期付款【英文标题】:Recurring payments through Paypal's Express Checkout REST API 【发布时间】:2017-09-02 05:03:52 【问题描述】:我的目标是使用“paypal”作为我们 SaaS 的付款方式来设置 6 个月和 12 个月的定期订阅。我正在使用其余 API,我找不到的一件事是如何使用 PayPal 的其余 API(我读过的是现在可能)。这就是我们所处的位置:
我有一个正在使用的advanced server integration,用于使用 Paypal 的 Express Checkout REST API 进行付款。
我按照上面链接中解释的代码示例以及来自贝宝的this example 中显示的内容创建了一次性销售。我尝试切换两步流程,转而包含计费协议创建和执行调用,但是打开以让用户登录到 paypal 的轻型窗口停止并显示错误消息“事情似乎没有目前正在工作。请稍后再试”。这是我的 javascript 代码,它与工作示例几乎完全相同,但路径不同。
paypal.Button.render(
// Set your environment
env: 'sandbox', // sandbox | production
// Wait for the PayPal button to be clicked
payment: function(resolve, reject)
// Make a call to the merchant server to set up the payment
console.log("button clicked")
return paypal.request.post('/payment/paypal/subscription', amount: '0.02')
.then(function(res)
resolve(res.payToken); #--WHERE I'M GOING WRONG
)
.catch(function(err) reject(err); );
,
// Wait for the payment to be authorized by the customer
onAuthorize: function(data)
// Make a call to the merchant server to execute the payment
return paypal.request.post('/payment/paypal/execute',
data: data,
paymentID: data.paymentID,
payerID: data.payerID
).then(function (res)
if (res.state == "active")
document.querySelector('#paypal-button-container-server').innerText = 'Payment Complete!';
else
console.log(res);
alert("Payment could not be approved, please try again.")
);
, '#paypal-button-container-server');
我可以看出我在支付功能上出了问题,即与resolve(res.payToken)
在线。我不知道我应该将 v1/payments/billing-agreements 响应中的哪些数据传递给此函数,但我尝试了 profile_id
和 approval_url
(实际的 href - "href": " https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=EC-XXXXXXXXXXXXX")。
我哪里出错了?如果这是可能的,我觉得我是一个远离完成这项工作的工作示例,但我想知道它是否不能像我那样做(在这种情况下,它可能需要通过 Payflow 完成?)。
注意:我完全了解计费协议和计费配置文件,我的问题不在于 REST API。除了有效的一次性销售实施之外,我还可以制定所有必要的计费配置文件/协议(通过 Postman 验证)。
下面是v1/payments/billing-agreements
沙盒调用的响应,任何人都可以指出其中的正确数据。
"name": "Magazine Subscription",
"description": "Monthly subscription with a regular monthly payment definition and two-month trial payment definition.",
"plan":
"id": "P-XXXXXXXXXXXXXXXXXXX",
"state": "ACTIVE",
"name": "1 Month Recurring",
"description": "A recurring payment plan for customers who sign a 1-month contract",
"type": "FIXED",
"payment_definitions": [
"id": "PD-924GIUJ3MWQ32E22348G0018",
"name": "Regular Payment Definition",
"type": "REGULAR",
"frequency": "Month",
"amount":
"currency": "USD",
"value": "150"
,
"cycles": "1",
"charge_models": [
"id": "CHM-940183BIUJ3MWQ5VK14226VH",
"type": "TAX",
"amount":
"currency": "USD",
"value": "10"
],
"frequency_interval": "1"
,
"id": "PD-5604RIUJ3MWQ4Y4221782C61",
"name": "Trial Payment Definition",
"type": "TRIAL",
"frequency": "Month",
"amount":
"currency": "USD",
"value": "120"
,
"cycles": "1",
"charge_models": [
"id": "CHM-640401IUJ3MWQ64W07759LB2",
"type": "TAX",
"amount":
"currency": "USD",
"value": "10"
],
"frequency_interval": "1"
],
"merchant_preferences":
"setup_fee":
"currency": "USD",
"value": "0"
,
"max_fail_attempts": "3",
"return_url": "http://localhost:8000/payment/success",
"cancel_url": "http://localhost:8000/payment/new",
"auto_bill_amount": "NO",
"initial_fail_amount_action": "CONTINUE"
,
"links": [
"href": "https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=EC-XXXXXXXXXXXXXX",
"rel": "approval_url",
"method": "REDIRECT"
,
"href": "https://api.sandbox.paypal.com/v1/payments/billing-agreements/EC-XXXXXXXXXXXXX/agreement-execute",
"rel": "execute",
"method": "POST"
],
"start_date": "2017-12-22T09:13:49Z"
【问题讨论】:
嗨@DNestoff,我有同样的问题,你是怎么解决的 @KoredeLawrenceOluwafemi 我也在尝试这样做。有人知道吗? 【参考方案1】:REST API 仍会传回快速结帐 URL,但为了将其与 checkout.js 前端集成一起使用,您需要传回在批准 URL 中找到的令牌。您可以解析此 URL 并获取服务器上的令牌部分并将其返回或在调用 resolve 方法之前将其传递。
即
approval_url =
"href": "https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=EC-XXXXXXXXXXXXX"
var token = "EC-XXXXXXXXXXXXX";
resolve(token);
【讨论】:
以上是关于通过 Paypal Express Checkout REST API 定期付款的主要内容,如果未能解决你的问题,请参考以下文章
通过 PHP 在 PayPal 上确认 Express Checkout
通过 Paypal Express Checkout REST API 定期付款
使用 PayPal REST API 通过 Express Checkout 测试拒绝付款