android - 如何添加保存到手机谷歌按钮并通过 xamarin 移动应用添加支付通行证
问题描述
我已经成功创建了支持 ASP.Net 核心的 Google 支付通行证,并且我的 rest api 成功返回了 JWT 令牌。而且我使用了保存到电话网络按钮并使用了工作正常的 JWT,但我需要将通行证与我的 Xamarin 表单应用程序集成,在 xamrin 中我如何添加保存到电话按钮以及如何将 JWT 与该按钮绑定?
解决方案
最后,我找到了解决方案。如果您的后端服务器为您的 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 正常,您将在浏览器中看到通行证
达到
在 PCL 项目中创建接口
namespace App.DependencyServices { public interface IGPayPass { void LoadGPayPass(string JWT); } }
在您的 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); } }
通过依赖服务使用 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 并传递到谷歌钱包
推荐阅读
- javascript - 本地路径的链接没有在浏览器中打开?
- mixed-models - mgcv::gam 混合模型中平滑随机项的正式引用
- postgresql - 如何在 PostgreSQL 中获取唯一的日期范围?
- ios - iOS:从 Interface Builder 设置 UITextView 行距?
- vb.net - Vb.net:我如何同时单击 2 个按钮?
- spring-boot - 在 Spring Boot 中将 HTTP 定向到代理后面的 HTTPS
- c - 使用 strtok 后添加指向已建立指针数组的指针
- c# - 如何仅在下拉列表中选择日期来填充 HTMLTableCell?
- ios - Alamofire 对象映射器:如何快速解析嵌套数组
- python - MySQL table left join not matching (MySQL, Python)