ambitionphp / laravel-bitpay
laravel 的 BitPay 封装
v3.0.2
2021-07-06 00:07 UTC
Requires
- php: >=7.3 || ^8.0
- bitpay/sdk: ~v5.2
Requires (Dev)
- phpunit/phpunit: >=9.5
README
使用 Laravel 应用程序和 BitPay 客户端接受您的业务中的比特币和比特币现金。
需要 PHP ^7.3
内容
安装
安装包
您可以通过 composer 安装此包
composer require vrajroham/laravel-bitpay
发布配置文件
使用以下命令发布配置文件
php artisan vendor:publish --provider="Vrajroham\LaravelBitpay\LaravelBitpayServiceProvider"
添加配置值
将以下键添加到 .env
文件中,并更新详细信息(查看更多配置信息)
BITPAY_PRIVATE_KEY_PATH=/tmp/bitpay.pri BITPAY_PUBLIC_KEY_PATH=/tmp/bitpay.pub BITPAY_NETWORK=testnet BITPAY_KEY_STORAGE_PASSWORD=SomeRandomePasswordForKeypairEncryption BITPAY_TOKEN=
添加 webhook 事件监听器
默认情况下,包能够处理 webhook 请求。BitPay 支付状态更新完全基于 webhook。每当从服务器接收到 webhook 时,都会触发 BitpayWebhookReceived
事件。您只需要为此事件提供一个监听器。
您可以将监听器添加如下所示:
<?php namespace App\Listeners; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Queue\InteractsWithQueue; use Vrajroham\LaravelBitpay\Events\BitpayWebhookReceived; class ProcessBitpayWebhook { /** * Create the event listener. */ public function __construct() { } /** * Handle the event. * * @param object $event */ public function handle(BitpayWebhookReceived $event) { $orderId = $event->payload['orderId']; $status = $event->payload['status']; // Other payload properties // You will receive 3 webhooks for single payment with different status. // 1. status = paid // 2. status = confirmed // 3. status = completed } }
接下来,将监听器添加到 EventServiceProvider 的 $listen
数组中,如下所示:
<?php class EventServiceProvider extends ServiceProvider{ protected $listen = [ // Other events and listeners BitpayWebhookReceived::class => [ ProcessBitpayWebhook::class, ], ]; public function boot() { parent::boot(); } }
连接到服务器并验证客户端
-
创建密钥对,并将您的客户端(应用程序)与 BitPay 服务器配对。
php artisan laravel-bitpay:createkeypair
-
上述命令到底做了什么?
- 上述命令将创建 私钥和公钥,使用您提供的密码通过 bitpay secure storage 类加密您的私钥。
- 为您的客户端创建一个 SIN(服务识别号),以唯一识别来自您的服务器的请求。
- 使用 SIN,将在 bitpay 服务器上为您的客户端创建 新令牌和配对代码,并将显示在您的控制台输出中。
- 令牌将用于所有未来的 BitPay 请求,并将自动复制到您的
.env
文件中。 - 根据您设置的环境 测试/生产,命令将提供用于批准您的客户端的 URL,您需要复制并搜索 BitPay 服务器上的配对代码并批准它。
-
您已经设置好了。 ⛳
示例
创建发票和结账
让我们一步一步来做。
- 创建您的内部系统订单,然后通过创建以下 BitPay 发票来启动工作流程:
use Illuminate\Support\Facades\Redirect; use Vrajroham\LaravelBitpay\LaravelBitpay; public function createInvoice() { // Create instance of invoice $invoice = LaravelBitpay::Invoice(); // Set item details (Only 1 item) $invoice->setItemDesc('Photo'); $invoice->setItemCode('sku-1'); $invoice->setPrice(1); // Please make sure you provide unique orderid for each invoice $invoice->setOrderId(12345); // E.g. Your order number // Create Buyer Instance $buyer = LaravelBitpay::Buyer(); $buyer->setName('Vaibhavraj Roham'); $buyer->setEmail('test@example.me'); $buyer->setAddress1('Kopargaon'); $buyer->setNotify(true); // Add buyer to invoice $invoice->setBuyer($buyer); // Set currency $invoice->setCurrency('USD'); // Set redirect url to get back after completing the payment. GET Request $invoice->setRedirectURL(route('bitpay-redirect-back')); // Optional config. setNotificationUrl() // By default, package handles webhooks and dispatches BitpayWebhookReceived event as described above. // If you want to handle webhooks your way, you can provide url below. // If handled manually, BitpayWebhookReceived event will not be dispatched. $invoice->setNotificationUrl('Your custom POST route to handle webhooks'); // Create invoice on bitpay server. $invoice = LaravelBitpay::createInvoice($invoice); // You can save invoice ID from server, for your your reference $invoiceId = $invoice->getId(); $paymentUrl = $invoice->getUrl(); // Redirect user to following URL for payment approval. return Redirect::to($paymentUrl); }
- 一旦您获得了付款的发票 URL,请将用户重定向到该 URL。用户将在浏览器中看到以下内容:
- 接下来,打开您的 BitPay 钱包,扫描代码并进行付款。如下所示:
- 付款完成后,将显示成功页面,用户需要点击 返回到商店名称。
- 付款完成!现在您需要等待 webhook 通知有关付款状态的详细信息。
变更日志
有关最近更改的更多信息,请参阅 变更日志。
贡献
有关详细信息,请参阅 贡献指南。
安全
如果您发现任何与安全相关的问题,请通过电子邮件 vaibhavraj@vrajroham.me 联系,而不是使用问题跟踪器。
致谢
许可
MIT 许可证(MIT)。有关更多信息,请参阅 许可证文件。