lab404/laravel-stripe-server 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

lab404/laravel-stripe-server

Composer 安装命令:

composer require lab404/laravel-stripe-server

包简介

Laravel Stripe Server is a library to handle Stripe SCA checkout.

README 文档

README

Laravel Stripe Server is a library to handle Stripe SCA checkout for your models.

Requirements

  • Laravel 6.x or 7.x
  • PHP >= 7.2

Laravel support

Version Release
6.x, 7.x 1.1
6.x 1.0
5.8 0.3

Intended workflow

  • You have an Order model with a stripe checkout. You create the order in your controller.

Example model:

use App\Models\Order;
use Lab404\StripeServer\Facades\Stripe;
use Illuminate\Http\Request;

class OrderController
{
    public function store(Request $request)
    {
        // Create your order
        $order = new Order($request->validated());
        $order->save();
        
        // Create your checkout session
        $session = Stripe::requestCreateSession()
                    ->setCustomerEmail($request->user()->email)
                    ->setReturnUrls($confirm_url, $cancel_url)
                    ->setProduct('T-shirt', 5000, 'EUR', 1, $picture_url)
                    ->call();
        
        // Create your checkout model
        $checkout = Stripe::registerCheckout($session, $order);
        
        return Stripe::redirectSession($response->id);
    }
}
  • Your user is redirected to stripe, he fills his informations and he's redirected to your success URL. The order is not paid yet. Asynchronously, the plugin will try to get new events from Stripe and will dispatch the CheckoutSessionCompleted event:

Example listener:

use Lab404\StripeServer\Events\CheckoutSessionCompleted;
use Lab404\StripeServer\Models\StripeCheckout;

class CheckoutListener
{
    public function handle(CheckoutSessionCompleted $event): void
    {
        /** @var StripeCheckout $checkout */
        $checkout = $event->checkout;
        /** Your charged model */
        $chargeable = $checkout->chargeable;
        /** The PaymentIntent returned by Stripe */
        $payment = $event->paymentIntent;
      
        /** Important! Mark the checkout as paid */
        $checkout->markAsPaid();
    }
}
  • You can use your model like this:
$order = Order::with('checkout')->first();
if ($order->checkout->is_paid) {
    echo 'Order is paid';
} else {
    echo 'Order is not paid';
}

Installation

  1. Require it with Composer:
composer require lab404/laravel-stripe-server
  1. Configure your Stripe keys in config/services.php.

  2. Publish migrations and views with php artisan vendor:publish --tag=stripe-server.

  3. Migrate 2019_06_19_101000_create_stripe_checkouts_table.php.

  4. Schedule the command in app\Console\Kernel.php:

protected function schedule(Schedule $schedule)
{
    $schedule->command('stripe:checkout-session-completed')->everyMinute();
}
  1. Add the Lab404\StripeServer\Models\HasStripeCheckout or HasStripeCheckouts (if a model can have multiple checkouts) to your chargeable models.

Going deeper

Stripe documentation

Access the Stripe Manager

With facade:

Stripe::method();

With DI:

public function index(Lab404\StripeServer\Stripe $stripe)
{
    $stripe->method();
}

With container:

app('stripe')->method();

Available methods

  • redirectSession(string $session_id): Illuminate\Contracts\View\View
  • registerCheckout(\Stripe\Checkout\Session $session, Model $model): Illuminate\Database\Eloquent\Model
  • requestCreateCharge(): StripeServer\Requests\CreateCharge
  • requestCreateSession(): StripeServer\Requests\CreateSession
  • requestPaymentIntent(string $id): StripeServer\Requests\PaymentIntent
  • requestEvents(string $type, int $hours = 24): StripeServer\Requests\Events
  • requestSessionCheckoutCompletedEvents(int $hours = 24): StripeServer\Requests\Events

Working with your models

Model with many checkouts

When a model has the Lab404\StripeServer\Models\HasStripeCheckouts you have access to the following methods and scopes:

// Scopes
Model::hasCheckouts()->get();
Model::hasPaidCheckouts()->get();
Model::hasUnpaidCheckouts()->get();

// Methods
$models->checkouts(); // returns all checkout for the model

// Eager loading
$models = Model::with('checkouts')->get(); 

Model with one checkout

When a model has the Lab404\StripeServer\Models\HasStripeCheckout you have access to the following methods and scopes:

// Scopes
Model::hasCheckout()->get();
Model::hasPaidCheckout()->get();
Model::hasUnpaidCheckout()->get();

// Methods
$models->checkout(); // returns the checkout for the model

// Eager loading
$models = Model::with('checkout')->get(); 

Customize the StripeCheckout model

Configure model in config/stripe-server.php. Your custom model should extend the default one.

Customize the redirector

When you use the redirectSession() method, an instance of Illuminate\View\View is returned. You can do:

return Stripe::redirectSession('...')->with([
    'title' => 'My custom title',
    'message' => 'My customer redirect message'
]);

Artisan commands

stripe:checkout-session-completed

Get all checkout.session.completed Stripe events and dispatch the event CheckoutSessionCompleted for each with the succeeded status.

stripe:events

Get all Stripe events and dispatch StripeEvent for each one.

stripe:purge

Delete all unpaid StripeCheckout older than the given days. Customize with the --days option, defaults to 7. It's convenient to call it in a scheduler:

$schedule->command('stripe:purge')->dailyAt('12:00');

Nova

If you're using Laravel Nova you can add the Lab404\StripeServer\Nova\StripeCheckout resource:

Laravel\Nova\Nova::resources([
    Lab404\StripeServer\Nova\StripeCheckout::class,
]);

This resource is not dynamically registered because it's quite simple and you may want to override it. More Nova features like Refund Action or Cards are coming.

TODOs and ideas

[x] Purge unpaid stripe checkouts
[ ] Refund
[ ] Nova actions

Tests

TODO

Contribute

This package is still in development, feel free to contribute!

Contributors

Licence

MIT

lab404/laravel-stripe-server 适用场景与选型建议

lab404/laravel-stripe-server 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 47 次下载、GitHub Stars 达 15, 最近一次更新时间为 2019 年 06 月 19 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「package」 「plugin」 「payment」 「checkout」 「server」 「stripe」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 lab404/laravel-stripe-server 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 lab404/laravel-stripe-server 我们能提供哪些服务?
定制开发 / 二次开发

基于 lab404/laravel-stripe-server 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 47
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 15
  • 点击次数: 15
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 15
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-06-19