index.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. import $http from '@/common/request/index'
  2. // #ifdef H5
  3. import wxsdk from '@/common/wechat/sdk'
  4. // #endif
  5. import wechat from '@/common/wechat/wechat'
  6. import {
  7. router as $Router
  8. } from '@/common/router/index.js'
  9. import store from '@/common/store';
  10. import $platform from '@/common/platform';
  11. import {
  12. wxPayPamars
  13. } from '@/api/public/index.js'
  14. /**
  15. * 支付
  16. *
  17. * @param {String} payment = ['wechat','alipay','wallet'] - 支付方式
  18. * @param {Object} order = {} - 订单详情
  19. * @param {String} orderType = ['goods','recharge'] - 订单类型
  20. */
  21. export default class ShoproPay {
  22. // wxOfficialAccount wxMiniProgram App H5
  23. // wechat 公众号JSSDK支付 小程序支付 微信开放平台支付 H5网页支付
  24. // alipay 复制网址 复制网址 支付宝开放平台支付 直接跳转链接
  25. // wallet v v v v
  26. constructor(orderId, couseId) {
  27. // this.payment = payment;
  28. this.orderId = orderId;
  29. this.couseId = couseId;
  30. // this.orderType = orderType;
  31. // this.platform = $platform.get();
  32. let payMehod = this.getPayMethod();
  33. payMehod();
  34. }
  35. getPayMethod() {
  36. return this.wxOfficialAccountPay()
  37. // var payMethod = {
  38. // 'wxOfficialAccount': {
  39. // 'wechat': () => {
  40. // this.wxOfficialAccountPay()
  41. // }
  42. // },
  43. // 'H5': {
  44. // 'wechat': () => {
  45. // this.wechatWapPay()
  46. // }
  47. // },
  48. // }
  49. // return payMethod[this.platform][this.payment];
  50. // return payMethod['wxOfficialAccount']['wechat'];
  51. }
  52. // 预支付
  53. prepay() {
  54. let that = this;
  55. // console.log(this.platform);
  56. return new Promise(async (resolve, reject) => {
  57. let that = this;
  58. // let params = {
  59. // order_number: that.order.order_number,
  60. // payment: that.payment,
  61. // platform: that.platform
  62. // }
  63. // if (uni.getStorageSync('openid')) {
  64. // params.openid = uni.getStorageSync('openid');
  65. // }
  66. let params = {
  67. // order_number: '123456',
  68. // // payment: 'wechat',
  69. // // platform: 'wxOfficialAccount'
  70. order_id: '666'
  71. }
  72. // params.openid = 'wx96cad2970c232f6e';
  73. params.openid = uni.getStorageSync('wxId');
  74. let res6 = await wxPayPamars(params)
  75. console.log('获取微信支付参数返回值', res6);
  76. $http('pay.prepay', params, '支付中').then(res => {
  77. // console.log(res);
  78. if (res.code === 0) {
  79. res.data === 'no_openid' ?
  80. uni.showModal({
  81. title: '微信支付',
  82. content: '请先绑定微信再使用微信支付',
  83. confirmColor: '#00CA88',
  84. success: function(res) {
  85. if (res.confirm) {
  86. // wechat.bind();
  87. }
  88. },
  89. }) :
  90. resolve(res);
  91. }
  92. })
  93. });
  94. }
  95. // 微信H5支付
  96. async wxOfficialAccountPay() {
  97. console.log('wxOfficialAccountPay()中获取的课程支付订单id', this.orderId);
  98. let res6 = await wxPayPamars({
  99. order_id: this.orderId
  100. })
  101. console.log('获取微信支付参数返回值--pay', res6);
  102. // uni.showToast({
  103. // title: res6.data,
  104. // icon: 'none',
  105. // duration: 20000
  106. // })
  107. return
  108. wxsdk.wxpay(res6.data, (res) => {
  109. console.log('微信支付返回值-------', res);
  110. if (res.errMsg == "chooseWXPay:ok") {
  111. uni.showToast({
  112. title: '付款成功',
  113. icon: 'success',
  114. duration: 1500
  115. })
  116. // setTimeout(() => {
  117. // uni.navigateTo({
  118. // url: '/pages/index/scorePaySuc/index?id=' + that.couseId
  119. // })
  120. // }, 1500)
  121. } else {
  122. uni.showToast({
  123. title: '支付失败,请稍后重试!',
  124. icon: 'error',
  125. duration: 1500
  126. })
  127. }
  128. });
  129. return
  130. if (res6.code == 1 && res6.msg == 'open_id 不存在,请绑定微信!') {
  131. // if (false) {
  132. uni.showModal({
  133. title: '微信支付',
  134. content: '请先绑定微信再使用微信支付',
  135. confirmColor: '#00CA88',
  136. success: function(res) {
  137. if (res.confirm) {
  138. // wechat.bind();
  139. console.log('点击了确定------');
  140. uni.navigateTo({
  141. url: '/pages/signin/signin'
  142. })
  143. }
  144. },
  145. })
  146. return
  147. } else {
  148. let that = this;
  149. let parmas = res6.data
  150. // let result = await this.prepay();
  151. // let parmas = {
  152. // appId: res6.data.appId,
  153. // mch_id: '',
  154. // nonce_str: res6.data.nonceStr,
  155. // sign: res6.data.paySign,
  156. // body: '',
  157. // out_trade_no: '',
  158. // total_fee: 0.01,
  159. // spbill_create_ip: '',
  160. // notify_url: '',
  161. // trade_type: "JSAPI",
  162. // openid: '',
  163. // scene_info: ''
  164. // }
  165. // uni.showToast({
  166. // title: res6.data.appId
  167. // })
  168. console.log('微信支付参数', res6.data);
  169. // uni.showToast({
  170. // title: res6.data,
  171. // icon: 'success',
  172. // duration: 1500
  173. // })
  174. // setTimeout(() => {
  175. // }, 5000)
  176. }
  177. }
  178. //浏览器微信支付
  179. async wechatWapPay() {
  180. let that = this;
  181. let result = await this.prepay();
  182. if (result.code === 1) {
  183. var url = result.data.pay_data.match(/url\=\'(\S*)\'/);
  184. let reg = new RegExp('&', 'g') //g代表全部
  185. let newUrl = url[1].replace(reg, '&');
  186. let domain = store.getters.initShop.domain; //域名需要https
  187. let params = encodeURIComponent(
  188. `${domain}pages/order/payment/result?orderId=${that.order.id}&type=${that.payment}&orderType=${that.orderType}`
  189. )
  190. // let params = encodeURIComponent(
  191. // `${domain}pages/user/public/paymentdetail?orderId=${that.order.id}&type=${that.payment}&orderType=${that.orderType}`
  192. // )
  193. window.location.href = newUrl + '&redirect_url=' + params;
  194. }
  195. }
  196. // 微信小程序支付
  197. async wxMiniProgramPay() {
  198. let that = this;
  199. let result = await this.prepay();
  200. uni.requestPayment({
  201. provider: 'wxpay',
  202. ...result.data,
  203. success: res => {
  204. console.log(res);
  205. that.payResult('success')
  206. },
  207. fail: err => {
  208. console.log('支付取消或者失败:', err);
  209. err.errMsg !== "requestPayment:fail cancel" && that.payResult('fail')
  210. uni.showModal({
  211. title: '失败',
  212. content: '支付取消或者失败',
  213. confirmText: '重新支付',
  214. confirmColor: '#00CA88',
  215. success: (res) => {
  216. if (res.confirm) {
  217. that.wxMiniProgramPay()
  218. }
  219. }
  220. })
  221. }
  222. });
  223. }
  224. // 余额支付
  225. async walletPay() {
  226. let that = this;
  227. let result = await this.prepay();
  228. result.code === 1 && that.payResult('success')
  229. }
  230. // 支付宝复制链接支付
  231. async copyPayLink() {
  232. let that = this;
  233. let result = await this.prepay();
  234. if (result.code === 1) {
  235. //引入showModal 点击确认 复制链接;
  236. uni.showModal({
  237. title: '支付宝支付',
  238. content: '复制链接到外部浏览器',
  239. confirmText: '复制链接',
  240. confirmColor: '#00CA88',
  241. success: (res) => {
  242. if (res.confirm) {
  243. uni.setClipboardData({
  244. data: result.data.pay_data,
  245. success: function(data) {
  246. that.$u.toast('已复制到剪切板');
  247. }
  248. });
  249. }
  250. }
  251. })
  252. }
  253. }
  254. // 支付链接
  255. async goToPayLink() {
  256. let that = this;
  257. let result = await this.prepay();
  258. if (result.code === 1) {
  259. window.location = result.data.pay_data;
  260. }
  261. }
  262. // 支付宝支付
  263. async aliPay() {
  264. let that = this;
  265. let result = await this.prepay();
  266. if (result.code === 1) {
  267. uni.requestPayment({
  268. provider: 'alipay',
  269. orderInfo: result.data.pay_data, //支付宝订单数据
  270. success: res => {
  271. that.payResult('success')
  272. },
  273. fail: err => {
  274. console.log('支付取消或者失败:', err);
  275. err.errMsg !== "requestPayment:fail cancel" && that.payResult('fail')
  276. }
  277. });
  278. }
  279. }
  280. // 微信支付
  281. async wechatPay() {
  282. let that = this;
  283. let result = await this.prepay();
  284. if (result.code === 1) {
  285. uni.requestPayment({
  286. provider: 'wxpay',
  287. orderInfo: JSON.parse(result.data.pay_data), //微信订单数据(官方说是string。实测为object)
  288. success: res => {
  289. that.payResult('success')
  290. },
  291. fail: err => {
  292. err.errMsg !== "requestPayment:fail cancel" && that.payResult('fail')
  293. console.log('支付取消或者失败:', err);
  294. }
  295. });
  296. }
  297. }
  298. // 支付结果跳转,success:成功,fail:失败
  299. payResult(resultType) {
  300. const that = this;
  301. if (resultType == 'success') {
  302. store.dispatch('getUserInfo')
  303. $Router.replace({
  304. path: '/pages/user/public/paymentdetail',
  305. query: {
  306. orderId: that.order.id,
  307. type: that.payment, //重新支付的时候使用
  308. payState: resultType,
  309. orderType: that.orderType
  310. }
  311. });
  312. store.commit("subscribeMessage", 'unread_msg');
  313. } else {
  314. uni.showToast({
  315. title: '支付失败',
  316. icon: 'none'
  317. })
  318. }
  319. }
  320. }