1

Android 通过scheme跳转支付宝实现支付 - Stars-one

 1 year ago
source link: https://www.cnblogs.com/stars-one/p/17352990.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.

Android 通过scheme跳转支付宝实现支付

本文为作者原创,允许转载,不过请在文章开头明显处注明链接和出处!!! 谢谢配合~
作者:stars-one
链接:https://www.cnblogs.com/stars-one/p/17352990.html

本篇大约有2733个字,阅读预计需要3.42分钟


原文地址: Android 通过scheme跳转支付宝实现支付 - Stars-One的杂货小窝

需求的来源是支付功能,由于支付宝不允许个人开通具有webhook的支付服务,所以是对接了一个支付系统(应该算作四方支付)。创建订单后,系统会返回一个h5链接。
对于PC版的软件,将此链接转为二维码,然后提示用户使用支付宝扫码就能够触发付款操作;
而对于Android软件来说,这样的流程可不合适

实际上,扫码之后就是通过支付宝内置浏览器打开了h5,如果我能有个方法,可以触发让支付宝去打开支付的h5链接,那么就是相当于实现了唤起APP支付的功能了

于是就轮到了今天所说的scheme实现了功能

1.知道对应的scheme

首先,我们要知道对应的scheme,这个是重点,但好在酷安人均大佬,已经有人找到了,就是下面的这个,需要在后面拼接下网址即可

alipays://platformapi/startapp?appId=20000067&url=

2.触发scheme

关于触发scheme,这里就涉及到Android里的Intent相关的知识了,这里不详细说明了,直接上代码

val payUrl =""//支付的h5地址
val scheme = "alipays://platformapi/startapp?appId=20000067&url=${payUrl}"
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(scheme))
ActivityUtils.startActivity(intent)

PS: ActivityUtils是个AndroidUtilCode库里的一个工具类,各位也可以直接用activity对象的startActivity方法

当然,这里我们还得加上个判断,来判断用户是否安装了支付宝应用

//判断是否安装支付宝
if (AppUtils.isAppInstalled("com.eg.android.AlipayGphone")) {
    val scheme = "alipays://platformapi/startapp?appId=20000067&url=${payUrl}"
    val intent = Intent(Intent.ACTION_VIEW, Uri.parse(scheme))
    ActivityUtils.startActivity(intent)
} else {
    ToastUtils.showShort("抱歉,当前手机未安装支付宝APP...")
}

【开源库推荐】#2 AndroidUtilCode Android常用工具类大全(附API使用说明) - Stars-One的杂货小窝

不过这里遇到了个问题了,由用户反馈说支付不了

排查一下,发现是我项目里的target SDK变更为了30,而API 30,无法获取包名,详情可见此文声明软件包可见性需求  |  Android 开发者  |  Android Developers

不过由于我用的是工具库,所以把AndroidUtilCode库更新到最新版本就解决了此问题

PS: 实际上实现效果与官方支付宝的APP支付有些不同,上述的实现效果就是,会先触发支付宝打开一个网页,然后再弹出一个付款的对话框;

而官方支付宝的APP支付的话,就是会直接弹出付款的对话,没有打开网页的这一步操作,不过也是还能接受

最后我碎碎念下,微信这个APP就是垃圾,限制太多了,对开发者极度不友好,不同于支付宝,它是直接限制了scheme,除非你是它白名单里,否则不允许你用它里面浏览器打开页面

1210268-20230425161056544-498209989.png

所以估计我以后不会对接微信支付!!

补充 scheme收集

下面是我从上面文章里抽取一些可能开发会用的scheme:

应用 说明 scheme
QQ 指定QQ号聊天页跳转 mqqwpa://im/chat?chat_type=wpa&uin=qq号
QQ QQ群聊天页 mqqwpa://im/chat?chat_type=group&uin=群号
QQ 指定群资料页 mqqapi://card/show_pslcard?src_type=internal&version=1&uin=群号&card_type=group
QQ 指定QQ号资料页 mqqapi://card/show_pslcard?src_type=internal&version=1&uin=QQ号
QQ 空间跳转 mqqapi://qzone/activefeed
微信 付款 android-app://com.tencent.mm/#Intent;action=com.tencent.mm.action.BIZSHORTCUT;launchFlags=0x4000000;S.LauncherUI.Shortcut.LaunchType=launch_type_offline_wallet;end
微信 扫一扫 #Intent;action=com.tencent.mm.action.BIZSHORTCUT;launchFlags=0x4000000;B.LauncherUI.From.Scaner.Shortcut=true;end
微信 联系人/群聊/公众号跳转 android-app://com.tencent.mm/#Intent;action=com.tencent.mm.action.BIZSHORTCUT;B.LauncherUI.From.Biz.Shortcut=true;S.LauncherUI.Shortcut.Username=ID;end
微信 跳转微信收藏
微信

上面微信的联系人跳转,需要个ID,关于如何获取,可见下图

1210268-20230425155638675-1290981838.jpg

补充 - 跳转网易云音乐打开链接

val intent = Intent(Intent.ACTION_VIEW)
intent.data = Uri.parse("https://www.cnblogs.com/stars-one")
intent.setPackage("com.netease.cloudmusic")
ActivityUtils.startActivity(intent)

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK