alexis-riot / laravel-coinbase
Laravel 对 Coinbase Commerce API 的封装
Requires
- php: ^7.3|^8.0
- guzzlehttp/guzzle: ^7.0.1
Requires (Dev)
- orchestra/testbench: ^6.0.0
- phpunit/phpunit: ^9.3.3
README
安装
您可以通过 composer 安装此包
composer require alexis-riot/laravel-coinbase
服务提供者将自动注册自身
您必须使用以下命令发布配置文件
php artisan vendor:publish --provider="AlexisRiot\Coinbase\CoinbaseServiceProvider" --tag="config"
这是将在 config/coinbase.php
中发布的配置文件内容
return [ 'apiKey' => env('COINBASE_API_KEY'), 'apiVersion' => env('COINBASE_API_VERSION'), 'webhookSecret' => env('COINBASE_WEBHOOK_SECRET'), 'webhookJobs' => [ // 'charge:created' => \App\Jobs\CoinbaseWebhooks\HandleCreatedCharge::class, // 'charge:confirmed' => \App\Jobs\CoinbaseWebhooks\HandleConfirmedCharge::class, // 'charge:failed' => \App\Jobs\CoinbaseWebhooks\HandleFailedCharge::class, // 'charge:delayed' => \App\Jobs\CoinbaseWebhooks\HandleDelayedCharge::class, // 'charge:pending' => \App\Jobs\CoinbaseWebhooks\HandlePendingCharge::class, // 'charge:resolved' => \App\Jobs\CoinbaseWebhooks\HandleResolvedCharge::class, ], 'webhookModel' => AlexisRiot\Coinbase\Models\CoinbaseWebhookCall::class, ];
在配置文件的 webhookSecret
键中,您应添加一个有效的 webhook 密钥。您可以在 Coinbase Commerce 控制台中的 webhook 配置设置中找到使用的密钥。
接下来,您必须使用以下命令发布迁移
php artisan vendor:publish --provider="AlexisRiot\Coinbase\CoinbaseServiceProvider" --tag="migrations"
迁移发布后,您可以通过运行迁移来创建 coinbase_webhook_calls
表
php artisan migrate
最后,注意路由:在 Coinbase Commerce 控制台中,您必须添加 webhook 端点,例如:https://example.com/api/coinbase/webhook
用法
费用
列出费用
$charges = Coinbase::getCharges();
创建费用
$charge = Coinbase::createCharge([ 'name' => 'Name', 'description' => 'Description', 'local_price' => [ 'amount' => 100, 'currency' => 'USD', ], 'pricing_type' => 'fixed_price', ]);
显示费用
$charge = Coinbase::getCharge($chargeId);
结账
列出结账
$checkouts = Coinbase::getCheckouts();
创建结账
$checkout = Coinbase::createCheckout([ 'name' => 'Name', 'description' => 'Description', 'local_price' => [ 'amount' => 100, 'currency' => 'USD', ], 'pricing_type' => 'fixed_price', ]);
显示结账
$checkout = Coinbase::getCheckout($checkoutId);
更新结账
$checkout = Coinbase::updateCheckout($checkoutId, [ 'name' => 'New Name', 'description' => 'New Description', 'local_price' => [ 'amount' => 200, 'currency' => 'USD', ], 'requested_info' => [ 'name', ], ]);
事件
列出事件
$events = Coinbase::getEvents();
显示事件
$event = Coinbase::getEvent($eventId);
Webhooks
Coinbase Commerce 将为几种事件类型发送 webhooks。您可以在 Coinbase Commerce 文档中找到 事件类型的完整列表。
Coinbase Commerce 将为击中您的应用 webhook url 的所有请求签名。此包将自动验证签名是否有效。如果签名无效,则请求可能不是由 Coinbase Commerce 发送的。
除非出了大问题,否则此包将始终以 200
响应 webhook 请求。发送 200
将防止 Coinbase Commerce 反复重发相同的事件。所有带有有效签名的 webhook 请求都将记录在 coinbase_webhook_calls
表中。该表有一个 payload
列,其中保存了传入 webhook 的整个有效负载。
如果签名无效,则请求将不会记录在 coinbase_webhook_calls
表中,而将抛出 AlexisRiot\Coinbase\Exceptions\WebhookFailed
异常。如果 webhook 请求期间出现错误,则抛出的异常将保存在 exception
列中。在这种情况下,控制器将发送 500
而不是 200
。
此包有两种方式可以用来处理 webhook 请求:您可以选择队列作业或监听该包将触发的事件。
使用作业处理 webhook 请求
如果您想在特定事件类型到来时执行某些操作,您可以定义一个执行工作的作业。以下是一个此类作业的示例
<?php namespace App\Jobs\CoinbaseWebhooks; use Illuminate\Bus\Queueable; use Illuminate\Queue\SerializesModels; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Contracts\Queue\ShouldQueue; use AlexisRiot\Coinbase\Models\CoinbaseWebhookCall; class HandleCreatedCharge implements ShouldQueue { use InteractsWithQueue, Queueable, SerializesModels; /** @var \AlexisRiot\Coinbase\Models\CoinbaseWebhookCall */ public $webhookCall; public function __construct(CoinbaseWebhookCall $webhookCall) { $this->webhookCall = $webhookCall; } public function handle() { // do your work here // you can access the payload of the webhook call with `$this->webhookCall->payload` } }
我们强烈建议您将此作业设为可队列的,因为这将最小化 webhook 请求的响应时间。这允许您处理更多的 Coinbase Commerce webhook 请求并避免超时。
创建您的作业后,您必须在 coinbase.php
配置文件中的 jobs
数组中注册它。键应该是 Coinbase Commerce 事件类型 的名称,但将 .
替换为 _
。值应该是完全限定的类名。
// config/coinbase.php 'jobs' => [ 'charge:created' => \App\Jobs\CoinbaseWebhooks\HandleCreatedCharge::class, ],
使用事件处理 webhook 请求
在收到webhook请求时,您可以选择监听此包将要触发的事件。每当一个有效的请求击中您的应用时,该包将触发一个coinbase::<事件名称>
事件。
事件的负载将是为传入请求创建的CoinbaseWebhookCall
实例。
让我们看看您如何监听此类事件。在EventServiceProvider
中,您可以注册监听器。
/** * The event listener mappings for the application. * * @var array */ protected $listen = [ 'coinbase::charge:created' => [ App\Listeners\ChargeCreatedListener::class, ], ];
以下是一个此类监听器的示例
<?php namespace App\Listeners; use Illuminate\Contracts\Queue\ShouldQueue; use AlexisRiot\Coinbase\Models\CoinbaseWebhookCall; class ChargeCreatedListener implements ShouldQueue { public function handle(CoinbaseWebhookCall $webhookCall) { // do your work here // you can access the payload of the webhook call with `$webhookCall->payload` } }
我们强烈建议您将事件监听器设置为可排队,因为这将最小化webhook请求的响应时间。这允许您处理更多的Coinbase Commerce webhook请求,并避免超时。
上述示例只是Laravel中处理事件的一种方式。要了解其他选项,请阅读Laravel处理事件文档。
高级用法
重试处理webhook
所有传入的webhook请求都将写入数据库。当处理webhook调用时发生错误时,这一点非常有价值。您可以在调查并修复失败原因后,轻松地重试处理webhook调用,如下所示
use AlexisRiot\Coinbase\Models\CoinbaseWebhookCall; CoinbaseWebhookCall::find($id)->process();
执行自定义逻辑
您可以通过使用自己的模型,在队列作业调度之前和/或之后执行一些自定义逻辑。您可以通过指定自己的模型在coinbase
配置文件的model
键中来实现。该类应扩展AlexisRiot\Coinbase\Models\CoinbaseWebhookCall
。
以下是一个示例
use AlexisRiot\Coinbase\Models\CoinbaseWebhookCall; class MyCustomWebhookCall extends CoinbaseWebhookCall { public function process() { // do some custom stuff beforehand parent::process(); // do some custom stuff afterwards } }
许可证
MIT许可证(MIT)。有关更多信息,请参阅许可证文件。