gpt4 book ai didi

php - PHP : Error: Expected an order id to be passed 中的 Paypal 智能按钮服务器集成

转载 作者:行者123 更新时间:2023-12-05 06:08:40 25 4
gpt4 key购买 nike

我正在尝试为智能按钮 Paypal 实现服务器端集成。我已经尝试解决这个问题好几天了。我是新手,这是一个个人项目,我正在努力学习。

每次单击 paypal 按钮时,我都会收到“错误:需要传递一个订单 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());
}

最佳答案

您从服务器返回给客户端的究竟是什么?您是否在服务器端或更简单地使用浏览器开发工具的“网络”选项卡记录了这一点?

在您的客户端代码中,您有:

return data.orderID; // Use the same key name for order ID on the client and server

具体来说,您是在何处使用 PayPal API 返回的值“id”设置键“orderID”?我没有看到任何地方发生这种情况。

如果您的服务器只是简单地重复 PayPal API JSON,请将您的客户端代码改为 return data.id,因为 id key 是PayPal v2/checkout/orders API。

这是最直接的示例,您的 HTML/JS 基于:https://developer.paypal.com/demo/checkout/#/pattern/server

关于php - PHP : Error: Expected an order id to be passed 中的 Paypal 智能按钮服务器集成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65079042/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com