A detailed guideline on integrating Stripe in iOS apps using Swift

By victoria, 28 January, 2015
A detailed guideline on integrating Stripe in iOS apps using Swift

Over the years, a large number of improvements have been made to the entire concept of developing iOS apps. Whether you're inclined on maximizing the reach of your business or simply want to circulate a social message to worldwide population, a brilliantly designed iOS application can enable you to do all this and much more. Specifically talking about developing an iOS business app, you need to make proper arrangements for allowing the app users to carry out purchases via frictionless payment methods. Stripe is one such stunning payment gateway that works as the best match for ensuring secure and efficient payments via iOS applications. Keep on reading this blog to unveil a lot more about integrating this very popular payment gateway into apps that are specially designed for Apple products including iPhones, iPads, Mac. Here, Swift is the programming language that I'll be using for executing the project.

Stripe- An overview

As one of the most popular payment gateways, Stripe allows users to make payments for different products/services via a single click. Backed by a highly optimized UI(User interface), Stripe works best for cross-device applications.

Stripe and Apple Pay go hand in hand

Stripe supports the very popular and highly recommended Apple Pay feature. That means, whenever any iPhone/iPad/Mac user creates an account with Stripe, he/she can automatically begin working with Apple Pay in his/her chosen iOS application(s). With Apple Pay, you can make payments for products/service with a single touch using the credit card details that are already stored in your Stripe account. The best part about using Apple Pay along with Stripe is that all your in-app purchases remain in-tact and you can continue using them for buying extra content such as physical goods comprising of apparels, groceries, appliances etc. and services such as hotel reservations, tickets for events, bonus game levels, subscriptions etc.).

An insight into integrating Stripe in iOS applications built using Swift programming language

Prior to proceeding ahead with the steps involved in integrating Stripe in iOS app using Swift, you must be familiar with the three ways in which the user's credit card information is being collected:

  1. Using Stripe's default form component called PaymentKit for collecting the details of a new credit card
  2. Building your own individual credit card from scratch
  3. Using the Apple Pay framework for accessing the credit card information that has already been saved by the user.

And now, let's come to the steps involved with Stripe integration into iOS apps using Swift

Step 1 : Create a new project

For this, simply go to the File menu and select New-> Project. Here, select 'Single View Application' as the template for the new project. Assign a suitable name for the project, along with selecting the language as 'Swift'. Once you're done with this, close the project in Xcode.

Step 2 : Configure Stripe using Cocoa-pods

For those of you who aren't familiar with Cocoa-pods, here's an explanation. Built on the Ruby platform, Cocoa-pods is a dependency manager specially designed for projects that need to implemented using the Objective C programming language. Equipped with thousands of libraries, Cocoa-pods allows you to scale your iOS app development projects in an elegant manner. Well, there might be situations where Cocoa-pods might lack the pod that suits your dependencies. Under such a scenario, you can always opt for creating a pod using the below mentioned code snippet:

$ pod spec create Peanut
$ edit Peanut.podspec
$ pod spec lint Peanut.podspec

Now, coming back to the configuration/setting up of Stripe, you simply need to run the below mentioned command on the terminal window:

sudo gem install cocoapods
pod init

After this, opt for tweaking the Podfile available under project directory by simply adding the pod 'Stripe' and executing the below command:

pod install

Finally, go to the project folder and launch the file which has an extension as 'workspace'. Once you're through with this, you'll be able to see the Stripe frameworks listed within the Pods directory in addition to the default project files.

Step 3 : Do the coding for integrating Stripe in iOS using Swift

In this step, you'll need to edit the ViewController.swift by adding the below IBOutler variable for the chosen button:

  @IBOutlet var saveButton: UIButton!

After this, you need to make arrangements for holding the instance of STPVIew class. For this, you need to simply add the below variable declaration to the ViewController.swift file:

  var stripeView: STPView = STPView()

Step 4 : Add Stripe control that accepts users' credit card information (credit card number, expiry date etc.)

In this step, you just need to add the following lines of code to the viewDidLoad function, ensuring that the ViewController class adheres to everything that's been mentioned in the STPViewDelegate protocol:

class ViewController: UIViewController, STPViewDelegate{
  override func viewDidLoad() {
    super.viewDidLoad()
    stripeView = STPView(frame: CGRectMake(13, 23, 259, 51), andKey: “pk_test_7pRNASCoBOKtIshFeQd4XMUh”)
    stripeView.delegate = self
    view.addSubview(stripeView)
    saveButton.enabled = false
  }
}

If you carefully observe the above code, you'll find that the save button has been disabled. The reason for this is that there will be a function that will get triggered each time a user enters his/her credit card details. This function will include a boolean parameter that will determine whether the entered credit card details are valid or not. On the basis of this value obtained from the function, the save button would get enabled or disabled. The function that I'm referring to is mentioned below:

func stripeView(view: STPView!, withCard card: PKCard!, isValid valid: Bool) {
  if (valid) {
    saveButton.enabled = true
  } else {
    saveButton.enabled = false
  }
}

Step 5 : Add code for token generation

As the last step, just add the below IBAction function that uses STPView's createToken function for generating a new token each time, the user tries making a payment via his/her credit card:

@IBAction func saveButton(sender: AnyObject) {
  stripeView.createToken { (stpToken, error) -> Void in
    if (error != nil) {
      println(error)
    } else {
    println(stpToken)
    }
  }
}

Conclusion

So that was all about integrating the very cool and most recommended Stripe payment gateway into iOS apps, keeping Swift as the programming language. Hope you'd have found the tutorial worthy enough to follow in all your projects where you're expected to offer your iOS app users the ultimate convenience of leveraging Stripe features.

Author Bio:
Victoria Brinsley is working with an enterprise of mobile app development services - Appsted Ltd. In case, you are willing to avail more information about development tips and tricks, feel free to get in touch with her.