2017-04-06 128 views
5

我的目標是使用「paypal」作爲我們的SaaS的付款方式設置定期6個月和12個月的訂閱。我正在使用其餘的API,而我無法找到的一件事是如何進行一次性銷售並使用PayPal的rest API將代碼重新轉換爲定期付款的工作實現(我讀過的是現在可能)。這裏是我們在哪裏:通過Paypal的Express Checkout REST API定期付款

我有一個工作advanced server integration使用貝寶的Express Checkout REST API進行支付。

我按照上面的鏈接說明的代碼示例創建了一次性銷售,以及在paypal中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_idapproval_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" 
} 
+0

@DNestoff您好,我有同樣的問題,你是如何能夠解決它 –

+0

@KoredeLawrenceOluwafemi我也想這樣做。任何人都知道嗎? – kspearrin

回答

0

的REST API的仍然傳回的快速結帳的URL,但爲了你需要傳回批准URL中找到的令牌checkout.js前端集成使用。您可以解析此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);