In this blog, we are going to learn about how to implement CCAvenue payment gateway integration in android with SDK approach.
Before starting integration first, we need to know what a payment gateway is.
A payment gateway is a merchant service provided by an e-commerce application service provider. Which authorizes card or direct payment processing.
CCAvenue payment gateway accepts credit payments online by supporting the Mastercard, American Express, Visa, RuPay, Diners Club, and eZeClick, as well as net banking and debit cards.
If you are planning on the CCAvenue payment gateway integration in android with SDK by using SDK then this blog will help you to do so.
Implementation
1. Initial setup
Adding SDK dependencies in your Application
- Add below dependency in your app-level build.gradle file.
1 |
api 'com.android.volley:volley:1.2.1' |
2. Initialize payment
Start initializing the data for the payment flow.
1 2 3 4 |
private fun startSDk(ccAvenueData: CCAvenueData) { val intent = Intent(mFragmentContext.context, CCAvenueWebPaymentActivity::class.java) mFragmentContext.startActivityForResult(intent, RC_PAYMENT) } |
Amount and currency will be encrypted like below:
1 2 3 4 5 6 7 8 9 10 11 |
fun encrypt(plainText: String, key: String?): String? { try { val publicKey = KeyFactory.getInstance("RSA").generatePublic(X509EncodedKeySpec(Base64.decode(key, Base64.DEFAULT))) val cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding") cipher.init(Cipher.ENCRYPT_MODE, publicKey) return Base64.encodeToString(cipher.doFinal(plainText.toByteArray(charset("UTF-8"))), Base64.DEFAULT) } catch (e: Exception) { e.printStackTrace() } return null } |
Inside your webview payment activity initialize the data like below:
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 |
private fun setupWebView() { val vEncVal = StringBuffer("") vEncVal.append(addToPostParams("amount", "200")) vEncVal.append(addToPostParams("currency", "INT")) mBinding.paymentWebview.settings.javaScriptEnabled = true mBinding.paymentWebview.settings.domStorageEnabled = true mBinding.paymentWebview.settings.javaScriptCanOpenWindowsAutomatically = true mBinding.paymentWebview.addJavascriptInterface(CustomJavaScriptInterface(), "HTMLOUT") val postData: String = ACCESS_CODE + "=" + URLEncoder.encode("your accessCode", "UTF-8") + "&" + CCAVENUE_MERCHANT_ID + "=" + URLEncoder.encode("your merchantId", "UTF-8") + "&" + CCAVENUE_ORDER_ID + "=" + URLEncoder.encode("your orderId", "UTF-8") + "&" + CCAVENUE_REDIRECT_URL + "=" + URLEncoder.encode("your redirectUrl", "UTF-8") + "&" + CCAVENUE_CANCEL_URL + "=" + URLEncoder.encode("cancelUrl", "UTF-8") + "&" + CCAVENUE_BILLING_NAME + "=" + URLEncoder.encode("billingName", "UTF-8") + "&" + CCAVENUE_BILLING_ADDRESS + "=" + URLEncoder.encode("billingAddress", "UTF-8") + "&" + CCAVENUE_BILLING_CITY + "=" + URLEncoder.encode("billingCity", "UTF-8") + "&" + CCAVENUE_BILLING_STATE + "=" + URLEncoder.encode("billingState", "UTF-8") + "&" + CCAVENUE_BILLING_ZIP + "=" + URLEncoder.encode("billingZip", "UTF-8") + "&" + CCAVENUE_BILLING_COUNTRY + "=" + URLEncoder.encode("billingCountry", "UTF-8") + "&" + CCAVENUE_BILLING_TEL + "=" + URLEncoder.encode("billingTel", "UTF-8") + "&" + CCAVENUE_BILLING_EMAIL + "=" + URLEncoder.encode("billingEmail", "UTF-8") + "&" + CCAVENUE_DELIVERY_NAME + "=" + URLEncoder.encode("deliveryName", "UTF-8") + "&" + CCAVENUE_DELIVERY_ADDRESS + "=" + URLEncoder.encode("deliveryAddress", "UTF-8") + "&" + CCAVENUE_DELIVERY_CITY + "=" + URLEncoder.encode("deliveryCity", "UTF-8") + "&" + CCAVENUE_DELIVERY_STATE + "=" + URLEncoder.encode("deliveryState", "UTF-8") + "&" + CCAVENUE_DELIVERY_COUNTRY + "=" + URLEncoder.encode("deliveryCountry", "UTF-8") + "&" + CCAVENUE_DELIVERY_ZIP + "=" + URLEncoder.encode("deliveryZip", "UTF-8") + "&" + CCAVENUE_DELIVERY_TEL + "=" + URLEncoder.encode("deliveryTel", "UTF-8") + "&" + CCAVENUE_ENC_VALUE + "=" + URLEncoder.encode(encrypt(vEncVal.substring(0, vEncVal.length - 1).toString(), "RSA key url generated on server end").toString(), "UTF-8") mBinding.paymentWebview.postUrl(CCAVENUE_PRODUCTION_TRANSACTION_URL, postData.toByteArray()) } |
1 2 3 4 5 6 7 8 9 10 |
mBinding.paymentWebview.webViewClient = object : WebViewClient() { override fun onPageFinished(view: WebView, url: String) { super.onPageFinished(mBinding.paymentWebview, url) Log.d("TEST_PAY_LOG", "onPageFinished: $url ") cancelLoading() if (url.contains("redirect url") || url.contains("cancel url")) { mBinding.paymentWebview.loadUrl("") } } } |
1 2 3 4 5 6 7 8 9 10 |
mBinding.paymentWebview.webViewClient = object : WebViewClient() { override fun onPageFinished(view: WebView, url: String) { super.onPageFinished(mBinding.paymentWebview, url) Log.d("TEST_PAY_LOG", "onPageFinished: $url ") cancelLoading() if (url.contains("redirect url") || url.contains("cancel url")) { mBinding.paymentWebview.loadUrl("your javascript code to return on the the JavascriptInterface") } } } |
3. Handling payment callback:
It’s time to handle the payment callback. For this, use the CustomJavaScriptInterface
that you have added on the web view during the initialization as :
1 2 3 4 5 6 7 8 9 10 11 12 13 |
inner class CustomJavaScriptInterface { @JavascriptInterface fun processHTML(html: String) { val status = Html.fromHtml(html).toString() val statusResponse = status.substring(status.indexOf("{"),status.lastIndexOf("}") + 1) val paymentResponse = ObjectMapper().readValue(statusResponse, BaseModel::class.java) if (paymentResponse.success) { setResultDataOkay(paymentResponse.message) } else { setResultDataCancel(paymentResponse.message) } } } |
Conclusion:
In this blog, you have learned about the implementation of the CCAvenue payment gateway integration by using SDK.
For more information regarding the CCAvenue payment gateway follow the link.
Thanks for reading this blog. You can also check other blogs from here.