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:
Step 2
Generate the API keys and download as Excel. This is needed later.
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,
It should display the below information else you need to reverify the installation.
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
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
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,
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.
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,
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,
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.
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:
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.
- using Foundation;
- using System;
- using UIKit;
- using Com.RazorPay;
- namespace RazorPayiOSDemo {
- public partial class ViewController: UIViewController {
- protected RazorpayPaymentCompletionProtocol paymentDelegate {
- get;
- set;
- }
- Razorpay razorPay;
- public ViewController(IntPtr handle): base(handle) {}
- public override void ViewDidLoad() {
- base.ViewDidLoad();
-
- paymentDelegate = new PaymentDelegate();
- razorPay = Razorpay.InitWithKey("Your Key", paymentDelegate);
- }
- public override void ViewDidAppear(bool animated) {
- base.ViewDidAppear(animated);
- this.ShowPaymentForm();
- }
- private void ShowPaymentForm() {
- var keys = new [] {
- new NSString("amount"),
- new NSString("currency"),
- new NSString("description"),
- new NSString("order_id"),
- new NSString("name")
- };
- var objects = new NSObject[] {
-
- new NSString("100"),
- new NSString("INR"),
- new NSString("Purchase of phone"),
- new NSString("order_EB0LCuQn3qVC5Z"),
- new NSString("My business")
- };
- var options = new NSDictionary < NSString,
- NSObject > (keys, objects);
- razorPay.Open(options);
- }
- public override void DidReceiveMemoryWarning() {
- base.DidReceiveMemoryWarning();
-
- }
- public void OnPaymentError(int code, string str, NSDictionary response) {}
- public void OnPaymentSuccess(string payment_id, NSDictionary response) {}
- public void OnPaymentError(int code, string str) {
- throw new NotImplementedException();
- }
- public void OnPaymentSuccess(string payment_id) {
- throw new NotImplementedException();
- }
- }
- }
Step 15
Once the payment is successful, in 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 iOS and seen its Usage in Demo. Until then, happy coding.
References
- https://razorpay.com/docs/payment-gateway/ios-integration/
- https://gist.github.com/JonDouglas/dda6d8ace7d071b0e8cb
- https://docs.microsoft.com/en-us/xamarin/cross-platform/macios/binding/objective-sharpie/