payUtil.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. import { gotoOrderAppPayApi, gotoOrderH5PayApi, gotoOrderPayApi, payOrderSuccessApi } from '../api/anotherTFInterface';
  2. import { getPayMiniProgramQueryApi } from '../api/anotherTFInterface';
  3. import store from '../store';
  4. import { getUserId, isH5InWebview } from '.';
  5. // #ifdef H5
  6. const jweixin = require('jweixin-module');
  7. /**
  8. * 普通H5处理
  9. * @param payInfo 结算返回的支付信息
  10. */
  11. async function payH5InEquipment(payInfo) {
  12. try {
  13. const res = await gotoOrderH5PayApi(payInfo);
  14. location.replace(res.data.mwebUrl);
  15. } catch (e) {
  16. uni.showToast({ title: '支付失败', icon: 'none' });
  17. if ([1, 2].includes(payInfo.purchaseMode)) {
  18. uni.switchTab({ url: '/pages/order/order' });
  19. } else if ([3, 4, 5].includes(payInfo.purchaseMode)) {
  20. uni.redirectTo({ url: '/user/otherServe/payment-completed/index?state=fail' });
  21. }
  22. } finally {
  23. uni.hideLoading();
  24. }
  25. }
  26. /**
  27. * 微信内H5处理
  28. * @param payInfo 结算返回的支付信息
  29. * @param orderId 订单ID
  30. */
  31. async function payH5InWechat(payInfo) {
  32. const res = await gotoOrderPayApi(payInfo);
  33. jweixin.config({
  34. debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
  35. appId: res.data.appId, // 必填,公众号的唯一标识
  36. timestamp: res.data.timeStamp, // 必填,生成签名的时间戳
  37. nonceStr: res.data.nonceStr, // 必填,生成签名的随机串
  38. signature: res.data.paySign, // 必填,签名,见附录1
  39. jsApiList: ['chooseWXPay'] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
  40. });
  41. if ([1, 2].includes(payInfo.purchaseMode)) {
  42. jweixin.ready(function () {
  43. jweixin.checkJsApi({
  44. jsApiList: ['chooseWXPay'], // 需要检测的JS接口列表,所有JS接口列表见附录2,
  45. success(res) {},
  46. fail(res) {}
  47. });
  48. jweixin.chooseWXPay({
  49. timestamp: res.data.timeStamp, // 支付签名时间戳,注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
  50. nonceStr: res.data.nonceStr, // 支付签名随机串,不长于 32 位
  51. package: res.data.package, // 统一支付接口返回的prepay_id参数值,提交格式如:prepay_id=***)
  52. signType: res.data.signType, // 签名方式,默认为'SHA1',使用新版支付需传入'MD5'
  53. paySign: res.data.paySign, // 支付签名
  54. success(res) {
  55. // 支付成功后的回调函数
  56. uni.showToast({ icon: 'none', title: '支付成功' });
  57. uni.redirectTo({ url: '/another-tf/another-serve/paySuccessful/index?orderId=' + payInfo.orderId });
  58. },
  59. cancel(r) {
  60. uni.showToast({ icon: 'none', title: '用户取消支付' });
  61. uni.switchTab({ url: '/pages/order/order' });
  62. },
  63. fail(res) {
  64. uni.showToast({ icon: 'none', title: '微信内支付错误' });
  65. uni.switchTab({ url: '/pages/order/order' });
  66. }
  67. });
  68. });
  69. jweixin.error(function (res) {
  70. uni.showToast({ icon: 'none', title: '微信内支付加载失败', duration: 3000 });
  71. uni.switchTab({ url: '/pages/order/order' });
  72. });
  73. } else if ([3, 4, 5].includes(payInfo.purchaseMode)) {
  74. jweixin.ready(function () {
  75. jweixin.checkJsApi({
  76. jsApiList: ['chooseWXPay'], // 需要检测的JS接口列表,所有JS接口列表见附录2,
  77. success(res) {},
  78. fail(res) {}
  79. });
  80. jweixin.chooseWXPay({
  81. timestamp: res.data.timeStamp, // 支付签名时间戳,注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
  82. nonceStr: res.data.nonceStr, // 支付签名随机串,不长于 32 位
  83. package: res.data.package, // 统一支付接口返回的prepay_id参数值,提交格式如:prepay_id=***)
  84. signType: res.data.signType, // 签名方式,默认为'SHA1',使用新版支付需传入'MD5'
  85. paySign: res.data.paySign, // 支付签名
  86. success(res) {
  87. // 支付成功后的回调函数
  88. uni.showToast({ icon: 'none', title: '支付成功' });
  89. uni.redirectTo({ url: '/user/otherServe/payment-completed/index' });
  90. },
  91. cancel(r) {
  92. uni.showToast({ icon: 'none', title: '用户取消支付' });
  93. uni.redirectTo({ url: '/user/otherServe/payment-completed/index?state=fail' });
  94. },
  95. fail(res) {
  96. uni.showToast({ icon: 'none', title: '微信内支付错误' });
  97. uni.redirectTo({ url: '/user/otherServe/payment-completed/index?state=fail' });
  98. }
  99. });
  100. });
  101. jweixin.error(function (res) {
  102. uni.showToast({ icon: 'none', title: '微信内支付加载失败', duration: 3000 });
  103. uni.redirectTo({ url: '/user/otherServe/payment-completed/index?state=fail' });
  104. });
  105. }
  106. }
  107. /**
  108. * H5拉起支付
  109. * @param payInfo 结算返回的支付信息
  110. */
  111. async function h5Pay(payInfo) {
  112. const ua = navigator.userAgent.toLowerCase();
  113. if (ua.match(/MicroMessenger/i) == 'micromessenger') {
  114. await payH5InWechat(payInfo);
  115. } else {
  116. await payH5InEquipment(payInfo);
  117. }
  118. }
  119. // #endif
  120. // #ifdef MP-ALIPAY
  121. /**
  122. * 支付宝小程序拉起支付
  123. * @param payInfo 结算返回的支付信息
  124. * @return {Promise<void>}
  125. */
  126. async function mpAliPay(payInfo) {
  127. if ([1, 2].includes(payInfo.purchaseMode)) {
  128. try {
  129. const res = await gotoOrderPayApi(payInfo);
  130. uni.requestPayment({
  131. provider: 'alipay',
  132. orderInfo: res.data.tradeNo,
  133. success(payRes) {
  134. if (payRes.resultCode == '6001') {
  135. uni.showToast({ icon: 'none', title: '取消支付' });
  136. uni.switchTab({ url: '/pages/order/order' });
  137. } else if (payRes.resultCode == '9000') {
  138. uni.showToast({ icon: 'none', title: '支付成功' });
  139. uni.redirectTo({ url: '/another-tf/another-serve/paySuccessful/index?orderId=' + payInfo.orderId });
  140. }
  141. },
  142. fail(err) {
  143. uni.showToast({ icon: 'none', title: '支付取消' });
  144. uni.switchTab({ url: '/pages/order/order' });
  145. }
  146. });
  147. } catch (e) {
  148. uni.showToast({ title: '支付宝支付异常', icon: 'none' });
  149. uni.switchTab({ url: '/pages/order/order' });
  150. } finally {
  151. uni.hideLoading();
  152. }
  153. } else if ([3, 4, 5].includes(payInfo.purchaseMode)) {
  154. try {
  155. const res = await gotoOrderPayApi(payInfo);
  156. uni.requestPayment({
  157. provider: 'alipay',
  158. orderInfo: res.data.tradeNo,
  159. success(payRes) {
  160. if (payRes.resultCode == '6001') {
  161. uni.showToast({ icon: 'none', title: '取消支付' });
  162. uni.redirectTo({ url: '/user/otherServe/payment-completed/index?state=fail' });
  163. } else if (payRes.resultCode == '9000') {
  164. uni.showToast({ icon: 'none', title: '支付成功' });
  165. uni.redirectTo({ url: '/user/otherServe/payment-completed/index' });
  166. }
  167. },
  168. fail(err) {
  169. uni.showToast({ icon: 'none', title: '支付取消' });
  170. uni.redirectTo({ url: '/user/otherServe/payment-completed/index?state=fail' });
  171. }
  172. });
  173. } catch (e) {
  174. uni.showToast({ title: '支付宝支付异常', icon: 'none' });
  175. uni.redirectTo({ url: '/user/otherServe/payment-completed/index?state=fail' });
  176. } finally {
  177. uni.hideLoading();
  178. }
  179. }
  180. }
  181. // #endif
  182. // #ifdef MP-WEIXIN
  183. /**
  184. * 微信小程序拉起支付
  185. * @param payInfo
  186. * @return {Promise<void>}
  187. */
  188. async function mpWechatPay(payInfo) {
  189. if ([1, 2].includes(payInfo.purchaseMode)) {
  190. try {
  191. const res = await gotoOrderPayApi(payInfo);
  192. uni.requestPayment({
  193. provider: 'wxpay',
  194. timeStamp: res.data.timeStamp,
  195. nonceStr: res.data.nonceStr,
  196. package: res.data.package,
  197. signType: res.data.signType,
  198. paySign: res.data.paySign,
  199. success: async (payRes) => {
  200. // 拼团微信支付成功回调
  201. if (payInfo.collageId) await payOrderSuccessApi({ orderId: payInfo.orderId, collageId: payInfo.collageId });
  202. uni.showToast({ icon: 'none', title: '支付成功' });
  203. uni.redirectTo({ url: '/another-tf/another-serve/paySuccessful/index?orderId=' + payInfo.orderId });
  204. },
  205. fail(err) {
  206. uni.showToast({ icon: 'none', title: '用户取消支付' });
  207. uni.switchTab({ url: '/pages/order/order' });
  208. }
  209. });
  210. } catch (e) {
  211. uni.showToast({ title: '微信支付拉起失败', icon: 'none' });
  212. uni.switchTab({ url: '/pages/order/order' });
  213. }
  214. } else if ([3, 4, 5].includes(payInfo.purchaseMode)) {
  215. try {
  216. const res = await gotoOrderPayApi(payInfo);
  217. uni.requestPayment({
  218. provider: 'wxpay',
  219. timeStamp: res.data.timeStamp,
  220. nonceStr: res.data.nonceStr,
  221. package: res.data.package,
  222. signType: res.data.signType,
  223. paySign: res.data.paySign,
  224. success: async (payRes) => {
  225. // 拼团微信支付成功回调
  226. if (payInfo.collageId) await payOrderSuccessApi({ orderId: payInfo.orderId, collageId: payInfo.collageId });
  227. uni.showToast({ icon: 'none', title: '支付成功' });
  228. uni.redirectTo({ url: '/user/otherServe/payment-completed/index' });
  229. },
  230. fail(err) {
  231. uni.showToast({ icon: 'none', title: '用户取消支付' });
  232. uni.redirectTo({ url: '/user/otherServe/payment-completed/index?state=fail' });
  233. }
  234. });
  235. } catch (e) {
  236. uni.showToast({ title: '微信支付拉起失败', icon: 'none' });
  237. uni.redirectTo({ url: '/user/otherServe/payment-completed/index?state=fail' });
  238. }
  239. }
  240. }
  241. // #endif
  242. // #ifdef APP
  243. /**
  244. * App拉起微信支付
  245. * @param payInfo
  246. * @return {Promise<void>}
  247. */
  248. async function appWechatPay(payInfo) {
  249. if ([1, 2].includes(payInfo.purchaseMode)) {
  250. try {
  251. const res = await gotoOrderAppPayApi(payInfo);
  252. const obj = {
  253. appid: res.data.appId,
  254. noncestr: res.data.nonceStr,
  255. package: 'Sign=WXPay',
  256. prepayid: res.data.prepayId,
  257. timestamp: res.data.timeStamp,
  258. sign: res.data.paySign,
  259. partnerid: res.data.partnerId
  260. };
  261. uni.requestPayment({
  262. provider: 'wxpay',
  263. orderInfo: obj,
  264. success(payRes) {
  265. uni.showToast({ icon: 'none', title: '支付成功' });
  266. uni.redirectTo({ url: '/another-tf/another-serve/paySuccessful/index?orderId=' + payInfo.orderId });
  267. },
  268. fail(err) {
  269. uni.showToast({ icon: 'none', title: '用户取消支付' });
  270. uni.switchTab({ url: '/pages/order/order' });
  271. }
  272. });
  273. } catch (e) {
  274. uni.showToast({ title: 'APP拉起微信支付失败', icon: 'none' });
  275. uni.switchTab({ url: '/pages/order/order' });
  276. } finally {
  277. uni.hideLoading();
  278. }
  279. } else if ([3, 4, 5].includes(payInfo.purchaseMode)) {
  280. try {
  281. const res = await gotoOrderAppPayApi(payInfo);
  282. const obj = {
  283. appid: res.data.appId,
  284. noncestr: res.data.nonceStr,
  285. package: 'Sign=WXPay',
  286. prepayid: res.data.prepayId,
  287. timestamp: res.data.timeStamp,
  288. sign: res.data.paySign,
  289. partnerid: res.data.partnerId
  290. };
  291. uni.requestPayment({
  292. provider: 'wxpay',
  293. orderInfo: obj,
  294. success(payRes) {
  295. uni.showToast({ icon: 'none', title: '支付成功' });
  296. uni.redirectTo({ url: '/user/otherServe/payment-completed/index' });
  297. },
  298. fail(err) {
  299. uni.showToast({ icon: 'none', title: '用户取消支付' });
  300. uni.redirectTo({ url: '/user/otherServe/payment-completed/index?state=fail' });
  301. }
  302. });
  303. } catch (e) {
  304. uni.showToast({ title: 'APP拉起微信支付失败', icon: 'none' });
  305. uni.redirectTo({ url: '/user/otherServe/payment-completed/index?state=fail' });
  306. } finally {
  307. uni.hideLoading();
  308. }
  309. }
  310. }
  311. // #endif
  312. /**
  313. * 银行卡支付
  314. * @param payInfo
  315. * @return {Promise<void>}
  316. */
  317. async function bankCardPay(payInfo) {
  318. try {
  319. let res;
  320. if ([1, 2].includes(payInfo.purchaseMode)) {
  321. } else if (payInfo.purchaseMode === 3) {
  322. // res = await payGotoH5SettlePayApi(payInfo)
  323. // uni.redirectTo({ url: '/user/otherServe/payment-completed/index' })
  324. } else if (payInfo.purchaseMode === 4) {
  325. // res = await payGotoH5VoucherApi(payInfo)
  326. // uni.redirectTo({ url: '/user/otherServe/payment-completed/index' })
  327. }
  328. } catch (e) {
  329. if ([1, 2].includes(payInfo.purchaseMode)) {
  330. } else if ([3, 4, 5].includes(payInfo.purchaseMode)) {
  331. uni.showToast({ title: '银行卡支付失败', icon: 'none' });
  332. uni.redirectTo({ url: '/user/otherServe/payment-completed/index?state=fail' });
  333. }
  334. } finally {
  335. uni.hideLoading();
  336. }
  337. }
  338. /**
  339. * 通联H5处理
  340. * @param payInfo 结算返回的支付信息
  341. */
  342. async function h5TonglianPay(payInfo) {
  343. try {
  344. const res = await gotoOrderH5PayApi(payInfo);
  345. console.log(res);
  346. if (!res.data.package) {
  347. // 零元支付情况
  348. uni.redirectTo({ url: '/user/otherServe/payment-completed/index' });
  349. } else {
  350. const payData = JSON.parse(res.data.package);
  351. const form = document.createElement('form');
  352. form.setAttribute('action', res.data.mwebUrl);
  353. form.setAttribute('method', 'POST');
  354. let input;
  355. for (const key in payData) {
  356. input = document.createElement('input');
  357. input.name = key;
  358. input.value = payData[key];
  359. form.appendChild(input);
  360. }
  361. document.body.appendChild(form);
  362. form.submit();
  363. document.body.removeChild(form);
  364. }
  365. } catch (e) {
  366. console.log(e);
  367. uni.showToast({ title: '支付失败', icon: 'none' });
  368. if ([1, 2].includes(payInfo.purchaseMode)) {
  369. uni.switchTab({ url: '/pages/order/order' });
  370. } else if ([3, 4, 5].includes(payInfo.purchaseMode)) {
  371. uni.redirectTo({ url: '/user/otherServe/payment-completed/index?state=fail' });
  372. }
  373. } finally {
  374. uni.hideLoading();
  375. }
  376. }
  377. /**
  378. * 处理支付
  379. * @param submitResult 结算结果
  380. */
  381. export async function handleDoPay(submitResult, purchaseMode) {
  382. uni.showLoading({
  383. mask: true,
  384. title: '支付中...'
  385. });
  386. if (purchaseMode) { // 1-商城 2-本地 3-入驻 4-代金券 5-地图红包
  387. const submitInfo = { ...submitResult, purchaseMode };
  388. if (submitInfo.paymentMode === 999) {
  389. await bankCardPay(submitInfo);
  390. } else if (submitInfo.paymentMode === 1) {
  391. // 微信支付
  392. // #ifdef APP
  393. await appWechatPay(submitInfo);
  394. // #endif
  395. // #ifdef MP-WEIXIN
  396. await mpWechatPay(submitInfo);
  397. // #endif
  398. // #ifdef H5
  399. await h5Pay(submitInfo);
  400. // #endif
  401. } else if ([2, 3].includes(submitInfo.paymentMode)) {
  402. // 支付宝
  403. // #ifdef MP-ALIPAY
  404. await mpAliPay(submitInfo);
  405. // await appWechatPay(submitResult,this.orderId)
  406. throw new Error('支付宝相关支付暂时只支持支付宝小程序');
  407. // #endif
  408. } else if (submitInfo.paymentMode === 4) {
  409. // #ifdef H5
  410. if (store.state.app.isInMiniProgram || isH5InWebview()) {
  411. const payAppesult = await getPayMiniProgramQueryApi({
  412. orderNo: submitInfo.orderSn,
  413. purchaseMode,
  414. paymentMode: submitInfo.paymentMode
  415. });
  416. let query = '';
  417. for (const key in payAppesult.data) {
  418. query += key + '=' + payAppesult.data[key] + '&';
  419. }
  420. wx.miniProgram.navigateTo({
  421. url: '/pages/loading/loading?' + query + 'orderNo=' + submitInfo.orderSn + '&userId=' + getUserId()
  422. });
  423. } else {
  424. await h5TonglianPay(submitInfo);
  425. }
  426. // #endif
  427. } else if ([5, 6].includes(submitInfo.paymentMode)) {
  428. await h5TonglianPay(submitInfo);
  429. }
  430. }
  431. }