laravel / cashier-paddle

Cashier Paddle provides an expressive, fluent interface to Paddle's subscription billing services.
https://laravel.com/docs/cashier-paddle
MIT License
245 stars 57 forks source link

Handling the case when Paddle returns null #191

Closed kjfdebruin closed 1 year ago

kjfdebruin commented 1 year ago

Cashier Paddle Version

1.6.1

Laravel Version

9.19

PHP Version

8.2

Database Driver & Version

MySQL 8.0.33-0ubuntu0.22.04.2

Description

When calling a Subscription Single Charge, an ErrorException of Trying to access array offset on value of type null is sometimes thrown.

This seems to be an intermittent Paddle issue. However, it might be worth more gracefully handling the case when it does happen. It has happened twice in about ~40 transactions in the last two weeks.

The error occurs on /vendor/laravel/cashier-paddle/src/Cashier.php:156, which is happens when $response is null below:

protected static function makeApiCall($method, $uri, array $payload = [])
    {
        $response = Http::$method($uri, $payload);

        if ($response['success'] === false) {
            throw new PaddleException($response['error']['message'], $response['error']['code']);
        }

        return $response;
    }

I would suggest something like this to solve it (and happy to create a PR if it suits the library)

if (!$response) {
     throw new PaddleException("No response.", $code = 101);
}

The 101 is Bad method call, based on these Paddle Error codes

Steps To Reproduce

Unfortunately this is intermittent, and I think it only occurs when Paddle fails to respond.

driesvints commented 1 year ago

Thanks. We'd accept a PR for this.

kjfdebruin commented 1 year ago

After digging a bit deeper, it unfortunately doesn't look like an easy fix. The transaction succeeded (SubscriptionPaymentSucceeded was triggered), even though the Paddle response to charge() was null. So I am not sure what the appropriate Cashier behaviour would be here. At least the issue is documented in case anyone else comes digging.