maui BlazorWebView+本地html (vue、uniapp等都可以) 接入支付宝sdk 进行支付宝支付...
source link: https://www.cnblogs.com/lkd3063601/p/17241939.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
maui BlazorWebView+本地html (vue、uniapp等都可以) 接入支付宝sdk 进行支付宝支付 开发 Android app
首先添加支付宝sdk的绑定库
nuget 包:Chi.MauiBinding.Android.AliPay
项目地址:https://github.com/realZhangChi/MauiBinding
新建maui Blazor应用,在根目录创建一个静态类PublicMethods.cs (类名位置都可以自定义,这个静态类主要给html js 调用使用的,js调用服务端方法 从 ASP.NET Core Blazor 中的 JavaScript 函数调用 .NET 方法 | Microsoft Learn)
注意其中的 #if ANDROID IOS 指在不同的平台下执行操作
using Microsoft.JSInterop; using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; using System.Text.Encodings.Web; using System.Text.Json; using System.Text.Unicode; using System.Threading.Tasks; using static System.Runtime.InteropServices.JavaScript.JSType; namespace MauiApp7 { public static class PublicMethods { [JSInvokable] public static async Task AliPays(IJSObjectReference objRef,string aliPayStrs) { #if ANDROID _ =Task.Run(async () => { string con = aliPayStrs;//调用支付宝app支付接口返回的内容
var act = Microsoft.Maui.ApplicationModel.Platform.CurrentActivity; Com.Alipay.Sdk.App.PayTask pa = new Com.Alipay.Sdk.App.PayTask(act); var result = pa.PayV2(con, true); var resultStatus = result.TryGetValue("resultStatus",out string resultStatusDic)? resultStatusDic:"-1"; var memo = result.TryGetValue("memo",out string memoDic)? memoDic:""; if (resultStatus == "9000") { memo = "支付成功"; } else if (resultStatus == "-1") { memo = "支付失败"; } //执行前端html window上注册的回调方法 await objRef.InvokeVoidAsync("aliPayCallBack", new { resultStatus = resultStatus, memo = memo }); }); #endif } } }
1.修改MainPage.xaml 中的代码,删除BlazorWebView 下子内容,修改后的代码为
<BlazorWebView x:Name="blazorWebView" HostPage="wwwroot/index.html" BlazorWebViewInitialized="blazorWebView_BlazorWebViewInitialized"> </BlazorWebView>
修改MainPage.xaml.cs中代码添加BlazorWebViewInitialized 事件,此事件是允许BlazorWebView在Android平台下能够同时访问http和https的混合请求,需搭配android:usesCleartextTraffic="true" 使用 具体参考 maui BlazorWebView Android 中混合使用https和http - 落叶子 - 博客园 (cnblogs.com)
private void blazorWebView_BlazorWebViewInitialized(object sender, Microsoft.AspNetCore.Components.WebView.BlazorWebViewInitializedEventArgs e) { #if ANDROID e.WebView.Settings.MixedContentMode = Android.Webkit.MixedContentHandling.AlwaysAllow; #endif }
2. 修改 wwwroot下的index.html 具体代码为:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no"> <title>MauiApp7</title> <base href="/" /> <!--引用jquery 可以自行引入--> <script src="lib/jquery.min.js"></script> </head> <body> <button style="width: 100%;background: rgb(5, 200, 235);border: 0rem;" id="zhifuapp">支付宝App支付</button><br /><br /> <script src="_framework/blazor.webview.js" autostart="false" crossorigin="anonymous"></script> <script> $(function () { var jsObjectReference; $("#zhifuapp").click(function () { // 调佣服务接口获取支付宝app支付需要的请求字符串(res) returnUrl quitUrl参数忽略这是我自己测试用的 $.post("https://xxxx/ali/create-maui-app", { returnUrl:"", quitUrl: "" }, function (res) { jsObjectReference = DotNet.createJSObjectReference(window); DotNet.invokeMethodAsync('MauiApp7', 'AliPays', jsObjectReference, res) .then(data => { console.log(data); }); }).error(function (res) { alert("出现错误:" + JSON.stringify(res)); }) }) //window上注册支付回调方法 window.aliPayCallBack = (res) => { if (jsObjectReference) { DotNet.disposeJSObjectReference(jsObjectReference); } alert("执行了支付宝支付回调" + JSON.stringify(res)); } }) </script> </body> </html>
Recommend
-
89
本文是新成立的支付产品架构群的第一次分享, 非常不错的一次起步。 目前这个群的名额还比较充裕。如想加入,请在本文下留言,简要介绍目前所在的公司、负责的工作以及入群后可以分享的主题及时间。一、主题分享:微服务下支付宝接入大家...
-
131
分享发现 - @cybermonster - 难道在 steam 上的消费会引起国际收支不平衡?难道有人通过 steam 洗钱?
-
108
以色列商户接入支付宝当面付 中东开启移动支付
-
138
全国41家Apple Store零售店接入支付宝
-
48
微信、微博、支付宝都可以永久销号了,这份指南一步步教你如何操作
-
33
本文使用的相关技术及组件为uniapp+uview,使用hbuilder运行在小程序也同样生效。 一、定义template模板 <template> <view :class="change_pop_show ? 'project_data scroll-lock' : 'project_data'"> <u-button t...
-
8
Rainbond 部署的应用自动接入 CDN 进行静态资源分发 本文档适合的场景是:解决因分布、带宽、服务器性能带来的访问延迟问题,将运行于 Rainbond 的 Nodejs 前端项目进行动静分离,通过 阿里云CDN 加速 对象存储OSS 中的静态资源,提升用户访问速度。
-
3
GitHub 的 Action 接入 Stryker.NET 进行自动化测试单元测试鲁棒性假设有一个捣蛋的小伙伴加入了你的团队,这个捣蛋的小伙伴喜欢乱改代码,请问此时的单元测试能否拦住这些逗比行为?如果不能拦住逗比行为,是否代表着单元测试有所欠缺,或者有某些分支逻...
-
3
新疆若羌:本地电视频道接入电信IPTV平台导语:近日,新疆若羌本地电视频道接入电信IPTV(交互式网络电视)平台,并成功上线若羌本地节目,为居民群众了解本地新闻动态、丰富精神文化生活提供了便利条件。 近日,新疆...
-
3
Uniapp进行APP打包——iOS 系统 一、打包过程 1、创建唯一标识符 首先,申请苹果开发者账号。 没有苹果开发者账号是无法进行 ios 打包上线的。 (2) 进入
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK