prevailexcel/laravel-coinremitter

适用于 Coinremitter 的 Laravel 扩展包

1.0.0 2022-12-09 14:03 UTC

This package is auto-updated.

Last update: 2024-09-09 18:23:57 UTC


README

Latest Stable Version License

一个无缝工作与 Coinremitter 的 Laravel 扩展包

安装

需要 PHP 5.4+ 或 HHVM 3.3+,以及 Composer。

要获取 Laravel Coinremitter 的最新版本,只需将其包含即可。

composer require prevailexcel/laravel-coinremitter

或者将以下行添加到您的 composer.json 文件的 require 块中。

"prevailexcel/laravel-coinremitter": "1.0.*"

然后,您需要运行 composer installcomposer update 来下载它并更新自动加载器。

一旦安装了 Laravel Coinremitter,您需要注册服务提供者。打开 config/app.php 并将以下内容添加到 providers 键中。

如果您使用的是 Laravel >= 5.5,则可以跳过此步骤并转到 配置

'providers' => [
    ...
    PrevailExcel\Coinremitter\CoinremitterServiceProvider::class,
    ...
]
  • PrevailExcel\Coinremitter\CoinremitterServiceProvider::class

同时,注册 Facade 如下

'aliases' => [
    ...
    'Coinremitter' => PrevailExcel\Coinremitter\Facades\Coinremitter::class,
    ...
]

配置

您可以使用此命令发布配置文件

php artisan vendor:publish --provider="PrevailExcel\Coinremitter\CoinremitterServiceProvider"

一个名为 coinremitter.php 的配置文件,其中包含一些合理的默认值,将被放置在您的 config 目录中

<?php

return [

    'coins' => [

        'BTC' => [
            'api_key' => 'API_KEY_FROM_WEBSITE',
            'password' => 'PASSWORD',
        ],

        'LTC' => [
            'api_key' => 'API_KEY_FROM_WEBSITE',
            'password' => 'PASSWORD',
        ],
    ],
];

通用支付流程

尽管有多种支付订单的方式,但大多数支付网关都希望您在结账过程中遵循以下流程

1. 客户被重定向到支付提供商

当客户完成结账流程并准备支付时,客户必须被重定向到支付提供商的网站。

重定向是通过提交包含一些隐藏字段的表单来完成的。该表单必须向支付提供商的网站发送 POST 请求。隐藏字段至少指定了必须支付的金额、订单 ID。

2. 客户在支付提供商的网站上支付

客户到达支付提供商的网站并可以选择支付方式。支付订单所需的所有步骤都由支付提供商处理。

3. 客户被重定向回您的网站

支付订单后,客户被重定向回。在重定向请求到商店网站时,会返回一些值。这些值通常是订单 ID、支付结果。

使用方法

当使用 Facade 或 Helper 时,必须使用 request()->coin = 'COIN' 来传递货币。如果不这样做,则使用

use PrevailExcel\Coinremitter\Coinremitter;

$coinremitter = new Coinremitter('COIN');
$coinremitter->balance();

1. 将用户重定向到网关

// Laravel 5.1.17 and above
Route::post('/pay', 'PaymentController@redirectToGateway')->name('pay');

Route::post('/pay', [
    'uses' => 'PaymentController@redirectToGateway',
    'as' => 'pay'
]);

// Laravel 8 & 9
Route::post('/pay', [App\Http\Controllers\PaymentController::class, 'redirectToGateway'])->name('pay');
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Redirect;
use PrevailExcel\Coinremitter\Facades\Coinremitter;

class PaymentController extends Controller
{
    
    public function __construct()
    {
        if(!request()->coin)
            request()->coin = 'BTC'; //set a default coin
    }

    /**
     * Collect Order data and Redirect user to Payment gateway
     */
    public function redirectToGateway()
    {
        try{
            //You should collect the details you need from a form

           return Coinremitter::redirectToGateway();

           // or alternatively use the helper
           return coinremitter()->redirectToGateway();
            
        }catch(\Exception $e) {
            return Redirect::back()->withMessage(['msg'=>"There's an error in the data", 'type'=>'error']);
        }        
    }
}
/**
 *  In the case where you need to pass the data from your 
 *  controller instead of a form
 *  Make sure to send:
 *  required: amount, currency
 *  optionally: name, expire_time, notify_url, suceess_url, description, custom_data1,custom_data2'
 *  e.g:
 *  
 */
$data = array(
            'amount' => 500, //required
            'name' => 'random-name', //Name accepts only numbers, letters, hyphens.
            'currency' => 'usd',
            'expire_time' => '20', //optional, invoice will expire in 20 minutes. 
            'notify_url'=>'https://yourdomain.com/notify-url', //optional,url on which you wants to receive notification
            'fail_url' => 'https://yourdomain.com/fail-url', //optional,url on which user will be redirect if user cancel invoice,
            'suceess_url' => 'https://yourdomain.com/success-url', //optional,url on which user will be redirect when invoice paid,    
            'description' => '',
            'custom_data1' => '',
            'custom_data2' => '',
        );

return Coinremitter::redirectToGateway($data);

// or alternatively use the helper
return coinremitter()->redirectToGateway($data);

2. 如果您想使用自己的 UI

不将用户重定向到 Coinremitter 支付页面,您可以使用以下示例中的 Coinremitter::createInvoice()

// Laravel 5.1.17 and above
Route::post('/get-invoice', 'PaymentController@createCryptoPayment')->name('get.invoice');

Route::post('/get-invoice', [
    'uses' => 'PaymentController@createCryptoPayment',
    'as' => 'get.invoice'
]);

// Laravel 8 & 9
Route::post('/get-invoice', [App\Http\Controllers\PaymentController::class, 'createCryptoPayment'])->name('get.invoice');
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Redirect;
use PrevailExcel\Coinremitter\Facades\Coinremitter;

class PaymentController extends Controller
{
    
    public function __construct()
    {
        if(!request()->coin)
            request()->coin = 'BTC'; //set a default coin
    }

    /**
     * Collect Order data and Redirect user to Payment gateway
     */
    public function createCryptoPayment()
    {
        try{
            //You should collect the details you need from a form

            $invoiceDetails = Coinremitter::createInvoice();

            // or alternatively use the helper
            $invoiceDetails = coinremitter()->createInvoice();


            dd($invoiceDetails);
            // Now you have the payment details,
            // you can store the authorization_code in your db to allow for recurrent subscriptions
            // you can then redirect or do whatever you want

            
        }catch(\Exception $e) {
            return Redirect::back()->withMessage(['msg'=>"There's an error in the data", 'type'=>'error']);
        }        
    }
}
/**
 *  In the case where you need to pass the data from your 
 *  controller instead of a form
 *  Make sure to send:
 *  required: amount, currency
 *  optionally: name, expire_time, notify_url, suceess_url, description, custom_data1,custom_data2'
 *  e.g:
 *  
 */
$data = array(
            'amount' => 500, //required
            'name' => 'random-name', //Name accepts only numbers, letters, hyphens.
            'currency' => 'usd',
            'expire_time' => '20', //optional, invoice will expire in 20 minutes. 
            'notify_url'=>'https://yourdomain.com/notify-url', //optional,url on which you wants to receive notification
            'fail_url' => 'https://yourdomain.com/fail-url', //optional,url on which user will be redirect if user cancel invoice,
            'suceess_url' => 'https://yourdomain.com/success-url', //optional,url on which user will be redirect when invoice paid,    
            'description' => '',
            'custom_data1' => '',
            'custom_data2' => '',
        );

$invoiceDetails = Coinremitter::createInvoice($data);

// or alternatively use the helper
$invoiceDetails = coinremitter()->createInvoice($data);


dd($invoiceDetails);
// Now you have the payment details,
// you can store the authorization_code in your db to allow for recurrent subscriptions
// you can then redirect or do whatever you want

此扩展包提供的某些流畅方法列于此。

/**
 * This is the method to create an inoice. You need to provide your data as an array.
 * @returns array
 */
Coinremitter::createInvoice();

/**
 * Alternatively, use the helper.
 */
coinremitter()->createInvoice();


/**
 * This is the method to create an inoice and redirct user to payment gateway.
 * @returns array
 */
Coinremitter::redirectToGateway();

/**
 * Alternatively, use the helper.
 */
coinremitter()->redirectToGateway();


/**
 * Get balance of specified coin.
 * @returns array
 */
Coinremitter::balance();

/**
 * Alternatively, use the helper.
 */
coinremitter()->balance();


/**
 * Get crypto rate of given fiat_symbol and fiat_amount
 * @returns array
 */
Coinremitter::getRateFromFiat();

/**
 * Alternatively, use the helper.
 */
coinremitter()->getRateFromFiat();


/**
 * Get invoice details of given invoice id
 * @returns array
 */
Coinremitter::getInvoice()

/**
 * Alternatively, use the helper.
 */
coinremitter()->getInvoice();


/**
 * Get transaction details of given transaction address.
 */
Coinremitter::getTransactionByAddress();

/**
 * Alternatively, use the helper.
 */
coinremitter()->getTransactionByAddress();


/**
 *   Get transaction details of given transaction id.
 */
Coinremitter::getTransaction();

/**
 * Alternatively, use the helper.
 */
coinremitter()->getTransaction();


/**
 * Withdraw coin to specific address.
 * @returns array
 */
Coinremitter::withdraw();
/**
 * Alternatively, use the helper.
 */
coinremitter()->withdraw();


/**
 * Get new address for specified coin
 * @returns array
 */
Coinremitter::createAddress();
/**
 * Alternatively, use the helper.
 */
coinremitter()->createAddress();


/**
 * Validate address for specified coin.
 * @returns array
 */
Coinremitter::validateAddress();
/**
 * Alternatively, use the helper.
 */
coinremitter()->validateAddress();


/**
 *  Get all coins usd rate.
 * @returns array
 */
Coinremitter::getRates();
/**
 * Alternatively, use the helper.
 */
coinremitter()->getRates();

示例表单将如下所示

<form method="POST" action="{{ route('pay') }}" accept-charset="UTF-8" class="form-horizontal" role="form">
@csrf
    <div class="row" style="margin-bottom:50px;">
        <div class="col-md-8 col-md-offset-2">
            <p>
                <div>
                    Deluxe Package
                    0.01 BTC
                </div>
            </p>
            <input type="hidden" name="amount" value="1.5"> {{-- required --}}
            <input type="hidden" name="coin" value="BNB"> {{-- required -- Make sure you have set up your BTC wallet and have added it in your config file--}}
            <input type="hidden" name="name" value="username">
            <input type="hidden" name="metadata" value="email" > {{-- For other necessary things you want to add to your payload. it is optional though --}}
            
            <p>
                <button class="btn btn-success btn-lg btn-block" type="submit" value="Pay Now!">
                    <i class="fa fa-plus-circle fa-lg"></i> Pay Now!
                </button>
            </p>
        </div>
    </div>
</form>

贡献

请随意分支此扩展包,并通过提交拉取请求来贡献以增强功能。

我该如何感谢你?

为什么不给 github 仓库加星?我很乐意得到关注!为什么不分享此存储库的链接到 Twitter 或 HackerNews?传播消息!

别忘了 关注我的推特!还可以查看我在 medium 上的页面,以获取有关 Laravel 的文章和教程关注我的 medium

谢谢!Chimeremeze Prevail Ejimadu。

许可

MIT许可(MIT)。请参阅许可文件获取更多信息。