antimech / coinbase
Laravel 对 Coinbase Commerce API 的封装
Requires
- php: ^8.1
- guzzlehttp/guzzle: ^7.0.1
Requires (Dev)
- nunomaduro/collision: ^7.4
- orchestra/testbench: ^8.5
- phpunit/phpunit: ^10.2
README
此 Composer 包是 Coinbase Commerce 的 SDK。它遵循 "Laravel 方式",并从开发者那里移除大量手动工作。
安装
您可以通过 composer 安装此包
composer require antimech/coinbase
服务提供器将自动注册自己。
您可以使用以下命令发布配置文件
php artisan vendor:publish --provider="Antimech\Coinbase\CoinbaseServiceProvider" --tag="config"
这是将要发布到 config/coinbase.php
的配置文件内容
return [ 'api_key' => env('COINBASE_COMMERCE_API_KEY'), 'api_version' => env('COINBASE_COMMERCE_API_VERSION', '2018-03-22'), 'webhook_secret' => env('COINBASE_COMMERCE_WEBHOOK_SECRET'), 'webhook_jobs' => [ // '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, ], 'webhook_model' => Antimech\Coinbase\Models\CoinbaseWebhookCall::class, ];
在配置文件的 webhook_secret
键中,您应该添加一个有效的 webhook 密钥。您可以在 Coinbase Commerce 控制台中的 webhook 配置设置 中找到此密钥。
接下来,您必须使用以下命令发布迁移
php artisan vendor:publish --provider="Antimech\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);
取消费用
$charge = Coinbase::cancelCharge($chargeId);
解决费用
$charge = Coinbase::resolveCharge($chargeId);
结账
列出结账
$checkouts = Coinbase::getCheckouts();
创建结账
$checkout = Coinbase::createCheckout([ 'name' => 'Name', 'description' => 'Description', 'requested_info' => [], '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', ], ]);
删除结账
$checkout = Coinbase::deleteCheckout($checkoutId);
发票
列出发票
$invoices = Coinbase::getInvoices();
创建发票
$invoice = Coinbase::createInvoice([ 'business_name' => 'Business Name', 'customer_email' => 'customer@example.com', 'customer_name' => 'Customer Name', 'local_price' => [ 'amount' => 100, 'currency' => 'USD', ], 'memo' => 'A memo/description for the invoice', ]);
显示发票
$invoice = Coinbase::getInvoice($invoiceId);
作废发票
$invoice = Coinbase::voidInvoice($invoiceId);
解决发票
$invoice = Coinbase::resolveInvoice($invoiceId);
事件
列出事件
$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
表中,但会抛出 Antimech\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 Antimech\Coinbase\Models\CoinbaseWebhookCall; class HandleCreatedCharge implements ShouldQueue { use InteractsWithQueue, Queueable, SerializesModels; public function __construct( public CoinbaseWebhookCall $webhookCall, ) {} public function handle(): void { // do your work here // you can access the payload of the webhook call with `$this->webhookCall->payload` } }
我们强烈建议您将此作业设置为可排队,因为这将最小化 webhook 请求的响应时间。这允许您处理更多的 Coinbase Commerce webhook 请求并避免超时。
在创建作业后,您必须在 coinbase.php
配置文件中的 webhook_jobs
数组中注册它。键应该是 Coinbase Commerce 事件类型 的名称,但将 .
替换为 _
。值应该是完全限定的类名。
// config/coinbase.php 'webhook_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 Antimech\Coinbase\Models\CoinbaseWebhookCall; class ChargeCreatedListener implements ShouldQueue { public function handle(CoinbaseWebhookCall $webhookCall): void { // 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 Antimech\Coinbase\Models\CoinbaseWebhookCall; CoinbaseWebhookCall::find($id)->process();
执行自定义逻辑
您可以使用自己的模型添加一些应在排队的作业调度之前和/或之后执行的自定义逻辑。您可以通过在 coinbase.php
配置文件的 webhook_model
键中指定自己的模型来实现这一点。该类应该扩展 Antimech\Coinbase\Models\CoinbaseWebhookCall
。
以下是一个示例
use Antimech\Coinbase\Models\CoinbaseWebhookCall; class MyCustomWebhookCall extends CoinbaseWebhookCall { public function process(): void { // do some custom stuff beforehand parent::process(); // do some custom stuff afterwards } }
许可
MIT 许可证 (MIT)。有关更多信息,请参阅 许可文件。