techtailor / laravel-paytm
Paytm支付网关(PG)的Laravel包装器。现在可以快速设置并接受通过Paytm PG的UPI、信用卡/借记卡、网上银行和EMI支付。
Requires
- php: ^8.1
- illuminate/contracts: ^9.0
- paytm/paytmchecksum: ^1.1
- spatie/laravel-package-tools: ^1.9.2
Requires (Dev)
- nunomaduro/collision: ^6.0
- nunomaduro/larastan: ^2.0.1
README
此包提供了一个简单的Laravel包装器,用于Paytm PG(支付网关),以便您可以轻松启动新的laravel应用程序,并开始接受UPI、钱包、信用卡/借记卡、网上银行和EMI支付,无需任何麻烦。在开始之前,请务必阅读Paytm的文档,以获得更好的理解。
注意:您还需要拥有有效的Paytm商业商户账户才能开始使用Paytm PG(支付网关)
安装
您可以通过composer安装此包
composer require TechTailor/Laravel-Paytm
您可以使用以下命令发布配置文件
php artisan vendor:publish --tag="paytm-config"
您必须使用以下命令发布资产文件
php artisan vendor:publish --tag=paytm-assets
使用
在您可以使用此包之前,您必须熟悉Paytm PG的支付工作流程。请在此处阅读官方文档here
基本来说,您通过提供金额和客户详情来生成唯一的支付令牌,然后使用响应中接收到的txnId和orderId在您的前端中启动Checkout JS。
设置环境
将以下变量添加到您的.env文件中,或者您也可以发布配置文件并相应地更新它。
PAYTM_ENV="testing" PAYTM_MERCHANT_ID="_YOUR_MERCHANT_ID_FROM_PAYTM_" PAYTM_MERCHANT_KEY="_YOUR_MERCHANT_KEY_FROM_PAYTM_" PAYTM_WEBSITE="WEBSTAGING" PAYTM_CALLBACK_URL="_YOUR_APP_CALLBACK_URL_" PAYTM_ORDER_ID_PREFIX="PAYTM_ORDERID_"
一些解释 -
PAYTM_ENV - Your payments environment. Can be set to "testing" or "production". PAYTM_MERCHANT_ID - Your Unique Merchant ID from Paytm. Use the test id and key when in the testing environment. PAYTM_MERCHANT_KEY - Your Unique Merchant Key from Paytm .Keep it safe. PAYTM_WEBSITE - Set it to "WEBSTAGING" for testing environment or to "DEFAULT" when in the production environment. You can also use a custom one after setting it up in your Paytm Bussiness Dashboard. PAYTM_CALLBACK_URL - The url to redirect to after payment is completed. Ex: https://yoursite.com/callback/paytm PAYTM_ORDER_ID_PREFIX - Your custom prefix for the order id. Can be anything.
步骤 # 1 - 生成令牌
// Import facade at the top use TechTailor\Paytm\Facades\Paytm; $amount = '1.0'; // Amount to charge the customer. Can be an integer or a float value upto 2 decimals. $customer = array( 'custId' => $custId, // MANDATORY - A unique identifier generated by your system. 'mobile' => $mobile, // OPTIONAL - Required in case of EMI Payment Option is selected. 'email' => $email, // OPTIONAL 'firstName' => $firstName, // OPTIONAL 'lastName' => $lastName // OPTIONAL ); // This is an optional url which you can pass to customize the callback url per transaction. // If null is provided, the app will use the callback url set in the config/paytm.php file. $callback = 'https://yourwebsite.com/callback/new'; // Call the getTransactionToken function. $response = Paytm::getTransactionToken($amount, $customer, $callback);
$response将返回一个包含
$response['success'] => true, // true or false $response['orderId'] => $orderId, // unique order_id generated for this txn $response['txnToken'] => $token, // unique transaction token. Only if 'success' => true $response['amount'] => $amount, // amount to be paid $response['message'] => 'Success', // a response message according to the result. Ex: Success, System error, Failed, etc.
步骤 # 2 - 调用支付页面并收集支付
您可以在这里阅读Paytm Checkout JS的详细文档。
设置前端
首先,将@paytmScripts
标签添加到您的页面中的<head>
标签中。例如
<head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Laravel</title> <!-- Fonts --> <link href="https://fonts.bunny.net/css2?family=Nunito:wght@400;600;700&display=swap" rel="stylesheet"> <!-- Scripts --> @paytmScripts </head>
接下来,要启动Checkout支付页面,您有2种可用的方法 -
方法 # 1
此方法将调用支付网关,并在完成时将用户重定向到之前设置的回调URL(或在配置文件中设置)。
为此,您需要调用openJsCheckoutPopup(orderId, txnToken, amount)
函数,并传递在步骤 # 1中接收到的orderId
、txntoken
和amount
。
// Somewhere in your page <button type="button" id="JsCheckoutPayment" name="submit" class="btn btn-primary">Pay Now</button> // Before the closing </body> tag <script type="application/javascript"> document.getElementById("JsCheckoutPayment").addEventListener("click", function() { var orderId = "{{ $response['orderId'] }}"; var txnToken = "{{ $response['txnToken'] }}"; var amount = "{{ $response['amount'] }}"; openJsCheckoutPopup(orderId, txnToken, amount); } ); </script>
点击“立即支付”按钮后,将打开一个带有所有支付选项的Paytm PG弹出窗口。支付完成后,您将被重定向到在.env文件中设置的PAYTM_CALLBACK_URL
中设置的回调URL。
方法 # 2
此方法允许您在相同页面上处理响应(并忽略为此交易设置的任何回调URL)。当您想在不进行重定向的情况下处理事务时非常有用。
// Somewhere in your page <button type="button" id="JsCheckoutPayment" name="submit" class="btn btn-primary">Pay Now</button> // Before the closing </body> tag <script type="application/javascript"> document.getElementById("JsCheckoutPayment").addEventListener("click", function() { var orderId = "{{ $response['orderId'] }}"; var txnToken = "{{ $response['txnToken'] }}"; var amount = "{{ $response['amount'] }}"; // Pass an additional "false" attribute which marks redirect as false. openJsCheckoutPopup(orderId, txnToken, amount, false); } ); // To be executed upon completion of the payment (only if false is passed above). function paymentCompleted(paymentStatus) { window.Paytm.CheckoutJS.close(); // Close the Paytm PG Pop-up. console.log(paymentStatus); // Log or use the payment status/details returned. } </script>
一旦将重定向标志设置为false,您就可以使用返回的数据调用paymentCompleted
函数来执行进一步的查询。
步骤 # 3 - 接收响应
来自Paytm PG(通过回调或相同页面)的响应将提供以下数组(对于成功的交易) -
array:14 [▼ "BANKNAME" => "State Bank of India" "BANKTXNID" => "10319428304" "CHECKSUMHASH" => "8bEpNUiRkfmLodMtsqV4ZUYaUL1QzhUsM=......." "CURRENCY" => "INR" "GATEWAYNAME" => "SBI" "MID" => "YOUR_MERCHANT_ID" "ORDERID" => "PAYTM_ORDERID_1661341467" "PAYMENTMODE" => "NB" "RESPCODE" => "01" "RESPMSG" => "Txn Success" "STATUS" => "TXN_SUCCESS" "TXNAMOUNT" => "10.00" "TXNDATE" => "2022-08-24 17:14:28.0" "TXNID" => "20220824111212800110168559404001025" ]
使用上述数据,您可以在数据库中更新订单详情,并将交易和订单ID添加到其中,并相应地处理它。
验证交易
您只需使用订单号即可检查和验证任何交易的状态。
$orderId = 'ORDER_ID_GENERATED_BY_THE_TRANSACTION_TOKEN'; $response = Paytm::getTransactionStatus($orderId);
$response将返回一个包含
$response['success'] => true, // true or false $response['orderId'] => $orderId, // unique order_id generated for this txn $response['txnToken'] => $token, // unique transaction token. Only if 'success' => true $response['amount'] => $amount, // amount to be paid $response['message'] => '', // a response message according to the result.
测试
composer test
变更日志
有关最近变更的详细信息,请参阅变更日志。
贡献
有关详细信息,请参阅贡献指南。
安全漏洞
请查阅我们的安全策略了解如何报告安全漏洞。
致谢
许可证
MIT许可证(MIT)。有关更多信息,请参阅许可证文件。
Patym、Paytm PG、Paytm for Business以及所有相关标志均为One97 Communications Limited的注册商标。