| | |
| | | <script setup lang="ts"> |
| | | import IconWeixin from '../../assets/icon-weixin-pay.png'; |
| | | import IconAliPay from '../../assets/icon-alipay.png'; |
| | | import { |
| | | useLifeRechargeContext, |
| | | SetLifePayOrderPayTypeInput, |
| | | LifeRechargeConstants, |
| | | GetPayOrderForJsAPIInput, |
| | | } from '@life-payment/core-vue'; |
| | | import { useQuery } from '@tanstack/vue-query'; |
| | | import { LifeRechargeConstants } from '@life-payment/core-vue'; |
| | | import { Toast as NutToast } from '@nutui/nutui-taro'; |
| | | import { onMounted, reactive, ref, computed } from 'vue'; |
| | | import Taro from '@tarojs/taro'; |
| | | import { computed, toRef } from 'vue'; |
| | | import { useSelectPayType, useGetPayStatusByOrderNo } from '../../hooks/selectPayType'; |
| | | |
| | | defineOptions({ |
| | | name: 'SelectPayTypeView', |
| | |
| | | lifePayOrderType?: LifeRechargeConstants.LifePayOrderTypeEnum; |
| | | showAliPay?: boolean; |
| | | showWeixinPay?: boolean; |
| | | openId?: string; |
| | | isInWeChat?: boolean; |
| | | isH5?: boolean; |
| | | appId?: string; |
| | | isFocus?: boolean; |
| | | getOpenId?: () => Promise<string>; |
| | | }; |
| | | |
| | | const props = withDefaults(defineProps<Props>(), { |
| | |
| | | (e: 'payOrderForJsAPISuccess'): void; |
| | | }>(); |
| | | |
| | | const state = reactive({ |
| | | show: false, |
| | | msg: '', |
| | | const { state, invokeAliPay, invokeWeixinPay } = useSelectPayType({ |
| | | getOpenId: toRef(props, 'getOpenId'), |
| | | isInWeChat: toRef(props, 'isInWeChat'), |
| | | isH5: toRef(props, 'isH5'), |
| | | appId: toRef(props, 'appId'), |
| | | }); |
| | | |
| | | const { blLifeRecharge } = useLifeRechargeContext(); |
| | | |
| | | async function handleAliPay() { |
| | | try { |
| | | let res = await setLifePayOrderPayType(blLifeRecharge.constants.LifePayTypeEnum.AliPay); |
| | | if (res) { |
| | | location.href = res; |
| | | } |
| | | await invokeAliPay(props.orderNo); |
| | | } catch (error) {} |
| | | } |
| | | |
| | | async function handleWeixinPay() { |
| | | if (props.isH5) { |
| | | if (props.isInWeChat) { |
| | | handleWeixinPayByJsApi(); |
| | | } else { |
| | | // try { |
| | | // let res = await setLifePayOrderPayType(blLifeRecharge.constants.LifePayTypeEnum.WxPay); |
| | | // if (res) { |
| | | // location.href = res; |
| | | // } |
| | | // } catch (error) {} |
| | | state.msg = '请在微信中打开'; |
| | | state.show = true; |
| | | } |
| | | } else if (Taro.getEnv() === Taro.ENV_TYPE.WEAPP) { |
| | | handleWeixinPayByMini(); |
| | | } |
| | | } |
| | | |
| | | async function getPayOrderForJsAPI() { |
| | | try { |
| | | let params: GetPayOrderForJsAPIInput = { |
| | | orderNo: props.orderNo, |
| | | lifePayType: blLifeRecharge.constants.LifePayTypeEnum.WxPay, |
| | | openId: props.openId, |
| | | attach: Date.now().toString(), |
| | | payAppId: props.appId, |
| | | }; |
| | | let res = await blLifeRecharge.services.getPayOrderForJsAPI(params); |
| | | return res; |
| | | await invokeWeixinPay(props.orderNo); |
| | | } catch (error) {} |
| | | } |
| | | |
| | | async function handleWeixinPayByJsApi() { |
| | | try { |
| | | let res = await getPayOrderForJsAPI(); |
| | | |
| | | if (res) { |
| | | //@ts-ignore |
| | | if (WeixinJSBridge) { |
| | | //@ts-ignore |
| | | WeixinJSBridge.invoke( |
| | | 'getBrandWCPayRequest', |
| | | { |
| | | appId: props.appId, //公众号ID,由商户传入 |
| | | timeStamp: res.timestamp, //时间戳,自1970年以来的秒数 |
| | | nonceStr: res.nonceStr, //随机串 |
| | | package: res.package, |
| | | signType: res.signType, //微信签名方式: |
| | | paySign: res.paySign, //微信签名 |
| | | }, |
| | | function (res) { |
| | | if (res.err_msg == 'get_brand_wcpay_request:ok') { |
| | | // 使用以上方式判断前端返回,微信团队郑重提示: |
| | | //res.err_msg将在用户支付成功后返回ok,但并不保证它绝对可靠,商户需进一步调用后端查单确认支付结果。 |
| | | } else if (res.err_msg == 'system:access_denied') { |
| | | state.msg = '请在手机微信中打开'; |
| | | state.show = true; |
| | | } else { |
| | | // state.msg = res.err_msg; |
| | | // state.show = true; |
| | | } |
| | | } |
| | | ); |
| | | } |
| | | } |
| | | } catch (error) {} |
| | | } |
| | | |
| | | async function handleWeixinPayByMini() { |
| | | try { |
| | | let res = await getPayOrderForJsAPI(); |
| | | |
| | | if (res) { |
| | | Taro.requestPayment({ |
| | | timeStamp: res.timestamp, |
| | | nonceStr: res.nonceStr, |
| | | package: res.package, |
| | | signType: res.signType as any, |
| | | paySign: res.paySign, |
| | | success: function (res) { |
| | | console.log('res: ', res); |
| | | // if(res.errMsg) |
| | | }, |
| | | fail: function (res) {}, |
| | | }); |
| | | } |
| | | } catch (error) {} |
| | | } |
| | | |
| | | async function setLifePayOrderPayType(lifePayType: LifeRechargeConstants.LifePayTypeEnum) { |
| | | try { |
| | | let params: SetLifePayOrderPayTypeInput = { |
| | | orderNo: props.orderNo, |
| | | lifePayType: lifePayType, |
| | | }; |
| | | return await blLifeRecharge.services.setLifePayOrderPayType(params); |
| | | } catch (error) {} |
| | | } |
| | | |
| | | useQuery({ |
| | | queryKey: ['platformServicePayServices/getPayStatusByOrderNo', props.orderNo], |
| | | queryFn: async () => { |
| | | return await blLifeRecharge.services.getPayStatusByOrderNo( |
| | | { |
| | | orderNo: props.orderNo, |
| | | }, |
| | | { |
| | | showLoading: false, |
| | | } |
| | | ); |
| | | }, |
| | | onSuccess(data) { |
| | | if (data !== blLifeRecharge.constants.LifePayStatusEnum.未支付) { |
| | | emit('paySuccess', props.orderNo, props.lifePayOrderType); |
| | | } |
| | | }, |
| | | refetchInterval: 1000 * 3, |
| | | useGetPayStatusByOrderNo({ |
| | | orderNo: toRef(props, 'orderNo'), |
| | | enabled: computed(() => props.isFocus), |
| | | onPaySuccess() { |
| | | emit('paySuccess', props.orderNo, props.lifePayOrderType); |
| | | }, |
| | | }); |
| | | </script> |