Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to integrate Apple Pay in Xamarin Forms?

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.

like image 543
Narendra Avatar asked May 06 '19 05:05

Narendra


1 Answers

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.

enter image description here

like image 86
Narendra Avatar answered Oct 23 '22 13:10

Narendra