2017-12-01 4 views
0

私はFlutterアプリケーションのApple Payを実装しようとしています。以下のコードは、正常に入力されたApple Payビューを正常に表示します。ただし、有料化しようとするとpaymentAuthorizationViewController - >didAuthorizePaymentメソッドが起動するはずです。関数がヒットすると、サーバーから請求を作成するためにトークンをAPIに送り返します。ViewController AppDelegateから起動されなかったメソッドをデリートします

私はこの問題がライン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

あなたAppDelegatePKPaymentAuthorizationViewControllerDelegateに準拠していないので、キャストが失敗し、as?リターンnil。したがって、paymentAuthorizationViewControllerには代理人がなく、その代理人メソッドは呼び出されません。 AppDelegatePKPaymentAuthorizationViewControllerDelegateに準拠させる必要があります。

@objc class AppDelegate: FlutterAppDelegate, PKPaymentAuthorizationViewControllerDelegate{ 
関連する問題