微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Apple Pay 处理错误资金不足或其他边缘情况

如何解决Apple Pay 处理错误资金不足或其他边缘情况

我已经在我的 SwiftUI 应用中实现了 Apple Pay,它运行良好。但是我需要处理错误情况。假设用户没有足够的钱或发生其他事情。我需要这个,所以我可以取消购买并显示一条消息。

我还需要确认付款成功。

据我所知,没有一个 PKPaymentAuthorizationControllerDelegate 方法涵盖这些情况。

知道如何获得成功/错误确认吗?

func paymentAuthorizationController(_ controller: PKPaymentAuthorizationController,didAuthorizePayment payment: PKPayment,completion: @escaping (PKPaymentAuthorizationStatus) -> Void) {
    completion(.success)
}

func paymentAuthorizationControllerDidFinish(_ controller: PKPaymentAuthorizationController) {
    controller.dismiss(completion: nil)
    
}

解决方法

您必须实现一些用于支付处理的网络代码,并在出现错误等情况下处理它的响应。这里有一些基本代码,您可以为您的案例使用和改进:

func paymentAuthorizationViewController(
    _ controller: PKPaymentAuthorizationViewController,didAuthorizePayment payment: PKPayment,handler completion: @escaping (PKPaymentAuthorizationResult) -> Void
) {
    // some network code you have to process for payment with payment token and all data what you need
    // it produce some NETWORK_RESPONSE yo can now use - it's up to you what format it will have

    if NETWORK_RESPONSE.success {
        let successResult = PKPaymentAuthorizationResult(status: .success,errors: nil)
        completion(successResult)
    } else if let someErrors = NETWORK_RESPONSE.errors {
        let errorResult = responsePrepared(with: error)
        completion(errorResult)
    } else {
        let defaultFailureResult = PKPaymentAuthorizationResult(status: .failure,errors: nil)
        completion(defaultFailureResult)
    }
}

这里是我上面使用的方法,用来产生一些错误对象,在我的例子中,我说提供的电话号码是错误的。

// it's up to you to produce here the error response object with error messages and pointing
func responsePrepared(with error: Error) -> PKPaymentAuthorizationResult{
    let phoneNumberError = NSError.init(
        domain: PKPaymentErrorDomain,code: PKPaymentError.shippingContactInvalidError.rawValue,userInfo: [
            NSLocalizedDescriptionKey: message,PKPaymentErrorKey.contactFieldUserInfoKey.rawValue: PKContactField.phoneNumber
        ])

    return PKPaymentAuthorizationResult(status: .failure,errors: [phoneNumberError])
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。