ankurk91/laravel-stripe-exceptions

Handle Stripe exceptions gracefully in Laravel php framework.

3.16.0 2024-10-20 06:24 UTC

README

Packagist GitHub tag License Downloads tests codecov

This package makes it easy to handle Stripe exceptions in Laravel

How do you handle Stripe errors? Are you repeating same code again and again?

Installation

You can install the package via composer:

composer require ankurk91/laravel-stripe-exceptions

Usage

Handle Stripe charge/transfer exceptions by wrapping the API calls in try/catch like:

<?php

try {
    $response = \Stripe\Charge::create([
        'source' => request('source'),
        'amount' => 1000,
        'currency' => 'usd',
    ]);
} catch (\Throwable $exception) {
    // send back an errored JSON response to browser
    throw new \Ankurk91\StripeExceptions\ApiException($exception);
}

Handle Stripe connect exceptions:

<?php

try {
    $response = \Stripe\OAuth::token([
        'grant_type' => 'authorization_code',
        'code' => request('code')
    ]);
} catch (\Throwable $exception) {
    // redirect with failed error message
    // `error` will be flashed in session to destination page
    throw new \Ankurk91\StripeExceptions\OAuthException($exception, route('stripe.failed'));
}

Modifying error messages

You can publish the translation messages via this command

php artisan vendor:publish --provider="Ankurk91\StripeExceptions\StripeServiceProvider" --tag=translations

Features

  • Takes advantage of Laravel's inbuilt Reportable & Renderable Exceptions.
  • Reports all exceptions when APP_DEBUG is true
  • Prevents logging of exceptions caused by user input, for example Invalid Card
  • Captures logged-in user information when an exception gets reported

Security

If you discover any security issues, please email pro.ankurk1[at]gmail[dot]com instead of using the issue tracker.

Changelog

Please see CHANGELOG for more information what has changed recently.

License

The MIT License.