index.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. let res6 = await wxPayPamars(params)
  74. console.log('获取微信支付参数返回值', res6);
  75. $http('pay.prepay', params, '支付中').then(res => {
  76. // console.log(res);
  77. if (res.code === 0) {
  78. res.data === 'no_openid' ?
  79. uni.showModal({
  80. title: '微信支付',
  81. content: '请先绑定微信再使用微信支付',
  82. confirmColor: '#00CA88',
  83. success: function(res) {
  84. if (res.confirm) {
  85. // wechat.bind();
  86. }
  87. },
  88. }) :
  89. resolve(res);
  90. }
  91. })
  92. });
  93. }
  94. // 微信H5支付
  95. async wxOfficialAccountPay() {
  96. console.log('wxOfficialAccountPay()中获取的课程支付订单id', this.orderId);
  97. let res6 = await wxPayPamars({
  98. order_id: this.orderId
  99. })
  100. console.log('获取微信支付参数返回值--pay', res6);
  101. // uni.showToast({
  102. // title: res6.data,
  103. // icon: 'none',
  104. // duration: 20000
  105. // })
  106. return
  107. wxsdk.wxpay(res6.data, (res) => {
  108. console.log('微信支付返回值-------', res);
  109. if (res.errMsg == "chooseWXPay:ok") {
  110. uni.showToast({
  111. title: '付款成功',
  112. icon: 'success',
  113. duration: 1500
  114. })
  115. // setTimeout(() => {
  116. // uni.navigateTo({
  117. // url: '/pages/index/scorePaySuc/index?id=' + that.couseId
  118. // })
  119. // }, 1500)
  120. } else {
  121. uni.showToast({
  122. title: '支付失败,请稍后重试!',
  123. icon: 'error',
  124. duration: 1500
  125. })
  126. }
  127. });
  128. return
  129. if (res6.code == 1 && res6.msg == 'open_id 不存在,请绑定微信!') {
  130. // if (false) {
  131. uni.showModal({
  132. title: '微信支付',
  133. content: '请先绑定微信再使用微信支付',
  134. confirmColor: '#00CA88',
  135. success: function(res) {
  136. if (res.confirm) {
  137. // wechat.bind();
  138. console.log('点击了确定------');
  139. uni.navigateTo({
  140. url: '/pages/signin/signin'
  141. })
  142. }
  143. },
  144. })
  145. return
  146. } else {
  147. let that = this;
  148. let parmas = res6.data
  149. // let result = await this.prepay();
  150. // let parmas = {
  151. // appId: res6.data.appId,
  152. // mch_id: '',
  153. // nonce_str: res6.data.nonceStr,
  154. // sign: res6.data.paySign,
  155. // body: '',
  156. // out_trade_no: '',
  157. // total_fee: 0.01,
  158. // spbill_create_ip: '',
  159. // notify_url: '',
  160. // trade_type: "JSAPI",
  161. // openid: '',
  162. // scene_info: ''
  163. // }
  164. // uni.showToast({
  165. // title: res6.data.appId
  166. // })
  167. console.log('微信支付参数', res6.data);
  168. // uni.showToast({
  169. // title: res6.data,
  170. // icon: 'success',
  171. // duration: 1500
  172. // })
  173. // setTimeout(() => {
  174. // }, 5000)
  175. }
  176. }
  177. //浏览器微信支付
  178. async wechatWapPay() {
  179. let that = this;
  180. let result = await this.prepay();
  181. if (result.code === 1) {
  182. var url = result.data.pay_data.match(/url\=\'(\S*)\'/);
  183. let reg = new RegExp('&', 'g') //g代表全部
  184. let newUrl = url[1].replace(reg, '&');
  185. let domain = store.getters.initShop.domain; //域名需要https
  186. let params = encodeURIComponent(
  187. `${domain}pages/order/payment/result?orderId=${that.order.id}&type=${that.payment}&orderType=${that.orderType}`
  188. )
  189. // let params = encodeURIComponent(
  190. // `${domain}pages/user/public/paymentdetail?orderId=${that.order.id}&type=${that.payment}&orderType=${that.orderType}`
  191. // )
  192. window.location.href = newUrl + '&redirect_url=' + params;
  193. }
  194. }
  195. // 微信小程序支付
  196. async wxMiniProgramPay() {
  197. let that = this;
  198. let result = await this.prepay();
  199. uni.requestPayment({
  200. provider: 'wxpay',
  201. ...result.data,
  202. success: res => {
  203. console.log(res);
  204. that.payResult('success')
  205. },
  206. fail: err => {
  207. console.log('支付取消或者失败:', err);
  208. err.errMsg !== "requestPayment:fail cancel" && that.payResult('fail')
  209. uni.showModal({
  210. title: '失败',
  211. content: '支付取消或者失败',
  212. confirmText: '重新支付',
  213. confirmColor: '#00CA88',
  214. success: (res) => {
  215. if (res.confirm) {
  216. that.wxMiniProgramPay()
  217. }
  218. }
  219. })
  220. }
  221. });
  222. }
  223. // 余额支付
  224. async walletPay() {
  225. let that = this;
  226. let result = await this.prepay();
  227. result.code === 1 && that.payResult('success')
  228. }
  229. // 支付宝复制链接支付
  230. async copyPayLink() {
  231. let that = this;
  232. let result = await this.prepay();
  233. if (result.code === 1) {
  234. //引入showModal 点击确认 复制链接;
  235. uni.showModal({
  236. title: '支付宝支付',
  237. content: '复制链接到外部浏览器',
  238. confirmText: '复制链接',
  239. confirmColor: '#00CA88',
  240. success: (res) => {
  241. if (res.confirm) {
  242. uni.setClipboardData({
  243. data: result.data.pay_data,
  244. success: function(data) {
  245. that.$u.toast('已复制到剪切板');
  246. }
  247. });
  248. }
  249. }
  250. })
  251. }
  252. }
  253. // 支付链接
  254. async goToPayLink() {
  255. let that = this;
  256. let result = await this.prepay();
  257. if (result.code === 1) {
  258. window.location = result.data.pay_data;
  259. }
  260. }
  261. // 支付宝支付
  262. async aliPay() {
  263. let that = this;
  264. let result = await this.prepay();
  265. if (result.code === 1) {
  266. uni.requestPayment({
  267. provider: 'alipay',
  268. orderInfo: result.data.pay_data, //支付宝订单数据
  269. success: res => {
  270. that.payResult('success')
  271. },
  272. fail: err => {
  273. console.log('支付取消或者失败:', err);
  274. err.errMsg !== "requestPayment:fail cancel" && that.payResult('fail')
  275. }
  276. });
  277. }
  278. }
  279. // 微信支付
  280. async wechatPay() {
  281. let that = this;
  282. let result = await this.prepay();
  283. if (result.code === 1) {
  284. uni.requestPayment({
  285. provider: 'wxpay',
  286. orderInfo: JSON.parse(result.data.pay_data), //微信订单数据(官方说是string。实测为object)
  287. success: res => {
  288. that.payResult('success')
  289. },
  290. fail: err => {
  291. err.errMsg !== "requestPayment:fail cancel" && that.payResult('fail')
  292. console.log('支付取消或者失败:', err);
  293. }
  294. });
  295. }
  296. }
  297. // 支付结果跳转,success:成功,fail:失败
  298. payResult(resultType) {
  299. const that = this;
  300. if (resultType == 'success') {
  301. store.dispatch('getUserInfo')
  302. $Router.replace({
  303. path: '/pages/user/public/paymentdetail',
  304. query: {
  305. orderId: that.order.id,
  306. type: that.payment, //重新支付的时候使用
  307. payState: resultType,
  308. orderType: that.orderType
  309. }
  310. });
  311. store.commit("subscribeMessage", 'unread_msg');
  312. } else {
  313. uni.showToast({
  314. title: '支付失败',
  315. icon: 'none'
  316. })
  317. }
  318. }
  319. }