I am trying to call the PayPal server to set up the transaction but it returns a 419 PAGE EXPIRED error on the console.
PayPal create order script
createOrder: function(data, actions) {
return fetch('/create-payment', {
method: 'post'
}).then(function(res) {
return res.json();
}).then(function(orderData) {
return orderData.id;
});
},
Laravel Route
Route::post('/create-payment', [PayPalController::class, 'create_payment'])->name('create-payment');
Controller
public function create_payment()
{
$order = new OrdersCreateRequest();
$order->prefer('return=representation');
$order->body = array(
'intent' => 'CAPTURE',
'application_context' =>
array(
'return_url' => '/pages/orders',
'cancel_url' => '/pages/orders'
),
'purchase_units' =>
array(
0 =>
array(
'amount' =>
array(
'currency_code' => 'USD',
'value' => '420.00'
)
)
)
);
try {
$result = $this->client->execute($order);
return $result;
}
catch(HttpException $ex) {
print_r($ex->getMessage());
}
}
Blade
<div id="paypal-button-container"></div>
I know I need to add @csrf but where?
2
Answers
CSRF verification enabled by default in Laravel. so either you need to pass crsf token or skip that route from verification middleware.
Option 1
Add this route to csrf verification middleware except array.
Option 2
Add this in your page head.
and this in script
I think even you can override script from paypal without disabling csrf token .
Ref:http://findnerd.com/list/view/Paypal-Checkout-REST-api-integration-in-Laravel-5-7-with-smart-payment-buttons/74304/
Ref:https://developer.paypal.com/demo/checkout/#/pattern/server