Integrate paytm payment with rails app

Paytm. The fast growing payment network, it’s quite easy to integrate paytm payment in your rails app. Here is simple step by step guide on the same.

1. Register on paytm payment website.

For registering on paytm payment as a merchant you need to sign up here, you need

  1. TAN number
  2. Email id
  3. Website name
  4. Company name.

After registration they will give you

  1. Category id under which your business falls
  2. Merchant key
  3. MID
  4. Industry type
  5. Staging wallet credentials
  6. Staging server URL
  7. Channel ID

2. Specify the callback URL, called after payment.

You need to specify in your account, a callback url to which user will be redirected after successful payment. If you need to modify callback URL open a thread here

3. Download paytm web sample kit ruby.

Download the kit here. It gives you all the ruby code you need to integrate paytm.

4. Setup routes in routes.rb

You need to setup two routes, one for beginning payment and second for verification of payment.

5. Make a yml file with all paytm credentials.

And intialize in application.rb
config.paytm_keys = YAML::load_file("#{Rails.root}/config/paytm.yml")[Rails.env]

6. Make a paytm controller for handling two routes.

All this code is extracted from paytm ruby kit.
In start_payment you need to make hash and from this you generate checksum hash.

7. Write a helper to generate and verify checksum hash.

Extract encryption_new_pg.rb form ruby kit and add it to app/helpers/paytm/encryption_new_pg.rb

8. Write views for two methods in paytm controller.

9. Add paytm payment button which initializes payment.

<%= form_tag paytm_payment_path, method: :post, remote: false, :style => "" do %>
			<button >Start Payment</button>
	<% end %>

10. Summary

  1. Start payment button will call start_payment method in paytm_controller
  2. In start_payment method we make a hash with all values and generate checksum and then call start_payment view.
  3. This view redirects to paytm payment URL with all params and checksum.
  4. On staging server you need to make 3 payments with staging wallet provided by paytm.
  5. After payment paytm redirects you to callback URL.
  6. This calls verify_payment method which recieves all params, these all params are used to verify checksum.
  7. And then we call verify_payment view.
  8. Happy Coding.:)

Also check out paytm web integration api page. Checkout all options you can send to payment url, like CALLBACK_URL. Checkout payment status api which comes handy in cases of network failures.

Archive