https://www.studytutorial.in/payubiz-android-payubiz-payment-gateway-integration-tutorial
This tutorial explains, how to integrate PayUBiz payment gateway with in your Android Application.
Introduction
PayUBiz is a B2B payment gateway provider that used to collect online payment. PayUBiz provides Web API and Mobile SDK to integrate Website and Android App to collect online payment.
Types Of PayUBiz
We will integrate two types of PayUBiz:
Non-Seamless Integration(Using PayU SDK’s UI)
Seamless Integration(Using Custom UI)
In this type, we are using PayU SDK’s UI and flows during the transaction. On the SDK payment option, customer will need to select the payment option and fill the respective card details. After this, SDK will launch the desired bank webpage (on the native webview) for further authentication.
In this type, we will design our custom UI to collect customer details and pass it to the PayU SDK by calling appropriate APIs which then launches the selected bank webpage.
This tutorial explains about Non-Seamless Integration.
Create an Account
To integrate PayUBiz, you need to create an account on PayUBiz. It provides Merchant Key and Salt that will be use in Android App.
Create a New Project
Create a new Project in android Studio goto File ⇒ New ⇒ New Projects.
Add SDKUI (Payu sdk) as a module in your project
We need to add sdkui as module in your project. Download PayUBiz SDK and follow below step to add as a module.
Right click on the app go toNew ⇒ Module and select Import Gradle Project then click on the next button then browse to your sdkui directory then click on finish button.
Compile SDKUI in Project
We need to compile SDKUI module, so add below code in the dependency of build.gradle(app module) file.
1
| compile project( ':sdkui' ) |
Configure PayU Environment
Add merchant key and salt as constant. We need to set Instance of PayU and set Environment of PayUBiz to make successful Payment.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
| public class MainActivity extends AppCompatActivity { private String merchantKey = "Your Merchant KEY" , salt = "UzUTFOHh" , userCredentials; // These will hold all the payment parameters private PaymentParams mPaymentParams; // This sets the configuration private PayuConfig payuConfig; // Used when generating hash from SDK private PayUChecksum checksum; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button onlinePayment = (Button) findViewById(R.id.onlinePayment); Payu.setInstance( this ); int environment = PayuConstants.PRODUCTION_ENV; payuConfig = new PayuConfig(); payuConfig.setEnvironment(environment); mPaymentParams = new PaymentParams(); onlinePayment.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { setParameter(); generateHashFromSDK(mPaymentParams, salt); } }); } } |
Add data to PaymentParams object
We need to add data to PaymentParams.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
| public void setParameter() { mPaymentParams.setKey(merchantKey); mPaymentParams.setAmount( "100" ); mPaymentParams.setProductInfo( "product_info" ); mPaymentParams.setFirstName( "Cutomer Name" ); mPaymentParams.setEmail( "test@gmail.com" ); mPaymentParams.setPhone( "8448217808" ); mPaymentParams.setAddress1( "Customer Address" ); mPaymentParams.setZipCode( "Customer ZipCode" ); //Set Your transaction ID. example- "USER ID "+System.currentTimeMillis() mPaymentParams.setTxnId(System.currentTimeMillis()+ "" ); /** * Surl --> Success url is where the transaction response is posted by PayU on successful transaction * Furl --> Failre url is where the transaction response is posted by PayU on failed transaction */ mPaymentParams.setNotifyURL(mPaymentParams.getSurl()); //for lazy pay mPaymentParams.setUdf1( "udf1" ); mPaymentParams.setUdf2( "udf2" ); mPaymentParams.setUdf3( "udf3" ); mPaymentParams.setUdf4( "udf4" ); mPaymentParams.setUdf5( "udf5" ); /** * These are used for store card feature. If you are not using it then user_credentials = "default" * user_credentials takes of the form like user_credentials = "merchant_key : user_id" * here merchant_key = your merchant key, * user_id = unique id related to user like, email, phone number, etc. * */ userCredentials = merchantKey + ":" + "Your CUSTOMER UNIQUE ID" ; mPaymentParams.setUserCredentials(userCredentials); //mPaymentParams.setUserCredentials(PayuConstants.DEFAULT); } |
- IF you do not have any data to set field like Product description, Product Discount, UDF1 – UDF5(User Define Fields) fields then put empty string “”.
Generate Hash Key
For successful transaction, it is compulsory to set Hash Key. You can generate has key From Mobile SDK or send above data to Your Web Service to generate Hash Key. Using below code, you can Generate Hash Key from App.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
| public void generateHashFromSDK(PaymentParams mPaymentParams, String salt) { PayuHashes payuHashes = new PayuHashes(); PostData postData = new PostData(); // payment Hash; checksum = null ; checksum = new PayUChecksum(); checksum.setAmount(mPaymentParams.getAmount()); checksum.setKey(mPaymentParams.getKey()); checksum.setTxnid(mPaymentParams.getTxnId()); checksum.setEmail(mPaymentParams.getEmail()); checksum.setSalt(salt); checksum.setProductinfo(mPaymentParams.getProductInfo()); checksum.setFirstname(mPaymentParams.getFirstName()); checksum.setUdf1(mPaymentParams.getUdf1()); checksum.setUdf2(mPaymentParams.getUdf2()); checksum.setUdf3(mPaymentParams.getUdf3()); checksum.setUdf4(mPaymentParams.getUdf4()); checksum.setUdf5(mPaymentParams.getUdf5()); postData = checksum.getHash(); if (postData.getCode() == PayuErrors.NO_ERROR) { payuHashes.setPaymentHash(postData.getResult()); } // checksum for payemnt related details // var1 should be either user credentials or default String var1 = mPaymentParams.getUserCredentials() == null ? PayuConstants.DEFAULT : mPaymentParams.getUserCredentials(); String key = mPaymentParams.getKey(); if ((postData = calculateHash(key, PayuConstants.PAYMENT_RELATED_DETAILS_FOR_MOBILE_SDK, var1, salt)) != null && postData.getCode() == PayuErrors.NO_ERROR) // Assign post data first then check for success payuHashes.setPaymentRelatedDetailsForMobileSdkHash(postData.getResult()); //vas if ((postData = calculateHash(key, PayuConstants.VAS_FOR_MOBILE_SDK, PayuConstants.DEFAULT, salt)) != null && postData.getCode() == PayuErrors.NO_ERROR) payuHashes.setVasForMobileSdkHash(postData.getResult()); // getIbibocodes if ((postData = calculateHash(key, PayuConstants.GET_MERCHANT_IBIBO_CODES, PayuConstants.DEFAULT, salt)) != null && postData.getCode() == PayuErrors.NO_ERROR) payuHashes.setMerchantIbiboCodesHash(postData.getResult()); if (!var1.contentEquals(PayuConstants.DEFAULT)) { // get user card if ((postData = calculateHash(key, PayuConstants.GET_USER_CARDS, var1, salt)) != null && postData.getCode() == PayuErrors.NO_ERROR) // todo rename storedc ard payuHashes.setStoredCardsHash(postData.getResult()); // save user card if ((postData = calculateHash(key, PayuConstants.SAVE_USER_CARD, var1, salt)) != null && postData.getCode() == PayuErrors.NO_ERROR) payuHashes.setSaveCardHash(postData.getResult()); // delete user card if ((postData = calculateHash(key, PayuConstants.DELETE_USER_CARD, var1, salt)) != null && postData.getCode() == PayuErrors.NO_ERROR) payuHashes.setDeleteCardHash(postData.getResult()); // edit user card if ((postData = calculateHash(key, PayuConstants.EDIT_USER_CARD, var1, salt)) != null && postData.getCode() == PayuErrors.NO_ERROR) payuHashes.setEditCardHash(postData.getResult()); } if (mPaymentParams.getOfferKey() != null ) { postData = calculateHash(key, PayuConstants.OFFER_KEY, mPaymentParams.getOfferKey(), salt); if (postData.getCode() == PayuErrors.NO_ERROR) { payuHashes.setCheckOfferStatusHash(postData.getResult()); } } if (mPaymentParams.getOfferKey() != null && (postData = calculateHash(key, PayuConstants.CHECK_OFFER_STATUS, mPaymentParams.getOfferKey(), salt)) != null && postData.getCode() == PayuErrors.NO_ERROR) { payuHashes.setCheckOfferStatusHash(postData.getResult()); } // we have generated all the hases now lest launch sdk's ui launchSdkUI(payuHashes); } private PostData calculateHash(String key, String command, String var1, String salt) { checksum = null ; checksum = new PayUChecksum(); checksum.setKey(key); checksum.setCommand(command); checksum.setVar1(var1); checksum.setSalt(salt); return checksum.getHash(); } |
Launch PayU UI
Now we just need to pass above data to PayUBaseActivity using intent.
1
2
3
4
5
6
7
8
9
10
| public void launchSdkUI(PayuHashes payuHashes) { mPaymentParams.setHash(payuHashes.getPaymentHash()); Intent intent = new Intent( this , PayUBaseActivity. class ); intent.putExtra(PayuConstants.PAYU_CONFIG, payuConfig); intent.putExtra(PayuConstants.PAYMENT_PARAMS, mPaymentParams); intent.putExtra(PayuConstants.PAYU_HASHES, payuHashes); startActivityForResult(intent, PayuConstants.PAYU_REQUEST_CODE); } |
Payment Gateway
This will open Payu payment UI. After you have completed the above steps, you should be successfully redirected to the payment gateway page.
Comments
Post a Comment