![](/img/trans.png)
[英]Is there a way to set up recurring payments with the PayPal REST API?
[英]Recurring payments through Paypal's Express Checkout REST API
我的目標是使用“paypal”作為我們SaaS的支付方式,設置定期的6個月和12個月訂閱。 我正在使用其余的API,而我找不到的一件事就是如何使用PayPal的其余API(我讀過的是一次性銷售並將我的代碼重新處理為定期付款)的工作實現現在可能)。 這是我們所處的位置:
我有一個有效的高級服務器集成 ,可以使用Paypal的Express Checkout REST API進行付款。
我按照上面鏈接中解釋的代碼示例創建了一次性銷售,以及paypal中此示例中顯示的內容。 我試圖切換出兩步流程,而不是包括計費協議創建和執行調用,但打開讓用戶登錄到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。 除了工作的一次性銷售實施,我還可以制作所有必要的賬單資料/協議(通過郵遞員驗證)。
以下是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"
}
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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.