我正在使用 xamarin Forms 开发应用程序。现在,我需要集成 Apple Pay 我试图通过互联网查找内容,但无法找到有效的解决方案。谁能建议我如何将 Apple pay 集成到我的应用程序中?
这是我的 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)
{
}
}
}
预期结果:
canmake paymentsUsingNetwork 方法应返回 true。但是,它总是返回 false。
注意: 我在权利中添加了正确的商家 ID。在 Apple Pay 文档中,只有当我们没有卡时,CanMakePaymentsUsingnetwork() 才会返回 false。我也在我的测试帐户中添加了卡。
我在 xamarin 表单共享应用程序中有一个按钮,当单击该按钮时,它将触发上面代码中的 AuthorizePayment() 方法。