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 cancel: () => {\r\n sheep.$helper.toast('支付已手动取消')\r\n },\r\n fail: (error) => {\r\n if (error.errMsg.indexOf('chooseWXPay:没有此SDK或暂不支持此SDK模拟') >= 0) {\r\n sheep.$helper.toast(\r\n '发起微信支付失败,原因:可能是微信开发者工具不支持,建议使用微信打开网页后支付',\r\n )\r\n return\r\n }\r\n this.payResult('fail')\r\n },\r\n })\r\n }\r\n\r\n // 浏览器微信 H5 支付 TODO 芋艿:待接入\r\n async wechatWapPay() {\r\n const { error, data } = await this.prepay()\r\n if (error === 0) {\r\n const redirect_url = `${getRootUrl()}pages/pay/result?id=${this.id}&payment=${this.payment}&orderType=${this.orderType}`\r\n location.href = `${data.pay_data.h5_url}&redirect_url=${encodeURIComponent(redirect_url)}`\r\n }\r\n }\r\n\r\n // 支付链接 TODO 芋艿:待接入\r\n async redirectPay() {\r\n const { error, data } = await this.prepay()\r\n if (error === 0) {\r\n const redirect_url = `${getRootUrl()}pages/pay/result?id=${this.id}&payment=${this.payment}&orderType=${this.orderType}`\r\n location.href = data.pay_data + encodeURIComponent(redirect_url)\r\n }\r\n }\r\n\r\n // #endif\r\n\r\n // 微信小程序支付\r\n async wechatMiniProgramPay() {\r\n // let that = this;\r\n const { code, data } = await this.prepay('wx_lite')\r\n if (code !== 0) {\r\n return\r\n }\r\n // 调用微信小程序支付\r\n const payConfig = JSON.parse(data.displayContent)\r\n uni.requestPayment({\r\n provider: 'wxpay',\r\n timeStamp: payConfig.timeStamp,\r\n nonceStr: payConfig.nonceStr,\r\n package: payConfig.packageValue,\r\n signType: payConfig.signType,\r\n paySign: payConfig.paySign,\r\n success: (res) => {\r\n this.payResult('success')\r\n },\r\n fail: (err) => {\r\n if (err.errMsg === 'requestPayment:fail cancel') {\r\n sheep.$helper.toast('支付已手动取消')\r\n } else {\r\n this.payResult('fail')\r\n }\r\n },\r\n })\r\n }\r\n\r\n // 余额支付\r\n async walletPay() {\r\n const { code } = await this.prepay('wallet')\r\n code === 0 && this.payResult('success')\r\n }\r\n\r\n // 模拟支付\r\n async mockPay() {\r\n const { code } = await this.prepay('mock')\r\n code === 0 && this.payResult('success')\r\n }\r\n\r\n // 支付宝复制链接支付 TODO 芋艿:待接入\r\n async copyPayLink() {\r\n const that = this\r\n const { error, data } = await this.prepay()\r\n if (error === 0) {\r\n // 引入showModal 点击确认 复制链接;\r\n uni.showModal({\r\n title: '支付宝支付',\r\n content: '复制链接到外部浏览器',\r\n confirmText: '复制链接',\r\n success: (res) => {\r\n if (res.confirm) {\r\n sheep.$helper.copyText(data.pay_data)\r\n }\r\n },\r\n })\r\n }\r\n }\r\n\r\n // 支付宝支付 TODO 芋艿:待接入\r\n async alipay() {\r\n const that = this\r\n const { error, data } = await this.prepay()\r\n if (error === 0) {\r\n uni.requestPayment({\r\n provider: 'alipay',\r\n orderInfo: data.pay_data, // 支付宝订单数据\r\n success: (res) => {\r\n that.payResult('success')\r\n },\r\n fail: (err) => {\r\n if (err.errMsg === 'requestPayment:fail [paymentAlipay:62001]user cancel') {\r\n sheep.$helper.toast('支付已手动取消')\r\n } else {\r\n that.payResult('fail')\r\n }\r\n },\r\n })\r\n }\r\n }\r\n\r\n // 微信支付 TODO 芋艿:待接入\r\n async wechatAppPay() {\r\n const that = this\r\n const { error, data } = await this.prepay()\r\n if (error === 0) {\r\n uni.requestPayment({\r\n provider: 'wxpay',\r\n orderInfo: data.pay_data, // 微信订单数据(官方说是string。实测为object)\r\n success: (res) => {\r\n that.payResult('success')\r\n },\r\n fail: (err) => {\r\n err.errMsg !== 'requestPayment:fail cancel' && that.payResult('fail')\r\n },\r\n })\r\n }\r\n }\r\n\r\n // 支付结果跳转,success:成功,fail:失败\r\n payResult(resultType) {\r\n sheep.$router.redirect('/modules/mall/pay/result', {\r\n id: this.id,\r\n orderType: this.orderType,\r\n payState: resultType,\r\n })\r\n }\r\n\r\n // 引导绑定微信\r\n bindWeixin() {\r\n uni.showModal({\r\n title: '微信支付',\r\n content: '请先绑定微信再使用微信支付',\r\n success: function (res) {\r\n if (res.confirm) {\r\n sheep.$platform.useProvider('wechat').bind()\r\n }\r\n },\r\n })\r\n }\r\n}\r\n\r\nexport function getPayMethods(channels) {\r\n const payMethods = [\r\n {\r\n icon: '/static/img/shop/pay/wechat.png',\r\n title: '微信支付',\r\n value: 'wechat',\r\n disabled: true,\r\n },\r\n {\r\n icon: '/static/img/shop/pay/alipay.png',\r\n title: '支付宝支付',\r\n value: 'alipay',\r\n disabled: true,\r\n },\r\n {\r\n icon: '/static/img/shop/pay/wallet.png',\r\n title: '余额支付',\r\n value: 'wallet',\r\n disabled: true,\r\n },\r\n {\r\n icon: '/static/img/shop/pay/apple.png',\r\n title: 'Apple Pay',\r\n value: 'apple',\r\n disabled: true,\r\n },\r\n {\r\n icon: '/static/img/shop/pay/wallet.png',\r\n title: '模拟支付',\r\n value: 'mock',\r\n disabled: true,\r\n },\r\n ]\r\n const platform = sheep.$platform.name\r\n\r\n // 1. 处理【微信支付】\r\n const wechatMethod = payMethods[0]\r\n if (\r\n (platform === 'WechatOfficialAccount' && channels.includes('wx_pub')) ||\r\n (platform === 'WechatMiniProgram' && channels.includes('wx_lite')) ||\r\n (platform === 'App' && channels.includes('wx_app'))\r\n ) {\r\n wechatMethod.disabled = false\r\n }\r\n wechatMethod.disabled = false // TODO 芋艿:临时测试\r\n\r\n // 2. 处理【支付宝支付】\r\n const alipayMethod = payMethods[1]\r\n if (\r\n (platform === 'WechatOfficialAccount' && channels.includes('alipay_wap')) ||\r\n (platform === 'WechatMiniProgram' && channels.includes('alipay_wap')) ||\r\n (platform === 'App' && channels.includes('alipay_app'))\r\n ) {\r\n alipayMethod.disabled = false\r\n }\r\n // 3. 处理【余额支付】\r\n const walletMethod = payMethods[2]\r\n if (channels.includes('wallet')) {\r\n walletMethod.disabled = false\r\n }\r\n // 4. 处理【苹果支付】TODO 芋艿:未来接入\r\n // 5. 处理【模拟支付】\r\n const mockMethod = payMethods[4]\r\n if (channels.includes('mock')) {\r\n mockMethod.disabled = false\r\n }\r\n return payMethods\r\n}\r\n"],"names":["sheep","PayOrderApi","uni"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAee,MAAM,SAAS;AAAA,EAC5B,YAAY,SAAS,WAAW,IAAI;AAClC,SAAK,UAAU;AACf,SAAK,KAAK;AACV,SAAK,YAAY;AACjB,SAAK,UAAW;AAAA,EACjB;AAAA,EAED,YAAY;AACV,UAAM,YAAY;AAAA,MAChB,uBAAuB;AAAA,QACrB,QAAQ,MAAM;AACZ,eAAK,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