Anybody that can help? Really out of my depth with this one
Jul 12, 2019
1
Level 2
Paypal API not posting item array
I have set up a Paypal API that grabs the price and item details from a shopping cart I have set up and then sends the price over to Paypal to charge the customer. However, although all the details of the items do show in the response header, they never get sent over to Paypal.
Here is the code responsible for grabbing the details:
<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>
And here is the controller
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);
}
What I expected to happen was for when the customer logs into the Paypal window that pops up, that the item details show there. Also on the Paypal account screen for the vendor, for the items to show there too. Is this possible?
Please or to participate in this conversation.