[英]Recurring payments through Paypal's Express Checkout REST API
My goal is to set up recurring 6 and 12 month subscriptions using "paypal" as the payment method for our SaaS. 我的目标是使用“paypal”作为我们SaaS的支付方式,设置定期的6个月和12个月订阅。 I'm using the rest API, and the one thing I can't find is a working implementation for how to take a one time sale and re-work my code into a recurring payment using PayPal's rest API (which I've read is now possible). 我正在使用其余的API,而我找不到的一件事就是如何使用PayPal的其余API(我读过的是一次性销售并将我的代码重新处理为定期付款)的工作实现现在可能)。 Here's where we are at: 这是我们所处的位置:
I have a working advanced server integration for making payments with Paypal's Express Checkout REST API. 我有一个有效的高级服务器集成 ,可以使用Paypal的Express Checkout REST API进行付款。
I created the one time sale by following the code examples as explained at the above link as well as what's shown in this example from paypal. 我按照上面链接中解释的代码示例创建了一次性销售,以及paypal中此示例中显示的内容。 I've tried to switch out the two step process to instead include the billing-agreement creation and execution calls, but light window that opens up to have users sign in to paypal is stopping short with the error message "Things don't appear to be working at the moment. Please try again later". 我试图切换出两步流程,而不是包括计费协议创建和执行调用,但打开让用户登录到paypal的轻窗口正在停止错误消息“事情似乎没有出现现在正在工作。请稍后再试“。 Here's my javascript code, which is almost exactly the same as the working example, but with different routes. 这是我的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');
I can tell that I'm going wrong in the payment function, namely on the line with resolve(res.payToken)
. 我可以告诉我,我在支付功能方面出错,即在resolve(res.payToken)
。 I don't know what piece of data from the v1/payments/billing-agreements response I should be passing to this function, but have tried both the profile_id
and the approval_url
(the actual href - "href": " https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=EC-XXXXXXXXXXXXX "). 我不知道我应该传递给这个函数的v1 / payments / billing-agreements响应中的哪些数据,但是已经尝试了profile_id
和approval_url
(实际的href - “href”:“ https:// www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=EC-XXXXXXXXXXXXX “)。
Where am I going wrong? 我哪里错了? If this is possible, I feel like I'm one working example away from making this work, but I'd like to know if it's not capable the way I'm doing it (in which case it might need to be done via Payflow?). 如果这是可能的,我觉得我只是一个工作的例子,而不是做这项工作,但我想知道它是否不能像我这样做(在这种情况下,它可能需要通过Payflow完成?)。
Note: I fully understand billing agreements and billing profiles, and my problems are not with the REST API. 注意:我完全理解结算协议和结算配置文件,而我的问题不在于REST API。 In addition to the working one-time sale implementation, I can make all necessary billing profile/agreements (verified through Postman). 除了工作的一次性销售实施,我还可以制作所有必要的账单资料/协议(通过邮递员验证)。
Below is the response from a v1/payments/billing-agreements
sandbox call in the event anyone can point out the right piece of data inside of it. 以下是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"
}
The REST API's still pass back the express checkout URL's but in order to use it with the checkout.js front end integration you need to pass back the token found within the approval URL. REST API仍会传回快速结账URL,但为了将其与checkout.js前端集成一起使用,您需要传回批准URL中找到的令牌。 You can either parse this URL and get the token section on the server and return it back or pass it before you call resolve method. 您可以解析此URL并获取服务器上的令牌部分,并在调用resolve方法之前将其返回或传递。
ie 即
approval_url = {
"href": "https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=EC-XXXXXXXXXXXXX"
}
var token = "EC-XXXXXXXXXXXXX";
resolve(token);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.