| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531 |
- import {
- ref,
- reactive
- } from "vue";
- import {
- orderPayAli,
- orderPayWx,
- orderCheck,
- orderPayApple
- } from "@/api/shop";
- import {
- toast
- } from "@/utils/common";
- export function usePay(opt = {}) {
- const {
- createOrderError, // 创建订单失败
- checkError, // 校验失败
- payError, // 支付失败
- paySuccess, //支付成功
- applePayError, // 苹果内购失败
- } = opt;
- const aliData = reactive({
- orderId: null, // 订单编号
- text: '', // 订单数据
- })
- const wxData = reactive({
- appid: '', // 应用ID(AppID)
- mchid: '',
- noncestr: '', // 随机字符串
- orderId: '',
- packageVal: '',
- partnerid: '', // 商户号(PartnerID)
- pid: '',
- prepayid: '', // 预支付交易会话ID
- timestamp: '', // 时间戳(单位:秒)
- sign: '', // 签名,这里用的 MD5 签名
- package: '', // 固定值
- })
- const appleData = reactive({
- chanpinId: null, // 产品ID
- paynum: "", // 支付号
- receipt: "", // 票据
- taocanId: null, // 套餐ID
- })
- function resetStatus() {
- Object.assign(aliData, {
- orderId: null, // 订单编号
- text: '', // 订单数据
- })
- Object.assign(wxData, {
- appid: '', // 应用ID(AppID)
- mchid: '',
- noncestr: '', // 随机字符串
- orderId: '',
- packageVal: '',
- partnerid: '', // 商户号(PartnerID)
- pid: '',
- prepayid: '', // 预支付交易会话ID
- timestamp: '', // 时间戳(单位:秒)
- sign: '', // 签名,这里用的 MD5 签名
- package: '', // 固定值
- })
- Object.assign(appleData, {
- chanpinId: null, // 产品ID
- paynum: "", // 支付号
- receipt: "", // 票据
- taocanId: null, // 套餐ID
- })
- }
- // 支付入口 type:业务类型:wx|ali|apple; options: 入参chanpinId,taocanId,applePid
- function OrderPay(type, options) {
- // 初始化支付状态
- resetStatus();
- const {
- chanpinId,
- taocanId,
- applePid, // 苹果商品Id ---> 用来获取支付号+票据
- } = options;
- if (!chanpinId) {
- toast('数据异常,产品信息丢失')
- return;
- }
- if (!taocanId) {
- toast('数据异常,套餐信息丢失')
- return;
- }
- if (type == 'wx') {
- uni.showLoading({
- title: '订单创建中...',
- mask: true
- });
- // 微信
- orderPayWx({
- chanpinId,
- taocanId
- }).then(res => {
- uni.hideLoading();
- if (res.data.code == 0) {
- Object.assign(wxData, res.data);
- // 开始支付
- wxPay(res.data);
- } else {
- toast('微信订单创建失败')
- // 业务异常
- createOrderError && createOrderError({
- type: 'CreateOrderError',
- msg: '业务异常,创建订单返回状态异常',
- err: new Error('业务异常,创建订单返回状态异常'),
- form: 'wx'
- })
- }
- }).catch(err => {
- uni.hideLoading();
- toast('创建微信订单失败')
- createOrderError && createOrderError({
- type: 'CreateOrderError',
- msg: '创建微信订单失败',
- err,
- form: 'wx'
- })
- })
- }
- if (type == 'ali') {
- uni.showLoading({
- title: '订单创建中...',
- mask: true
- });
- // 支付宝
- orderPayAli({
- chanpinId,
- taocanId
- }).then(res => {
- uni.hideLoading();
- if (res.data.code == 0) {
- Object.assign(aliData, res.data);
- // 开始支付
- aliPay(res.data);
- } else {
- toast('支付宝订单创建失败')
- // 业务异常
- createOrderError && createOrderError({
- type: 'CreateOrderError',
- msg: '业务异常,创建订单返回状态异常',
- err: new Error('业务异常,创建订单返回状态异常'),
- form: 'ali'
- })
- }
- }).catch(err => {
- uni.hideLoading();
- toast('创建支付宝订单失败')
- createOrderError && createOrderError({
- type: 'CreateOrderError',
- msg: '创建支付宝订单失败',
- err,
- form: 'ali'
- })
- })
- }
- if (type == 'apple') {
- if (!applePid) {
- toast('数据异常,商品信息丢失')
- return;
- }
- uni.showLoading({
- title: '订单创建中...',
- mask: true
- });
- applePay(options)
- }
- }
- // 微信支付
- function wxPay(options) {
- Object.assign(wxData, options);
- try {
- uni.requestPayment({
- provider: "wxpay",
- orderInfo: options,
- success: (res) => {
- uni.showLoading({
- title: '订单支付中...',
- mask: true
- });
- // 校验支付结果
- setTimeout(() => OrderCheckWx(), 1000)
- },
- fail: (err) => {
- toast('微信支付失败,请联系管理员')
- payError && payError({
- type: 'wxPay',
- msg: '微信支付失败',
- err,
- from: 'uni.requestPayment.fail'
- })
- }
- })
- } catch (err) {
- toast('微信支付环境检测异常')
- payError && payError({
- type: 'wxPay',
- msg: '微信支付API唤起失败',
- err,
- from: 'uni.requestPayment'
- })
- }
- }
- // 校验
- function OrderCheckWx() {
- orderCheck({
- id: wxData.orderId
- }).then(res => {
- if (res.code == 0 && res.data) {
- // 校验通过,支付成功
- paySuccessResult();
- } else {
- setTimeout(() => {
- orderCheck({
- id: wxData.orderId
- }).then(res2 => {
- if (res2.code == 0 && res2.data) {
- // 校验通过,支付成功
- paySuccessResult();
- }
- }).catch(err1 => {
- uni.hideLoading()
- toast('支付二次查验失败,请联系管理员')
- checkError && checkError({
- type: 'OrderCheckWx',
- msg: '支付二次查验失败,请联系管理员',
- err: err1,
- form: 'wx'
- })
- })
- }, 5000)
- }
- }).catch(err => {
- uni.hideLoading()
- toast('支付查验失败')
- checkError && checkError({
- type: 'OrderCheckWx',
- msg: '支付查验失败,请联系管理员',
- err: err,
- form: 'wx'
- })
- })
- }
- // 支付宝支付
- function aliPay(options) {
- Object.assign(aliData, options);
- try {
- uni.requestPayment({
- provider: "alipay",
- orderInfo: options.text, //此处为服务器返回的订单信息字符串
- success: (res) => {
- uni.showLoading({
- title: '订单支付中...',
- mask: true
- });
- // 校验支付结果
- setTimeout(() => OrderCheckAli(), 1000)
- },
- fail: (err) => {
- toast('支付宝支付失败,请联系管理员')
- payError && payError({
- type: 'aliPay',
- msg: '支付宝支付失败',
- err,
- from: 'uni.requestPayment.fail'
- })
- }
- })
- } catch (err) {
- toast('支付宝支付环境检测异常')
- payError && payError({
- type: 'aliPay',
- msg: '支付宝API唤起失败',
- err,
- from: "uni.requestPayment"
- })
- }
- }
- // 校验
- function OrderCheckAli() {
- orderCheck({
- id: aliData.orderId
- }).then(res => {
- if (res.code == 0 && res.data) {
- // 校验通过,支付成功
- paySuccessResult();
- } else {
- setTimeout(() => {
- orderCheck({
- id: aliData.orderId
- }).then(res2 => {
- if (res2.code == 0 && res2.data) {
- // 校验通过,支付成功
- paySuccessResult();
- }
- }).catch(err1 => {
- uni.hideLoading()
- toast('支付二次查验失败,请联系管理员')
- checkError && checkError({
- type: 'OrderCheckAli',
- msg: '支付二次查验失败,请联系管理员',
- err: err1,
- form: 'ali'
- })
- })
- }, 5000)
- }
- }).catch(err => {
- uni.hideLoading()
- toast('支付查验失败,请联系管理员')
- checkError && checkError({
- type: 'OrderCheckAli',
- msg: '支付查验失败,请联系管理员',
- err: err,
- form: 'ali'
- })
- })
- }
- // 苹果支付 --- 无需check 直接购买成功
- // 苹果支付 --- 无需check 直接购买成功
- function applePay(options) {
- Object.assign(appleData, options);
- // 核心修复1:提升iapChannel作用域,定义在applePay方法内,所有嵌套回调可访问
- let iapChannel = null;
- try {
- plus.payment.getChannels(
- function(channels) {
- uni.hideLoading();
- // 获取苹果内购通道,赋值给外层作用域的iapChannel
- iapChannel = channels.find((item) => item.id == "appleiap");
- if (!iapChannel) {
- toast('未找到产品内购信息,请联系管理员');
- applePayError && applePayError({
- type: "applePay",
- msg: "内购商品Id丢失,请确认是否已成功配置",
- err: new Error("内购商品Id丢失,请确认是否已成功配置"),
- from: "apple",
- });
- return;
- }
- const ids = [options.applePid];
- iapChannel.requestOrder(
- ids,
- function(e) {
- // 获取订单信息成功回调
- console.log('001 获取苹果订单成功', options);
- uni.requestPayment({
- provider: "appleiap",
- orderInfo: {
- productid: options.applePid,
- quantity: 1,
- manualFinishTransaction: true,
- },
- success: (payRes) => {
- // 苹果内购支付成功
- console.log('002 苹果支付成功', payRes);
- const {
- transactionIdentifier: paynum,
- transactionReceipt: receipt,
- } = payRes;
- Object.assign(appleData, {
- paynum,
- receipt
- });
- // 核心修复2:then回调内增加try/catch,隔离局部异常
- orderPayApple({
- chanpinId: appleData.chanpinId,
- paynum: paynum,
- receipt: receipt,
- taocanId: options.taocanId,
- }).then((res) => {
- try {
- uni.hideLoading();
- console.log('1231231 orderPayApple接口成功', res);
- if (res.code == 0 && res.data) {
- // 业务成功,收尾事务(此时iapChannel已存在,作用域正常)
- iapChannel.finishTransaction(payRes
- .transactionIdentifier);
- paySuccessResult(); // 执行支付成功逻辑
- } else {
- // 业务返回非0码,仍需收尾事务
- iapChannel.finishTransaction(payRes
- .transactionIdentifier);
- toast("业务异常,订单支付失败,请联系管理员");
- applePayError && applePayError({
- type: "orderPayApple",
- msg: "业务异常,订单支付失败",
- err: new Error(
- `业务返回码异常:${res.code}`),
- from: "apple",
- });
- }
- } catch (innerErr) {
- // 捕获then回调内的局部异常(如iapChannel意外丢失、res字段缺失等)
- console.log('innerErr',innerErr);
- uni.hideLoading();
- toast("订单处理异常,请联系管理员");
- applePayError && applePayError({
- type: "orderPayApple_inner",
- msg: "订单成功回调内执行异常",
- err: innerErr, // 打印具体的局部异常信息
- from: "apple",
- });
- }
- }).catch((err) => {
- uni.hideLoading();
- // 仅捕获orderPayApple接口请求失败的异常(如网络错误、后端1002码等)
- toast("订单支付校验失败catch,请联系管理员");
- applePayError && applePayError({
- type: "orderPayApple",
- msg: "订单支付校验失败",
- err: err,
- from: "apple",
- });
- });
- },
- fail: (payErr) => {
- uni.hideLoading();
- toast("苹果购买失败fail,请联系管理员");
- applePayError && applePayError({
- type: "uni.requestPayment",
- msg: "苹果内购失败",
- err: payErr,
- from: "apple",
- });
- },
- });
- },
- function(orderErr) {
- // 获取订单信息失败回调
- uni.hideLoading();
- toast('内购订单获取失败,请联系管理员');
- applePayError && applePayError({
- type: "iapChannel.requestOrder",
- msg: "苹果内购订单获取失败",
- err: orderErr,
- from: "apple",
- });
- }
- );
- },
- // 核心修复3:补全plus.payment.getChannels的失败回调(之前缺失)
- function(channelErr) {
- uni.hideLoading();
- toast('获取支付通道失败,请检查内购配置');
- applePayError && applePayError({
- type: "plus.payment.getChannels",
- msg: "获取苹果内购支付通道失败",
- err: channelErr,
- from: "apple",
- });
- }
- );
- } catch (err) {
- uni.hideLoading(); // 核心修复4:异常时隐藏loading,避免页面卡死
- toast('支付环境检测异常00,请联系管理员');
- applePayError && applePayError({
- type: "applePay_try_catch",
- msg: "苹果内购API唤起失败",
- err: err,
- from: "plus.payment.getChannels",
- });
- }
- }
- // 支付成功
- function paySuccessResult() {
- console.log('3423423423423');
- uni.hideLoading()
- toast('支付成功')
- paySuccess && paySuccess();
- }
- return {
- OrderPay
- }
- }
- /**
- * 错误
- * 1. 创建订单错误
- * 2. 支付SDK错误
- * 3. 支付成功后,后台错误
- *
- *
- *
- *
- *
- *
- *
- *
- *
- *
- *
- *
- */
|