123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360 |
- import $http from '@/common/request/index'
- // #ifdef H5
- import wxsdk from '@/common/wechat/sdk'
- // #endif
- import wechat from '@/common/wechat/wechat'
- import {
- router as $Router
- } from '@/common/router/index.js'
- import store from '@/common/store';
- import $platform from '@/common/platform';
- import {
- wxPayPamars
- } from '@/api/public/index.js'
- /**
- * 支付
- *
- * @param {String} payment = ['wechat','alipay','wallet'] - 支付方式
- * @param {Object} order = {} - 订单详情
- * @param {String} orderType = ['goods','recharge'] - 订单类型
- */
- export default class ShoproPay {
- // wxOfficialAccount wxMiniProgram App H5
- // wechat 公众号JSSDK支付 小程序支付 微信开放平台支付 H5网页支付
- // alipay 复制网址 复制网址 支付宝开放平台支付 直接跳转链接
- // wallet v v v v
- constructor(orderId, couseId) {
- // this.payment = payment;
- this.orderId = orderId;
- this.couseId = couseId;
- // this.orderType = orderType;
- // this.platform = $platform.get();
- let payMehod = this.getPayMethod();
- payMehod();
- }
- getPayMethod() {
- return this.wxOfficialAccountPay()
- // var payMethod = {
- // 'wxOfficialAccount': {
- // 'wechat': () => {
- // this.wxOfficialAccountPay()
- // }
- // },
- // 'H5': {
- // 'wechat': () => {
- // this.wechatWapPay()
- // }
- // },
- // }
- // return payMethod[this.platform][this.payment];
- // return payMethod['wxOfficialAccount']['wechat'];
- }
- // 预支付
- prepay() {
- let that = this;
- // console.log(this.platform);
- return new Promise(async (resolve, reject) => {
- let that = this;
- // let params = {
- // order_number: that.order.order_number,
- // payment: that.payment,
- // platform: that.platform
- // }
- // if (uni.getStorageSync('openid')) {
- // params.openid = uni.getStorageSync('openid');
- // }
- let params = {
- // order_number: '123456',
- // // payment: 'wechat',
- // // platform: 'wxOfficialAccount'
- order_id: '666'
- }
- // params.openid = 'wx96cad2970c232f6e';
- params.openid = uni.getStorageSync('wxId');
- let res6 = await wxPayPamars(params)
- console.log('获取微信支付参数返回值', res6);
- $http('pay.prepay', params, '支付中').then(res => {
- // console.log(res);
- if (res.code === 0) {
- res.data === 'no_openid' ?
- uni.showModal({
- title: '微信支付',
- content: '请先绑定微信再使用微信支付',
- confirmColor: '#00CA88',
- success: function(res) {
- if (res.confirm) {
- // wechat.bind();
- }
- },
- }) :
- resolve(res);
- }
- })
- });
- }
- // 微信H5支付
- async wxOfficialAccountPay() {
- console.log('wxOfficialAccountPay()中获取的课程支付订单id', this.orderId);
- let res6 = await wxPayPamars({
- order_id: this.orderId
- })
- console.log('获取微信支付参数返回值--pay', res6);
- // uni.showToast({
- // title: res6.data,
- // icon: 'none',
- // duration: 20000
- // })
- return
- wxsdk.wxpay(res6.data, (res) => {
- console.log('微信支付返回值-------', res);
- if (res.errMsg == "chooseWXPay:ok") {
- uni.showToast({
- title: '付款成功',
- icon: 'success',
- duration: 1500
- })
- // setTimeout(() => {
- // uni.navigateTo({
- // url: '/pages/index/scorePaySuc/index?id=' + that.couseId
- // })
- // }, 1500)
- } else {
- uni.showToast({
- title: '支付失败,请稍后重试!',
- icon: 'error',
- duration: 1500
- })
- }
- });
- return
- if (res6.code == 1 && res6.msg == 'open_id 不存在,请绑定微信!') {
- // if (false) {
- uni.showModal({
- title: '微信支付',
- content: '请先绑定微信再使用微信支付',
- confirmColor: '#00CA88',
- success: function(res) {
- if (res.confirm) {
- // wechat.bind();
- console.log('点击了确定------');
- uni.navigateTo({
- url: '/pages/signin/signin'
- })
- }
- },
- })
- return
- } else {
- let that = this;
- let parmas = res6.data
- // let result = await this.prepay();
- // let parmas = {
- // appId: res6.data.appId,
- // mch_id: '',
- // nonce_str: res6.data.nonceStr,
- // sign: res6.data.paySign,
- // body: '',
- // out_trade_no: '',
- // total_fee: 0.01,
- // spbill_create_ip: '',
- // notify_url: '',
- // trade_type: "JSAPI",
- // openid: '',
- // scene_info: ''
- // }
- // uni.showToast({
- // title: res6.data.appId
- // })
- console.log('微信支付参数', res6.data);
- // uni.showToast({
- // title: res6.data,
- // icon: 'success',
- // duration: 1500
- // })
- // setTimeout(() => {
- // }, 5000)
- }
- }
- //浏览器微信支付
- async wechatWapPay() {
- let that = this;
- let result = await this.prepay();
- if (result.code === 1) {
- var url = result.data.pay_data.match(/url\=\'(\S*)\'/);
- let reg = new RegExp('&', 'g') //g代表全部
- let newUrl = url[1].replace(reg, '&');
- let domain = store.getters.initShop.domain; //域名需要https
- let params = encodeURIComponent(
- `${domain}pages/order/payment/result?orderId=${that.order.id}&type=${that.payment}&orderType=${that.orderType}`
- )
- // let params = encodeURIComponent(
- // `${domain}pages/user/public/paymentdetail?orderId=${that.order.id}&type=${that.payment}&orderType=${that.orderType}`
- // )
- window.location.href = newUrl + '&redirect_url=' + params;
- }
- }
- // 微信小程序支付
- async wxMiniProgramPay() {
- let that = this;
- let result = await this.prepay();
- uni.requestPayment({
- provider: 'wxpay',
- ...result.data,
- success: res => {
- console.log(res);
- that.payResult('success')
- },
- fail: err => {
- console.log('支付取消或者失败:', err);
- err.errMsg !== "requestPayment:fail cancel" && that.payResult('fail')
- uni.showModal({
- title: '失败',
- content: '支付取消或者失败',
- confirmText: '重新支付',
- confirmColor: '#00CA88',
- success: (res) => {
- if (res.confirm) {
- that.wxMiniProgramPay()
- }
- }
- })
- }
- });
- }
- // 余额支付
- async walletPay() {
- let that = this;
- let result = await this.prepay();
- result.code === 1 && that.payResult('success')
- }
- // 支付宝复制链接支付
- async copyPayLink() {
- let that = this;
- let result = await this.prepay();
- if (result.code === 1) {
- //引入showModal 点击确认 复制链接;
- uni.showModal({
- title: '支付宝支付',
- content: '复制链接到外部浏览器',
- confirmText: '复制链接',
- confirmColor: '#00CA88',
- success: (res) => {
- if (res.confirm) {
- uni.setClipboardData({
- data: result.data.pay_data,
- success: function(data) {
- that.$u.toast('已复制到剪切板');
- }
- });
- }
- }
- })
- }
- }
- // 支付链接
- async goToPayLink() {
- let that = this;
- let result = await this.prepay();
- if (result.code === 1) {
- window.location = result.data.pay_data;
- }
- }
- // 支付宝支付
- async aliPay() {
- let that = this;
- let result = await this.prepay();
- if (result.code === 1) {
- uni.requestPayment({
- provider: 'alipay',
- orderInfo: result.data.pay_data, //支付宝订单数据
- success: res => {
- that.payResult('success')
- },
- fail: err => {
- console.log('支付取消或者失败:', err);
- err.errMsg !== "requestPayment:fail cancel" && that.payResult('fail')
- }
- });
- }
- }
- // 微信支付
- async wechatPay() {
- let that = this;
- let result = await this.prepay();
- if (result.code === 1) {
- uni.requestPayment({
- provider: 'wxpay',
- orderInfo: JSON.parse(result.data.pay_data), //微信订单数据(官方说是string。实测为object)
- success: res => {
- that.payResult('success')
- },
- fail: err => {
- err.errMsg !== "requestPayment:fail cancel" && that.payResult('fail')
- console.log('支付取消或者失败:', err);
- }
- });
- }
- }
- // 支付结果跳转,success:成功,fail:失败
- payResult(resultType) {
- const that = this;
- if (resultType == 'success') {
- store.dispatch('getUserInfo')
- $Router.replace({
- path: '/pages/user/public/paymentdetail',
- query: {
- orderId: that.order.id,
- type: that.payment, //重新支付的时候使用
- payState: resultType,
- orderType: that.orderType
- }
- });
- store.commit("subscribeMessage", 'unread_msg');
- } else {
- uni.showToast({
- title: '支付失败',
- icon: 'none'
- })
- }
- }
- }
|