PayMaya Integration with Rails

Jahnavi Sananse
5 min readOct 21, 2021

--

Reference Links :

  1. https://developers.paymaya.com/blog/entry/paymaya-api-and-sdk-documentation
  2. https://s3-us-west-2.amazonaws.com/developers.paymaya.com.pg/pay-by-paymaya/index.html#scan-dynamic-qr-payment-create-payment-post
  3. https://hackmd.io/@paymaya-pg/Checkout

Steps to check API in postman :

Header : Authorization → Basic cGstWjBPU3pMdkljT0kyVUl2RGhkVEdWVmZSU1NlaUdTdG5jZXF3VUU3bjBBaDo=

1. Create Checkout Payment

POST: https://pg-sandbox.paymaya.com/checkout/v1/checkouts

After hitting redirect Url on google, you will get a response like this -

2. Get Payment Token

POST: https://pg-sandbox.paymaya.com/checkout/v1/checkouts/:checkoutId

3. Payment Details

POST: https://pg-sandbox.paymaya.com/payments/v1/payments

4. Get PaymentId

GET: https://pg-sandbox.paymaya.com/payments/v1/payments/:paymentId

Now, Let’s have a look over Frontend Side :

Let’s Create one Rails Application.

So, firstly we have to add one button, which is specifically used to navigate to PayMaya’s Payment page.

steps to follow:-

  1. Create Controller: rails g controller paymaya_controller

2. Create file : views →paymaya →index.html.erb

3. Set routes/root to config →route.rb file

4. After running up to this, we will get output like below :

Now, Let’s move forward towards the Backend Side :

What’s the next step now? So, basically, now we would have to integrate PayMaya with our Rails application.

steps to integrate PayMaya with Rails Application:-

  1. Create new method (method name : pre_pay) inside paymaya_controller.rb file

[ Note: You will get below code of request.body from https://hackmd.io/@paymaya-pg/Checkout#Payment-API ]

2. Set route to config → routes.rb file

3. Let’s see the output :

Now, Moving forward :

What we are going to do is — have to manage the conditions for either transaction being successful or failing.

steps to manage conditions:-

  1. Create new methods inside the same controller to show either payment is successful or not.

controllers → paymaya_controller.rb

2. Create three different view files respective to the methods created in the controller.

views → paymaya →response_success.html.erb

views → paymaya →response_cancel.html.erb

views → paymaya →response_failure.html.erb

3. Set routes to config →routes.rb

4. Now, you will get a response like below according to your details.

Now, Let’s move towards webhook integration :

  1. First of all, What is Webhook?

→ “Basically, A webhook is an HTTP callback that allows a web application to POST a message to a URL when certain events take place. Webhooks can be used to receive data in real-time, pass it on to another application, or process the data faster than traditional APIs.

→ Webhooks typically are used to connect two different applications. When an event happens it triggers the application.

2. Now, You have to configure ngrok and start ngrok server by firing “ngrok HTTP 3000” command into the terminal and you will get a secure HTTP request.

3. Also, have to add a controller for webhook with receiver method

3. set route for this controller as it would be post request (like line 2 from below image)

4. Now, the main thing is you need to pass a secure URL in Pamaya portal like below to check the params that we are getting after the payment transaction.

--

--

Jahnavi Sananse
Jahnavi Sananse

Written by Jahnavi Sananse

Story teller from my preliterate days. I write them down now.

No responses yet