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 ?
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)
{
}
}
}
Expected Result:
canmakepaymentsUsingNetwork method should return true. But, it is always returning 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.
I have a button in xamarin forms shared app when that button clicked it will hit AuthorizePayment() method in above code.
PKPaymentButtonTypeInStore
unless you are a merchant looking to actually provision a Merchant ID (but the docs walk you thru that process you have to do with Apple) After that adding a button can be done in a custom View renderer or using the PKPaymentAuthorizationController (etc...) can be done via docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/…