第一个API的实现 Stripe 有三个 API,我已经正确实现了这三个 API,并正确显示了它们的结果,但我在打开付款表时遇到问题,打开了一半的付款表,然后应用程序崩溃了。
func postRequestForCustomer() {
let headers: HTTPHeaders = [
Constants.authorization: Constants.bearer
]
AF.request(Constants.stripeBaseURL + Constants.customers, method: .post, encoding: JSONEncoding.default, headers: headers)
.validate(statusCode: 200..<300)
.response { [self] response in
switch response.result {
case .success(let value):
if let jsonData = value {
do {
let json = try JSON(data: jsonData)
if let customerId = json["id"].string {
postRequestForEphemeralKey(customerID: customerId)
}
} catch {
print("Error parsing JSON for customer creation: \(error.localizedDescription)")
}
} else {
print("Unexpected response format for customer creation")
}
case .failure(let error):
print("Customer Creation Error: \(error.localizedDescription)")
}
}
}
第二个API的实现
func postRequestForEphemeralKey(customerID: String) {
let headers: HTTPHeaders = [
Constants.authorization: Constants.bearer,
Constants.stripe_Version: Constants.stripe_Version_date
]
let params: [String: Any] = [
Constants.Params.customer: customerID
]
AF.request(Constants.stripeBaseURL + Constants.ephemeral_keys, method: .post, parameters: params, encoding: URLEncoding.httpBody, headers: headers)
.validate(statusCode: 200..<300)
.response { [self] response in
switch response.result {
case .success(let value):
if let jsonData = value {
print("Ephemeral Key Response: \(String(data: jsonData, encoding: .utf8) ?? "")")
postRequestForPayment(customerID: customerID, amount: 10000)
} else {
print("Unexpected response format for ephemeral key")
}
case .failure(let error):
if let data = response.data, let errorMessage = String(data: data, encoding: .utf8) {
print("Ephemeral Key Error: \(errorMessage)")
} else {
print("Ephemeral Key Error: \(error.localizedDescription)")
}
}
}
}
第三个API的实现
func postRequestForPayment(customerID: String, amount: Int) {
let headers: HTTPHeaders = [
Constants.authorization: Constants.bearer,
]
let params: [String: Any] = [
Constants.Params.customer: customerID,
Constants.Params.amount: amount,
Constants.Params.currency: Constants.Params.usd
]
AF.request(Constants.stripeBaseURL + Constants.payment_intents, method: .post, parameters: params, encoding: URLEncoding.httpBody, headers: headers)
.validate(statusCode: 200..<300)
.response { [self] response in
switch response.result {
case .success(let value):
if let jsonData = value {
do {
let json = try JSON(data: jsonData)
if let ephKeyID = json["id"].string {
openPaymentSheet()
print("Ephemeral Key ID: \(ephKeyID)")
} else {
print("Ephemeral Key ID not found in response")
}
} catch {
print("Error parsing JSON for ephemeral key: \(error.localizedDescription)")
}
} else {
print("Unexpected response format for ephemeral key")
}
case .failure(let error):
if let data = response.data, let errorMessage = String(data: data, encoding: .utf8) {
print("Ephemeral Key Error: \(errorMessage)")
} else {
print("Ephemeral Key Error: \(error.localizedDescription)")
}
}
}
}
打开付款单
func openPaymentSheet() {
if let paymentSheet = paymentSheet {
paymentSheet.present(from: self) { paymentResult in
// MARK: Handle the payment result
switch paymentResult {
case .completed:
print("Your order is confirmed")
case .canceled:
print("Canceled!")
case .failed(let error):
print("Payment failed: \(error)")
}
}
} else {
print("Payment sheet is nil. Check the configuration.")
}
}
这里是viewDidLoad方法
override func viewDidLoad() {
super.viewDidLoad()
xibRegister()
setUI()
let configuration = PaymentSheet.Configuration()
paymentSheet = PaymentSheet(paymentIntentClientSecret: Constants.secret_Key, configuration: configuration)
}
我要打开付款单。