techtailor / laravel-paytm
A Laravel wrapper for the Paytm Payment Gateway (PG). Now quickly setup and accept UPI, Credit/Debit Card, Net Banking & EMI payments via Paytm PG starting today.
Fund package maintenance!
TechTailor
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
This package provides a simple Laravel wrapper for the Paytm PG (Payment Gateway) to allow you to easily spin up a new laravel app and start accepting UPI, Wallet, Credit/Debit, Net Banking & EMI payments without any hassle. Before getting started, be sure to go through the Paytm documentation to have a better understanding.
Note: You also need to have a valid Paytm Business Merchant Account to start using the Paytm PG (Payment Gatteway)
Installation
You can install the package via composer:
composer require TechTailor/Laravel-Paytm
You can publish the config file with:
php artisan vendor:publish --tag="paytm-config"
You must publish the assets file using:
php artisan vendor:publish --tag=paytm-assets
Usage
Before you can start using this package, you must familiarize yourself with the payment workflow for the Paytm PG. Read official documentation here
To give a basic gist, you generate a unique payment token by providing the amount and customer details, then use the txnId and orderId received in the response to initiate the Checkout JS in your front-end.
Setup Environment
Add the following variables to your .env file, or alternatively, you can publish the config file and update it accordingly.
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_"
A bit of explainantion -
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.
Step # 1 - Generate a Token
// 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);
The $response will return an array containing:
$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.
Step # 2 - Invoking Payment Page & Collecting Payment
You can read detailed documentation for Paytm Checkout JS here.
Setup Frontend
Start by adding the @paytmScripts
tag into the <head>
tag of your page. For ex:
<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>
Next, to initiate the Checkout Payment Page, you have 2 available methods -
Method # 1
This method will invoke the payment gateway and upon completion, will redirect the user to the callback url set earlier (or in the config file).
To achieve that, you need to call the openJsCheckoutPopup(orderId, txnToken, amount)
function and pass it the orderId
, txntoken
and the amount
received in Step # 1.
// 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>
Upon clicking the Pay Now
button, a pop-up for the Paytm PG will open with all the options to make the payment. Once the payment is complete, you will be redirected to the callback_url
set in the .env file as PAYTM_CALLBACK_URL
.
Method # 2
This method will allow you to handle the response on the same page (and ignore any callback urls set for this transaction). Useful when you want to process transaction without a redirect.
// 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>
Once you set the redirect flag as false, you can use the paymentCompleted
function to excute further queries using the returned data.
Step # 3 - Receiving Response
The response from the Paytm PG (via callback or same page) will provide an array containing the following (for a successful transaction) -
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" ]
Using the above data, you can update the order details in your database and add the transaction and order id to it and process it accordingly.
Verify Transaction
You can check and verify the status of any transaction using just the OrderId.
$orderId = 'ORDER_ID_GENERATED_BY_THE_TRANSACTION_TOKEN'; $response = Paytm::getTransactionStatus($orderId);
The $response will return an array containing:
$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.
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.
Patym, Paytm PG, Paytm for Business and all related logos are registered trademarks of and belongs to One97 Communications Limited.