• Getting Started
    • Website Integration
      Mobile Integration (Android/IOS)
      Enterprise Solutions
      Accept Payments
      Process Payments
      Disbursals
      API References
      Webhooks
      Testing
      Refunds
      Guides
      Other Processes

      Python SDK Integration

      Installation Guide


      Requirements

      Python 3.5 or later

      Installing Paytm's payment gateway SDK with pip

      1. Install Python package by using below command

        pip install paytm-pg

      Generate API Keys


      ATTRIBUTEDESCRIPTION
      MIDA unique merchant identifier issued by Paytm for your account.
      Merchant KeyA 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"
      MerchantProperty.initialize(environment, mid, key, website)
       # If you want to add log file to your project, use below code
      file_path = '/path/log/file.log'
      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

      EChannelId 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.get_payment_status(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(SampleData.get_sub_wallet_amount())
                  .set_extra_params_map(SampleData.get_extra_params_map()).build()
      response = Refund.do_refund(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.get_refund_status(refund_status_details)

      SDK Method References

      ClassMethodsHTTP requestDescription

      Payments

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

      Refunds

      initiateRefundPOST /refund/api/v1/async/refundInitiates the Refund
      getRefundStatusPOST /refund/api/v1/refundStatusReturns the Refund status

      Downloads


      Download the sample application

      Download the SDK

      Models