用于完成 PayPal 一次性付款的 API

问题描述

我正在寻找使用 PHP 进行 PayPal 付款的方法。我知道有已弃用的 API 和当前的 API。所以我找到了几种方法来做到这一点。但是很多文件把我带进了迷宫。我很困惑 de.g.客户端按钮渲染,如何调用我们的 PHP 脚本/什么是 PayPal-Request-Id 并获取它/我们可以用来自 webscr 的令牌做什么 ==> cmd=_express-checkout&token=EC-7BA65327KY8480517 等等。>

我尝试在我的页面上使用一个按钮来使用 html 表单提交一些 paypal 所需的参数。接收PHP文件根据需要形成支付记录,然后创建订单。我得到了订单 ID,但它有什么用?所以我设置了一个新参数,符合 PayPal 创建付款的需要。这给了我 4 个链接,我选择重定向一个以供客户端登录并继续。有了这个,我进入了登录页面,然后我执行了付款,但沙盒仪表板上的每笔付款都显示待处理。

然后我很困惑如何完成付款。这里有我的泰铢数据

{"id":"PAYID-MAKU2OI7RR56034B9692111L","intent":"sale","state":"created","payer":{"payment_method":"paypal"},"transactions":[{"amount":{"total":"1126.15","currency":"THB","details":{"subtotal":"979.26","tax":"146.89","shipping":"0.00","insurance":"0.00","handling_fee":"0.00","shipping_discount":"0.00"}},"description":"aonang to","custom":"0","invoice_number":"kbv.starlight_12","soft_descriptor":"MFWD","payment_options":{"allowed_payment_method":"INSTANT_FUNDING_SOURCE","recurring_flag":false,"skip_fmf":false},"item_list":{"items":[{"name":"MFWD","sku":"kbv.starlight_12","description":"aonang to  ","price":"979.26","quantity":1}]},"related_resources":[]}],"note_to_payer":"Contact us for any questions on your order.","create_time":"2021-01-30T12:12:40Z","links":[{"href":"https://api.sandBox.paypal.com/v1/payments/payment/PAYID-MAKU2OI7RR56034B9692111L","rel":"self","method":"GET"},{"href":"https://www.sandBox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=EC-1G552540DR9655320","rel":"approval_url","method":"REDIRECT"},{"href":"https://api.sandBox.paypal.com/v1/payments/payment/PAYID-MAKU2OI7RR56034B9692111L/execute","rel":"execute","method":"POST"}]}

Sandbox account interface screenshot

解决方法

您提到了经典的 Express Checkout API 和已弃用的 v1/payments API,但正确的解决方案是使用当前的 API,v2/checkout/orders

在您的服务器上创建两条路由,一条用于“创建订单”,另一条用于“捕获订单”,documented here。当被 XHR/fetch 调用时,这些路由应该返回 JSON 数据(并且只有 JSON 数据)。

将您的两条路线与以下审批流程配对:https://developer.paypal.com/demo/checkout/#/pattern/server