blognevis / lnowpayments
适用于 NOWPayments 的 Laravel 扩展包
Requires
- guzzlehttp/guzzle: ^7.0.1
README
一个用于无缝处理 NOWPayments 的 Laravel 扩展包
此扩展包现在附带一个默认仪表板,显示所有付款的列表,并显示从您的应用程序访问的端点日志,提供流畅的方法轻松处理付款,并从您的工作中移除繁重的工作。
安装
需要 PHP 5.4+ 或 HHVM 3.3+ 以及 Composer。
要获取 Laravel NOWPayments 的最新版本,只需将其要求即可。
composer require prevailexcel/laravel-nowpayments
或者,将以下行添加到您的 composer.json
文件的 require 块中。
"prevailexcel/laravel-nowpayments": "1.0.*"
然后,您需要运行 composer install
或 composer update
来下载它并更新自动加载器。
一旦安装了 Laravel NOWPayments,您需要注册服务提供程序。打开 config/app.php
并将以下内容添加到 providers
键中。
'providers' => [ ... PrevailExcel\Nowpayments\NowpaymentsServiceProvider::class, ... ]
如果您使用 Laravel >= 5.5,则可以跳过此步骤并转到
配置
PrevailExcel\Nowpayments\NowpaymentsServiceProvider::class
此外,还可以这样注册 Facade
'aliases' => [ ... 'Nowpayments' => PrevailExcel\Nowpayments\Facades\Nowpayments::class, ... ]
配置
您可以使用此命令发布配置文件
php artisan vendor:publish --provider="PrevailExcel\Nowpayments\NowpaymentsServiceProvider"
一个名为 nowpayments.php
的配置文件,其中包含一些合理的默认值,将被放置在您的 config
目录中
<?php return [ /** * API Key From NOWPayments Dashboard * */ 'apiKey' => env('NOWPAYMENTS_API_KEY'), /** * IPN Secret from NOWPayments Dashboard */ 'ipnSecret' => env('NOWPAYMENTS_IPN_SECRET'), /** * You enviroment can either be live or sandbox. * Make sure to add the appropriate API key after changing the enviroment in .env * */ 'env' => env('NOWPAYMENTS_ENV', 'sandbox'), /** * NOWPayments Live URL * */ 'liveUrl' => env('NOWPAYMENTS_LIVE_URL', "https://api.nowpayments.io/v1"), /** * NOWPayments Sandbox URL * */ 'sandboxUrl' => env('NOWPAYMENTS_SANDBOX_URL', "https://api-sandbox.nowpayments.io/v1"), /** * Your callback URL * */ 'callbackUrl' => env('NOWPAYMENTS_CALLBACK_URL'), /** * Your URL Path * */ 'path' => 'laravel-nowpayments', /** * You can add your custom middleware to access the dashboard here * */ 'middleware' => null, // "Authorise::class", /** * Your Nowpayment email here * */ 'email' => env('NOWPAYMENTS_EMAIL'), /** * Your Nowpayment password here * */ 'password' => env('NOWPAYMENTS_PASSWORD'), ];
请记住运行迁移以添加一个用于记录的数据库表,可以使用以下命令
php artisan migrate
您可以通过提供您的应用程序并转到 /laravel-nowpayments 来测试仪表板,以查看您的设置是否就绪。
127.0.0.1:8000/laravel-nowpayments
您可以通过更改 config
文件夹中的 nowpayments.php
文件中的 'path' 来更改此默认路径/URL。
<?php return [ ... // 'path' => 'laravel-nowpayments', 'path' => 'new-endpoint', ];
通用电子商务付款流程
1 UI - 询问客户选择要购买的项目以确定总金额
2 UI - 询问客户选择付款货币
3 API - 调用 Nowpayments::createPayment() 方法;
将用户数据作为数组传递。此方法执行魔法。首先,它获取货币对的最低付款金额,然后获取加密货币总金额的估计,并检查它是否大于最低付款金额。
如果是真的,它发送有效负载并获取包含为用户生成的钱包地址的付款数据。
4 UI - 询问客户将付款发送到生成的存款地址。
5 UI - 客户发送代币,NOWPayments 处理并兑换(如有必要),并将付款结算到您的结果钱包。
6 API - 检查付款状态
您可以通过 NOWPayments IPN 回调或手动使用 "nowpayments()->getPaymentStatus()" 来获取付款状态,并将其显示给客户,以便他们知道付款何时被处理。
7 在仪表板中检查所有付款的列表
此扩展包附带一个默认仪表板,显示所有付款的列表,并显示从您的应用程序访问的端点日志。此外,您还可以在 NOWPayments 网站上的您的账户中查看所有这些信息。
用法
打开您的 .env 文件并添加您的 API 密钥、环境、回调 URL,如下所示
NOWPAYMENTS_ENV="live" NOWPAYMENTS_API_KEY="*******-*******-*******-*******" NOWPAYMENTS_CALLBACK_URL="https://yourcallback.com" NOWPAYMENTS_EMAIL="hello@example.com" NOWPAYMENTS_PASSWORD="your password"
如果您正在使用像 Heroku 这样的托管服务,请确保将上述详细信息添加到您的配置变量中。
// Laravel 5.1.17 and above Route::post('/pay', 'PaymentController@createCryptoPayment')->name('pay');
或者
Route::post('/pay', [ 'uses' => 'PaymentController@createCryptoPayment', 'as' => 'pay' ]);
或者
// Laravel 8 & 9 Route::post('/pay', [App\Http\Controllers\PaymentController::class, 'createCryptoPayment'])->name('pay');
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Http\Requests; use App\Http\Controllers\Controller; use Illuminate\Support\Facades\Redirect; use PrevailExcel\Nowpayments\Facades\Nowpayments; class PaymentController extends Controller { /** * Collect Order data and create Payment * @return Url */ public function createCryptoPayment() { try{ $data = [ 'price_amount' => request()->price_amount ?? 100, 'price_currency' => request()->price_currency ?? 'usd', 'order_id' => request()->order_id ?? uniqid(), // you can generate your order id as you wish 'pay_currency' => request()->pay_currency ?? 'btc', 'payout_currency' => request()->payout_currency ?? 'btc', ]; $paymentDetails = Nowpayments::createPayment($data); dd($paymentDetails); // Now you have the payment details, // you can then redirect or do whatever you want return Redirect::back()->with(['msg'=>"Payment created successfully", 'type'=>'success'], 'data'=>$paymentDetails); }catch(\Exception $e) { return Redirect::back()->withMessage(['msg'=>"There's an error in the data", 'type'=>'error']); } } }
此扩展包提供的一些流畅方法如下。
/** * This is the method to create a payment. You need to provide your data as an array. * @returns array */ Nowpayments::createPayment(); /** * Alternatively, use the helper. */ nowpayments()->createPayment(); /** * Gets the payment details of a particular transaction including the status with the paymentId * @returns array */ Nowpayments::getPaymentStatus(); /** * Alternatively, use the helper. */ nowpayments()->getPaymentStatus(); /** * Get all currenices * @returns array */ Nowpayments::getCurrencies() /** * Alternatively, use the helper. */ nowpayments()->getCurrencies(); /** * Get the minimum payment amount for a specific pair. */ Nowpayments::getMinimumPaymentAmount(); /** * Alternatively, use the helper. */ nowpayments()->getMinimumPaymentAmount(); /** * Creates invoice with url where user can complete the payment. * @returns array */ Nowpayments::createInvoice(); /** * Alternatively, use the helper. */ nowpayments()->createInvoice(); /** * This method allows you to obtain information about all the payment plans you’ve created. * @returns array */ Nowpayments::getPlans(); /** * Alternatively, use the helper. */ nowpayments()->getPlans(); /** * Get information about a particular recurring payment via its ID. * @returns array */ Nowpayments::getSubscription(); /** * Alternatively, use the helper. */ nowpayments()->getSubscription(); /** * This method allows you to send payment links to your customers via email. * @returns array */ Nowpayments::emailSubscription(); /** * Alternatively, use the helper. */ nowpayments()->emailSubscription(); /** * Completely removes a particular payment from the recurring payment plan. * @returns array */ Nowpayments::deleteSubscription(); /** * Alternatively, use the helper. */ nowpayments()->deleteSubscription(); /** * Returns the entire list of all transactions, created with certain API key. * @returns array */ Nowpayments::getListOfPayments(); /** * Alternatively, use the helper. */ nowpayments()->getListOfPayments(); /** * This method gets the estitmate price of an amount in different pairs * @return array */ Nowpayments::getEstimatePrice(); /** * Alternatively, use the helper. */ nowpayments()->getEstimatePrice();
待办事项
- 添加全面的测试
- 添加对账单端点的支持
- 添加对付款端点的支持
贡献
请随意fork这个包,并通过提交pull request来增强其功能。
我该如何感谢你呢?
为何不star GitHub仓库?我非常希望得到关注!为何不在Twitter或HackerNews上分享这个仓库的链接?让更多人知道!
别忘了关注我!也请访问我的medium页面,查看关于Laravel的文章和教程,关注我!
谢谢!Chimeremeze Prevail Ejimadu。
许可协议
MIT许可协议(MIT)。请参阅许可文件以获取更多信息。