capture error

hedong
New Community Member

 

        protected $apiUrl;
    protected $clientId;
    protected $secret;

    public function __construct()
    {
        $mode = config('paypal.mode');
        $paypalConfig = config('paypal.' . $mode);
        $this->apiUrl = $mode === 'sandbox' ? 'https://api.sandbox.paypal.com' : 'https://api.paypal.com';
        $this->clientId = $paypalConfig['client_id'];
        $this->secret = $paypalConfig['secret'];
    }
    protected function makePaypalRequest($method, $endpoint, $data = [])
    {
        return Http::withBasicAuth($this->clientId, $this->secret)
            ->$method($this->apiUrl . $endpoint, $data);
    }
public function capturePayment(Request $request)
    {
        // Extract orderId from the request body
        $orderId = $request->input('orderId');
        if (!$orderId) {
            return response()->json(['error' => 'Order ID is required.'], 400);
        }
    
        try {
            // Log the capture payment request
            Log::channel('paypal')->info('Sending capture payment request', ['order_id' => $orderId]);
    
            // Send the HTTP POST request to capture the payment using the makePaypalRequest method
            // The request body is an empty JSON object
            $response = $this->makePaypalRequest('post', "/v2/checkout/orders/{$orderId}/capture", []);
    
            // Process the response
            if ($response->successful()) {
                $data = $response->json();
                Log::channel('paypal')->info('Order captured successfully', ['order_id' => $orderId, 'response' => $data]);
                return response()->json($data, 200);
            } else {
                return $this->handleApiError($response);
            }
        } catch (\Exception $e) {
            Log::channel('paypal')->error('Exception caught in capture payment', ['message' => $e->getMessage()]);
            return response()->json(['error' => 'An error occurred during the capture process.'], 500);
        }
    }

 

and log
[Removed. Phone #s not permitted]31:46] local.INFO: Sending capture payment request {"order_id":"9EF65198C1215791H"}
[Removed. Phone #s not permitted]31:47] local.ERROR: PayPal API request failed: {"name":"INVALID_REQUEST","message":"Request is not well-formed, syntactically incorrect, or violates schema.","debug_id":"070d19afcea08","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"}]}

Login to Me Too
1 REPLY 1

Kavyar
Moderator
Moderator

Good day @hedong 

 

Thank you for posting to the PayPal community.

 

We understand that you are currently facing an issue, which is commonly caused by a poorly formed request JSON. In order to resolve this, we kindly suggest that you carefully review and double-check both the URL and the request JSON body for any syntax errors.

 

To assist you with troubleshooting this issue, we have provided detailed guide link below:

 

https://developer.paypal.com/api/rest/reference/orders/v2/errors/

 

Please follow these guidelines to ensure that the request body is correct and all required fields are properly formatted.

 

For more information, kindly refer to the provided detailed guide link below.

 

https://developer.paypal.com/docs/api/orders/v2/

 

If your still facing issues, please create an MTS ticket via -  https://www.paypal-support.com/s/?language=en_US  with the detailed information and error details.

 

Sincerely,

Kavya

PayPal MTS

 

If this post or any other was helpful, please enrich the community by giving kudos or accepting it as a solution.

Login to Me Too

Haven't Found your Answer?

It happens. Hit the "Login to Ask the community" button to create a question for the PayPal community.