1 line
16 KiB
Plaintext
1 line
16 KiB
Plaintext
|
{"version":3,"file":"pay.js","sources":["../../../../../src/sheep/platform/pay.js"],"sourcesContent":["import sheep from '@/sheep'\r\n// #ifdef H5\r\nimport $wxsdk from '@/sheep/libs/sdk-h5-weixin'\r\n// #endif\r\nimport { getRootUrl } from '@/sheep/helper'\r\nimport PayOrderApi from '@/sheep/api/pay/order'\r\n\r\n/**\r\n * 支付\r\n *\r\n * @param {String} payment = ['wechat','alipay','wallet','mock'] \t- 支付方式\r\n * @param {String} orderType = ['goods','recharge','groupon'] \t- 订单类型\r\n * @param {String} id\t\t\t\t\t- 订单号\r\n */\r\n\r\nexport default class SheepPay {\r\n constructor(payment, orderType, id) {\r\n this.payment = payment\r\n this.id = id\r\n this.orderType = orderType\r\n this.payAction()\r\n }\r\n\r\n payAction() {\r\n const payAction = {\r\n WechatOfficialAccount: {\r\n wechat: () => {\r\n this.wechatOfficialAccountPay()\r\n },\r\n alipay: () => {\r\n this.redirectPay() // 现在公众号可以直接跳转支付宝页面\r\n },\r\n wallet: () => {\r\n this.walletPay()\r\n },\r\n mock: () => {\r\n this.mockPay()\r\n },\r\n },\r\n WechatMiniProgram: {\r\n wechat: () => {\r\n this.wechatMiniProgramPay()\r\n },\r\n alipay: () => {\r\n this.copyPayLink()\r\n },\r\n wallet: () => {\r\n this.walletPay()\r\n },\r\n mock: () => {\r\n this.mockPay()\r\n },\r\n },\r\n App: {\r\n wechat: () => {\r\n this.wechatAppPay()\r\n },\r\n alipay: () => {\r\n this.alipay()\r\n },\r\n wallet: () => {\r\n this.walletPay()\r\n },\r\n mock: () => {\r\n this.mockPay()\r\n },\r\n },\r\n H5: {\r\n wechat: () => {\r\n this.wechatWapPay()\r\n },\r\n alipay: () => {\r\n this.redirectPay()\r\n },\r\n wallet: () => {\r\n this.walletPay()\r\n },\r\n mock: () => {\r\n this.mockPay()\r\n },\r\n },\r\n }\r\n return payAction[sheep.$platform.name][this.payment]()\r\n }\r\n\r\n // 预支付\r\n prepay(channel) {\r\n return new Promise(async (resolve, reject) => {\r\n const data = {\r\n id: this.id,\r\n channelCode: channel,\r\n channelExtras: {},\r\n }\r\n // 特殊逻辑:微信公众号、小程序支付时,必须传入 openid\r\n if (['wx_pub', 'wx_lite'].includes(channel)) {\r\n const openid = await sheep.$platform.useProvider('wechat').getOpenid()\r\n // 如果获取不到 openid,微信无法发起支付,此时需要引导\r\n if (!openid) {\r\n this.bindWeixin()\r\n return\r\n }\r\n data.channelExtras.openid = openid\r\n }\r\n // 发起预支付 API 调用\r\n PayOrderApi.submitOrder(data).then((res) => {\r\n // 成功时\r\n res.code === 0 && resolve(res)\r\n // 失败时\r\n if (res.code !== 0 && res.msg.indexOf('无效的openid') >= 0) {\r\n // 特殊逻辑:微信公众号、小程序支付时,必须传入 openid 不正确的情况\r\n if (\r\n res.msg.indexOf('无效的openid') >= 0 || // 获取的 openid 不正确时,或者随便输入了个 openid\r\n res.msg.indexOf('下单账号与支付账号不一致') >= 0\r\n ) {\r\n // https://developers.weixin.qq.com/community/develop/doc/00008c53c347804beec82aed051c00\r\n this.bindWeixin()\r\n }\r\n }\r\n })\r\n })\r\n }\r\n\r\n // #ifdef H5\r\n // 微信公众号 JSSDK 支付\r\n async wechatOfficialAccountPay() {\r\n const { code, data } = await this.prepay('wx_pub')\r\n if (code !== 0) {\r\n return\r\n }\r\n const payConfig = JSON.parse(data.displayContent)\r\n $wxsdk.wxpay(payConfig, {\r\n success: () => {\r\n this.payResult('success')\r\n },\r\n
|