首页 > 解决方案 > 如何添加保存到手机谷歌按钮并通过 xamarin 移动应用添加支付通行证

问题描述

我已经成功创建了支持 ASP.Net 核心的 Google 支付通行证,并且我的 rest api 成功返回了 JWT 令牌。而且我使用了保存到电话网络按钮并使用了工作正常的 JWT,但我需要将通行证与我的 Xamarin 表单应用程序集成,在 xamrin 中我如何添加保存到电话按钮以及如何将 JWT 与该按钮绑定?

标签: androidxamarinxamarin.formsgoogle-pay

解决方案


最后,我找到了解决方案。如果您的后端服务器为您的 google pay pass 返回 JWT,您可以通过依赖服务在 Xamarin 表单中实现此目的。

如果您可以使用 JWT 链接和意图、JWT POST 请求方法和原生 Android SDK。请参考本指南

我的通行证类型是忠诚卡,所以我使用了JWT 链接和意图方法

在将 pass 与您的应用程序集成之前,您可以使用 chrome 浏览器测试您的 JWT。请在 chrome 浏览器中使用您的 JWT 访问此 ( https://pay.google.com/gp/v/save/{jwt_generated }) url,如果 JWT 正常,您将在浏览器中看到通行证

达到

  1. 在 PCL 项目中创建接口

          namespace App.DependencyServices
          {
              public interface IGPayPass
              {
                 void LoadGPayPass(string JWT);
              }
          }
    
  2. 在您的 Android 项目中创建原生实现

    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. 通过依赖服务使用 PCL 项目中的本机实现

            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);
            }
    

在 UI 中,没有像 google web 按钮这样的默认按钮,但您必须遵循品牌指南(https://developers.google.com/pay/passes/guides/get-started/api-guidelines/brand-指导方针

您可以使用任何类型的按钮并通过 onclik 或命令触发此方法

此方法使用来自服务器的通行证 JWT 并传递到谷歌钱包

在此处输入图像描述

在此处输入图像描述


推荐阅读