Checkout - iOS SDK

The iOS SDK library lets you seamlessly integrate Razorpay's entire payment ecosystem in your iOS application and start accepting payments.

Note:
We have moved our SDKs to Swift framework. This framework only supports iOS version 10.0 and above. Public contracts have not been changed.

We have ensured that you will not have to change anything in your code when you move from the Objective-C library to our new Swift framework. If you wish to use the older version, the documentation is available here.

Note:
We no longer support Swift 3, moving forward only the latest version of Swift will be supported.

Download the SDK#

SDK

Framework Version

Framework Compiled With

Download Link

Razorpay(with bit code enabled)

1.1.1

Swift 5

Link

Step 1: Import the Library#

For cocoapod users, our cocoapod page can be found here (bitcode enabled).

To import the SDK library to your Swift or Objective-C project:

For Swift:#

  1. Download and unzip the SDK.
  2. Open your project in XCode and go to file under Menu.
  3. Select Add files to "yourproject".
  4. Select Razorpay.framework in the directory you just unzipped.
  5. Check the Copy items if needed check-box.
  6. Click Add.
  7. Navigate to Target settings and then to General and add the Razorpay.framework in both Embeded Binaries and Linked Frameworks and Libraries.

For Objective-C:#

  1. Download and unzip the SDK.
  2. Open your project in XCode and go to file under Menu.
  3. Select Add files to "yourproject".
  4. Select Razorpay.framework in the directory you just unzipped.
  5. Check the Copy items if needed check-box.
  6. Click Add.
  7. Navigate to Target settings and then to General and add the Razorpay.framework in both Embeded Binaries and Linked Frameworks and Libraries.
  8. Go to Project Settings.
  9. Select Build Settings - All and Combined from the Menu of the project settings.
  10. Set Always Embed Swift Standard Libraries option to TRUE under Build Options .

Note:
For both iOS and Objective C projects, ensure the framework is added in both the Embedded Binaries and Linked Frameworks and Libraries under Target settings - General.

Step 2: Initialize the Razorpay SDK#

To initialize the Razorpay SDK, you will need the following:

  • API key
  • A delegate that implements RazorpayPaymentCompletionProtocol or RazorpayPaymentCompletionProtocolWithData
Copyimport Razorpay class ViewController: UIViewController, RazorpayPaymentCompletionProtocol { var razorpay: Razorpay! . . override func viewDidLoad() { super.viewDidLoad() . . razorpay = Razorpay.initWithKey(razorpayTestKey, andDelegate: self) } . . }
Copy#import <Razorpay/Razorpay.h> @interface ViewController () <RazorpayPaymentCompletionProtocol> { Razorpay *razorpay; . . - (void)viewDidLoad { [super viewDidLoad]; . . razorpay = [Razorpay initWithKey:@"YOUR_PUBLIC_KEY" andDelegate:self]; } }

Step 3: Pass Payment Options and Display Checkout Form#

Add the following code to your ViewController or where ever you want to initialize payments:

Copyinternal func showPaymentForm(){ let options: [String:Any] = [ "amount" : "100" //mandatory in paise "description": "purchase description" "image": "https://url-to-image.png", "name": "business or product name" "prefill": [ "contact": "9797979797", "email": "foo@bar.com" ], "theme": [ "color": "#F37254" ] ] razorpay.open(options) }
Copy- (void)showPaymentForm { // called by your app NSDictionary *options = @{ @"amount": @"1000", // mandatory, in paise // all optional other than amount. @"image": @"https://url-to-image.png", @"name": @"business or product name", @"description": @"purchase description", @"prefill" : @{ @"email": @"foo@bar.com", @"contact": @"9797979797" }, @"theme": @{ @"color": @"#F37254" } }; [razorpay open:options]; }

NOTE:
We now support another optional parameter to the open method displayController.

When this parameter is specified, the razorpay controller is pushed on to this controller's navigation controller if present or presented on this controller if absent.

For example the swift call is:
razorpay.open(options, displayController: self)

You can find the list of all supported options here.

Progress Bar:
To support theme color in the progress bar, pass HEX color values only.

Step 4: Handle Success and Errors#

You can handle success/errors when a payment is completed by implementing onPaymentSuccess and onPaymentError methods of the RazorpayPaymentCompletionProtocol.

Alternatively, you can also implement onPaymentSuccess and onPaymentError methods of RazorpayPaymentCompletionProtocolWithData.

Copypublic 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) }
Copy- (void)onPaymentSuccess:(nonnull NSString*)payment_id { [[[UIAlertView alloc] initWithTitle:@"Payment Successful" message:payment_id delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil] show]; } - (void)onPaymentError:(int)code description:(nonnull NSString *)str { [[[UIAlertView alloc] initWithTitle:@"Error" message:str delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil] show]; }

After a payment is done, you have to add the necessary actions based on success/error criteria.

Possible values for a failure code are:

  • 0: Network error
  • 1: Initialization failure / Unexpected behavior
  • 2: Payment cancelled by user

Success handler receives a payment_id that you can use later to capture the payment.

iOS 9 Update#

iOS 9 has higher requirements for secure URLs. As many Indian banks do not comply with the requirements, you can implement the following as a workaround:

Copy<key>NSAppTransportSecurity</key> <dict> <key>NSAllowsArbitraryLoads</key> <true/> </dict>

Add the above to your info.plist file. For more information click here.