Apps Script
Paytm Payment Gateway
Integration
Setup Guide
Source Code
Integrating Paytm allows you to accept UPI, Credit Card, and Debit Card payments directly in your Google Apps Script web app. We use a dedicated library to simplify the checksum generation.
Step 1
Get the Library
Purchase the Paytm Payment Gateway Library. It handles all the complex encryption and verification logic for you.
Get Library Here
Get Library Here
Step 2
Add to Project
1. Open your Apps Script Project.
2. Click on "Libraries" (+) in the left sidebar.
3. Paste the Script ID you received after purchase.
4. Select the latest version and click "Add".
2. Click on "Libraries" (+) in the left sidebar.
3. Paste the Script ID you received after purchase.
4. Select the latest version and click "Add".
Step 3
Configure Credentials
Replace
YOUR_MID and YOUR MERCHANT KEY in the code with your actual credentials from the Paytm Dashboard.
Set testing = false when you are ready to go live.
Secure
Checksum verification ensures no data tampering during the transaction.
Web App
Deploy as a Web App to get the URL needed for the callback.
Copy this code into your Code.gs file.
var mid = "YOUR_MID";
var paytmMerchantKey = 'YOUR MERCHANT KEY';
var testing = true; // SET FALSE FOR PRODUCTION
// AUTO-DETECT SCRIPT URL
var callbackurl = ScriptApp.getService().getUrl();
var WEBSITE = 'DEFAULT';
if(testing) {
WEBSITE = 'WEBSTAGING';
}
// 1. GENERATE PAYMENT PAGE
function doGet(e){
var params = {
'MID': mid,
'ORDER_ID': 'oid_' + Math.ceil(Math.random()*9999999),
'CUST_ID': 'cid120',
'TXN_AMOUNT': 1, // SET AMOUNT HERE
'CHANNEL_ID': 'WEB',
'INDUSTRY_TYPE_ID': 'Retail',
'WEBSITE': WEBSITE,
'CALLBACK_URL': callbackurl,
};
// Library generates the HTML form automatically
var html = Paytm.getPaymentHtml(params, paytmMerchantKey, testing);
return html;
}
// 2. HANDLE PAYMENT RESPONSE
function doPost(e) {
var params = e.parameter; // Response from Paytm
var check = false;
var result = "";
if(params.CHECKSUMHASH){
// Verify signature
check = Paytm.verifychecksum(params, paytmMerchantKey, params.CHECKSUMHASH);
if(check && params.STATUS == 'TXN_SUCCESS') {
result = "Payment Successful! Order ID: " + params.ORDERID;
// ADD YOUR LOGIC HERE (e.g. Save to Google Sheets)
} else {
result = "Payment Failed or Signature Mismatch.";
}
} else {
result = 'No checksum found in response.';
}
return ContentService.createTextOutput(result);
}
// 3. CHECK TRANSACTION STATUS MANUALLY
function checkStatus(){
var params = {
'MID': mid,
'ORDER_ID': 'ORDERID_TO_CHECK'
}
var cksm = Paytm.getChecksum(params, paytmMerchantKey).toString();
params.CHECKSUMHASH = cksm;
var statusurl = 'https://securegw.paytm.in/merchant-status/getTxnStatus';
if(testing)
statusurl ='https://securegw-stage.paytm.in/merchant-status/getTxnStatus';
var options ={
'method': 'post',
'payload': JSON.stringify(params)
}
var response = UrlFetchApp.fetch(statusurl, options).getContentText();
console.log(response);
}
Copy Code
Get Library
×
0 Comments