2017-04-06 71 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);