我已经建立了一个Paypal API,该API可以从我已建立的购物车中获取价格和商品详细信息,然后将价格发送给Paypal以向客户收费。但是,尽管项目的所有详细信息的确显示在响应标头中,但它们从未发送到Paypal。
以下是负责获取详细信息的代码:
<script>
paypal.Buttons({
createOrder: function(data, actions) {
// Set up the transaction
return actions.order.create({
purchase_units: [{
amount: {
value: {{ $totalPrice }},
currency: 'GBP',
item_list:{
items: [
@foreach($products as $product)
{
name: "{{ $product['item']['title'] }}",
sku: {{ $product['id'] }},
quantity: 1,
unit_amount: {
currency_code: "GBP",
value: {{ $product['price'] }}
}
},
@endforeach
],
}
},
},
]
});
},
onApprove: function(data, actions) {
// Capture the funds from the transaction
return actions.order.capture().then(function(details) {
// Show a success message to your buyer
alert('Transaction completed by ' + details.payer.name.given_name);
console.log(data.orderID);
return fetch('{{ route('PaypalServerController.getOrder')}}', {
method: 'post',
_token: '{{csrf_token()}}',
headers: {
'content-type': 'application/json'
},
body: JSON.stringify({
orderID: data.orderID
})
}).then(function(){
alert('SUCCESS!');
}, function(){
alert('IT FAILED');
});
});
}
}).render('#paypal-button-container');
</script>
这是控制器
public function authenticate(request $request){
return 'authenticate test';
}
public static function getOrder(request $request)
{
$orderId = json_decode($request->getContent())->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()))
{
PaypalServerController::getOrder('REPLACE-WITH-ORDER-ID', true);
}
我希望发生的事情是,当客户登录到弹出的Paypal窗口时,在那里显示了项目详细信息。同样在供应商的“贝宝”帐户屏幕上,项目也显示在此处。这可能吗?