Creating Xamarin Android Binding For RazorPay Payment Gateway - Part One

Introduction

 
With smartphone dependency increasing for day to day activities, there is a need for a variety of consumer and enterprise grade applications with faster go-to-market strategies. With this, developers are adopting a cross platform approach to reduce the development effort and in turn the cost of development. With Microsoft's acquisition, Xamarin has become  the leader in cross-platform tools,  providing near real-time updates for Android and iOS, with a rich user experience and a native-like feel. Even though we get all the standard libraries supported for Xamarin, there are certain requirements where we need to support native Android and iOS libraries, especially with poor support from  Xamarin for payment gateways like Razorpay, Instamojo, Paytm and many more.
 
In this article we will see a step by step approach to generate the Android binding for a famous Indian payment gateway,  RazorPay. Currently the Xamarin community has no support for RazorPay directly as binding or a NuGet package (though there is web API support and dot net support). We will use a native AAR file for binding, and also see a demo application.
 
Step 1
 
Visit the RazorPay website from here and register using valid email credentials and activate your account for Test Mode. It should look like this:
 
Creating Xamarin Android Binding For RazorPay Payment Gateway
 
Step 2
 
Generate the API keys and download as Excel. This is needed in a later part of the article.
 
Creating Xamarin Android Binding For RazorPay Payment Gateway
 
Step 3
 
Download the latest Android aar file for Razor pay from here and keep it on the desktop or any other accessible place.
 
Creating Xamarin Android Binding For RazorPay Payment Gateway
 
Step 4
 
In this step we will create a new Visual Studio solution for Android binding and add the downloaded AAR file in the JAR section. Visual Studio 2019 will automatically set its build action.
 
Creating Xamarin Android Binding For RazorPay Payment Gateway
 
Creating Xamarin Android Binding For RazorPay Payment Gateway
 
Step 5
 
In this step add the downloaded AAR file in the JAR section of the solution.
 
Creating Xamarin Android Binding For RazorPay Payment Gateway
 
Step 6
 
Right click on the project and select options as shown below.
 
Creating Xamarin Android Binding For RazorPay Payment Gateway
 
Step 7
 
In Android build, select the options for .jar parser and code generation target as shown below, otherwise it may result in an error.
 
Creating Xamarin Android Binding For RazorPay Payment Gateway
 
Step 8
 
Now build the solution and it may result in some errors, but don’t worry, that is  normal as some Java contracts get duplicated while binding to C#. In project explorer open the meatadata.xml file. This is called a Transformation file and these duplicates can be removed by adding one line of code and building again:
 
Creating Xamarin Android Binding For RazorPay Payment Gateway
 
Step 9
 
Boom, the project is built successfully and the DLL is generated in the bin folder of the project.
 
Step 10
 
Now we have a Xamarin version of RazorPay and we are ready to see it in action. But first we need to create a dummy order id for payment, because we are doing it in test mode. In real time the order id should be created from the server side using downloaded API keys.
 
Step 11
 
Open any Rest API testing tool, in my case I will be using Postman for generating the order id. This step needs an API key and secret and basic authentication where key acts as username and secret acts as password.
 
Creating Xamarin Android Binding For RazorPay Payment Gateway
 
Since this method is Post we need to pass some parameters in the Body section as Json data and hit the Send button. We get the sample order id in the response as below:
 
Creating Xamarin Android Binding For RazorPay Payment Gateway
 
Capture the order id returned and save it somewhere.
 
Step 12
 
Create a new Android solution and add the generated DLL from the previous project to it as reference.
 
Creating Xamarin Android Binding For RazorPay Payment Gateway
 
In the reference section I will give the link to my GitHub repository for code. Now we will only focus on steps.
 
Step 13
 
In this step construct the object to be sent for Payment gateway in JSONOBJECT as shown below:
  1. Checkout checkOut = new Checkout();  
  2. checkOut.SetKeyID("<Your API KEY (Dont Add secret)>");  
  3. Activity activity = this;  
  4. try {  
  5.     JSONObject options = new JSONObject();  
  6.     options.Put("description""My Sample Payment activity");  
  7.     options.Put("order_id""order_EAMgUyLXrMfbmL");  
  8.     options.Put("currency""INR");  
  9.     options.Put("amount""100");  
  10.     checkOut.Open(activity, options);  
  11. catch (Exception ex) {  
  12.     Console.WriteLine("error in payment");  
  13. }  
Step 14
 
Once the payment is done we will get success or failure in the below callbacks:
  1. public void OnPaymentError(int p0, string p1, PaymentData p2) {  
  2.     Console.WriteLine("error in payment");  
  3. }  
  4. public void OnPaymentSuccess(string p0, PaymentData p1) {  
  5.     Console.WriteLine("success");  
  6. }  
Step 15
 
Once the payment is successful, in the dashbaord we can see the result of the payment like below:
 
Creating Xamarin Android Binding For RazorPay Payment Gateway
 
Time for a demo:
 
click here to watch demo
 
Conclusion
 
In this article we have seen how to create a binding library for Razorpay for Android and seen its usage in a demo. We will see, how to bind iOS library in the next article. Until then, happy coding.
 
GitHub Link For Binding.
 
References
  1. https://razorpay.com/docs/payment-gateway/android-integration/standard/
  2. https://gist.github.com/JonDouglas/dda6d8ace7d071b0e8cb
  3. https://docs.microsoft.com/en-us/xamarin/android/platform/binding-java-library/binding-an-aar