Integrating Stripe in Swift for iOS development

Stripe is payment gateway that can be integrated with any website and mobile apps. In this tutorial we will see a quick and simple integration of Stripe in Swift for iOS development using Stripe documentation for iOS.


This hands on tutorial will help you to familiarise yourself in

  • Validating and creating token using Stripe IOS SDK
  • Installing third party library using Cocoa-pods (Stripe)
  • Calling Objective-C framework in Swift (Objective-C Bridging Headers settings)

Click File menu and select New -> Project


Select Single View Application as the template for the project.


Provide a name for your project and select the language as Swift.


Now close the project in Xcode and launch terminal window.

Setup Stripe using Cocoa-pods

Run the following commands on your terminal window to install Stripe.

sudo gem install cocoapods
pod init

Edit Podfile under project directory and add pod ‘Stripe’ then execute the below command in Terminal window.

pod install

Navigate to the project folder and launch the file with extension as workspace. Now you should see the Stripe frameworks included under Pods directory along with your default project files.


Write code to integrate Stripe

Edit ViewController.swift and add the following IBOutlet variable for the button.

@IBOutlet var saveButton: UIButton!

Now add the following variable declaration to ViewController.swift file to hold the instance of STPView class.

var stripeView: STPView = STPView()

Since the Stripe framework has been written in Objective-C, we need to make sure to add the implementation file as part of the Objective-C bridging Headers under Build Settings.

Select Project folder and navigate to Build Settings. Then use the search field to locate to Objective-C Bridging Header setting.


Then drag and drop the STPView.m file to Object-C Bridging Header section.


Update the viewDidLoad function and add the following Stripe integration code. Make sure the ViewController class conforms to STPViewDelegate protocol

class ViewController: UIViewController, STPViewDelegate{
	override func viewDidLoad() {
		stripeView = STPView(frame: CGRectMake(15, 20, 290, 55), andKey: )
		stripeView.delegate = self
		saveButton.enabled = false


The above lines of code will add the Stripe control that accepts credit card number, expiry date and code. The save button is by default disabled and will be enabled only after entering valid credit card number. Implement the following function that will be triggered after entering the card details. The boolean parameter in the function will indicate whether the user has entered a valid card information. Based on this value, the save button is enabled or disabled.

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

Add the following IBAction function which will be called on tap of the button. We are using STPView’s createToken function to generate token and a successful token will be written to the console window.

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

Navigate to Main.storyboard and add UIButton from object library to ViewController. Centre align the button both horizontally and vertically to the View Controller.


Then use Connection Inspector to connect the button to IBOutlet variable and saveButton function to Tap Up Inside event of the button.


Now build and run the project on simulator and you can use dummy card number 4242 4242 4242 4242 to test your implementation. For more details refer to Stripe Testing Documentation.

Sample Output

Successful token - tok_14YBF42eZvKYlo2CoZtuO3Md (test mode)

Error message - Error Domain=NSURLErrorDomain Code=-1009 "The Internet connection appears to be offline." UserInfo=0x7f9b7147b4a0 {NSUnderlyingError=0x7f9b71786ca0 "The Internet connection appears to be offline.", NSErrorFailingURLStringKey=, NSErrorFailingURLKey=, _kCFStreamErrorDomainKey=12, _kCFStreamErrorCodeKey=8, NSLocalizedDescription=The Internet connection appears to be offline.}

Download Demo Project from GitHub

In Category: Develop, ios, iPhone, Programming

Ravi Shankar

A polyglot software developer and now exploring Swift and iOS development. If you would like to learn from me then check out services page.

Show 10 Comments
  • Anand October 26, 2014, 10:34 am

    Hi Ravi,

    I tried integrating Stripe using your code. It shows “STPView.h” not available.

    Also, I was not able to drag and drop. Any other way to do it.

    Appreciate your help.


    • rshankar October 26, 2014, 11:02 am

      Can you pls try updating the Objective-C Bridging Header (Under Build Settings) to your currently project folder location.

      eg:- /Users/ravishankar/Downloads/SwiftStripeDemo-master/Pods/Stripe/Stripe/STPView.m


  • Anand November 8, 2014, 12:53 am

    Thanks Ravi, for the response.

    In the Pod directory I am not able to find STPView. Probably it has changed. Can you tell me Stripe version that you used.

    Also, how will work with PHP.

    I would appreciate if you have any sample app with swift and PHP.


  • Anand November 9, 2014, 1:17 am

    Hi Ravi,

    I tried to integrate using Objective C itself. But somehow if fails. I have posted my question in stack overflow. Here is the link. Would you let me know how to fix it.


    • rshankar November 9, 2014, 6:01 am

      Can you please try giving the publishable key under Stripe.m, you can get publishable key from Stripe -> Account Settings -> API

      static NSString *defaultKey =

      Since I have used a test card starting with 4242, I get an alert message with “Payment Not Successful”

      Hope this helps.

  • Brice Amram June 9, 2015, 11:36 am

    Hi Ravi,
    Many thanks for this tutorial that is helping me a lot! Thanks for putting the time to it.
    I’ve got a comment (1) and a question (2) :

    Everything worked fine 1) provided I did a small adjustment, which might be also useful to our friend Anand who posted earlier here , and 2) until the very last step which is the creation of the token and printing it to the console.

    Starting with my problem/question (2):
    The code builds and runs, I type valid card informations until the saveButton is enabled, but then NOTHING HAPPENS when I click on the saveButton. No error, no token. Nothing is printed on the console. I’ve tried a bunch of things (the action outlet does work so nothing to do with that). Do you think you can help me with that please ?

    Then, my comment (1) is that it seems that stripe has updated their stuff since then. As explained here in the ‘integration’ section ( , the STPView no longer exists and has been replaced in version 1.2.
    So, in order to make this tutorial as it is to work, you just have to replace the reference in the podfile // pod ‘Stripe’ // with // pod ‘Stripe’, ‘1.1.4’ // in order to use the former version of the API that still includes STPView.

    Hope this helps, and that I’ll be able to move on with the token issue thanks to your help on this forum!


    • rshankar June 11, 2015, 7:44 am

      Thanks Brice for sharing this info. Much Appreciated.

      Regarding the problem you are facing, I just tried again with my code and get a token after entering the valid card details after clicking Save Card Details button. I am using test publishable key with card number as 4242424242424242


  • Garrett M July 3, 2015, 9:09 pm

    Hey Ravi,
    I’m new to the site and have a couple questions. I was wondering if you could go over, or point me in the direction, of a peer-to-peer scenario using stripe and the transfer of funds process. Currently, I have a basic understanding of how to route the transaction/funds over to our web server via PHP. But I need to be able to take a small percentage of that transaction, and then route it back to the recipient of the initial payment.

  • Karthik Kuppili April 13, 2016, 4:15 am

    I want to use stripe in my project… and i am unable to follow some of the steps…. Can i have a brief description about what is happening there?

Leave a Comment