Published On: December 23rd, 20162.8 min read

Stripe is a powerful library to accept online payments for mobile apps. It is a payment solution for selling products via apps. “Stripe” concept is different from “In-App Purchase” concept in case of iOS. For selling physical goods like books, our products etc we can use Stripe as a payment solution whereas “In-App Purchase” is used to sell digital contents(e.g. bonus point for your app, an extra privilege for your app etc.). Using stripe we can easily enable our app to take a payment from a credit card.

Stripe Payment Cycle

* Send credit card data from app to Stripe
* Get token from Stripe in case of valid data
* Send token to web server from app
* Web application sends token to Stripe
* Get transaction response from Stripe
* Send back transaction response to the app

Stripe Integration | Nascenia

Adding Stripe Library to Project(Using Cocoapods)
  • pod ‘Stripe’,
  • pod ‘AFNetworking’,
  • pod install
Using Objective C in Swift App

Stripe library is written in Objective C. But we want to use it in a project which is written in swift. To use it in a swift project we need to set up a bridging header.

The procedure of Creating Bridging Header 

* Add a new header file to the Project directory by right-clicking the project directory in the project navigator, followed by clicking “New File…”.

* Select iOS -> Source category, choose the “Header File” template and click Next.

* Name the class “Project_Name-Bridging-Header.h”, then press continue to proceed and save the file.

* Next, go to “Build Settings”, and search for “Objective-C Bridging Header”. Set the value to “Project_Name/Project_Name-Bridging-Header.h”.

Once done, open the Project_Name-Bridging-Header.h file and write the following code:

 `#import ` 
 ` #import  `

In **AppDelegate.swift** insert

 func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
 // Stripe Configuration
 return true

to configure Stripe with our test publishable API key.

Collecting credit card information

To perform a transaction, an app should have a mean to collect the user’s information. Stripe suggests three ways to collect the payment information:

* Through Apple Pay to access users’ stored payment information
* By using Stripe’s built-in payment form component, PaymentKit
* By building own payment form in the app

Create Token with the Following Method
STPAPIClient.sharedClient().createTokenWithCard(stripCard, completion: { (token, error) -> Void in 
 if error != nil {
 //handle error
 // If successfull
 // postStripeToken(token!)

Then use the token to the backend for payment processing.

When backend is ready to process the payment, switch back to Xcode and start sending the token to backend by making an HTTP POST request.

 func postStripeToken(token: STPToken) {
 let URL = "urlString"
 let params = ["stripeToken": token.tokenId,
 "amount": self.amountTextField.text.toInt()!,
 "currency": "usd",
 "description": self.emailTextField.text]
 let manager = AFHTTPRequestOperationManager()
 manager.POST(URL, parameters: params, success: { (operation, responseObject) -> Void in
 if let response = responseObject as? [String: String] {
 UIAlertView(title: response["status"],
 message: response["message"],
 delegate: nil,
 cancelButtonTitle: "OK").show()
 }) { (operation, error) -> Void in
 //Handle error

To perform the actual charge, Stripe requires us to submit the token, plus the payment amount, currency, and description.

To verify if the payment succeeds, the developer can check out Stripe’s dashboard.

Stripe Integration in Swift | Nascenia

Contributor: Sadrul Islam, Nascenia

Share it, Choose Your Platform!

More to Explore

The Quest for Knowledge Continues. Fuel Your Curiosity.