使用PayPal智能按钮集成,我遇到了一个问题,例如:
1。客户按下按钮,批准交易,并被重定向回成功页面。
2。批准后,会将订单ID发送到我的服务器(贝宝提供的php文件)https://developer.paypal.com/docs/checkout/integrate/#6-verify-the-transaction
此时,我需要在服务器上打印从getOrder函数获得的响应。我怎样才能做到这一点?我只能在客户端接收数据,我应该在getOrder之后创建一个POST请求,以将其实际打印到客户端进行测试?
((已安装SDK)
对于Web开发还是新手,也许我缺少关键信息。
服务器端代码;
<?php
namespace Sample;
require __DIR__ . '/vendor/autoload.php';
//1. Import the PayPal SDK client that was created in `Set up Server-Side SDK`.
use Sample\PayPalClient;
use PayPalCheckoutSdk\Orders\OrdersGetRequest;
$data = json_decode(file_get_contents('php://input'), true);
class GetOrder
{
// 2. Set up your server to receive a call from the client
/**
*You can use this function to retrieve an order by passing order ID as an argument.
*/
public static function getOrder($orderId)
{
// 3. Call PayPal to get the transaction details
$client = PayPalClient::client();
$response = $client->execute(new OrdersGetRequest($orderId));
/**
*Enable the following line to print complete response as JSON.
*/
print json_encode($response->result);
print "Status Code: {$response->statusCode}\n";
print "Status: {$response->result->status}\n";
print "Order ID: {$response->result->id}\n";
print "Intent: {$response->result->intent}\n";
print "Links:\n";
foreach ($response->result->links as $link) {
print "\t{$link->rel}: {$link->href}\tCall Type: {$link->method}\n";
}
// 4. Save the transaction in your database. Implement logic to save transaction to your database for future reference.
print "Gross Amount: {$response->result->purchase_units[0]->amount->currency_code} {$response->result->purchase_units[0]->amount->value}\n";
// To print the whole response body, uncomment the following line
echo json_encode($response->result, JSON_PRETTY_PRINT);
}
}
/**
*This driver function invokes the getOrder function to retrieve
*sample order details.
*
*To get the correct order ID, this sample uses createOrder to create a new order
*and then uses the newly-created order ID with GetOrder.
*/
if (!count(debug_backtrace())) {
GetOrder::getOrder($data['orderID'], true);
}
客户端;
<script>
paypal.Buttons({
createOrder: function(data, actions) {
return actions.order.create({
purchase_units: [{
amount: {
value: '0.01'
}
}]
})
},
onApprove: function(data, actions) {
return actions.order.capture().then(function(details) {
alert('Transaction completed by ' + details.payer.name.given_name);
console.log(data.orderID);
// Call your server to save the transaction
return fetch('server.php', {
method: 'post',
headers: {
'content-type': 'application/json',
},
body: JSON.stringify({
orderID: data.orderID
})
}).then(function(res){
if(!res.ok){
throw new Error('Bad response from server.php');
}
window.location('succes.php');
});
});
},
onError: function(data, actions){
window.location('error.php');
}
}).render('#paypal-button-container');
</script>
在服务器端,您已经有:
echo json_encode($response->result, JSON_PRETTY_PRINT);
在此之前,您可能需要注释掉“打印”行,因为它们是多余的。
因此,正在返回数据。您想打印出来或对其进行处理。现在添加一个“ console.log”。:
return fetch('server.php', {
method: 'post',
headers: {
'content-type': 'application/json',
},
body: JSON.stringify({
orderID: data.orderID
})
.then(function(res){
console.log(res); //// ADD THIS
if(!res.ok){
throw new Error('Bad response from server.php');
}
window.location('succes.php');
});