How to capture the payment of an order using paypal checkout API on Laravel? Getting 400 Bad Request response

1 day ago 1
ARTICLE AD BOX

I'm integrating the PayPal API using its endpoint via Laravel Http requests. I've already done the Create Order function and it works:

public function RequestAjaxPayment(Request $req) { ... if (number_format($sum, 2, '.', '') == number_format($payAmount, 2, '.', '')) { $token = $this->GeneratePaypalToken(); try { $response = Http::withHeaders([ 'Authorization' => 'Bearer ' . $token, ])->post($this->baseUrl . '/v2/checkout/orders', [ 'intent' => 'CAPTURE', 'purchase_units' => [ [ 'amount' => [ 'currency_code' => session('currency'), 'value' => number_format($payAmount, 2, '.', ''), ], ] ], 'payment_source' => [ 'paypal' => [ 'experience_context' => [ 'payment_method_preference' => 'IMMEDIATE_PAYMENT_REQUIRED', 'landing_page' => 'LOGIN', 'shipping_preference' => 'NO_SHIPPING', 'user_action' => 'PAY_NOW', 'return_url' => url('get-success'), 'cancel_url' => url('payment-error'), ], ], ], ]); session(['SelectedPayments' => $selectedPayments]); $successUrl = ""; if ($response->successful()) { foreach ($response["links"] as $link) { if ($link['rel'] == "payer-action") { $successUrl = $link['href']; } } return response()->json([ 'status' => 'success', 'redirectUrl' => $successUrl, ]); ...

Then I get the response after user confirms the purchase in a route just to show a view. After that with AJAX I try to complete the process capturing the payment but it fails, telling me that the request is not well formed.

Code:

public function GetSuccess(Request $req) { return view('payment.processingPayment', [ 'PayerID' => $req->input('PayerID'), 'OrderID' => $req->query('token'), ]); } public function AjaxPaymentSuccess(Request $req) { $payerID = $req->input('PayerID'); $orderID = $req->input('OrderID'); $authToken = $this->GeneratePaypalToken(); if ($payerID && $orderID) { try { $response = Http::withHeaders([ 'Content-Type' => 'application/json', 'Authorization' => 'Bearer ' . $authToken, ])->post($this->baseUrl . '/v2/checkout/orders/' . $orderID . '/capture'); ...

Here is the response I get after it fails:

{ "status": 400, "message": "Pago fallido: Request is not well-formed, syntactically incorrect, or violates schema.", "paypal_error": { "name": "INVALID_REQUEST", "message": "Request is not well-formed, syntactically incorrect, or violates schema.", "debug_id": "f380818f756a9", "details": [ { "field": "/", "location": "body", "issue": "MALFORMED_REQUEST_JSON", "description": "The request JSON is not well formed." } ], "links": [ { "href": "https://developer.paypal.com/docs/api/orders/v2/#error-MALFORMED_REQUEST_JSON", "rel": "information_link", "encType": "application/json" } ] } }

What is the solution? Maybe I'm missing something from documentation that I need to do?

Relevant PayPal documentation:

Paypal request and response syntax: https://developer.paypal.com/docs/api/orders/v2/#orders_capture

Examples: https://developer.paypal.com/api/rest/integration/orders-api/api-use-cases/standard/

Read Entire Article