Borut
Borut

Reputation: 8138

Apple Pay without Stripe iOS SDK

I've been using Stripe iOS SDK for a while now and everything is clear regarding the implementation. Since our app is going to support App Clips on iOS 14, we are reducing the binary size and therefore decided to remove Stripe iOS SDK as well.

So my question here is if I can somehow send payment requests via the API and omitting the Stripe SDK altogether?

p.s.: It looks like I need to implement the /tokens endpoint passing the card data. Is there any example of the request to be made?

Upvotes: 0

Views: 1047

Answers (2)

Borut
Borut

Reputation: 8138

I've managed to solve this situation and here is the solution if anyone is interested. Here are the steps to make this happen:

  1. Prepare a request model
import Foundation
import PassKit

struct StripeTokenRequest: Encodable {
  let pkToken: String
  let card: Card
  let pkTokenInstrumentName: String?
  let pkTokenPaymentNetwork: String?
  let pkTokenTransactionId: String?
  
  init?(payment: PKPayment) {
    guard let paymentString = String(data: payment.token.paymentData, encoding: .utf8) else { return nil }
    pkToken = paymentString
    card = .init(contact: payment.billingContact)
    pkTokenInstrumentName = payment.token.paymentMethod.displayName
    pkTokenPaymentNetwork = payment.token.paymentMethod.network.map { $0.rawValue }
    pkTokenTransactionId = payment.token.transactionIdentifier
  }
}

extension StripeTokenRequest {
  struct Card: Encodable {
    let name: String?
    let addressLine1: String?
    let addressCity: String?
    let addressState: String?
    let addressZip: String?
    let addressCountry: String?
    
    init(contact: PKContact?) {
      name = contact?.name.map { PersonNameComponentsFormatter.localizedString(from: $0, style: .default, options: []) }
      addressLine1 = contact?.postalAddress?.street
      addressCity = contact?.postalAddress?.city
      addressState = contact?.postalAddress?.state
      addressZip = contact?.postalAddress?.postalCode
      addressCountry = contact?.postalAddress?.isoCountryCode.uppercased()
    }
  }
}
  1. Use JSONEncoder and set keyEncodingStrategy to .convertToSnakeCase.

  2. Create a POST request against https://api.stripe.com/v1/tokens endpoint where you need to url encode parameters. If you are using Alamofire, you need to set encoding to URLEncoding.default.

  3. Parse response. I use JSONDecoder with the following model:

import Foundation

struct StripeTokenResponse: Decodable {
  let id: String
}
  1. Create a payment StripeTokenResponse.id is the thing you need to pass to the backend where the payment will be processed. This is the same step as you'll do when using the SDK.

Upvotes: 2

Ken Chen
Ken Chen

Reputation: 721

You can check Strip checkout, it allows you to present a payment page in web format without any Stripe SDK on the client side.

Upvotes: 1

Related Questions