我有一个应用程序,其中有一个在情节提要中设计的视图控制器,它是STPAddCardViewController的类。据我所知,视图控制器可以正确显示,并且当我输入所有信用卡信息或对其进行扫描时,该过程都不会出现问题。
当我点击“完成”时,它没有添加卡。我知道“完成”按钮的接线是可行的,因为我已对其进行编程以显示UIAlertController警报作为测试。它还会解析我的代码,因为我收到了来自Stripe的JSON响应。
我已经设置了断点来逐步执行代码,并且看到cardParams始终为null。我认为这可能是问题所在。
AddPaymentVC:
import UIKit
import CoreData
import Stripe
import Firebase
import Alamofire
import SVProgressHUD
class AddCardVC: STPAddCardViewController, Alertable {
var stripeTool = StripeTools()
var paymentField = STPPaymentCardTextField()
var baseURLString: String? = "https://api.stripe.com/v1/customers"
var baseURL: URL {
if let urlString = self.baseURLString, let url = URL(string: urlString) {
return url
} else {
fatalError()
}
}
let theme = STPTheme()
override func viewDidLoad() {
super.viewDidLoad()
theme.font = UIFont(name: "Avenir Next Regular ", size: 17)
theme.emphasisFont = UIFont(name: "Avenir Next Bold", size: 17)
navigationItem.leftBarButtonItem?.tintColor = .white
navigationItem.rightBarButtonItem?.tintColor = .white
self.navigationItem.title = "ADD CARD"
self.navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .cancel, target: self, action: #selector(dismiss as () -> Void))
self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(done as () -> Void))
}
func createCard(stripeId: String, card: STPCardParams, completion: @escaping (_ success: String, _ error: NSError?) -> Void) {
stripeTool.generateToken(card: card) { (token) in
if(token != nil) {
let customerId = UserDefaults.standard.object(forKey: "stripeId") as? String
print(customerId!)
let url = self.baseURL.appendingPathComponent("\(customerId!)/sources")
var params = [String:String]()
params["source"] = token!.tokenId
let headers = ["Authorization": self.stripeTool.getBasicAuth()]
Alamofire.request(url, method: .post, parameters: params, encoding: URLEncoding.queryString, headers: headers)
.validate(statusCode: 200..<300)
.responseJSON { response in
// print(response)
// if let json = response.result.value as? JSONDictionary,
// let carddata = json["data"] as? [JSONDictionary] {
// print(carddata)
// }
}
}
}
}
@objc func dismiss() {
navigationController?.popViewController(animated: true)
dismiss(animated: true, completion: nil)
}
@objc func done() {
let cardParams: STPCardParams = STPCardParams()
cardParams.number = paymentField.cardNumber
cardParams.expMonth = paymentField.expirationMonth
cardParams.expYear = paymentField.expirationYear
cardParams.cvc = paymentField.cvc
STPAPIClient.shared().createToken(withCard: cardParams, completion: {(result, error) -> Void in
if error == nil {
let customerId = UserDefaults.standard.object(forKey: "stripeId") as? String
self.createCard(stripeId: customerId!, card: cardParams, completion: { (success, error) in
if let error = error {
// Present error to user...
}
else {
print("card updated")
}
})
}
else {
//handle error
}
})
navigationController?.popViewController(animated: true)
dismiss(animated: true, completion: nil)
}
}
StripeTools.swift
import Foundation
import Stripe
struct StripeTools {
private var stripeSecret = "key removed for Stack post"
func generateToken(card: STPCardParams, completion: @escaping (_ token: STPToken?) -> Void) {
STPAPIClient.shared().createToken(withCard: card) { token, error in
if let token = token {
completion(token)
}
else {
print(error!)
completion(nil)
}
}
}
func getBasicAuth() -> String{
return "Bearer \(self.stripeSecret)"
}
}
我希望能够在客户点击完成后将信用卡提交给Stripe API