In this blog, we are going to learn about Adyen Payment Gateway In Android. We are following the SDK approach with the latest SDK available on the Adyen console.
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.
For the integration of the payment gateway, we will follow the mentioned steps:
1. Add the Drop-In dependency
In this blog, we are using Adyen Drop-In dependency and its inbuild UI for processing the payment gateway. As the Checkout dependency is deprecated and Drop-In is the latest one. You can use any of the components as per your usage, mentioned on the Adyen console.
1 |
implementation "com.adyen.checkout:drop-in:4.4.0" |
2. Initialization of the Adyen SDK
Now, we will initialize the Adyen SDK for processing the payments. There are multiple steps for initializing the payment SDK.
I hope, you have the Adyen merchant or test account for checking the payment. If you did not have it, then please create the Account from the Adyen console. Because we need the Client Key and API authentication details (Username & Password) for further processing
2.1 Fetch the available payments methods
Adyen supports multiple payment methods for processing the payments on its SDK.
You can call the following API from your server or from your application for fetching the available based on your country, device, and payment amount
1 2 3 4 5 6 7 |
API : https://docs.adyen.com/api-explorer/#/CheckoutService/latest/post/paymentMethods METHOD TYPE: POST PARAMS:{ "merchantAccount": "YOUR_MERCHANT_ACCOUNT" } |
Or, you can save the response json in your asset file and fetch it for your usage. In this demo, we have done the same.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
{ "paymentMethods": [ { "brands": [ "amex", "bcmc", "cup", "diners", "discover", "jcb", "maestro", "mc", "visa" ], "name": "Credit Card", "type": "scheme" } ] } |
2.2 Creating Payment Service
Now, we will create the Payment service class which extends the DropInService() class and overrides their methods. In this method, we will receive the response of payment for further process.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
class PaymentService : DropInService() { override fun makePaymentsCall(paymentComponentJson: JSONObject): DropInServiceResult { Log.v("MyDropinService", "paymentComponentData --->$paymentComponentJson") val serializedPaymentComponentData = PaymentComponentData.SERIALIZER.deserialize(paymentComponentJson) return if (serializedPaymentComponentData != null) { DropInServiceResult.Finished("") } else { DropInServiceResult.Error("Empty payment data") } } override fun makeDetailsCall(actionComponentJson: JSONObject): DropInServiceResult { Log.v("MyDropinService", "make details $actionComponentJson") return DropInServiceResult.Finished("Authorised") } } |
2.3 Initilization of SDK
Now, we will initialize the payment SDK. We have used the MainActivity as a result Intent for getting the result in string form from the Adyen Service returns method.
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 |
val clientKey = "your Client key " val amount = Amount() amount.currency = "EUR" amount.value = 25 val cardConfiguration = CardConfiguration.Builder(this, clientKey).setShowStorePaymentField(false) .setEnvironment( Environment.TEST ).build() val dropInConfiguration = DropInConfiguration.Builder(this, PaymentService::class.java, clientKey) dropInConfiguration.setEnvironment(Environment.TEST) dropInConfiguration.setAmount(amount) dropInConfiguration.addCardConfiguration(cardConfiguration) dropInConfiguration.build() val methods = Helpers.payment(this, "paymentmethod.json") var paymentMethodsResponse: JSONObject? = null try { paymentMethodsResponse = JSONObject(methods) } catch (e: JSONException) { e.printStackTrace() } val resultIntent = Intent(this, MainActivity::class.java) val paymentMethodsApiResponse = PaymentMethodsApiResponse.SERIALIZER.deserialize( paymentMethodsResponse!! ) startPayment(this, paymentMethodsApiResponse, dropInConfiguration.build(), resultIntent) |
We have used the TEST MODE and its details for verifying the payment flow. You can changes the modes of payment as per the scenerios.
3. Submitting the response For payments
Once the user submits the details, then we will get the response from the Adyen SDK. We need to make a call to Adyen’s Payment API from our server.
Following are the dummy APIs and request that we have used for showing the demonstration of the payment flow. You can get all the APIs and their details in your account section.
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 |
API: https://docs.adyen.com/api-explorer/#/CheckoutService/v68/post/payments METHOD TYPE: POST PARAMS: { "amount": { "currency": "USD", "value": 1000 }, "reference": "Your order number", "paymentMethod": { "type": "scheme", "number": "4111111111111111", "expiryMonth": "03", "expiryYear": "2030", "holderName": "John Smith", "cvc": "737" }, "returnUrl": "https://your-company.com/...", "merchantAccount": "YOUR_MERCHANT_ACCOUNT" } RESPONSE: { "pspReference": "863643610917759E", "resultCode": "Authorised", "amount": { "currency": "USD", "value": 1000 }, "merchantReference": "Your order number" } |
NOTE: For better flow, you can host all the Adyen’s APIs on your server and call them accordinlgy & fetch the results in one API call.
NOTE: If you will face any issues with material theme and Adyen theme then you can use the following line of code in your style folder
1 |
<style name="AdyenCheckout" parent="AppTheme1" /> |
There are different responses that you will receive from Adyen’s APIs. Each and every response have its own significance.
Conclusion:
In this blog, you have learned about the Adyen Payment Gateway In Android.
For more information regarding the Adyen Payment Gateway follow the link.