我正在尝试为Flutter应用实施Apple Pay。下面的代码成功显示正确填充的Apple Pay视图。但是,当它试图收费时,paymentAuthorizationViewController
- >应该触发didAuthorizePayment
方法,而不是。应该发生的是该功能被命中,我将令牌发送回我的API以从服务器创建费用。
我猜这个问题与行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...
//}
})
}
}
答案 0 :(得分:1)
您的AppDelegate
不符合PKPaymentAuthorizationViewControllerDelegate
,因此投放失败,as?
返回nil
。因此,您的paymentAuthorizationViewController
没有委托,因此不会调用其委托方法。您应该使AppDelegate
符合PKPaymentAuthorizationViewControllerDelegate
,如下所示:
@objc class AppDelegate: FlutterAppDelegate, PKPaymentAuthorizationViewControllerDelegate{