123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355 |
- import sheep from '@/sheep';
- import $wxsdk from '@/sheep/libs/sdk-h5-weixin';
- import { getRootUrl } from '@/sheep/helper';
- import PayOrderApi from '@/sheep/api/pay/order';
- export default class SheepPay {
- constructor(payment, orderType, id) {
- this.payment = payment;
- this.id = id;
- this.orderType = orderType;
- this.payAction();
- }
- payAction() {
- const payAction = {
- WechatOfficialAccount: {
- wechat: () => {
- this.wechatOfficialAccountPay();
- },
- alipay: () => {
- this.redirectPay();
- },
- wallet: () => {
- this.walletPay();
- },
- mock: () => {
- this.mockPay();
- }
- },
- WechatMiniProgram: {
- wechat: () => {
- this.wechatMiniProgramPay();
- },
- alipay: () => {
- this.copyPayLink();
- },
- wallet: () => {
- this.walletPay();
- },
- mock: () => {
- this.mockPay();
- }
- },
- App: {
- wechat: () => {
- this.wechatAppPay();
- },
- alipay: () => {
- this.alipay();
- },
- wallet: () => {
- this.walletPay();
- },
- mock: () => {
- this.mockPay();
- }
- },
- H5: {
- wechat: () => {
- this.wechatWapPay();
- },
- alipay: () => {
- this.redirectPay();
- },
- wallet: () => {
- this.walletPay();
- },
- mock: () => {
- this.mockPay();
- }
- },
- };
- return payAction[sheep.$platform.name][this.payment]();
- }
-
- prepay(channel) {
- return new Promise(async (resolve, reject) => {
- let data = {
- id: this.id,
- channelCode: channel,
- channelExtras: {}
- };
-
-
- if (['wx_pub', 'wx_lite'].includes(channel)) {
- console.log(sheep.$platform.useProvider('wechat').getOpenid)
- const openid = await sheep.$platform.useProvider('wechat').getOpenid(true);
-
- if (!openid) {
- this.bindWeixin();
- return;
- }
- data.channelExtras.openid = openid;
- }
-
- PayOrderApi.submitOrder(data).then((res) => {
-
- res.code === 0 && resolve(res);
-
- if (res.code !== 0 && res.msg.indexOf('无效的openid') >= 0) {
-
- if (res.msg.indexOf('无效的openid') >= 0
- || res.msg.indexOf('下单账号与支付账号不一致') >= 0) {
- this.bindWeixin();
- }
- }
- });
- });
- }
-
-
- async wechatOfficialAccountPay() {
- let { code, data } = await this.prepay('wx_pub');
- if (code !== 0) {
- return;
- }
- const payConfig = JSON.parse(data.displayContent);
- $wxsdk.wxpay(payConfig, {
- success: () => {
- this.payResult('success');
- },
- cancel: () => {
- sheep.$helper.toast('支付已手动取消');
- },
- fail: (error) => {
- if (error.errMsg.indexOf('chooseWXPay:没有此SDK或暂不支持此SDK模拟') >= 0) {
- sheep.$helper.toast('发起微信支付失败,原因:可能是微信开发者工具不支持,建议使用微信打开网页后支付');
- return
- }
- this.payResult('fail');
- },
- });
- }
-
- async wechatWapPay() {
- const { error, data } = await this.prepay();
- if (error === 0) {
- const redirect_url = `${getRootUrl()}pages/pay/result?id=${this.id}&payment=${this.payment}&orderType=${this.orderType}`;
- location.href = `${data.pay_data.h5_url}&redirect_url=${encodeURIComponent(redirect_url)}`;
- }
- }
-
- async redirectPay() {
- let { error, data } = await this.prepay();
- if (error === 0) {
- const redirect_url = `${getRootUrl()}pages/pay/result?id=${this.id}&payment=${this.payment}&orderType=${this.orderType}`;
- location.href = data.pay_data + encodeURIComponent(redirect_url);
- }
- }
-
-
- async wechatMiniProgramPay() {
-
- let { code, data } = await this.prepay('wx_lite');
- if (code !== 0) {
- return;
- }
-
- const payConfig = JSON.parse(data.displayContent);
- uni.requestPayment({
- provider: 'wxpay',
- timeStamp: payConfig.timeStamp,
- nonceStr: payConfig.nonceStr,
- package: payConfig.packageValue,
- signType: payConfig.signType,
- paySign: payConfig.paySign,
- success: (res) => {
- this.payResult('success');
- },
- fail: (err) => {
- if (err.errMsg === 'requestPayment:fail cancel') {
- sheep.$helper.toast('支付已手动取消');
- } else {
- this.payResult('fail');
- }
- },
- });
- }
-
- async walletPay() {
- const { code } = await this.prepay('wallet');
- code === 0 && this.payResult('success');
- }
-
- async mockPay() {
- const { code } = await this.prepay('mock');
- code === 0 && this.payResult('success');
- }
-
- async copyPayLink() {
- let that = this;
- let { error, data } = await this.prepay();
- if (error === 0) {
-
- uni.showModal({
- title: '支付宝支付',
- content: '复制链接到外部浏览器',
- confirmText: '复制链接',
- success: (res) => {
- if (res.confirm) {
- sheep.$helper.copyText(data.pay_data);
- }
- },
- });
- }
- }
-
- async alipay() {
- let that = this;
- const { error, data } = await this.prepay();
- if (error === 0) {
- uni.requestPayment({
- provider: 'alipay',
- orderInfo: data.pay_data,
- success: (res) => {
- that.payResult('success');
- },
- fail: (err) => {
- if (err.errMsg === 'requestPayment:fail [paymentAlipay:62001]user cancel') {
- sheep.$helper.toast('支付已手动取消');
- } else {
- that.payResult('fail');
- }
- },
- });
- }
- }
-
- async wechatAppPay() {
- let that = this;
- let { error, data } = await this.prepay();
- if (error === 0) {
- uni.requestPayment({
- provider: 'wxpay',
- orderInfo: data.pay_data,
- success: (res) => {
- that.payResult('success');
- },
- fail: (err) => {
- err.errMsg !== 'requestPayment:fail cancel' && that.payResult('fail');
- },
- });
- }
- }
-
- payResult(resultType) {
- sheep.$router.redirect('/pages/pay/result', {
- id: this.id,
- orderType: this.orderType,
- payState: resultType
- });
- }
-
- bindWeixin() {
- uni.showModal({
- title: '微信支付',
- content: '请先绑定微信再使用微信支付',
- confirmText:'绑定',
- success: function (res) {
- if (res.confirm) {
- sheep.$platform.useProvider('wechat').bind();
- }
- },
- });
- }
- }
- export function getPayMethods(channels) {
- const payMethods = [
- {
- icon: '/static/img/shop/pay/wechat.png',
- title: '微信支付',
- value: 'wechat',
- disabled: true,
- },
- {
- icon: '/static/img/shop/pay/alipay.png',
- title: '支付宝支付',
- value: 'alipay',
- disabled: true,
- },
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- ];
- const platform = sheep.$platform.name
-
- const wechatMethod = payMethods[0];
- if ((platform === 'WechatOfficialAccount' && channels.includes('wx_pub'))
- || (platform === 'WechatMiniProgram' && channels.includes('wx_lite'))
- || (platform === 'App' && channels.includes('wx_app'))) {
- wechatMethod.disabled = false;
- }
- wechatMethod.disabled = false;
-
- const alipayMethod = payMethods[1];
- if ((platform === 'WechatOfficialAccount' && channels.includes('alipay_wap'))
- || platform === 'WechatMiniProgram' && channels.includes('alipay_wap')
- || platform === 'App' && channels.includes('alipay_app')) {
- alipayMethod.disabled = false;
- }
-
-
-
-
-
-
-
-
-
-
-
- return payMethods;
- }
|