ViewController委托方法未从AppDelegate触发

时间:2017-12-01 00:20:41

标签: ios swift stripe-payments flutter

我正在尝试为Flutter应用实施Apple Pay。下面的代码成功显示正确填充的Apple Pay视图。但是,当它试图收费时,paymentAuthorizationViewController - >应该触发didAuthorizePayment方法,而不是。应该发生的是该功能被命中,我将令牌发送回我的A​​PI以从服务器创建费用。

我猜这个问题与行paymentAuthorizationViewController.delegate = self as? PKPaymentAuthorizationViewControllerDelegate有关,但我还没有能够解决它。

import UIKit
import Flutter

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {

    var flutterViewController: FlutterViewController!

    override func application(
        _ application: UIApplication,
        didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?
        ) -> Bool {

        GeneratedPluginRegistrant.register(with: self)

        flutterViewController = (window.rootViewController as? FlutterViewController)

        let stripeChannel = FlutterMethodChannel(name: "com.tram.gondola.ios/stripe", binaryMessenger: flutterViewController)
        stripeChannel.setMethodCallHandler({
            (call: FlutterMethodCall, result: @escaping FlutterResult) -> Void in
            if ("handleApplePayButtonTapped" == call.method) {
                // create paymentRequest
                let paymentAmount: NSDecimalNumber = 12.34
                let merchantIdentifier = "merchant.com.merchantid"
                let paymentRequest = Stripe.paymentRequest(withMerchantIdentifier: merchantIdentifier, country: "US", currency: "USD")

                // Configure the line items on the payment request
                paymentRequest.paymentSummaryItems = [
                    // The final line should represent your company;
                    PKPaymentSummaryItem(label: "Company Name", amount: paymentAmount),
                ]

                if Stripe.canSubmitPaymentRequest(paymentRequest) {
                    // Setup payment authorization view controller
                    let paymentAuthorizationViewController = PKPaymentAuthorizationViewController(paymentRequest: paymentRequest)
                    paymentAuthorizationViewController.delegate = self as? PKPaymentAuthorizationViewControllerDelegate

                    // Present payment authorization view controller
                    self.flutterViewController.present(paymentAuthorizationViewController, animated: true)
                }

            }
            else {
                // There is a problem with your Apple Pay configuration
                result(false)
            }

        })

        return super.application(application, didFinishLaunchingWithOptions: launchOptions);
    }

    func paymentAuthorizationViewController(_ controller: PKPaymentAuthorizationViewController, didAuthorizePayment payment: PKPayment, completion: @escaping (PKPaymentAuthorizationStatus) -> Void) {
        print("didAuthorizePayment hit")
        STPAPIClient.shared().createToken(with: payment) { (token: STPToken?, error: Error?) in
            guard let token = token, error == nil else {
                // Present error to user...
                print("error")
                return
            }

            // send token back to flutter
            print(token)
            return
        }
    }

    func paymentAuthorizationViewControllerDidFinish(_ controller: PKPaymentAuthorizationViewController) {
        // Dismiss payment authorization view controller
        controller.dismiss(animated: true, completion: {
            //if (paymentSucceeded) {
            // Show a receipt page...
            //}
        })
    }
}

1 个答案:

答案 0 :(得分:1)

您的AppDelegate不符合PKPaymentAuthorizationViewControllerDelegate,因此投放失败,as?返回nil。因此,您的paymentAuthorizationViewController没有委托,因此不会调用其委托方法。您应该使AppDelegate符合PKPaymentAuthorizationViewControllerDelegate,如下所示:

@objc class AppDelegate: FlutterAppDelegate, PKPaymentAuthorizationViewControllerDelegate{