xcode - 如何将Apple Pay整合到Xamarin Forms中?

标签 xcode xamarin xamarin.forms xamarin.ios

我正在使用xamarin Forms开发App。现在,我有一个整合Apple Pay的要求,我试图通过Internet查找内容,但找不到有效的解决方案。有人可以建议我如何将Apple Pay集成到我的App中吗?

这是我的Apple Pay代码

using System;
using ApplePayTest.iOS.Dependencies;
using ApplePayTest.Services;
using Foundation;
using PassKit;
using StoreKit;
using Stripe.iOS;
using UIKit;

[assembly: Xamarin.Forms.Dependency(typeof(ApplePayAuthorizer))]
namespace ApplePayTest.iOS.Dependencies
{
    public class ApplePayAuthorizer :PKPaymentAuthorizationViewControllerDelegate,IApplePayAuthorizer
   {
       public bool AuthorizePayment()
       {

           SKProductsRequest req=new SKProductsRequest(new NSSet());
          NSString[] paymentNetworks = new NSString[] {
              PKPaymentNetwork.Visa,
            PKPaymentNetwork.MasterCard
           , PKPaymentNetwork.Amex
               };
            var canmakepayment = PKPaymentAuthorizationViewController.CanMakePayments;

            PKPaymentRequest paymentRequest = new PKPaymentRequest();
             paymentRequest.MerchantIdentifier = "mymerchant code"; 
             paymentRequest.SupportedNetworks = paymentNetworks;
             paymentRequest.MerchantCapabilities = PKMerchantCapability.ThreeDS;
             paymentRequest.CountryCode = "CA";
             paymentRequest.CurrencyCode = "CAD";


             paymentRequest.PaymentSummaryItems = new PKPaymentSummaryItem[]{
                   new PKPaymentSummaryItem()
        {
            Label = "Sample Purchase Item" ,
                      Amount = new NSDecimalNumber("1")
                   }
    };
             var canmakepayments = PKPaymentAuthorizationViewController.CanMakePaymentsUsingNetworks(paymentNetworks);
            //can make payment is always false
             if (canmakepayments)
            {

                PKPaymentAuthorizationViewController controller = new
                      PKPaymentAuthorizationViewController(paymentRequest);
                controller.Delegate = (PassKit.IPKPaymentAuthorizationViewControllerDelegate)Self;
                var rootController = UIApplication.SharedApplication.
                      KeyWindow.RootViewController;
                rootController.PresentViewController(controller,
                                                     true, null);
            }
             return false;
          }
       public override void DidAuthorizePayment(PKPaymentAuthorizationViewController controller, PKPayment payment,
             Action<PKPaymentAuthorizationStatus> completion)
       {

          completion(obj: PKPaymentAuthorizationStatus.Success);
       }


       public override void PaymentAuthorizationViewControllerDidFinish
             (PKPaymentAuthorizationViewController controller)

       {
             controller.DismissViewController(true, null);
      }

       public override void WillAuthorizePayment(PKPaymentAuthorizationViewController controller)
       {

       }



    }
}

预期结果:

canmakepaymentsUsingNetwork方法应返回true。但是,它总是返回false。

注意:
我在权利中添加了正确的商家ID。在Apple支付文档中,仅当我们没有卡时,CanMakePaymentsUsingnetwork()才会返回false。我也已经在测试帐户中添加了卡。

我在xamarin表单共享应用程序中有一个按钮,单击该按钮时,它将击上述代码中的AuthorizePayment()方法。

最佳答案

解决。

一切都配置正确。上面的代码按预期工作。唯一的事情是,我忘了在iOS项目设置中加载Entitlements.plist文件。那就是为什么我得到canmakepaymentsUsingNetwork(networks)总是错误的主要原因。

enter image description here

关于xcode - 如何将Apple Pay整合到Xamarin Forms中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55999193/

相关文章:

c# - 使按钮打开超链接

c# - 获取当前位置 Pin 以显示在 map 上

c# - Xamarin.Forms 从 mvvm ViewModel 设置焦点

iphone - 导航 Controller 不旋转

ios - 如何缩放以适合背景图像 objective-c

iphone - 隐藏标签栏后 View 大小减小

ios - 如果连接到的 UIImageView 在 UIScrollView 下,是否可以使 Tap Gesture Recognizer 工作?

ios 以毫秒为单位向 UITableView 添加数据

mvvm - 如何从 ViewModel 调用 TapGestureRecognizer

xamarin - SKGLView 比 SKCanvasView 暗得多