Android Development

Android integrating the fee gateway PayTM – E-commerce software

PayTM is the biggest cell commerce platform and digital pockets firm. It has a major consumer base of about 300 million and common transactions of 5 million a day. All digital funds will be made by paytm, comparable to cell recharges, electrical energy payments, bank cards, and so forth. Virtually all the things beneath the bottom is roofed by paytm 🙂 Paytm is a quick, dependable and extra accessible fee resolution.

PayTM additionally gives a fee gateway resolution that may be built-in with internet and cell purposes with UPI, Debit / bank card, NetBanking and Paytm Portfolio Cost Choices.

DEMO

In the present day, on this article, we’ll see easy methods to combine the PayTM gateway in a easy resolution (not very complicated in actual fact 😛) e-commerce app. Writing an e-commerce software that requires a little bit of code and structure cannot be dealt with in a single article. I’ve subsequently ready a pattern venture (Net and cell) and supplied the code on Github. I've additionally tried to simplify as a lot as doable so that each newbie can perceive it.

1. Mart9 e-commerce software

The applying instance that I created may be very minimal with very restricted screens. It contains database (Kingdom), community layer (Renovation) and fee choices.

Sign up, sign up screens to attach or create a brand new consumer.
Residence – To checklist out there merchandise with the title, the sticker and the value.
trolley – A BottomSheet view to handle the weather of the basket.
Cost display screen – Make the required calls on the principle server earlier than redirecting the consumer to the fee gateway.
Transactions – To show the checklist of transactions carried out by a consumer.

Listed here are the screenshots of the appliance.

2. Overview of PayTM Funds Lifecycle

The completion of the order includes the variety of calls between app, backend and PayTM server. The next is the life cycle of a transaction, from the start to the tip of the fee.

1. Preparation of the order: As soon as the client selects the gadgets, the cart gadgets will probably be despatched to the principle server. This may insert a brand new command line or replace the prevailing line within the database and generate Distinctive order ID. This order ID should be distinctive every time the consumer is redirected to the PayTM fee display screen, in any other case PayTM generates a reproduction order identification error.

2. Order ID and checksum: The subsequent step is to generate a checksum considering the command ID. The checksum should be generated considering the necessary fields and Service provider Identification.

three. Redirect to PayTM: As soon as the checksum is generated, the consumer will probably be redirected to the PayTM fee display screen displaying a number of fee choices comparable to Debit / bank card, Pockets, Internet Banking and so forth., On this step, if the checksum generated is wrong, the consumer will probably be redirected to the appliance with the error standing.

four. Financial institution transaction: The consumer completes the fee by selecting the choices supplied. PayTM is right here to speak with the financial institution and make the fee.

5. Verification of the transaction: As soon as the transaction is full, you should additionally verify on the server to keep away from fraudsters transactions. Right here the backend server does LOOP ask PayTM server to verify the standing of the transaction.

6. Order standing: The transaction standing is obtained from the server and the suitable command standing is proven to the consumer.

The diagram under illustrates the stream of communication between every occasion. Right here you’ll be able to see the life cycle of the transaction intimately.

three. Integration of the PayTM gateway

In a real-world state of affairs, creating an e-commerce software requires acceptable structure and safety measures. This text goals to clarify the stream so simple as doable. I’ve subsequently constructed the required to hold out the combination of the fee gateway. The combination of PayTM (or another gateway) includes the steps under.

three.1. Enroll with PayTM and get the required API Keys.
three.2 Construct a principal software with the required database schema and REST API. Integration of the PayTM server SDK.
three.three Integration of the PayTM cell SDK in an Android / iOS software.

three.1 PayTM SandBox – Take a look at API Particulars

To begin utilizing PayTM, you’ll be able to create a brand new account and get a check account to check the fee gateway. Observe the steps under to get your sandbox identification data.

1. Go to the PayTM Developer's Website online and comply with the steps required to create a brand new account / login to an present account.

2. As soon as registered, go to API Keys within the left navigation panel.

three. You’ll be able to discover the main points of the API for Take a look at and Manufacturing. As soon as the appliance examined within the sandbox, you’ll be able to contact the PayTM staff to place it dwell.

three.2 Constructing the backend – REST API

I selected the Laravel framework (PHP) to construct the backend module. On this, I wrote the required REST API required for this software. The admin panel is just not constructed on this venture.

This again / REST software is already on-line and publicly out there for testing.

Fundamental URL: https://demo.androidhive.data/paytm/public/api/

Postman Dump: https://www.getpostman.com/collections/8b2e7763a8b7e0673918

three.three Integrating the Android PayTM SDK

Not like my earlier tutorials, I focus solely on the necessary parts of this text. The total software code will be discovered on the Github web page. Right here, I’m notably fascinated with PayTMActivity.java as a result of it comprises the essential parts of the PayTM module.

The consumer will probably be redirected to the PayTM exercise as soon as the basket gadgets have been chosen and clicked Pay. This exercise communicates with the principle server a number of instances to execute the command. I wrote detailed feedback for every methodology used.

Step 1: The checklist of cart gadgets despatched to the server to create a brand new order. Right here we name / prepareOrder finish level to generate the Distinctive order ID.

2nd step: As soon as the order ID obtained, we then name / getChecksum by passing the command ID with different parameters to generate the checksum.

Step three: As soon as the checksum is obtained, we name pgService.startPaymentTransaction () to redirect the consumer to the PayTM fee display screen.

Step four: As soon as the fee is made, / transactionStatus is known as to confirm the transaction on the server. Right here we cross the identical order quantity to verify the standing.

Step 5: As soon as the standing of the transaction is obtained, a message indicating success or failure is displayed by calling showOrderStatus () methodology.

package deal data.androidhive.paytmgateway.ui.paytm;

import android.content material.Intent;
import android.os.Bundle;
import android.help.v4.content material.ContextCompat;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.paytm.pgsdk.PaytmOrder;
import com.paytm.pgsdk.PaytmPGService;
import com.paytm.pgsdk.PaytmPaymentTransactionCallback;
import com.wang.avi.AVLoadingIndicatorView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Record;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import data.androidhive.paytmgateway.BuildConfig;
import data.androidhive.paytmgateway.R;
import data.androidhive.paytmgateway.app.Constants;
import data.androidhive.paytmgateway.db.AppDatabase;
import data.androidhive.paytmgateway.db.mannequin.CartItem;
import data.androidhive.paytmgateway.db.mannequin.Consumer;
import data.androidhive.paytmgateway.networking.mannequin.AppConfig;
import data.androidhive.paytmgateway.networking.mannequin.ChecksumResponse;
import data.androidhive.paytmgateway.networking.mannequin.Order;
import data.androidhive.paytmgateway.networking.mannequin.OrderItem;
import data.androidhive.paytmgateway.networking.mannequin.PrepareOrderRequest;
import data.androidhive.paytmgateway.networking.mannequin.PrepareOrderResponse;
import data.androidhive.paytmgateway.ui.base.BaseActivity;
import data.androidhive.paytmgateway.ui.transactions.TransactionsActivity;
import io.realm.Realm;
import retrofit2.Name;
import retrofit2.Callback;
import retrofit2.Response;
import timber.log.Timber;

Public class PayTMActivity extends BaseActivity {
@BindView (R.id.lbl_status)
TextView lblStatus;

@BindView (R.id.loader)
AVLoadingIndicatorView loader;

@BindView (R.id.icon_status)
ImageView iconStatus;

@BindView (R.id.status_message)
TextView statusMessage;

@BindView (R.id.title_status)
TextView responseTitle;

@BindView (R.id.btn_check_orders)
TextView btnCheckOrders;

@BindView (R.id.layout_order_placed)
LinearLayout layoutOrderPlaced;

non-public kingdom;
AppConfig non-public appConfig;
non-public consumer consumer;

/ **
* Steps to course of the order:
* 1. Name the server to organize the order. What is going to create a brand new command within the database
* and returns the distinctive order ID
*

* 2. As soon as the order ID has been obtained, ship the PayTM settings to the server to calculate the
* Checksum Hash
*

* three. Ship the PayTM settings with the checksum hash to the PayTM gateway
*

* four. As soon as fee has been made, return the order ID to the server to confirm the fee.
* standing of the transaction
* /

@Override
Protected void onCreate (Bundle savedInstanceState)

@Override
public int getLayoutId ()

non-public void init ()

non-public void setStatus (int message)

/ **
* STEP 1: Sending all gadgets from the buying cart to the server and receiving the message
* Distinctive order ID to ship to PayTM
* /
non-public void prepareOrder ()
setStatus (R.string.msg_preparing_order);

itemizing cartItems = realm.the place (CartItem.class) .findAll ();
Request PrepareOrderRequest = new PrepareOrderRequest ();
itemizing orderItems = new ArrayList <> ();
for (CartItem cartItem: cartItems)

request.orderItems = orderItems;

getApi (). prepareOrder (request) .enqueue (new reminder() );

/ **
* 2ND STEP:
* Ship parameters to the server to generate the checksum
* to be despatched to PayTM
* /
empty getChecksum (PrepareOrderResponse response) {
setStatus (R.string.msg_fetching_checksum);

if (appConfig == null)
Timber.e ("The configuration of the appliance is nil! Cannot place the command. This could not often occur");
// consumer navigation to the login display screen
launchLogin (PayTMActivity.this);
end ();
return;

Map paramMap = preparePayTmParams (response);
Timber.d ("PayTm Params:% s", paramMap);

getApi (). getCheckSum (paramMap) .enqueue (new reminder()
@Override
public void onResponse (Name name, reply reply)

@Override
public void onFailure (Name name, disposable t)
);
}

public map preparePayTmParams (PrepareOrderResponse response)
Map paramMap = new HashMap();
paramMap.put ("CALLBACK_URL", String.format (BuildConfig.PAYTM_CALLBACK_URL, response.orderGatewayId));
paramMap.put ("CHANNEL_ID", appConfig.getChannel ());
paramMap.put ("CUST_ID", "CUSTOMER_" + consumer.id);
paramMap.put ("INDUSTRY_TYPE_ID", appConfig.getIndustryType ());
paramMap.put ("MID", appConfig.getMerchantId ());
paramMap.put ("WEBSITE", appConfig.getWebsite ());
paramMap.put ("ORDER_ID", response.orderGatewayId);
paramMap.put ("TXN_AMOUNT", response.quantity);
return paramMap;

/ **
* STEP three: Redirect to the PayTM gateway with the required parameters with the checksum
* This may redirect to the PayTM gateway and provides us the reply to the PayTM transaction
* /
public void placeOrder (Map params)
setStatus (R.string.msg_redirecting_to_paytm);

// alternative between staging and PayTM manufacturing
PaytmPGService pgService = BuildConfig.IS_PATM_STAGIN? PaytmPGService.getStagingService (): PaytmPGService.getProductionService ();

Order PaytmOrder = new PaytmOrder (params);

pgService.initialize (Order, null);

pgService.startPaymentTransaction (this, true, true,
new PaytmPaymentTransactionCallback () );

/ **
* STEP four: Checking the standing of the transaction after the PayTM transaction is full
* This calls the server (personal) -> server (PayTM) to verify the standing of the transaction
* /
non-public void verifyTransactionStatus (String orderId) {
setStatus (R.string.msg_verifying_status);
getApi (). checkTransactionStatus (orderId) .enqueue (new reminder() );
}

/ *
* Show of the standing of the command on the consumer interface. This switches the consumer interface between success instances and those who failed
* * /
showOrderStatus (boolean vacuum isSuccess)
loader.setVisibility (View.GONE);
lblStatus.setVisibility (View.GONE);
if (isSuccess) different
iconStatus.setImageResource (R.drawable.baseline_close_black_48);
iconStatus.setColorFilter (ContextCompat.getColor (this, R.coloration.btn_remove_item));
responseTitle.setText (R.string.order_failed);
statusMessage.setText (R.string.msg_order_placed_failed);

layoutOrderPlaced.setVisibility (View.VISIBLE);

@OnClick (R.id.btn_check_orders)
void onOrdersClick ()

@Override
Boolean public onOptionsItemSelected (MenuItem ingredient)
if (merchandise.getItemId () == android.R.id.house)
end ();
return true;

return tremendous.onOptionsItemSelected (merchandise);

@Override
void protected onDestroy ()
}

four. Take a look at the appliance

As soon as the PayTM integration is full, you’ll be able to check your software (or the appliance supplied on this article) with the assistance of the credentials supplied in check mode.

5. References

Under are some helpful hyperlinks you’ll be able to preserve whereas engaged on this venture.
1. PayTM Android SDK Documentation
2. REST PayTM API Documentation
three. PayTM Take a look at Mode Identification Data

I hope this text has simplified the PayTM integration course of. You probably have any questions / options, please submit within the remark part under.

Good coding

Hello! I’m founding father of androidhive and passionate programming. My expertise embody Android, iOS, PHP, Ruby on Rails and way more. You probably have an concept that you desire to me to develop? Let's discuss ravi@androidhive.data