Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Robarelli's avatar

Catch errors in Cashier's one off charge?

Is there a way to handle failed one off charges? In Cashier's StripeGateway, any one off charge failure returns false, seen here:

public function charge($amount, array $options = [])
    {
        $options = array_merge([
            'currency' => $this->getCurrency(),
        ], $options);

        $options['amount'] = $amount;

        if (! array_key_exists('source', $options) && $this->billable->hasStripeId()) {
            $options['customer'] = $this->billable->getStripeId();
        }

        if (! array_key_exists('source', $options) && ! array_key_exists('customer', $options)) {
            throw new InvalidArgumentException('No payment source provided.');
        }

        try {
            $response = StripeCharge::create($options);
        } catch (StripeErrorCard $e) {
            return false;
        }

        return $response;
    }

However, a lot of useful information comes from Stripe when a charge fails (e.g. card declined) so it would be great to grab that. Is there a way to get this information another way?

0 likes
0 replies

Please or to participate in this conversation.