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.
Solved.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With