【发布时间】:2018-05-15 01:13:16
【问题描述】:
我正在尝试为 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...
//}
})
}
}
【问题讨论】:
标签: ios swift stripe-payments flutter