In the first part – Integrate Apple payment method – of this tutorial series, we saw the entire process of integrating Apple pay in an iOS app.
Now in this part, we’ll learn about how to integrate Stripe payment gateway in Apple pay.
If you’re hearing about Stripe payment gateway for the first time in your life, then you must know that Stripe is one of the most powerful libraries to accept payments online and in mobile applications. And if you’re going to sell physical products in your app and looking for online payment services like Paypal, then Stripe payment gateway should be on top of your list.
Reason? Well, there are plenty of reasons, actually.
Let’s see:
Security:
Let’s admit it, Security is everyone’s highest priority and fortunately, Stripe has it! Stripe has implemented couple of layers for assurance specifically designed for mobile app transections. Additionally, Stripe is a certified PCI service provider level 1, and it has two factor validation that ensures and prevents unauthorized access to your account.
User-Friendly API:
This is a plus point for developers. The reason why Stripe got enormous amount of success is due to easy to understand and well document API.
Simple Pricing:
This is the best part about Stripe payment gateway that there is absolutely no need to setup or monthly expense and no added operational charges.
Looking at the benefits, there is no reason why you shouldn’t integrate it in your Apple pay.
Now if you’ve followed our part 1 of Apple payment method integration, then you already have a lightweight server to accept payments, the only remaining thing is to integrate the Stripe payment gateway.
Now, we’ll start implementing Stripe payment gateway in this Stripe tutorial.
Integrating The Stripe
First, create a Stripe account.
- Go to https://stripe.com/
- Click on “Create Account”
- Go for new registration/login(if you already have an account)
Once when you complete Registration/Login process then go to “Your Account” -> “Account Settings”.
It will open following options in a popup.
Select “Api Key” option and get Api key for use in Stripe integration in iOS app.
Now create a new project under file menu and select “Single View Application” in XCODE.
In the next tab, write name for your app. Here, we’re naming it as SOStripe.
Open terminal and create a pod file, install pod of Stripe
pod 'Stripe'
Go to AppDelegate.swift and import stripe in it
import Stripe
now configure stripe in didFinishLaunchingWithOptions.
STPPaymentConfiguration.sharedConfiguration().publishableKey = "pk_test_secretKEY”
in ViewController.swift we have two buttons
- Add Card View Controller
- Add Card TextField
When user click on “Add Card View Controller” then it will open Stripe view.
@IBAction func actionAddCardDefault(sender: AnyObject) { let addCardViewController = STPAddCardViewController() addCardViewController.delegate = self // STPAddCardViewController must be shown inside a UINavigationController. let navigationController = UINavigationController(rootViewController: addCardViewController) self.presentViewController(navigationController, animated: true, completion: nil) }
Set delegate of STPAddCardViewController in ViewController.swift class ViewController: UIViewController, STPAddCardViewControllerDelegate User can insert card detail and once user complete and click on “Done” button then it will call delegate of STPAddCardViewControllerDelegate. // MARK: STPAddCardViewControllerDelegate func addCardViewControllerDidCancel(addCardViewController: STPAddCardViewController) { self.dismissViewControllerAnimated(true, completion: nil) } func addCardViewController(addCardViewController: STPAddCardViewController, didCreateToken token: STPToken, completion: STPErrorBlock) { print(token) //Use token for backend process self.dismissViewControllerAnimated(true, completion: { completion(nil) }) }
Now click on “Add Card TextField” It will navigate to another view controller (StripeCardVC.swift).
Now create STPPaymentCardTextField object in viewDidLoad and add it in view.
let paymentField = STPPaymentCardTextField(frame: CGRect(x: 10, y: 100, width:self.view.frame.size.width - 20, height: 44)) paymentField.delegate = self self.view.addSubview(paymentField)
Add a button in StripeCardVC.swift which will be Disable and once user enter all the correct data in textField then button will be enable.
@IBOutlet weak var btnBuy: UIButton!
Create an object for save all the card related parameter which is inserted by user in textField.
let cardParams = STPCardParams()
Set delegate of STPPaymentCardTextField in StripeCardVC.swift class StripeCardVC: UIViewController, STPPaymentCardTextFieldDelegate
Now once user start to fill textField info then following delegate will be called every time and Once user complete then “btnBuy” should be enabled.
func paymentCardTextFieldDidChange(textField: STPPaymentCardTextField) { print("Card number: \(textField.cardParams.number) Exp Month: \(textField.cardParams.expMonth) Exp Year: \(textField.cardParams.expYear) CVC: \(textField.cardParams.cvc)") self.btnBuy.enabled = textField.isValid if btnBuy.enabled { btnBuy.backgroundColor = UIColor.blueColor() cardParams.number = textField.cardParams.number cardParams.expMonth = textField.cardParams.expMonth cardParams.expYear = textField.cardParams.expYear cardParams.cvc = textField.cardParams.cvc } }
Now click on “btnBuy” action, it will create a token for backend process.
@IBAction func actionGetStripeToken() { STPAPIClient.sharedClient().createTokenWithCard(cardParams) { (token, error) in if let error = error { // show the error to the user print(error) self.showAlertButtonTapped("Error", strMessage: error.localizedDescription) } else if let token = token { print(token) //Send token to backend for process } } } //MARK:- AlerViewController func showAlertButtonTapped(strTitle:String, strMessage:String) { // create the alert let alert = UIAlertController(title: strTitle, message: strMessage, preferredStyle: UIAlertControllerStyle.Alert) // add an action (button) alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil)) // show the alert self.presentViewController(alert, animated: true, completion: nil) }
Done!
By following the above process, you can easily integrate the Stripe payment gateway in your iOS app. If you face any problem implementing Stripe in your iOS app, contact us.
In case you’re wondering whether you can integrate both, Stripe as well as Paypal or not, then the answer is absolutely yes, you can!
And if you’d like to add Paypal payment method as well, in your iOS app, check out the complete guide on Paypal Integration in iOS.
In case, you’re not a technical guy, do hire iPhone app developer from Space-O Technologies.
We’re a leading iPhone app development company in india and we’ve already developed more than 2000+ mobile app throughout our journey. In fact, few of the apps even got featured in Techcrunch as well (you can check our iPhone app portfolio here). Additionally, we’ve worked with 25+ successful startups and helped young minds to achieve their goal. So after knowing all these, you can be rest assured that you’ll get a quality work in your project!
You can download source code of Stripe payment integration from Github as well.
This page was last edited on January 11th, 2021, at 2:59.
Pingback: Integrate Apple Payment Method in iOS App - iOS...
Hi I am trying to use Apple Pay in my app and then integrate from Apple Pay to Stripe.. can you help me do that?
Yes, sure, Liz.
In fact, our development team decided to write an article about it.
We will share the link with you before next week.
Thank you.
Thanks :). I have gotten this far before. The problem I am having is sending the tokens from Apple Pay to Stripe. I have all this set up but this doesn’t send the payments from Apple Pay to Stripe.