我正在尝试为 Apple Pay 支付处理创建一个基类

Posted

技术标签:

【中文标题】我正在尝试为 Apple Pay 支付处理创建一个基类【英文标题】:I am trying to create a base class for Apple Pay payment processing 【发布时间】:2020-11-10 14:21:35 【问题描述】:

我为 Apple Pay 功能创建了一个基类。但是该基类由 NSObject 继承。我只在这个类中创建了支付授权视图控制器,并将它的委托分配给自己。现在它没有调用 pkpaymentauthorizationviewcontrollerdelegate 的委托方法。如何解决这种情况并在基类中只实现一次委托方法并在应用程序的其他地方重用它。请帮忙。

class ApplePay: NSObject  
 
var applePayItem: PKPaymentSummaryItem?
var baseVC: UIViewController?
let payNetworks = [PKPaymentNetwork.masterCard, .visa, .amex, .discover]


init(forItem: Product) 
    applePayItem = PKPaymentSummaryItem.init(label: forItem.name ?? "",
                                             amount: NSDecimalNumber(value: forItem.price ?? 0))


func initiatePayment(complete: @escaping (Bool) -> Void) 
    if PKPaymentAuthorizationViewController.canMakePayments(usingNetworks: payNetworks) 
        let request = PKPaymentRequest()
            request.currencyCode = "USD"
            request.countryCode = "US"
            request.merchantIdentifier = <*identifier from my development account*>
            request.merchantCapabilities = PKMerchantCapability.capability3DS
            request.supportedNetworks = payNetworks
            request.paymentSummaryItems = [applePayItem!]
        guard let paymentVC = PKPaymentAuthorizationViewController(paymentRequest: request) else 
            return
        
        baseVC = UIApplication.shared.keyWindow?.rootViewController?.children.last
        baseVC?.present(paymentVC, animated: true, completion: nil)
        paymentVC.delegate = self
        complete(true)
     else 
        complete(false)
        CaAssembly.resolve(CaAlertProtocol.self)!.showAlert(CaConstant.alertTitles.addCardApplePay, dismiss: )
    


func paymentAuthorizationControllerDidFinish(_ controller: PKPaymentAuthorizationController) 
    controller.dismiss 
        

func paymentAuthorizationViewControllerDidFinish(_ controller: PKPaymentAuthorizationViewController) 
    controller.dismiss(animated: true, completion: nil)




 

 extension ApplePay: PKPaymentAuthorizationViewControllerDelegate, 
PKPaymentAuthorizationControllerDelegate 
 @available(ios 11.0, *)
 func paymentAuthorizationViewController(_ controller: 
   PKPaymentAuthorizationViewController, 
   didAuthorizePayment payment: PKPayment, 
   handler completion: @escaping 
  (PKPaymentAuthorizationResult) -> Void) 
    print(“PKPaymentAuthorizationViewController didAuthorizePayment” )
   

【问题讨论】:

您能分享一下您尝试过的方法以及您预计会导致意外功能的原因吗? 暂时我正在考虑让 ApplePay 类回到继承 UIViewController 并使其背景透明,然后调用在 viewdidappear 上呈现 Apple Pay 授权 VC .... 如果可行,那么它将是对我来说很好。 只是为了更新......我能够通过 UIViewController 继承的基类来实现它......使用库 [github.com/IcaliaLabs/Presentr] 使用它我将基类呈现为具有维度的模态(w:0, h:0) 并在其 viewdidappear 上启动 Apple Pay 【参考方案1】:

只是为了更新......我能够通过 UIViewController 继承的基类来实现它......使用库 [github.com/IcaliaLabs/Presentr] 使用它我将基类呈现为具有维度的模态(w:0, h:0) 并在其 viewdidappear 上启动 Apple Pay:

import UIKit
import PassKit
import Alamofire
import Presentr

struct Product 
   var name: String
   var price: Double


class ApplePay: UIViewController , PKPaymentAuthorizationControllerDelegate, 
PKPaymentAuthorizationViewControllerDelegate //
 
var applePayItem: PKPaymentSummaryItem?
let output = ""
var baseVC: UIViewController?
let payNetworks = [PKPaymentNetwork.masterCard, .visa, .amex, .discover]
var presenter: Presentr = 
    let customPresenter = Presentr(
        presentationType: .custom(
            width: ModalSize.custom(size: 0),
            height: ModalSize.custom(size: 0),
            center: ModalCenterPosition.bottomCenter
        )
    )
    customPresenter.keyboardTranslationType = .compress
    return customPresenter
()
var forItem: Product?

override func viewDidLoad() 
    applePayItem = PKPaymentSummaryItem.init(label: forItem?.name ?? "",
                                             amount: NSDecimalNumber(value: forItem?.price ?? 0))

override func viewDidAppear(_ animated: Bool) 
    initiatePayment()

func initiatePayment() 
    if PKPaymentAuthorizationViewController.canMakePayments(usingNetworks: payNetworks) 
        let request = PKPaymentRequest()
            request.currencyCode = "USD"
            request.countryCode = "US"
            request.merchantIdentifier = <merchant id created on developer account>
            request.merchantCapabilities = PKMerchantCapability.capability3DS
            request.supportedNetworks = payNetworks
            request.paymentSummaryItems = [applePayItem!]
        guard let paymentVC = PKPaymentAuthorizationViewController(paymentRequest: request) else 
            return
        
        self.present(paymentVC, animated: true, completion: nil)
        paymentVC.delegate = self
     


func paymentAuthorizationControllerDidFinish(_ controller: PKPaymentAuthorizationController) 
    controller.dismiss 
        
    

func paymentAuthorizationViewControllerDidFinish(_ controller: PKPaymentAuthorizationViewController) 
    controller.dismiss(animated: true, completion: nil)


@available(iOS 11.0, *)
func paymentAuthorizationViewController(_ controller: PKPaymentAuthorizationViewController, didAuthorizePayment payment: PKPayment, handler completion: @escaping (PKPaymentAuthorizationResult) -> Void) 
    



【讨论】:

以上是关于我正在尝试为 Apple Pay 支付处理创建一个基类的主要内容,如果未能解决你的问题,请参考以下文章

苹果会强迫您通过Apple Pay进行应用内购买和订阅吗?

Apple Pay - 如何将商家公钥与支付令牌中的 publicKeyHash 进行比较?

Apple Pay - 支付后端服务器 - iOS

Apple Pay - 获取完整的送货地址(包含“addressLines”)

Apple Pay支付

Apple Pay 是不是可以使用订阅支付模式?