forecho / 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
代码版权属于 shakurov 和 alexstewartja,但由于他太长时间没有合并 这个该死的拉取请求,我不得不复制一份并更改命名空间,以便于 composer 请求。
安装
您可以通过 composer 安装此包
composer require forecho/coinbase
服务提供器将自动注册自己。
您必须使用以下命令发布配置文件
php artisan vendor:publish --provider="Forecho\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' => Forecho\Coinbase\Models\CoinbaseWebhookCall::class, ];
在配置文件的 webhookSecret
键中,您应该添加一个有效的 webhook 密钥。您可以在 Coinbase Commerce 控制台的 webhook 配置设置中找到使用的密钥。
接下来,您必须使用以下命令发布迁移
php artisan vendor:publish --provider="Forecho\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', '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 端点签发所有请求。此包会自动验证签名是否有效。如果不有效,则请求可能不是由 Coinbase Commerce 发送的。
除非出了大问题,否则此包将始终对 webhook 请求响应 200
。发送 200
将防止 Coinbase Commerce 反复重新发送相同的事件。所有具有有效签名的 webhook 请求都将记录在 coinbase_webhook_calls
表中。该表有一个 payload
列,其中保存了传入 webhook 的整个有效负载。
如果签名无效,则请求不会记录在 coinbase_webhook_calls
表中,但会抛出 Forecho\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 Forecho\Coinbase\Models\CoinbaseWebhookCall; class HandleCreatedCharge implements ShouldQueue { use InteractsWithQueue, Queueable, SerializesModels; /** @var \Forecho\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 Forecho\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 Forecho\Coinbase\Models\CoinbaseWebhookCall; CoinbaseWebhookCall::find($id)->process();
执行自定义逻辑
您可以通过指定自己的模型并在 coinbase
配置文件的 model
键中指定它,来添加一些在计划队列作业之前和/或之后应执行的自定义逻辑。该类应扩展 Forecho\Coinbase\Models\CoinbaseWebhookCall
。
以下是一个示例
use Forecho\Coinbase\Models\CoinbaseWebhookCall; class MyCustomWebhookCall extends CoinbaseWebhookCall { public function process() { // do some custom stuff beforehand parent::process(); // do some custom stuff afterwards } }
许可
MIT 许可证 (MIT)。有关更多信息,请参阅 许可文件。