如何在 Xamarin Forms 中集成 Apple Pay? [英] How to integrate Apple Pay in Xamarin Forms?

查看:20
本文介绍了如何在 Xamarin Forms 中集成 Apple Pay?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 xamarin Forms 开发应用程序.现在,我需要集成 Apple pay 我试图通过互联网查找内容,但无法找到可行的解决方案.谁能建议我如何将 Apple pay 集成到我的应用程序中?

I am Developing App using xamarin Forms. Now, i have a requirement to integrate Apple pay I tried to find the content over the internet but unable to find working solution. Can anyone suggest me how to integrate Apple pay within my App ?

这是我的 Apple Pay 代码

Here is my code for 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.

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

NOTE: I have added correct merchant id in entitlements.In Apple pay document, CanMakePaymentsUsingnetwork() will return false only when we have no card. I have added card in my test account too.

我在 xamarin 表单共享应用中有一个按钮,当点击该按钮时,它会点击上面代码中的 AuthorizePayment() 方法.

I have a button in xamarin forms shared app when that button clicked it will hit AuthorizePayment() method in above code.

推荐答案

已解决.

一切都配置正确.上面的代码按预期工作.唯一的问题是,我忘记在 iOS 项目设置中加载 Entitlements.plist 文件.这就是我得到 canmakepaymentsUsingNetwork(networks) 总是 false 的主要原因.

Everything was configured correctly. Above code works as expected. Only thing is, I forgot to load Entitlements.plist file in iOS Project Settings. That is the main reason why i am getting canmakepaymentsUsingNetwork(networks) always false.

这篇关于如何在 Xamarin Forms 中集成 Apple Pay?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆