简体   繁体   中英

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?

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

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.

您可以检查Strip checkout ,它允许您以 Web 格式显示付款页面,而无需在客户端使用任何 Stripe SDK。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM