ios - 如何让我的 Stripe 支付系统正常工作?
问题描述
我正在尝试在我的应用程序中实现条带,但是当我使用以下代码时,付款页面不会停止加载。我究竟做错了什么?
ViewController()
是接受订单的按钮是:
import UIKit
import Firebase
import Stripe
class ViewController: UIViewController {
var totalAmount = 0
override func viewDidLoad() {
}
@IBAction func goToPaymentButton(_ sender: UIButton) {
print("checkoutClicked")
let addCardViewController = STPAddCardViewController()
addCardViewController.delegate = self
navigationController?.pushViewController(addCardViewController, animated: true)
}
}
extension OrderDetialsViewController: STPAddCardViewControllerDelegate {
func addCardViewControllerDidCancel(_ addCardViewController: STPAddCardViewController) {
navigationController?.popViewController(animated: true)
print("0")
}
private func addCardViewController(_ addCardViewController: STPAddCardViewController,
didCreateToken token: STPToken,
completion: @escaping STPErrorBlock) {
print("addcardviewcontroller")
totalAmount = 0
if localData.shoppingCart.itemSizeArray.count > 0{
for i in 0...localData.shoppingCart.itemSizeArray.count-1{
if localData.shoppingCart.itemSizeArray[i] == "hd"{
totalAmount = totalAmount+2000
}
else if localData.shoppingCart.itemSizeArray[i] == "d"{
totalAmount = totalAmount+4000
}
else if localData.shoppingCart.itemSizeArray[i] == "free" {
totalAmount = totalAmount + 0
}
else if localData.shoppingCart.itemSizeArray[i] == "home"{
totalAmount = totalAmount + 500
}
}
}
StripeClient.shared.completeCharge(with: token, amount: totalAmount) { result in
print("stripeclientsharedcompletecharge")
switch result {
// 1
case .success:
print("success")
completion(nil)
let alertController = UIAlertController(title: "Congrats",
message: "Your payment was successful!",
preferredStyle: .alert)
let alertAction = UIAlertAction(title: "OK", style: .default, handler: { _ in
self.navigationController?.popViewController(animated: true)
})
alertController.addAction(alertAction)
self.present(alertController, animated: true)
// 2
case .failure(let error):
print("failure")
completion(error)
}
}
}
}
我也有StripeClient
如下设置:
import Foundation
import Alamofire
import Alamofire
import Stripe
enum Result {
case success
case failure(Error)
}
final class StripeClient {
static let shared = StripeClient()
private init() {
// private
}
private lazy var baseURL: URL = {
guard let url = URL(string: Constants.baseURLString) else {
fatalError("Invalid URL")
}
return url
}()
func completeCharge(with token: STPToken, amount: Int, completion: @escaping (Result) -> Void) {
// 1
let url = baseURL.appendingPathComponent("charge")
// 2
let params: [String: Any] = [
"token": token.tokenId,
"amount": amount,
"currency": Constants.defaultCurrency,
"description": Constants.defaultDescription
]
// 3
Alamofire.request(url, method: .post, parameters: params)
.validate(statusCode: 200..<300)
.responseString { response in
switch response.result {
case .success:
completion(Result.success)
case .failure(let error):
completion(Result.failure(error))
}
}
}
}
解决方案
推荐阅读
- python - 如何在`pandas`中合并部分多索引
- go - 致命错误:所有 goroutine 都处于休眠状态 - 死锁!当使用带有 for-select 循环的 WaitGroup 时
- postgresql - Postgresql:禁用特定用户(root)的远程连接?
- c# - C# 错误:CS0103 当前上下文中不存在名称“”
- webstorm - 如果输入来自 WebStorm 中的 Web,如何在控制台中查看输出
- java - 如果不存在则根据点字符串包名创建目录
- python - 我如何阅读特定的一行以及该行下的另外 7 行
- c++ - 读取函数会影响文件的获取指针吗?
- gcc - 如何在 rustc 中使用 gcc 标志
- javascript - 根据位置替换 url 中的 GET 参数(正则表达式)