PHP 中的 Paypal 智能按钮服务器集成:错误:需要传递一个订单 ID
Posted
技术标签:
【中文标题】PHP 中的 Paypal 智能按钮服务器集成:错误:需要传递一个订单 ID【英文标题】:Paypal smart button server integration in PHP: Error: Expected an order id to be passed 【发布时间】:2021-03-12 16:45:22 【问题描述】:我正在尝试为智能按钮贝宝实现服务器端集成。我已经尝试解决这个问题好几天了。我是新手,这是一个个人项目,我正在努力学习。
每次单击贝宝按钮时,我都会不断收到“错误:需要传递订单 ID”。我错过了什么?
这是我的客户端代码:
paypal.Buttons(
// Customize button (optional)
style:
color: 'blue',
shape: 'pill',
,
//Set up a payment
// This function sets up the details of the transaction, including the amount and line item details.
createOrder: function()
return fetch('<?php echo URLROOT; ?>/libraries/Paypal.php',
method: 'post',
headers:
'content-type': 'application/json',
,
).then(function(res)
//console.log(res.json());
return res.text();
).then(function(data)
return data.orderID; // Use the same key name for order ID on the client and server
);
,
// Execute the payment
onApprove: function (data)
//This function captures the funds from the transaction.
return fetch('<?php echo URLROOT; ?>/libraries/Paypal.php',
method: 'post',
headers:
'content-type': 'application/json'
,
body: JSON.stringify(
orderID: data.orderID
)
).then(function (res)
return res.json();
).then(function(orderData)
if (errorDetail && errorDetail.issue === 'INSTRUMENT_DECLINED')
return actions.restart();
if (errorDetail)
var msg = 'Sorry, your transaction could not be processed.';
if (errorDetail.description) msg += '\n\n' + errorDetail.description;
if (orderData.debug_id) msg += ' (' + orderData.debug_id + ')';
return alert(msg);
alert('Transaction completed by ' + orderData.payer.name.given_name);
);
,
onError: function (err)
alert('An Error occured ' + err);
).render('#payment-btn');
这是我的服务器端:
// Creating an environment
$clientId = "HIDDEN";
$clientSecret = "HIDDEN";
$environment = new SandboxEnvironment($clientId, $clientSecret);
$client = new PayPalHttpClient($environment);
// Construct a request object and set desired parameters
// Here, OrdersCreateRequest() creates a POST request to /v2/checkout/orders
use PayPalCheckoutSdk\Orders\OrdersCreateRequest;
$request = new OrdersCreateRequest();
$request->prefer('return=representation');
$request->body = array(
'intent' => 'CAPTURE',
'application_context' =>
array(
'locale' => 'en-US',
'return_url' => URLROOT.'/bookings/payment',
'cancel_url' => URLROOT.'/bookings/payment'
),
'purchase_units' =>
array(
0 =>
array(
'amount' =>
array(
'currency_code' => 'EUR',
'value' => $_SESSION["total-price"]
)
)
)
);
try
// Call API with your client and get a response for your call
$response = $client->execute($request);
return $response->result;
// If call returns body in response, you can get the deserialized version from the result attribute of the response
print_r($response);
catch (HttpException $ex)
echo $ex->statusCode;
print_r($ex->getMessage());
use PayPalCheckoutSdk\Orders\OrdersCaptureRequest;
// Here, OrdersCaptureRequest() creates a POST request to /v2/checkout/orders
// $response->result->id gives the orderId of the order created above
//$orderId = $response->result->id;
$request = new OrdersCaptureRequest($orderId);
$request->prefer('return=representation');
try
// Call API with your client and get a response for your call
$response = $client->execute($request);
// If call returns body in response, you can get the deserialized version from the result attribute of the response
print_r($response);
catch (HttpException $ex)
echo $ex->statusCode;
print_r($ex->getMessage());
【问题讨论】:
【参考方案1】:你从你的服务器返回给客户端的究竟是什么?您是否在服务器端或更简单地使用浏览器开发工具的“网络”选项卡记录了此内容?
在您的客户端代码中,您有:
return data.orderID; // Use the same key name for order ID on the client and server
具体来说,您在哪里将键“orderID”设置为 PayPal API 返回的“id”值?我没有看到这种情况发生在任何地方。
如果您的服务器只是反刍 PayPal API JSON,请将您的客户端代码更改为 return data.id
,因为 id
密钥是 PayPal v2/checkout/orders API 使用的。
以下是基于 html/JS 的最直接示例:https://developer.paypal.com/demo/checkout/#/pattern/server
【讨论】:
我为此花费了太多时间,这非常容易。顺便说一句,我不知道“网络”工具,这也有很大帮助。谢谢,非常感谢。以上是关于PHP 中的 Paypal 智能按钮服务器集成:错误:需要传递一个订单 ID的主要内容,如果未能解决你的问题,请参考以下文章