mollsoft / laravel-bitcoin-module

Laravel Bitcoin Module

v1.0.7 2023-12-14 14:45 UTC

This package is auto-updated.

Last update: 2024-09-14 16:25:20 UTC


README

Pest Laravel Expectations

Latest Version on Packagist Php Version Php Version Total Downloads

Website Telegram

Laravel Bitcoin Module 是一个用于处理加密货币比特币的 Laravel 扩展包。您可以创建描述符钱包、生成地址、跟踪当前余额、收集交易历史、在您的网站上组织支付接受,以及自动化出金转账。

您可以通过联系我获取将支付接受集成到您项目的帮助。

示例

创建描述符钱包

$name = 'my-wallet';
$password = 'password for encrypt wallet files';
$title = 'My First Wallet';

$node = Bitcoin::createNode('localhost', 'LocalHost', '127.0.0.1');
$wallet = Bitcoin::createWallet($node, $name, $password, $title);

使用描述符导入描述符钱包

$name = 'my-wallet';
$password = 'password for encrypt wallet files';
$descriptions = json_decode('DESCRIPTORS JSON', true);
$title = 'My First Wallet';

$node = Bitcoin::createNode('localhost', 'LocalHost', '127.0.0.1');
$wallet = Bitcoin::importWallet($node, $name, $descriptions, $password, $title);

创建地址

$wallet = BitcoinWallet::firstOrFail();
$title = 'My address title';

$address = Bitcoin::createAddress($wallet, AddressType::BECH32, $title);

验证地址

$address = '....';

$node = BitcoinNode::firstOrFail();
$addressType = Bitcoin::validateAddress($node, $address);
if( $addressType === null ) {
    die('Address is not valid!');
} 

var_dump($addressType); // Enum value of AddressType

从钱包发送所有 BTC

$wallet = BitcoinWallet::firstOrFail();
$address = 'to_address';

$txid = Bitcoin::sendAll($wallet, $address);

echo 'TXID: '.$txid;

从钱包发送 BTC

$wallet = BitcoinWallet::firstOrFail();
$address = 'to_address';
$amount = 0.001;

$txid = Bitcoin::send($wallet, $address, $amount);

echo 'TXID: '.$txid;

安装

您可以通过 composer 安装此包

composer require mollsoft/laravel-bitcoin-module

之后,您可以使用以下命令运行安装程序

php artisan bitcoin:install

并运行迁移

php artisan migrate

在 app 目录中注册服务提供者和外观,编辑 config/app.php

'providers' => ServiceProvider::defaultProviders()->merge([
    ...,
    \Mollsoft\LaravelBitcoinModule\BitcoinServiceProvider::class,
])->toArray(),

'aliases' => Facade::defaultAliases()->merge([
    ...,
    'Bitcoin' => \Mollsoft\LaravelBitcoinModule\Facades\Bitcoin::class,
])->toArray(),

app/Console/Kernel 文件中的 schedule(Schedule $schedule) 方法中添加

$schedule->command('bitcoin:sync')
    ->everyMinute()
    ->runInBackground();

命令

扫描交易并更新余额

> php artisan bitcoin:sync

扫描交易并更新钱包的余额

> php artisan bitcoin:sync-wallet {wallet_id}

WebHook

您可以设置一个 WebHook,当检测到新的 BTC 到账时将被调用。

在 config/bitcoin.php 文件中,您可以设置以下参数

'webhook_handler' => \Mollsoft\LaravelBitcoinModule\WebhookHandlers\EmptyWebhookHandler::class,

示例 WebHook 处理程序

class EmptyWebhookHandler implements WebhookHandlerInterface
{
    public function handle(BitcoinWallet $wallet, BitcoinAddress $address, BitcoinDeposit $transaction): void
    {
        Log::error('Bitcoin Wallet '.$wallet->name.' new transaction '.$transaction->txid.' for address '.$address->address);
    }
}

要求

此版本支持以下 PHP 版本。

  • PHP 8.2 及以下
  • PHP 扩展:Decimal。