search

Python SDK Integration

Installation Guide

Requirements

Python 3.5 or later

 

Installing Paytm's payment gateway SDK with pip

Install Python package by using below command

pip install paytm-pg

Generate API Keys

ATTRIBUTE DESCRIPTION
MID A unique merchant identifier issued by Paytm for your account.
Merchant Key A 16-digit unique merchant identifier issued by Paytm for your account.

Note: Merchant Key is a secret key used for encryption so never share this with anyone. And, You have to generate separate API Keys for test and production environment.

 

Steps to generate API keys

  1. Log into your Dashboard.
  2. Select the environment (Test/Production) for which you want to generate the API key.
  3. For Test mode, click Generate Key to generate key under Test API details tab.
  4. For Production mode, activate your account here to generate Production API details.

SDK Code

 

Initialization

Merchant will initialize the following mandatory parameters: environment, mid, key and website, after which merchant can directly call SDK methods to call APIs of payment gateway.

# For Staging 
environment = LibraryConstants.STAGING_ENVIRONMENT

# For Production 
# environment = LibraryConstants.PRODUCTION_ENVIRONMENT

# Find your mid, key, website in your Paytm Dashboard at https://dashboard.paytm.com/next/apikeys
mid = "YOUR_MID_HERE"
key = "YOUR_KEY_HERE"
website = "YOUR_WEBSITE_NAME"
client_id = "YOUR_CLIENT_ID_HERE"

callbackUrl = "MERCANT_CALLBACK_URL"
MerchantProperty.set_callback_url(callbackUrl)

MerchantProperty.initialize(environment, mid, key, client_id, website)
 # If you want to add log file to your project, use below code
file_path = '/path/log/file.log'
mode = "w"
handler = logging.FileHandler(file_path, mode)
formatter = logging.Formatter("%(name)s: %(levelname)s: %(message)s")
handler.setFormatter(formatter)
MerchantProperty.set_log_handler(handler)
MerchantProperty.set_logging_disable(False)
MerchantProperty.set_logging_level(logging.DEBUG)

Note: Make sure log file (/path/log/file.log) has write permission.

 

Payments


Create Transaction Token

channel_id = EChannelId.WEB
order_id = "UNIQUE_ORDER_ID"
txn_amount = Money(EnumCurrency.INR, "1.00")
user_info = UserInfo()
user_info.set_cust_id("CUSTOMER_ID")
user_info.set_address("CUSTOMER_ADDRESS")
user_info.set_email("CUSTOMER_EMAIL_ID")
user_info.set_first_name("CUSTOMER_FIRST_NAME")
user_info.set_last_name("CUSTOMER_LAST_NAME")
user_info.set_mobile("CUSTOMER_MOBILE_NO")
user_info.set_pincode("CUSTOMER_PINCODE")
payment_details = PaymentDetailsBuilder(channel_id, order_id, txn_amount, user_info).build()
response = Payment.createTxnToken(payment_details)

 

GetPaymentStatus

order_id = "YOUR_ORDER_ID"
read_timeout = 30*1000
payment_status_detail = PaymentStatusDetailBuilder(order_id).set_read_timeout(read_timeout).build()
response = Payment.getPaymentStatus(payment_status_detail)

 

Refunds


InitiateRefund

order_id = "YOUR_ORDER_ID"
ref_id = "REFERENCE_ID"
txn_id = "TRANSACTION_ID"
txn_type = "REFUND"
refund_amount = "1"
refund = RefundDetailBuilder(order_id, ref_id, txn_id, txn_type, refund_amount).set_sub_wallet_amount(sub_wallet_amount).set_extra_params_map(extra_params_map).build()
response = Refund.doRefund(refund)

 

GetRefundStatus

order_id = "ORDER_ID"
ref_id = "REFERENCE_ID"
refund_status_details = RefundStatusDetailBuilder().set_order_id(order_id).set_ref_id(ref_id).build()
response = Refund.getRefundStatus(refund_status_details)

 

SDK Method References

Class Methods HTTP request Description

Payments

createTxnToken POST/theia/api/v1/initiateTransaction Returns a token which will be used in further frontend Payment calls
getPaymentStatus POST/merchant-status/api/v1/getPaymentStatus Returns the Payment status

Refunds

initiateRefund POST/refund/api/v1/async/refund Initiates the Refund
getRefundStatus POST/refund/api/v1/refundStatus Returns the Refund status

 

 

Downloads

Models