通过Paypal的Express Checkout REST API

时间:2017-04-06 23:46:33

标签: javascript php paypal express-checkout

我的目标是使用" paypal"设置定期的6个月和12个月订阅。作为我们SaaS的付款方式。我使用其余的API,我无法找到的一件事就是如何进行一次性销售,并使用PayPal的其余部分将我的代码重新定为定期付款API(我现在已经阅读过了)。我们在这里:

我有一个工作advanced server integration用于使用Paypal的Express Checkout REST API进行付款。

我按照上面链接中解释的代码示例创建了一次性销售,以及来自paypal的this example中显示的内容。我已经尝试将两步流程切换为包含计费协议创建和执行调用,但是打开用户登录到paypal的轻窗口正在停止显示错误消息" Things此刻似乎没有工作。请稍后再试#34;这是我的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-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"
}

1 个答案:

答案 0 :(得分:0)

REST API仍会传回快速结帐网址,但为了将其与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);