【uniapp】苹果应用内支付支付
接入准备:
1、苹果后台开发者https://developer.apple.com/account/resources/certificates/list 需要把打包的配置文件勾上苹果支付
2、申请支付参数,https://appstoreconnect.apple.com/apps/1615504939/appstore/addons?m=App 内购买项目添加支付的参数
(这个参数需要先上一个版本的app后才能申请让他审核,要是有一个版本app在上面还没审核,打电话或者留个邮件给他叫他审核)
3、在https://appstoreconnect.apple.com/access/users
用户访问>沙盒测试员添加测试账号。在手机设置>App Store上登录沙盒账号。
(沙盒环境支付没有真的扣费,唤起支付和回调会有点慢,耐心调试)
开发流程:
1、打开项目的manifest.json文件,在“App模块配置”项的“Payment(支付)”下勾选“Apple应用内支付”:
(真机运行时请使用自定义调试基座)
2、支付初始化,可以在进入页面的时候或者弹窗的时候处理,需要初始化一次才能发起支付
2.1、获取应用内支付对象
getIapChannels() { console.log('------getIapChannels' ); let _this = this; plus.payment.getChannels(function(channels){ for (let i in channels) { let channel = channels[i]; // 获取 id 为 'appleiap' 的 channel if (channel.id == 'appleiap') { _this.iap = channel; _this.requestIapOrder(); } } }, function(e){ showToasts("获取iap支付通道失败:" + e.message); }); },
2.2、获取到支付对象后,初始化支付项目列表(官方文档说是获取订单信息,有歧义,其实就是获取申请的内购商品列表,初始化支付)
requestIapOrder() { console.log('------requestIapOrder' ); // #ifdef APP-PLUS let ids = [this.productid]; this.iap.requestOrder(ids, function(res) { // console.log(res); this.iapOrder = true; }, function(e) { this.vm.jumpPay = false; showToasts("获取订单信息失败:" + e.code); }); // #endif },
3、发起支付,请求后端获取支付标识,username写入发起支付,用于回调的时候业务处理
goApplePay() { console.log('------goApplePay' ); // 获取订单 let _this = this; payApply().then(res => { // 发起支付 plus.payment.request(this.iap, { productid: this.productid, username: res.data.out_trade_no, }, function(result){ console.log(result); _this.appleNotify(result); }, function(e){ // console.log(e); this.vm.jumpPay = false; showToasts("支付失败"); }); }).catch(err => { showToasts(err.message || '获取数据失败'); }) },
4、回调处理,这里是用的前端回调返回给服务端处理
appleNotify(notifyData) { console.log('------appleNotify' ); // 关闭支付弹窗 this.popPayClose(); // 支付成功 appleNotify({notify: notifyData}).then(res => { console.log(res); if(res.data == 1) { this.$emit('apply-pay-success'); } }).catch(err => { showToasts("支付失败"); }); },
5、服务端回调处理,在手机端支付完成后,会得到一个transactionReceipt,来进行二次验证是否支付成功,入参只有一个,就是receipt-data,POST方式请求
正式验证API:
沙盒验证API:
public function appleNotify($notify) { // 校验签名 $verifyUrl = config('pay.apply_pay.verify_url'); $header = HuannaoSpider::buildHeader(); $res = HttpQuery::postRequest($verifyUrl, json_encode(['receipt-data' => $notify['transactionReceipt']]), $header); $res = json_decode($res, true); if (empty($res) || $res['status'] != 0) { return 0; } return 1; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了