Search code examples
androidxamarinxamarin.formsgoogle-pay

How to add save to phone google button and add pay passes via xamarin mobile app


I have created the Google pay Passes successfully with ASP.Net core backed and my rest api successfully return JWT Token. And alo i used the save to phone web button and used JWT that working fine but i need to integrate the passes with my Xamarin Forms Application, In xamrin how i add Save to phone button and how to bind the JWT along with that button?


Solution

  • Finally, I have found the solution. If your back end server return the JWT for your google pay pass you can achieve this in Xamarin forms via dependency service.

    If you can use JWT link and intent, JWT POST request method and native Android SDK. Please refer this guidelines

    My pass type is Loyality card so, I have used JWT link and intent method

    Before integrate the pass with your App, you can test your JWT with chrome browser. Please hit this(https://pay.google.com/gp/v/save/{jwt_generated}) url with your JWT in the chrome browser, if the JWT is fine, you will see the pass in the browser

    To achieve

    1. Create an interface in your PCL project

            namespace App.DependencyServices
            {
                public interface IGPayPass
                {
                   void LoadGPayPass(string JWT);
                }
            }
      
    2. Create native implementation in your Android project

      public class GPayImplementation : IGPayPass
      {
       /// <summary>
       ///     summary:
       ///         This methode load the google pay pass from the JWT and open the pay passes in Google pay app
       ///         To show the pass in google pay we have to create an ACTION_VIEW
       ///         If pass is the loyality pass we can use the JWT link and intent method(https://developers.google.com/pay/passes/guides/get-started/implementing-the-api/save-to-google-pay#use-jwt-link-and-intent)
      ///         To load the pass send the https request to google pass class object like https://pay.google.com/gp/v/save/{jwt_generated}
      ///
      ///     parameters:
      ///         JWT : The server return this token for pay passes and pass via dependency service to this native implementation
      ///
      ///     returns:
      ///
      /// </summary>
      /// <param name="JWT"></param>
         public void LoadGPayPass(string JWT)
         {
            //create context
            Context context = Android.App.Application.Context;
      
            string url = "https://pay.google.com/gp/v/save/" + JWT;
      
            //Send the https request to google pay pass class object via Android intent
            Intent intent = new Intent(Intent.ActionView, Uri.Parse(url));
      
            //Assign new task Flag for intent otherwise runtime exepption will return
            intent.AddFlags(ActivityFlags.NewTask);
            context.StartActivity(intent);
         }
      }
      
    3. Consume the native implementation in PCL project via dependency service

              if (response.HttpCode == HttpStatusCode.OK)
              {
                  ResponseSting = response.Results.ToString();
      
                  //Remove quates from the JWT string
                  MembershipCardAndroidJWT = ResponseSting.TrimStart('"').TrimEnd('"');
      
                  //Pass The JWT string via dependency service to the Anroid native Environment
                  DependencyService.Get<IGPayPass>().LoadGPayPass(MembershipCardAndroidJWT);
              }
      

    In the UI, There is no any default button like google web button but you have to follow the brand guide lines (https://developers.google.com/pay/passes/guides/get-started/api-guidelines/brand-guidelines)

    you can use any kind of button and trigger this method via onclik or command

    This method consuming the pass JWT from the server and pass to the google wallet

    enter image description here

    enter image description here