srmklive/laravel-paypal

Error on $provider->getAccessToken() and $provider->createOrder()

Closed this issue · 7 comments

dabiko commented

Hello,
I am using Laravel 10. x. Installed version 3 of this package (composer require srmklive/paypal:~3.0) for Paypal payment. But I keep getting this weird error about json_decode error: Syntax error. I thought it was my project so I tried again on a new fresh project but I got the same error. Below is my code

$provider->getAccessToken();

$response = $provider->createOrder([
            'intent' => 'CAPTURE',
            'application_context' => [
                'return_url' => route('user.paypal.success'),
                'cancel_url' => route('user.paypal.cancel'),
            ],
            'purchase_units' => [
                [
                          'amount' => [
                              'currency_code' => 'USD',
                              'value' => $payableAmount
                         ]
                ]
            ]
        ]);

Is there a workaround for this?

try adding this in your .env file in your local development environment.
PAYPAL_VALIDATE_SSL=false

I would suggest that you review the config files. The README is pretty self explanatory.

I believe this is being caused by the catch block in the doPayPalRequest method in the PayPalHttpClient Trait:

https://github.com/srmklive/laravel-paypal/blob/9dc9454ba31d04a5f04a8024ddfcebd6463e83e8/src/Traits/PayPalHttpClient.php#L214C39-L214C39

Specifically, the following code:

Utils::jsonDecode($t->getMessage(), true)

is expecting $t->getMessage() to return JSON. However, that is not always the case. That method will not always return JSON. For example, I've seen the following string returned from that method:

Server error: `POST https://api-m.sandbox.paypal.com/v1/identity/generate-token` resulted in a `504 Gateway Timeout` response:
{"name":"GATEWAY_TIMEOUT","debug_id":"7cbeb8a41f069","message":"gateway timeout","links":[]}

^ this is clearly not valid JSON, which, when passed into the Utils::jsonDecode() method, will throw the error described by the OP.

I believe this issue should be re-opened until a fix is identified.

any update on this issue ?
I encountered the same issue today

I would suggest you do your own implementation of doPayPalRequest in a child class, and validate such a response there. I would use the Laravel Validator for validating if the response is a valid JSON.

I have tagged a new release. In case of API error, a valid JSON check is done. If false, the entire string is returned as intended in the ticket.

that's great, thank you.
you saved me.
I tried to use Laravel Http Request, and it works fine.
But since you updated it, now it's all good using my previous code.