shakurov / coinbase
Requires
- php: ^8.1
- guzzlehttp/guzzle: ^7.0.1
Requires (Dev)
- nunomaduro/collision: ^7.4
- orchestra/testbench: ^8.5
- phpunit/phpunit: ^10.2
README
该软件包已被废弃,不再维护。作者建议使用 antimech/coinbase 软件包。
安装
您可以通过 composer 安装此软件包
composer require shakurov/coinbase
服务提供程序将自动注册自己。
您必须使用以下命令发布配置文件:
php artisan vendor:publish --provider="Shakurov\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' => Shakurov\Coinbase\Models\CoinbaseWebhookCall::class, ];
在配置文件的 webhookSecret
键中,您应该添加一个有效的 webhook 密钥。您可以在 Coinbase Commerce 控制台的 webhook 配置设置中找到此密钥。
接下来,您必须使用以下命令发布迁移:
php artisan vendor:publish --provider="Shakurov\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 端点发送的所有请求进行签名。此软件包将自动验证签名是否有效。如果签名无效,则请求可能不是由 Coinbase Commerce 发送的。
除非出现严重错误,否则此软件包将始终以 200
响应 webhook 请求。发送 200
将防止 Coinbase Commerce 反复重发相同的事件。所有具有有效签名的 webhook 请求都将记录在 coinbase_webhook_calls
表中。该表有一个 payload
列,其中保存了传入 webhook 的整个有效负载。
如果签名无效,则请求将不会记录在 coinbase_webhook_calls
表中,但会抛出 Shakurov\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 Shakurov\Coinbase\Models\CoinbaseWebhookCall; class HandleCreatedCharge implements ShouldQueue { use InteractsWithQueue, Queueable, SerializesModels; public function __construct( 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
配置文件中的 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 Shakurov\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 Shakurov\Coinbase\Models\CoinbaseWebhookCall; CoinbaseWebhookCall::find($id)->process();
执行自定义逻辑
您可以使用自己的模型添加一些自定义逻辑,该逻辑应在排队作业之前和/或之后执行。您可以通过在 coinbase
配置文件的 model
键中指定自己的模型来实现此操作。该类应扩展 Shakurov\Coinbase\Models\CoinbaseWebhookCall
。
以下是一个示例
use Shakurov\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)。有关更多信息,请参阅 许可文件。