Hi guys, today we will learn about how to implement Razorpay Payment Gateway in an iOS application.
Before starting to code 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 that authorizes card or direct payments processing for e-businesses, online retailers, bricks, and clicks, or traditional brick and mortar.
Note: They have moved their SDK to Swift framework. Razorpay Payment Gateway framework only supports iOS version 10.0 and later. Public contracts have not been changed. They have ensured that everyone would not have to change anything in his code when he move from Objective-C library to our new Swift framework.
Thus, it’s enough for us now. Let’s start by following the instructions below.
STEPS TO INTEGRATE
Step 1 – Import the Razorpay iOS Standard SDK Library
1.) Download the SDK and unzip it.
2.) Open your project in XCode and go to file under Menu. Select Add files to “yourproject”.
3.) Select Razorpay.framework in the directory you just unzipped.
4.) Select the Copy items if needed check-box.
5.) Click Add.
6.) Navigate to Target settings > General and add the Razorpay.framework in both Embedded Binaries and Linked Frameworks and Libraries.
Step 2 – Initialize the Razorpay iOS Standard SDK
To initialize the Razorpay iOS Standard SDK, you need the following :
- API keys. You can generate this from the Razorpay Dashboard.
- A delegate that implements
RazorpayPaymentCompletionProtocol
orRazorpayPaymentCompletionProtocolWithData
.
1 2 3 4 5 6 7 8 9 10 11 |
import Razorpay class ViewController: UIViewController, RazorpayPaymentCompletionProtocol { var razorpay: Razorpay! override func viewDidLoad() { super.viewDidLoad() razorpay = Razorpay.initWithKey(razorpayTestKey, andDelegate: self) } |
Step 3 – Pass Payment Options and Display Checkout Form
Add the following code to your ViewController or wherever you want to initialize payments:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
func showPaymentForm( dict: JSON){ order_ID = dict["description"].stringValue let options: [String:Any] = [ "amount": dict["amount"].stringValue, "currency": dict["currency"].stringValue, "description": dict["description"].stringValue, "order_id": dict["order_id"].stringValue, "name": dict["name"].stringValue, "prefill": [ "contact": dict["contact"].stringValue, "email": dict["email"].stringValue ], "theme": [ "color": "#F37254" ] ] razorpay.open(options, displayController: self) } |
Step 4 – Handle Success and Errors Events
You can handle success or error events when a payment is completed by implementing onPaymentSuccess and onPaymentError methods of the RazorpayPaymentCompletionProtocol.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
public func onPaymentError(_ code: Int32, description str: String){ let alertController = UIAlertController(title: "FAILURE", message: str, preferredStyle: UIAlertControllerStyle.alert) let cancelAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.cancel, handler: nil) alertController.addAction(cancelAction) self.view.window?.rootViewController?.present(alertController, animated: true, completion: nil) } public func onPaymentSuccess(_ payment_id: String){ let alertController = UIAlertController(title: "SUCCESS", message: "Payment Id \(payment_id)", preferredStyle: UIAlertControllerStyle.alert) let cancelAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.cancel, handler: nil) alertController.addAction(cancelAction) self.view.window?.rootViewController?.present(alertController, animated: true, completion: nil) } |
Step 5 – Insert the Following XML Source Code Snippet Into the Body of Your Info.Plist File Just Before the Final.
1 2 3 4 5 |
<key>NSAppTransportSecurity</key> <dict> <key>NSAllowsArbitraryLoads</key> <true/> </dict> |
Conclusion
So please follow the above step and and if you have any issue or suggestion you can leave your query/suggestion in the comment section I will try to solve that.