ankurk91/laravel-stripe-exceptions

在 Laravel php 框架中优雅地处理 Stripe 异常。

3.15.0 2024-07-23 10:27 UTC

This package is auto-updated.

Last update: 2024-09-23 10:49:47 UTC


README

Packagist GitHub tag License Downloads tests codecov

此包使得在 Laravel 中处理 Stripe 异常变得简单。

你是如何处理 Stripe 错误的?你是否反复在代码中重复相同的处理方式?

安装

您可以通过 composer 安装此包。

composer require ankurk91/laravel-stripe-exceptions

用法

通过在 API 调用中使用 try/catch 来处理 Stripe 充值/转账异常,例如:

<?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);
}

处理 Stripe 连接异常

<?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'));
}

修改错误信息

您可以通过以下命令发布翻译信息:

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

特性

  • 利用 Laravel 内置的 可报告 & 可渲染 异常
  • APP_DEBUGtrue 时,报告所有异常。
  • 防止记录由用户输入引起的异常,例如 无效的卡
  • 当报告异常时,捕获已登录用户的信息。

安全

如果您发现任何安全问题,请通过电子邮件 pro.ankurk1[at]gmail[dot]com 反馈,而不是使用问题跟踪器。

变更日志

请参阅 CHANGELOG 了解最近更改的更多信息。

许可证

MIT 许可证。