如何在 WKWebView 中使用 Stripe JS 接受 Apple Pay?

How to accept Apple Pay with Stripe JS inside WKWebView?

我正在尝试在 iOS 上的 WKWebView 中加载一个使用 Stripe.js 的网页(使用 Xamarin.Forms)。

一切正常(可以接受卡支付),Apple Pay 除外:paymentRequest.canMakePayment() 总是 returns 无效。

SFSafariViewController 中的同一网页工作正常,因此问题似乎与 WKWebView 中的限制有关。 然而,根据 https://webkit.org/blog/9674/new-webkit-features-in-safari-13/ this should now be supported and indeed the same WKWebView also loads up the official Apple Pay demo page (https://applepaydemo.apple.com/) 没有问题。

问题似乎最终出在 Stripe.js 的幕后工作方式(可能与脚本注入有关)。

我在 Xamarin 中的自定义渲染器如下所示:

        WKWebView wkWebView;
        protected override void OnElementChanged(ElementChangedEventArgs<NativeWebView> e)
        {
            base.OnElementChanged(e);

            if (Control == null)
            {
                var config = new WKWebViewConfiguration();
                wkWebView = new WKWebView(Frame, config);
                wkWebView.WeakNavigationDelegate = new WebNavigationDelegate();
                SetNativeControl(wkWebView);
            }

            if (e.NewElement != null)
                Control.LoadRequest(new NSUrlRequest(new NSUrl(Element.Url)));
        }

以及网页中的JS(取自Stripe Element样本):

            //set up payment request
            var paymentRequest = stripe.paymentRequest({
                country: 'GB',
                currency: 'gbp',
                total: {
                    label: 'sample order',
                    amount: 100,
                },
                requestPayerName: true,
                requestPayerEmail: true,
            });

            paymentRequest.canMakePayment().then(function (result) {
                // result is always false!
                ...
            });

是否可能有额外的设置可以添加到 WKWebView 以使其工作?

这不再是问题。 Stripe 承认这是他们的 JS 包装器中的一个缺陷,并已在最近对官方库的更新中得到修复。