niush / laravel-nano-to
Laravel 中的 Nano.to 支付网关
Requires
- php: ^7.2|^8.0
- guzzlehttp/guzzle: ^7.3
- illuminate/support: ^6.0|^7.0|^8.0|^9.0
Requires (Dev)
- orchestra/testbench: ^7.0
- phpunit/phpunit: ^9.0
README
轻松集成 Nano.to 支付网关到 Laravel 应用程序,并完全控制。
Laravel 上安装
您可以通过 composer 安装此软件包
composer require niush/laravel-nano-to
然后使用 artisan 发布配置文件。
php artisan vendor:publish --provider="Niush\NanoTo\NanoToServiceProvider"
更新配置文件(《config/nano-to.php》)以使用您想要的设置。在需要的地方使用 env。
配置
在您的 env 文件中添加 Nano Webhook Secret。确保它是一个难以猜测的随机字符串。不要使用与 APP_ENV 相同的内容。
NANO_WEBHOOK_SECRET=
查看生成的配置文件并根据需要进行更新。确保所有 Nano 地址都属于您并且可访问。您可以提供默认标题和描述。
必需的命名路由
为了正确工作,您必须创建以下 3 个命名路由来处理您的业务逻辑。命名路由可以在配置文件中自定义。
-
nano-to-success:当支付成功时重定向。不要使用此路由确认支付。确认必须在 webhook URL 中处理。Route::get('/order/success/{id}', [OrderController::class, 'success'])->name('nano-to-success'); // E.g. Shows a static order success page.
-
nano-to-cancel:当使用后退按钮取消支付时重定向。这对于将用户重定向回购物车页面等很有用。Route::get('/order/cancel/{id}', [OrderController::class, 'cancel'])->name('nano-to-cancel'); // E.g. Redirects back to Cart Page
-
nano-to-webhook:当支付成功处理后,Nano.to 会调用此 POST 路由。请求包含在头部中的支付信息、哈希和 webhook secret。更多信息请见下文。// PERFORM PAYMENT CONFIRMED BUSINESS LOGIC Route::post('/order/webhook/{id}', [OrderController::class, 'webhook'])->name('nano-to-webhook');
使用完整路由代替
默认情况下,配置文件接受 webhook 和成功页面的命名路由。如果您想使用完整路由,例如在具有不同域名和后端处理 webhook 或成功页面的情况下,您可以更新配置文件中的 success_url、cancel_url 和 webhook_url 以包含带有域名的完整 URL。
由于您拥有完全控制权,因此它也可以通过 API 轻松实现。
使用方法
注意:金额始终以美元货币计算。
对于启动支付过程
// 1) With Specific Amount return NanoTo::amount(10)->create($order->id, function($checkout_url) { // Do Something with $checkout_url if required. // For SPA send link as JSON response etc. })->send(); // 2) With Custom Info (Else uses title and description from config) return NanoTo::info("Payment for Subscription", "<i>Also accepts HTML</i>") ->amount(9.99) ->create($order->id)->send(); // 3) With Additional Metadata (Can be received in Webhook) return NanoTo::amount(9.99)->metadata([ "user_id" => $user->id, "order_id" => $order->id ])->create($order->id)->send(); // 4) For Suggest based payment. Useful in cases like Donation. return NanoTo::info("Donate Us") ->suggest([ ["name" => "Coffee", "price" => "10"], ["name" => "Meal", "price" => "50"] ]) ->create($uuid)->send(); // 5) Use RAW friendly Amount in QR Codes (e.g. for Natrium) return NanoTo::asRaw()->amount(9.99)->create($order->id)->send(); // 6) With Custom Image in Checkout page return NanoTo::withImage("full_url_of_image")->amount(9.99)->create($order->id)->send(); // 7) Or Simply, if no need to track anything. And, required routes do not need {id} param. return NanoTo::create(); // Receiving Nano Address will randomly be picked from config file. // The first parameter of create (e.g. $order->id) will be used as params in named routes.
您可能想要使用自定义 Webhook Secret,以便每次结账时都不同。因此,您不需要使用相同的环境变量。您可以进行以下操作
return NanoTo::amount(10)->secret( config("nano-to.webhook_secret") . $order->secret_id . $user->id )->create($order->id)->send();
Webhook 响应示例
// Request Headers { "Accept": "application/json, text/plain, */*", "Connection": "close", "Content-Length": "1005", "Content-Type": "application/json", "Webhook-Secret": "XXXXXXXXXXXXXXXX" }
// You can get the Header and compare with your config secret. In Webhook Controller: $request->header('Webhook-Secret') == config("nano-to.webhook_secret") // Valid
// Request Body (JSON) { "id": "ffceexxxxxx", // Transaction ID of Nano.to "status": "complete", // Status must be complete "amount": "10", // Amount in USD "method": { "symbol": "nano", // Crypto Currency Used "address": "nano_3gxhq...", // Receiving Address "name": "Nano", "rate": "5.589960", // Currency Rate (Nano → USD) "amount": "1.788115", // Nano Received "value": "0.01", "raw": false }, "plan": { // If using Suggest mode "price": 10, "name": "Meal" }, "block": { // Block Information "type": "receive", // You must be receiving :) "representative": "nano_3chart...", "balance": "3.726479", "previous": "1922BFA40E86C....", "account": "nano_36qn7ydq...", // Sender Address "amount": "1.788115", // Nano Received "height": "37", "hash": "9829B0306E5269A9A0...", // Transaction Identifier (Most important piece to store.) "work": "210862fa...", "timestamp": "16319544..", "amount_raw": "1788115000000000000..." // RAW Nano "balance_raw": "372647920414...", "from": "nano_36qn7ydq..." "to": "nano_3gxhq..." "message": "" }, "metadata": { // All Additional Metadata sent "user_id": "my_meta" } }
// Compare the body, store required info in DB and finally update the order status. In Webhook Controller. $request->input('amount') == $order->amount_in_usd; $request->input('status') == "complete"; $request->input('block.type') == "receive"; // You can also compare receiver address is in config or not. $order->via = "nano"; $order->hash = $request->input('block.hash'); $order->status = "complete"; $order-save();
Webhook 控制器完整示例
点击展开!
public function webhook(Request $request, Order $order) { if($request->header('Webhook-Secret') != config("nano-to.webhook_secret")) { $order->status = "failed"; // Webhook Secret is MALFORMED $order->remarks = "Payment Verification Malformed"; } else { if( $request->input('amount') == $order->amount_in_usd && $request->input('status') == "complete" && $request->input('block.type') == "receive" && $request->input('block.hash') ) { $order->status = "complete"; $order->hash = $request->input('block.hash'); $order->remarks = "Payment Complete from Address: " . $request->input('block.account') . " , with Amount: " . $request->input('method.amount'); $order->save(); } else { $order->status = "failed"; // Payment Amount is not correct or not complete etc. $order->remarks = "Payment Was Not Fully Completed"; } } // You can also utilize Metadata for verification: // $request->input('metadata.user_id') == $order->user_id; $order->save(); return ["success" => true]; }
高级使用(API/助手)
use Niush\NanoTo\NanoToApi; // 1) Get CoinMarketCap conversion rate NanoToApi::getPrice("NANO", "USD"); NanoToApi::getPrice("XMR", "NPR"); NanoToApi::getPrice("NANO", "XMR"); // 2) Get Nano.to Custom Username alias information NanoToApi::getUsername("moon"); // 3) Get Nano Address Information (OR Nano.to alias info) NanoToApi::getNanoAddressInfo("nano_3xxxx"); // 4) Get Total Nano Balance from all nano address provided in config file NanoToApi::getTotalNanoBalance(); // 5) Get Pending Nano Blocks NanoToApi::getPendingNanoBlocks("nano_3xxxx"); // 6) Get Last 50+ Block History NanoToApi::getNanoAddressHistory("nano_3xxxx"); // 7) Get Nano Transaction by specific Amount (Amount must be in Nano decimal format) NanoToApi::getNanoTransactionByAmount("nano_3xxxx", "2.101"); // 8) Get Nano Transaction by block HASH NanoToApi::getNanoTransactionByHash("NANO_HASH"); // 9) Get JSON Representation of given checkout URL. Only has 12 hour lifespan. NanoToApi::getCheckoutUrlAsJson("https://nano.to/checkout/xxx"); // 10) Get List Of Public Representatives for Nano. And, Search by first parameter. NanoToApi::getListOfPublicRepresentatives("ninja"); // 11) Get List Of Nano.to known Usernames. And, Search by first parameter. NanoToApi::getListOfNanoUsernames("esteban"); // 12) Check if nanocrawler is down or unreachable. Returns boolean true if down. NanoToApi::isNanoCrawlerDown(); // 13) Check if Nano.to base_url is down or unreachable. Returns boolean true if down. NanoToApi::isNanoToDown();
翻译
如果需要,请添加这些消息的翻译。
nano-to.checkout-page-not-loaded= "无法加载结账页面"。nano-to.no-receiver= "接收者账户不可用"。
测试
欢迎贡献。
composer test
变更日志
请参阅 CHANGELOG 了解最近更改的信息。
贡献
请参阅 CONTRIBUTING 了解详细信息。
安全
如果您发现任何与安全相关的问题,请通过电子邮件联系作者,而不是使用问题跟踪器。
致谢
许可
MIT 许可证(MIT)。有关更多信息,请参阅 许可文件。
Nano.to 是 Forward Miami, LLC 的产品。⚡
帮助我使用 Nano?
-
什么是 Nano?
Nano 是现代世界的免费、环保、即时数字货币。
-
如何验证或查看我的交易区块信息?
显示支持
nano_378shkx4k3wd5gxmj3xnjwuxtaf9xrehyz7ugakpiemh8arxq8w9a9xniush