ambitionphp/laravel-bitpay 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

ambitionphp/laravel-bitpay

Composer 安装命令:

composer require ambitionphp/laravel-bitpay

包简介

Bitpay wrapper for laravel

README 文档

README

Latest Version on Packagist Build Status Quality Score Total Downloads

Accept Bitcoin and Bitcoin Cash for your business with your Laravel application and BitPay client.

Requires PHP ^7.3

Contents

Installation

Install package

You can install the package via composer:

composer require vrajroham/laravel-bitpay

Publish config file

Publish config file with:

php artisan vendor:publish --provider="Vrajroham\LaravelBitpay\LaravelBitpayServiceProvider"

Add configuration values

Add following keys to .env file and updated the details (view more about configuration):

BITPAY_PRIVATE_KEY_PATH=/tmp/bitpay.pri
BITPAY_PUBLIC_KEY_PATH=/tmp/bitpay.pub
BITPAY_NETWORK=testnet
BITPAY_KEY_STORAGE_PASSWORD=SomeRandomePasswordForKeypairEncryption
BITPAY_TOKEN=

Add webhook event listener

By default package is capable of handling of webhook requests. Bitpay payment status updates are completely based on webhooks. Whenever webhook is received from server, BitpayWebhookReceived event is dispatched. You just need to provide a listener for this event.

You can add your listener as below,

<?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
    }
}

Next, add listener to EventServiceProvider's $listen array as below,

<?php

class EventServiceProvider extends ServiceProvider{
    protected $listen = [
        // Other events and listeners
        BitpayWebhookReceived::class => [
            ProcessBitpayWebhook::class,
        ],
    ];

    public function boot()
    {
        parent::boot();
    }
}

Connect to server and authenticate the client

  • Create keypairs and pair your client(application) with BitPay server.

    php artisan laravel-bitpay:createkeypair

Screenshot-2019-11-03-at-7-59-55-PM

  • What exactly above command do?

    • Above command will create Private and Public key, encrypt your private key using bitpay secure storage class using your provided password.
    • SIN (Service Identification Number) for your client will be created to uniquely identify requests from your server.
    • By using SIN new Token and Pairing Code will be created for your client on bitpay server and will be shown on your console output.
    • Token will be used for all future request to bitpay and will automatically be copied to your .env file.
    • Based on environment you set TEST/LIVE, command will provide URL to approve your client and you need to copy and search Pairing Code on bitpay server & approve it.
  • You are all set. ⛳

Examples

Create Invoice and checkout

Let's go step by step.

  • Create your internal system order and then initiate the workflow by creating bitpay invoice as below,
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);
}
  • Once you get the invoice url for payment, redirect user to that particular url. Use will see something like below on browser.

Screenshot-2019-11-03-at-5-31-33-PM

  • Next, open your bitpay wallet, scan the code and make a payment. Something like below,

IMG-3639

  • Once payment is done, success screen will be displayed and user needs to click on Return to Shop Name.

Screenshot-2019-11-03-at-5-32-05-PM

  • Payment done! Now you need to wait for webhook to get notification regarding status of payment.

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email vaibhavraj@vrajroham.me instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

ambitionphp/laravel-bitpay 适用场景与选型建议

ambitionphp/laravel-bitpay 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2 次下载、GitHub Stars 达 0, 最近一次更新时间为 2021 年 07 月 06 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「laravel」 「btc」 「bitapy」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 ambitionphp/laravel-bitpay 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 ambitionphp/laravel-bitpay 我们能提供哪些服务?
定制开发 / 二次开发

基于 ambitionphp/laravel-bitpay 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 2
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 3
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 37
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-07-06