在 Paypal 结帐中添加自定义字段

Adding a custom field in Paypal checkout

我们正在为我们的网络应用程序使用 Paypal 结账。我们有设置付款的客户端库。在后端,我们试图通过 webhook 捕获处理后的细节。我们在沙盒帐户上对此进行了测试。 Webhook 正在按预期发送除自定义字段之外的所有详细信息。我们需要自定义字段以便将付款与特定服务相关联。

在我们的网络应用程序中,我们有以下内容:

  paypalConfig = {
    env: 'sandbox',
    style: {
          size: 'responsive',
          color: 'white',
          shape: 'rect',
          label: 'pay',
          layout: 'horizontal',
          tagline: 'false'
    },
    client: {
      sandbox: 'SANDBOX_ID',
    },
    commit: false,
    payment: (data, actions) => {
      console.log("data is", data, actions);
      return actions.order.create({
        payment: {
          transactions: [
            { amount: { total: this.finalAmount * 100, currency: 'USD' }, job_id: this.jobId }
          ]
        }
      });
    },
    onApprove: (data, actions) => {
          return actions.order.capture().then((details) => {
            // This function shows a transaction success message to your buyer.
            // alert('Transaction completed by ' + details.payer.name.given_name);
            this.openModel('modal1');
      }).catch(err => {console.log("Error in authorize in paypal", err); this.openModel('modal2');})
    }
  }

如您所见,付款处理程序正在为交易对象添加 job_id 属性。在后端,我们正在监听以下事件:

结帐订单已完成,付款捕获已完成,付款销售已完成

我们只需要监听一个事件(比如收到付款),告诉我们交易何时完成。我不确定,所以我添加了所有看起来相关的事件,因为没有任何名为“已收到付款”的事件。

这可以像我们在这里尝试的那样完成吗?我们没有在 webhook 中获得自定义 job_id 字段。

首先你似乎在使用旧的 PayPal checkout.js,切换到最新的 sdk.js

其次,您正在使用仅客户端集成,切换到适当的客户端-服务器模式。这是前端:https://developer.paypal.com/demo/checkout/#/pattern/server

您的服务器上需要两个相应的路由,'Set Up Transaction' 和 'Capture Transaction',记录在此处:https://developer.paypal.com/docs/checkout/reference/server-integration/

使用上述解决方案,您可以立即同步 API 支付捕获响应。不需要来自 webhook 的额外异步通知,因此这些对您来说基本上是多余的。


一旦以上所有方法都起作用并为您创建成功的交易,还需要考虑一件事:传播失败。也就是说,如果买方的资金来源未能获得,例如,在一条不愉快的道路上会发生什么。他们的卡被拒绝了? how to send that error back to the UI 有一个指南,因此他们可以添加或选择不同的卡片。无论如何,这只是最后一个需要担心的细节。

只需创建一个 paypal 表单,然后将自定义添加为值之一。这是一个例子。此外,这里有一个很棒的教程可以指导您完成它 https://youtu.be/HIwRzATH6iU

<form action="https://sandbox.paypal.com/cgi-bin/webscr" method="post">
    <input type="hidden" name="cmd" value="_xclick">
    <input type="hidden" name="amount" value="50.00">
    <input type="hidden" name="currency_code" value="USD">
    <input type="hidden" name="custom" value="{MemberID}">
    <input type="hidden" name="business" value="youremail@business.example.com">
    <input type="hidden" name="item_name" value="Whatever Item">
    <input type="hidden" name="item_number" value="600">
    <input type="hidden" name="no_shipping" value="2">
    <input type="hidden" name="return" value="https://www.yourwebsite.com/success/">
    <input type="hidden" name="cancel_return" value="https://www.yourwebsite.com/failure">
    <input type="hidden" name="notify_url" value="https://www.yourwebsite.com/ipn">
    <input type="submit" value="Pay Now" name="submit" title="PayPal - The safer, easier way to pay online!" class="btn btn-primary">
</form>