如何从php中的paypal curl响应获取付款参考号
Posted
技术标签:
【中文标题】如何从php中的paypal curl响应获取付款参考号【英文标题】:How to get payment reference number from paypal curl response in php 【发布时间】:2021-11-11 09:57:12 【问题描述】:我想要 php 中来自 paypal curl 响应的付款参考号 但没有得到参考号作为回应 以下是我提出的要求:-
谁能知道我如何从 curl 响应中获取参考号,或者我在 curl 请求的某个地方出错了?
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://api.sandbox.paypal.com/v1/payments/payment',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => $paymentJson,
CURLOPT_HTTPHEADER => array(
'Authorization: Bearer ' . $accessToken,
'Content-Type: application/json',
'Cookie: cookie_check=yes; ui_experience=d_id%3D83f0753501654cbf94ee30789ad96f6c1610704307225; ts=vreXpYrS%3D1705312306%26vteXpYrS%3D1610706106%26vr%3Df6bc3f351760a4be4056d862fffa1217%26vt%3D057541121770a78050dba8bfffa9d7eb%26vtyp%3Dreturn; ts_c=vr%3Df6bc3f351760a4be4056d862fffa1217%26vt%3D057541121770a78050dba8bfffa9d7eb'
)
));
$response = curl_exec($curl);
curl_close($curl);
$result = json_decode($response, TRUE);
获取$result
为:-
Array
(
[id] => PAYID-MFBR34I2D9383578Y5951435
[intent] => sale
[state] => created
[payer] => Array
(
[payment_method] => paypal
)
[transactions] => Array
(
[0] => Array
(
[amount] => Array
(
[total] => 1785.00
[currency] => EUR
[details] => Array
(
[subtotal] => 1350.00
[tax] => 285.00
[shipping] => 0.00
[insurance] => 0.00
[handling_fee] => 150.00
[shipping_discount] => 0.00
)
)
[description] => This is the payment transaction description.
[custom] => This is the payment transaction description.
[invoice_number] => 159916
[soft_descriptor] => This is the payment transaction description.
[item_list] => Array
(
[items] => Array
(
[0] => Array
(
[name] => Item 1
[sku] => 1
[description] => This is the payment transaction description.
[price] => 1350.00
[currency] => EUR
[quantity] => 1
)
)
)
[related_resources] => Array
(
)
)
)
[note_to_payer] => Vimtox
[create_time] => 2021-09-16T10:35:29Z
[links] => Array
(
[0] => Array
(
[href] => https://api.sandbox.paypal.com/v1/payments/payment/PAYID-MFBR34I2D9383578Y5951435
[rel] => self
[method] => GET
)
[1] => Array
(
[href] => https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=EC-4R254380AW710703U
[rel] => approval_url
[method] => REDIRECT
)
[2] => Array
(
[href] => https://api.sandbox.paypal.com/v1/payments/payment/PAYID-MFBR34I2D9383578Y5951435/execute
[rel] => execute
[method] => POST
)
)
)
谁能解决这个问题?
【问题讨论】:
【参考方案1】:什么是“参考号”?这样的数字从何而来?请具体说明您的问题。
如果您正在寻找 PayPal 交易 ID,那将是 v1/payments API 的销售 ID,您只有在执行付款后才会收到该 ID。这必须在买家批准后进行。
但是,更重要的是,v1/payments API 已弃用,不应用于任何新的集成。请改用当前的 v2/checkout/orders API....:
在您的服务器上创建两条路由,一条用于“创建订单”,一条用于“捕获订单”,documented here。这些路由应该只返回 JSON 数据(没有 html 或文本)。当捕获响应成功时,将其生成的付款详细信息存储在您的数据库中(特别是purchase_units[0].payments.captures[0].id
,PayPal 交易 ID)并在发送返回 JSON 之前执行任何必要的业务逻辑(例如发送确认电子邮件或预订产品)。
将这两条路线与以下批准流程配对:https://developer.paypal.com/demo/checkout/#/pattern/server
【讨论】:
以上是关于如何从php中的paypal curl响应获取付款参考号的主要内容,如果未能解决你的问题,请参考以下文章
Paypal Express 结帐 + php cURL 执行付款不在沙箱上显示付款通知