PHP code example of techtailor / laravel-paytm

1. Go to this page and download the library: Download techtailor/laravel-paytm library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

techtailor / laravel-paytm example snippets


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.  

// 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['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.

<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>

// 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>

// 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>

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"
]

$orderId = 'ORDER_ID_GENERATED_BY_THE_TRANSACTION_TOKEN';

$response = Paytm::getTransactionStatus($orderId);

$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.
bash
php artisan vendor:publish --tag="paytm-config"
bash
php artisan vendor:publish --tag=paytm-assets