0

I am trying to implement the SetUp Apple Pay button on my iOS Xamarin application. I have added button and click handler for it. Then I Use PKPassLibrary.OpenPaymentSetup() to open Wallet. Then if user has successfully added Card into Wallet I need to handle this event via changing "SetUp ApplePay button" to "Pay with Apple Pay". But I can't find working any event handler or something like that.

What I have tried:

private PKPassLibrary _library;
private NSObject _walletNotificationSubscription;
private void OnSetuApplePayClicked(object button, EventArgs args)
{
   _library = new PKPassLibrary();
   _library.OpenPaymentSetup();
    _walletNotificationSubscription = PKPassLibrary.Notifications.ObserveDidChange(_library, HandleEventHandler);
}
void HandleEventHandler(object sender, NSNotificationEventArgs e)
      {
         _walletNotificationSubscription.Dispose();

         ViewModel.UpdateApplePay();
         SetButtonVisibility();
      }

but it does not work.

P.S.: I guess I maybe observe incorrect events.

Oleg Kosuakiv
  • 174
  • 2
  • 15

2 Answers2

1

Try to use the following code :

     if(PKPaymentAuthorizationViewController.CanMakePayments)
         {
            //the device supports Apple Pay

            //check whether the user can make a payment with a bank card ,such as Amex ,MasterCard,Visa,ChinaUnion and so on
            NSString[] paymentString = { PKPaymentNetwork.Amex, PKPaymentNetwork.ChinaUnionPay, PKPaymentNetwork.MasterCard, PKPaymentNetwork.Visa };

            if(PKPaymentAuthorizationViewController.CanMakePaymentsUsingNetworks(paymentString))
            {
                //user has added bank card ,do something you want
            }
            else
            {
                //user has not added bank card
            }

         }
       else
         {
            //the device doesn't support Apple Pay
         }

There are also some other way of payment ,you can check them in

public static class PKPaymentNetwork

Lucas Zhang
  • 18,630
  • 3
  • 12
  • 22
  • The problem is the case is the following: In branch of your code "user has not added bank card" I open Wallet App and prompting him to add a card. So, I need a callback/event for the case if user has added card (to change UI). – Oleg Kosuakiv Sep 04 '18 at 09:02
  • you can add a judgement in the method ViewWillAppear ,when the user add a card and return your app ,you can update your UI – Lucas Zhang Sep 04 '18 at 09:13
  • Because you can only add card in Wallet App ,so there is no callback between two apps – Lucas Zhang Sep 04 '18 at 09:14
  • yeap, I have to track to the WiiAppearForeground app lifecycle event and also ensure that user was navigated to the Wallet with bool property. I am afraid that there maybe some edge use cases when this approach may not work correctly – Oleg Kosuakiv Sep 04 '18 at 10:31
1

It seems that there is no callbacks or events (at least at Xamarin). So, I had to switch a boolean property of controller when user is sent to the Wallet, and then, when user goes back to app I track "WillEnterForeground" App event where I check whether boolean property is true (if true then user returns from Wallet).

Please note that "ViewWillAppear" does not work in this case, it is not an analogue to the Android's "OnResume".

Also please note that card is activated after 15-20 seconds after adding it to wallet, so I am using "listening cycle" to track card activation.

When card is finally activated I am switching the button from Setup Apple Pay to Pay with Apple Pay.

Oleg Kosuakiv
  • 174
  • 2
  • 15