通过Paypal Express Checkout REST API实现定期付款

5
我的目标是使用Paypal作为我们SaaS的支付方式,设置每6个月和12个月的循环订阅。我正在使用rest API,但我找不到一个可行的实现方法来将一次性销售转换为使用PayPal的rest 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_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"
}

嗨@DNestoff,我也遇到了同样的问题,你是如何解决的? - Korede Lawrence Oluwafemi
@KoredeLawrenceOluwafemi 我也在尝试做这个。有人解决了吗? - kspearrin
1个回答

0

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);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接