In this article, we are going to learn how we can integrate the IPay88 payment gateway into android.
We are going to do this step by step
1. First, you need to download the .jar or .aar file of the SDK. Download the IPay88 SDK from this link.
2. Once downloaded you need to place the SDK in the libs folder and update your app level gradle file to read the SDK.
1 |
api fileTree(include: ['*.aar', '*.jar'], dir: 'libs') |
3. Now we need to initialize the payment.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
private fun initPayment(data: IPay88Data): IPayIHPayment { return IPayIHPayment().apply { responseURL = data.responseUrl // replace this with the response URL that comes from server backendPostURL = data.postUrl // replace this with server's post url country = data.country merchantKey = data.merchantKey merchantCode = data.merchantCode paymentId = data.paymentId refNo = data.refNo amount = data.amount currency = data.currency prodDesc = data.productDescription userName = data.username userEmail = data.userEmail userContact = data.userContact remark = data.remark lang = data.language } } |
IPay88Data.kt class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
class IPay88Data( var responseUrl: String, var postUrl: String, var country: String, var merchantKey: String, var merchantCode: String, var paymentId: String, var refNo: String, var amount: String, var currency: String, var productDescription: String, var username: String, var userEmail: String, var userContact: String, var remark: String, var language: String ) |
Now we need to instantiate the payment process.
1 2 3 4 5 6 7 8 |
// code ... val intent = IPayIH.getInstance() .checkout(payment, this, ResultDelegate(), IPayIH.PAY_METHOD_CREDIT_CARD) startActivityForResult(intent, PAYMENNT_CODE) // code ... |
Here ResultDelegate class is responsible for the result (i.e success, failed, cancelled etc)
ResultDelegate.kt class
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 |
class ResultDelegate : IPayIHResultDelegate, Serializable { override fun onPaymentSucceeded( transactionId: String?, p1: String?, p2: String?, p3: String?, p4: String?, p5: String?, p6: String?, p7: String?, p8: String? ) { MainActivity.paymentSuccess = true Log.i(TAG, "onPaymentSucceeded") } override fun onPaymentFailed( transactionId: String?, p1: String?, p2: String?, p3: String?, p4: String?, p5: String?, p6: String?, p7: String?, p8: String? ) { MainActivity.paymentFailed = true Log.i(TAG, "onPaymentFailed: ") } override fun onPaymentCanceled( transactionId: String?, p1: String?, p2: String?, p3: String?, p4: String?, p5: String?, p6: String?, p7: String?, p8: String? ) { MainActivity.paymentCancelled = true Log.i(TAG, "onPaymentCanceled: ") } override fun onRequeryResult(p0: String?, p1: String?, p2: String?, p3: String?) { Log.i(TAG, "onRequeryResult: ") } override fun onConnectionError( p0: String?, p1: String?, p2: String?, p3: String?, p4: String?, p5: String?, p6: String? ) { Log.i(TAG, "onConnectionError: ") } companion object { private const val TAG = "ResultDelegate" } } |
Create static variables to keep track of the result state i.e success, failed, canceled etc.
1 2 3 4 5 6 7 8 9 |
// Static members to keep track of different cases @JvmField var paymentSuccess = false @JvmField var paymentFailed = false @JvmField var paymentCancelled = false |
4. We also need to declare the IPayActivity in the manifest as well.
Add the following line in your Manifest file under the application tag.
1 2 3 |
<activity android:name="com.ipay.IPayIHActivity" android:configChanges="orientation|screenSize" /> |
Note: If you have declared minSdk to 21 then you might get the following error.
To fix the above issue add this line in your Manifest file under the application tag
1 2 3 |
<uses-library android:name="org.apache.http.legacy" android:required="false" /> |
5. Once your payment is initiated you should see a screen like this.
6. Finally manage the different cases using static variables that we have created earlier.
I hope this blog will help you to understand how we can integrate IPay88 into android.
Enjoy this blog, head over here to read more android related blogs.