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

我正在尝试为Apple Pay付款处理创建基类

如何解决我正在尝试为Apple Pay付款处理创建基类

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


const reducer = (state,{ type,i,checked,number }) => {
    switch (type) {
      case "setValues":
        // reducer needs a new copy of the state
        const newState = [...state];
        newState[i] = {
          id: i,checked: checked ? checked : state[i]?.checked,number: number ? number : state[i]?.number
        };
        console.log(newState);
        return newState;

      default:
        break;
    }
  };

const [state,dispatch] = useReducer(reducer,initState);

return(
  <InputNumber
     min={1}
     max={10}
     onChange={(number) => {
                  dispatch({ type: "setValues",number });
              }}
  />)

解决方法

仅用于更新...我能够通过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) {
    
}

}

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