如何解决带条纹的ApplePay中的无效地址
我正在将Apple Pay与Stripe一起使用,并且在无法运输时可以正常工作。
当有总是选择它运送地址给出无效地址错误。 (与Stripe SandBox Key一起使用)
我正在使用 STKPaymentContext API,并按照以下链接进行操作。
https://stripe.com/docs/mobile/ios/basic
在配置中,我已经写了这个。
let config = STPPaymentConfiguration.shared()
config.requiredShippingAddressFields = [.postalAddress,.phoneNumber,.name]
不确定这是怎么回事。
这是它的外观。
这是我的密码
extension CheckoutTableViewController:STPPaymentContextDelegate{
func paymentContext(_ paymentContext: STPPaymentContext,didUpdateShippingAddress address: STPAddress,completion: @escaping STPShippingMethodsCompletionBlock) {
guard
let buyerPostalCode = address.postalCode,let buyerCountry = address.country,let productId = self.productDetailsData?.productId
else{
completion(.invalid,nil,nil)
return
}
guard let phone = address.phone,phone.count > 0 else {
completion(.invalid,RatesError.phoneNumberrequired,[],nil)
return
}
var shipmentItem:[String:Any] = [:]
shipmentItem["order_amount"] = self.productCost
shipmentItem["actual_weight"] = 8
shipmentItem["height"] = 7
shipmentItem["width"] = 10
shipmentItem["length"] = 13
shipmentItem["currency"] = "USD"
shipmentItem["destination_postal_code"] = buyerPostalCode
shipmentItem["destination_country_code"] = buyerCountry
shipmentItem["product_id"] = productId
shipmentItem["category"] = "fashion"
enum RatesError:Error,LocalizedError{
case NoDeliveryOptionsFound
case phoneNumberrequired
public var errorDescription: String? {
switch self {
case .NoDeliveryOptionsFound:
return "No couriers are available at the address.\nPlease try with different address."
case .phoneNumberrequired:
return "Please enter phone number."
}
}
}
fetchShippingOptions(forItem: shipmentItem,completionSuccess: {[weak self] (response) in
guard let self = `self` else {
return
}
if
let responseValue = response as? [String:Any],let rates = responseValue["rates"] as? [[String:Any]]{
self.shippingRates = []
for rate in rates{
if let fullName = rate["courier_display_name"] as? String,let identifier = rate["courier_id"] as? String,let amount = rate["shipment_charge_total"] as? Double,let detail = rate["full_description"] as? String
{
let method = PKShippingMethod()
method.amount = NSDecimalNumber.init(value: amount.currency)
method.identifier = identifier
method.label = fullName
method.detail = detail.replacingOccurrences(of: fullName,with: "")
self.shippingRates.append(method)
}
}
completion(.valid,self.shippingRates,self.shippingRates.first)
}else{
completion(.invalid,RatesError.NoDeliveryOptionsFound,nil)
}
}) { (error) in
completion(.invalid,error,nil)
}
}
func paymentContextDidChange(_ paymentContext: STPPaymentContext) {
if let paymentOption = paymentContext.selectedPaymentOption {
self.lblPaymentMethod.text = paymentOption.label
} else {
self.lblPaymentMethod.text = "Select Payment"
}
if let shippingMethod = paymentContext.selectedShippingMethod {
if let selectedrate = self.shippingRates.first(where: { (method) -> Bool in
guard let leftValue = method.identifier,let rightValue = shippingMethod.identifier else{
return false
}
return leftValue == rightValue
}){
self.lblAddress.text = selectedrate.label
self.shippingCharges = Double(truncating: selectedrate.amount).currency
self.lblShippingCharges.text = "$\(shippingCharges)"
self.getStripeFees(forAmount: self.productCost + self.shippingCharges)
}
} else {
self.lblAddress.text = "Select Address"
}
self.updatetotalCost()
}
func paymentContext(_ paymentContext: STPPaymentContext,didFailToLoadWithError error: Error) {
let alertController = UIAlertController(
title: "Error",message: error.localizedDescription,preferredStyle: .alert
)
let cancel = UIAlertAction(title: "Cancel",style: .cancel,handler: { action in
// Need to assign to _ because optional binding loses @discardableResult value
// https://bugs.swift.org/browse/SR-1681
_ = self.navigationController?.popViewController(animated: true)
})
let retry = UIAlertAction(title: "Retry",style: .default,handler: { action in
self.paymentContext?.retryLoading()
})
alertController.addAction(cancel)
alertController.addAction(retry)
self.present(alertController,animated: true,completion: nil)
}
func paymentContext(_ paymentContext: STPPaymentContext,didCreatePaymentResult paymentResult: STPPaymentResult,completion: @escaping STPPaymentStatusBlock) {
self.callPaymentIntentAPI(paymentContext,didCreatePaymentResult: paymentResult,completion: completion)
}
func paymentContext(_ paymentContext: STPPaymentContext,didFinishWith status: STPPaymentStatus,error: Error?) {
OperationQueue.main.addOperation {
SVProgressHUD.dismiss()
}
let title: String
let message: String
switch status {
case .error:
title = "Error"
message = error?.localizedDescription ?? ""
UIAlertController.showAlert(withTitle: title,andMessage: message,andButtonTitles: ["Okay"]) {[weak self] (selectedindex) in
OperationQueue.main.addOperation {
self?.navigationController?.popViewController(animated: true)
}
}
case .success:
title = "Success"
message = "Your purchase was successful!"
UIAlertController.showAlert(withTitle: title,andButtonTitles: ["Okay"]) {[weak self] (selectedindex) in
OperationQueue.main.addOperation {
self?.onPaymentCompletion?()
var isControllerFound:Bool = false
for controller in self?.navigationController?.viewControllers ?? []{
if (controller is ProductDetailsViewController) || (controller is ChatVC){
isControllerFound = true
self?.navigationController?.popToViewController(controller,animated: true)
break
}
}
if !isControllerFound{
self?.navigationController?.popViewController(animated: true)
}
}
}
case .userCancellation:
return()
@unkNown default:
return()
}
}
}
解决方法
最后,我发现了一个错误。
Apple在付款时会调用didUpdateShippingAddress
方法,但出于安全目的,它不会提供所有信息。因此,就我而言,电话号码验证是导致该错误的原因。
所以我从该方法中删除了以下代码。
guard let phone = address.phone,phone.count > 0 else {
completion(.invalid,RatesError.phoneNumberRequired,[],nil)
return
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。