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:
Step 2
Generate the API keys and download as Excel. This is needed in a later part of the article.
Step 3
Download the latest Android aar file for Razor pay from here and keep it on the desktop or any other accessible place.
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.
Step 5
In this step add the downloaded AAR file in the JAR section of the solution.
Step 6
Right click on the project and select options as shown below.
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.
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:
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.
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:
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.
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:
- Checkout checkOut = new Checkout();
- checkOut.SetKeyID("<Your API KEY (Dont Add secret)>");
- Activity activity = this;
- try {
- JSONObject options = new JSONObject();
- options.Put("description", "My Sample Payment activity");
- options.Put("order_id", "order_EAMgUyLXrMfbmL");
- options.Put("currency", "INR");
- options.Put("amount", "100");
- checkOut.Open(activity, options);
- } catch (Exception ex) {
- Console.WriteLine("error in payment");
- }
Step 14
Once the payment is done we will get success or failure in the below callbacks:
- public void OnPaymentError(int p0, string p1, PaymentData p2) {
- Console.WriteLine("error in payment");
- }
- public void OnPaymentSuccess(string p0, PaymentData p1) {
- Console.WriteLine("success");
- }
Step 15
Once the payment is successful, in the dashbaord we can see the result of the payment like below:
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.
References
- https://razorpay.com/docs/payment-gateway/android-integration/standard/
- https://gist.github.com/JonDouglas/dda6d8ace7d071b0e8cb
- https://docs.microsoft.com/en-us/xamarin/android/platform/binding-java-library/binding-an-aar