[英]Apple Pay without Stripe iOS SDK
I've been using Stripe iOS SDK for a while now and everything is clear regarding the implementation.我已经使用 Stripe iOS SDK 一段时间了,关于实现的一切都很清楚。 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.
由于我们的应用程序将在 iOS 14 上支持 App Clips,我们正在减少二进制大小,因此决定也删除 Stripe iOS SDK。
So my question here is if I can somehow send payment requests via the API and omitting the Stripe SDK altogether?所以我的问题是我是否可以通过 API 以某种方式发送付款请求并完全省略 Stripe SDK?
ps: It looks like I need to implement the /tokens endpoint passing the card data. ps:看起来我需要实现传递卡片数据的/tokens端点。 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:
以下是实现这一目标的步骤:
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()
}
}
}
Use JSONEncoder
and set keyEncodingStrategy
to .convertToSnakeCase
.使用
JSONEncoder
并设置keyEncodingStrategy
到.convertToSnakeCase
。
Create a POST request against https://api.stripe.com/v1/tokens
endpoint where you need to url encode parameters.针对需要 url 编码参数的
https://api.stripe.com/v1/tokens
端点创建 POST 请求。 If you are using Alamofire, you need to set encoding to URLEncoding.default
.如果您使用的是 Alamofire,则需要将 encoding 设置为
URLEncoding.default
。
Parse response.解析响应。 I use
JSONDecoder
with the following model:我将
JSONDecoder
与以下模型一起使用:
import Foundation
struct StripeTokenResponse: Decodable {
let id: String
}
StripeTokenResponse.id
is the thing you need to pass to the backend where the payment will be processed.StripeTokenResponse.id
是您需要传递给将处理付款的后端的东西。 This is the same step as you'll do when using the SDK.您可以检查Strip checkout ,它允许您以 Web 格式显示付款页面,而无需在客户端使用任何 Stripe SDK。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.