randomstate/stripe

一个用于轻松测试的Stripe包装器

v1.2.0 2020-02-24 11:08 UTC

This package is auto-updated.

Last update: 2024-08-24 21:09:38 UTC


README

A stripe (PHP) wrapper for seriously easy testing. Brought to you with ❤️ from Random State.

此包仍在实际应用中进行测试。我确信它做它所说的,但代码中可能仍有多个路径不完全模拟Stripe API。

安装

composer require randomstate/stripe

Laravel

  • 创建一个服务提供者对象 - 例如 php artisan make:provider BillingServiceProvider
  • 将其添加到app.php的providers数组中
  • 绑定您的账单提供者

对于Stripe

public function register() {
    $this->app->bind(\RandomState\Stripe\BillingProvider::class, \RandomState\Stripe\Stripe::class);
    $this->app->bind(\RandomState\Stripe\Stripe::class, function() {
        return new \RandomState\Stripe\Stripe(env("STRIPE_SECRET"));
    });
}

对于测试,您可以将BillingProvider契约绑定到\RandomState\Stripe\Fake::class,并像与Stripe交互一样使用它。

用法

每个提供者都遵循BillingProvider契约,但本质上遵循Stripe API的自然工作方式。

例如

$stripe->charges()->create([
    'amount' => 100,
    // etc
])

$stripe->subscriptions()->items()->retrieve($itemId); // etc

测试辅助工具

此包自带以下实用功能

  • 支持在all查询中使用starting_afterending_beforelimit
  • 使用事件数据进行Webhook欺骗。在Laravel中,您可能需要进行如下操作来模拟Webhook调用
$webhooks = new WebhookListener($this->stripe->events());
$webhooks->record();

// perform actions

$events = $webhooks->play();

// Alternatively,
$events = $webhooks->during(function() {
    // perform actions
});


// Forward webhooks to your controllers in Laravel like so:
$webhooks->listen(function(Event $event) {
    $this->postJson('/my/webhooks/endpoint', $event->jsonSerialize());
});

$webhooks->during(function() {
     // perform actions and all events will be played automatically by sending the data as 
     // a POST request to your webhook endpoint as defined above.
});

许可证

MIT。自行承担风险,我们不承担此代码使用的任何责任。