通过Paypal的Express Checkout REST API定期付款

DNe*_*off 6 javascript php paypal express-checkout

我的目标是使用"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');
Run Code Online (Sandbox Code Playgroud)

我可以说我在支付功能上出错了,就是在线上resolve(res.payToken).我不知道我应该传递给这个函数的v1/payments/billing-agreement响应中的哪一段数据,但是已经尝试了profile_idapproval_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"
}
Run Code Online (Sandbox Code Playgroud)

cod*_*lay 0

REST API 仍然会传回快速结帐 URL,但为了将其与 checkout.js 前端集成一起使用,您需要传回在批准 URL 中找到的令牌。您可以解析此 URL 并获取服务器上的令牌部分并将其返回或在调用解析方法之前传递它。

IE

approval_url = { 
    "href": "https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=EC-XXXXXXXXXXXXX" 
}

var token = "EC-XXXXXXXXXXXXX";
resolve(token);
Run Code Online (Sandbox Code Playgroud)