I am trying to create interface to communicate with apple pay sheet from flutter app. Sheet is opening and I get callback for present(paymentVC)
so sheet is initialized and I can enter touch ID but events like paymentAuthorizationViewControllerDidFinish
and paymentAuthorizationViewControllerWillAuthorizePayment
not firing although I defined a delegate. Here is my code:
AppDelegate.swift
import UIKit
import Flutter
import PassKit
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}
Plugin.swift
import Flutter
import PassKit
import UIKit
public class SwiftFlutterPayPlugin: NSObject, FlutterPlugin {
public static func register(with registrar: FlutterPluginRegistrar) {
let channel = FlutterMethodChannel(name: "flutter_pay", binaryMessenger: registrar.messenger())
let instance = SwiftFlutterPayPlugin()
registrar.addMethodCallDelegate(instance, channel: channel)
registrar.addApplicationDelegate(instance)
}
public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
if (call.method == "openApplePaySheet") {
let vc = PaymentViewController()
vc.modalPresentationStyle = .overCurrentContext
UIApplication.shared.delegate!.window!!.rootViewController!.present(vc, animated: false, completion: nil)
print("Making module")
vc.makeModule()
print("Showed apple pay eventually")
result("iOS " + UIDevice.current.systemVersion)
}
}
}
class PaymentViewController: UIViewController, PKPaymentAuthorizationViewControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .black
view.alpha = 0.5
view.isOpaque = true
}
func displayDefaultAlert(title: String?, message: String?) {
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
let okAction = UIAlertAction(title: "Ok", style: .default, handler: nil)
alert.addAction(okAction)
self.present(alert, animated: true, completion: nil)
// UIApplication.shared.delegate!.window!!.rootViewController!.present(alert, animated: true, completion: nil)
}
func makeModule() {
let paymentNetworks = [PKPaymentNetwork.masterCard, PKPaymentNetwork.visa]
guard PKPaymentAuthorizationViewController.canMakePayments() else {
print("Cannot make payments")
return
}
let countryCode = Locale.current.regionCode ?? "RU"
let merchantIdentifier = "merchant.somename"
let paymentRequest = PKPaymentRequest()
paymentRequest.merchantIdentifier = merchantIdentifier
paymentRequest.countryCode = countryCode
paymentRequest.currencyCode = "RUB"
paymentRequest.supportedNetworks = paymentNetworks
paymentRequest.merchantCapabilities = .capability3DS
paymentRequest.paymentSummaryItems = [PKPaymentSummaryItem(label: "Концерт Славы", amount: 15.0)]
guard let paymentVC = PKPaymentAuthorizationViewController(paymentRequest: paymentRequest) else {
print("Cannot show payment VC")
return
}
print("Gonna present")
present(paymentVC, animated: true) {
print("Completion")
}
}
func paymentAuthorizationViewControllerDidFinish(_ controller: PKPaymentAuthorizationViewController) {
print("Dismissed did finish")
}
func paymentAuthorizationViewControllerWillAuthorizePayment(_ controller: PKPaymentAuthorizationViewController) {
print("Will authorize payment")
}
func paymentAuthorizationViewController(_ controller: PKPaymentAuthorizationViewController,
didAuthorizePayment payment: PKPayment,
completion: @escaping (PKPaymentAuthorizationStatus) -> Void) {
print("Payment authorization finished")
dismiss(animated: true, completion: nil)
}
}