Creating Xamarin iOS Binding For Razorpay Payment Gateway

Introduction

 
With the dependency on smartphones increasing for day to day activities, there is a need for a variety of consumer and enterprise grade applications with a faster go-to-market strategy, and with this developers are adopting a cross platform approach to reduce development efforts,  and in turn the cost of development. With Microsoft's acquisition, Xamarin has become a leader in cross platform tools,  providing near real-time updates for Android and iOS, with a rich user experience and native like feel. Even though we get all the standard libraries supported for Xamarin, sometimes there are certain requirements where we need to support native Android and iOS libraries, especially with poor support by 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 iOS binding for a famous Indian payment gateway, RazorPay. Currently the Xamarin community has no support for RazorPay directly as binding or NuGet package, although there is web api and dot net support. We will use Native Razorpay cocoapod 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 and it should look like this:
 
Creating Xamarin iOS Binding For Razorpay Payment Gateway
 
Step 2
 
Generate the API keys and download as Excel. This is needed later. 
 
Creating Xamarin iOS Binding For Razorpay Payment Gateway
 
Step 3
 
iOS binding is a bit tricky and needs a lot of experience, fortunately Microsoft has built a tool called Objective sharpie, which will generate the binding definitions for us. Download and install it from here.
 
Step 4
 
Once tool is installed, open the terminal in Mac and type sharpie -help like below,
 
Creating Xamarin iOS Binding For Razorpay Payment Gateway
 
It should display the below information else you need to reverify the installation.
 
Creating Xamarin iOS Binding For Razorpay Payment Gateway
 
Step 5
 
Now create a folder in Desktop and in the terminal change to that folder using CD command. Install cocoapods 1.7.5 using gem command (Google for gem command usage.)
 
Step 6
 
Once the cocoapods have installed then run objective sharpie command as shown below,
 
Sharpie pod init -f ios razorpay-pod
 
Creating Xamarin iOS Binding For Razorpay Payment Gateway
 
This will download the latest RazorPay framework from cocoapods (NPM of iOS world) and it may take some time the first time. It will download the required files to the folder created in previous steps. Change the iOS version in info.plist file located at /Users/<UserName>/Desktop/RazorPaySDK/Pods/razorpay-pod/Pod/Razorpay.framework/Info.plist
 
Step 7
 
Now execute sharpie bind command as shown below,
 
Sharpie pod bind
 
Creating Xamarin iOS Binding For Razorpay Payment Gateway
 
This step will generate the required ApiDefinition.cs file and Framework and will be located at the same Folder inside Binding sub folder.
 
Step 8
 
Now we will create Visual Studio solution for iOS binding project,
 
Creating Xamarin iOS Binding For Razorpay Payment Gateway
 
Step 9
 
Copy the Apidefinition.cs file generated from binding and paste in APidefinition.cs file of Visual Studio project and change the namespace according to your requirement.
 
Creating Xamarin iOS Binding For Razorpay Payment Gateway
 
We need to change a few parameters inside the file, but I have done that for you. In the reference section I will add a few links where you can learn troubleshooting.
 
Step 10
 
Add the downloaded Framework file in Native reference section and modify its properties by right clicking as shown below,
 
Creating Xamarin iOS Binding For Razorpay Payment Gateway
 
Creating Xamarin iOS Binding For Razorpay Payment Gateway
 
Step 11
 
Build the project and if you don’t see any errors then Binding DLL will be found in bin folder and we are done. In the next step we will create iOS solution and add the DLL as reference.
 
Step 12
 
Create an iOS project with single view app and add the DLL as shown below,
 
Creating Xamarin iOS Binding For Razorpay Payment Gateway
 
Step 13
 
Now that we have a Xamarin version of RazorPay, we are ready to see that in action. But before that, we need to create a dummy order id for payment because we are doing it in test mode. In Realtime order id should be created from server side using downloaded API keys.
 
Step 14
 
Open any Rest api testing tool, in my case I will be using Postman for generating the order id. This step needs API key and secret and basic authentication where key acts as Username and secret acts as password.
 
Creating Xamarin iOS 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 Send button and we get the sample order id in the Response as below: 
 
Creating Xamarin iOS Binding For Razorpay Payment Gateway
 
Creating Xamarin iOS Binding For Razorpay Payment Gateway
 
Capture the order id returned and save it somewhere.
 
In the reference section I will give the link to my GitHub repository for code. Now we will only focus on steps.
 
Step 15
 
I have done a sample and added the below code in ViewController class. 
  1. using Foundation;  
  2. using System;  
  3. using UIKit;  
  4. using Com.RazorPay;  
  5. namespace RazorPayiOSDemo {  
  6.     public partial class ViewController: UIViewController {  
  7.         protected RazorpayPaymentCompletionProtocol paymentDelegate {  
  8.             get;  
  9.             set;  
  10.         }  
  11.         Razorpay razorPay;  
  12.         public ViewController(IntPtr handle): base(handle) {}  
  13.         public override void ViewDidLoad() {  
  14.             base.ViewDidLoad();  
  15.             // Perform any additional setup after loading the view, typically from a nib.  
  16.             paymentDelegate = new PaymentDelegate();  
  17.             razorPay = Razorpay.InitWithKey("Your Key", paymentDelegate);  
  18.         }  
  19.         public override void ViewDidAppear(bool animated) {  
  20.             base.ViewDidAppear(animated);  
  21.             this.ShowPaymentForm();  
  22.         }  
  23.         private void ShowPaymentForm() {  
  24.             var keys = new [] {  
  25.                 new NSString("amount"),  
  26.                     new NSString("currency"),  
  27.                     new NSString("description"),  
  28.                     new NSString("order_id"),  
  29.                     new NSString("name")  
  30.             };  
  31.             var objects = new NSObject[] {  
  32.                 // don't have to be strings... can be any NSObject.  
  33.                 new NSString("100"),  
  34.                     new NSString("INR"),  
  35.                     new NSString("Purchase of phone"),  
  36.                     new NSString("order_EB0LCuQn3qVC5Z"),  
  37.                     new NSString("My business")  
  38.             };  
  39.             var options = new NSDictionary < NSString,  
  40.                 NSObject > (keys, objects);  
  41.             razorPay.Open(options);  
  42.         }  
  43.         public override void DidReceiveMemoryWarning() {  
  44.             base.DidReceiveMemoryWarning();  
  45.             // Release any cached data, images, etc that aren't in use.  
  46.         }  
  47.         public void OnPaymentError(int code, string str, NSDictionary response) {}  
  48.         public void OnPaymentSuccess(string payment_id, NSDictionary response) {}  
  49.         public void OnPaymentError(int code, string str) {  
  50.             throw new NotImplementedException();  
  51.         }  
  52.         public void OnPaymentSuccess(string payment_id) {  
  53.             throw new NotImplementedException();  
  54.         }  
  55.     }  
  56. }  
Step 15
 
Once the payment is successful, in Dashbaord we can see the result of the payment like below,
 
Creating Xamarin iOS Binding For Razorpay Payment Gateway
 
Time for a Demo in Action --  click here for demo.
 

Conclusion

 
In this article we have seen how to create a binding library for Razorpay for iOS and seen its Usage in Demo. Until then, happy coding.
References
  1. https://razorpay.com/docs/payment-gateway/ios-integration/
  2. https://gist.github.com/JonDouglas/dda6d8ace7d071b0e8cb
  3. https://docs.microsoft.com/en-us/xamarin/cross-platform/macios/binding/objective-sharpie/