payUtil.js 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970
  1. // import { ANOTHER_TF_INTERFACE } from '../config'
  2. // import { T_PAY_ORDER, T_STORAGE_KEY } from '../constant'
  3. // import { gotoOrderAppPayApi, gotoOrderH5PayApi, getSessionKeyAppApi, gotoOrderPayApi, payOrderSuccessApi, getPayMiniProgramQueryApi } from '../api/anotherTFInterface'
  4. // import store from '../store'
  5. // import { getUserId, isInWx, jumpToOtherProject } from './index'
  6. const T_PAY_ORDER = 'T_PAY_ORDER'
  7. import { gotoOrderH5PayApi } from '@/api/table'
  8. const isInWx = () => {
  9. // #ifdef H5
  10. var ua = navigator.userAgent.toLowerCase()
  11. if (ua.match(/MicroMessenger/i) == 'micromessenger') return true
  12. // #endif
  13. return false
  14. }
  15. // #ifdef H5
  16. // const jweixin = require('jweixin-module')
  17. /**
  18. * 普通H5处理
  19. * @param payInfo 结算返回的支付信息
  20. */
  21. async function payH5InEquipment(payInfo) {
  22. try {
  23. const res = await gotoOrderH5PayApi(payInfo)
  24. location.replace(res.data.mwebUrl)
  25. } catch (e) {
  26. uni.showToast({ title: '支付失败', icon: 'none' })
  27. setTimeout(() => {
  28. if ([1, 2].includes(payInfo.purchaseMode)) {
  29. uni.switchTab({ url: '/pages/order/order' })
  30. } else if ([3, 4, 5].includes(payInfo.purchaseMode)) {
  31. uni.redirectTo({ url: '/user/otherServe/payment-completed/index?state=fail' })
  32. }
  33. }, 2000)
  34. } finally {
  35. uni.hideLoading()
  36. }
  37. }
  38. /**
  39. * 微信内H5处理
  40. * @param payInfo 结算返回的支付信息
  41. * @param orderId 订单ID
  42. */
  43. async function payH5InWechat(payInfo) {
  44. const res = await gotoOrderPayApi(payInfo)
  45. jweixin.config({
  46. debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
  47. appId: res.data.appId, // 必填,公众号的唯一标识
  48. timestamp: res.data.timeStamp, // 必填,生成签名的时间戳
  49. nonceStr: res.data.nonceStr, // 必填,生成签名的随机串
  50. signature: res.data.paySign, // 必填,签名,见附录1
  51. jsApiList: [ 'chooseWXPay' ] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
  52. })
  53. if ([1, 2].includes(payInfo.purchaseMode)) {
  54. jweixin.ready(function () {
  55. jweixin.checkJsApi({
  56. jsApiList: [ 'chooseWXPay' ], // 需要检测的JS接口列表,所有JS接口列表见附录2,
  57. success(res) {},
  58. fail(res) {}
  59. })
  60. jweixin.chooseWXPay({
  61. timestamp: res.data.timeStamp, // 支付签名时间戳,注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
  62. nonceStr: res.data.nonceStr, // 支付签名随机串,不长于 32 位
  63. package: res.data.package, // 统一支付接口返回的prepay_id参数值,提交格式如:prepay_id=***)
  64. signType: res.data.signType, // 签名方式,默认为'SHA1',使用新版支付需传入'MD5'
  65. paySign: res.data.paySign, // 支付签名
  66. success(res) {
  67. // 支付成功后的回调函数
  68. uni.showToast({ icon: 'none', title: '支付成功' })
  69. setTimeout(() => { uni.redirectTo({ url: '/another-tf/another-serve/paySuccessful/index?orderId=' + payInfo.orderId }) }, 2000)
  70. },
  71. cancel(r) {
  72. uni.showToast({ icon: 'none', title: '用户取消支付' })
  73. setTimeout(() => { uni.switchTab({ url: '/pages/order/order' }) }, 2000)
  74. },
  75. fail(res) {
  76. uni.showToast({ icon: 'none', title: '微信内支付错误' })
  77. setTimeout(() => { uni.switchTab({ url: '/pages/order/order' }) }, 2000)
  78. }
  79. })
  80. })
  81. jweixin.error(function (res) {
  82. uni.showToast({ icon: 'none', title: '微信内支付加载失败', duration: 3000 })
  83. setTimeout(() => { uni.switchTab({ url: '/pages/order/order' }) }, 2000)
  84. })
  85. } else if ([3, 4, 5].includes(payInfo.purchaseMode)) {
  86. jweixin.ready(function () {
  87. jweixin.checkJsApi({
  88. jsApiList: [ 'chooseWXPay' ], // 需要检测的JS接口列表,所有JS接口列表见附录2,
  89. success(res) {},
  90. fail(res) {}
  91. })
  92. jweixin.chooseWXPay({
  93. timestamp: res.data.timeStamp, // 支付签名时间戳,注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
  94. nonceStr: res.data.nonceStr, // 支付签名随机串,不长于 32 位
  95. package: res.data.package, // 统一支付接口返回的prepay_id参数值,提交格式如:prepay_id=***)
  96. signType: res.data.signType, // 签名方式,默认为'SHA1',使用新版支付需传入'MD5'
  97. paySign: res.data.paySign, // 支付签名
  98. success(res) {
  99. // 支付成功后的回调函数
  100. uni.showToast({ icon: 'none', title: '支付成功' })
  101. setTimeout(() => { uni.redirectTo({ url: '/user/otherServe/payment-completed/index' }) }, 2000)
  102. },
  103. cancel(r) {
  104. uni.showToast({ icon: 'none', title: '用户取消支付' })
  105. setTimeout(() => { uni.redirectTo({ url: '/user/otherServe/payment-completed/index?state=fail' }) }, 2000)
  106. },
  107. fail(res) {
  108. uni.showToast({ icon: 'none', title: '微信内支付错误' })
  109. setTimeout(() => { uni.redirectTo({ url: '/user/otherServe/payment-completed/index?state=fail' }) }, 2000)
  110. }
  111. })
  112. })
  113. jweixin.error(function (res) {
  114. uni.showToast({ icon: 'none', title: '微信内支付加载失败', duration: 3000 })
  115. setTimeout(() => { uni.redirectTo({ url: '/user/otherServe/payment-completed/index?state=fail' }) }, 2000)
  116. })
  117. }
  118. }
  119. // #endif
  120. /**
  121. * 支付宝小程序拉起支付
  122. * @param payInfo 结算返回的支付信息
  123. * @return {Promise<void>}
  124. */
  125. async function zhiAliPay(payInfo) {
  126. if ([1, 2].includes(payInfo.purchaseMode)) {
  127. try {
  128. const res = await gotoOrderPayApi(payInfo)
  129. uni.requestPayment({
  130. provider: 'alipay',
  131. orderInfo: res.data.tradeNo,
  132. success(payRes) {
  133. if (payRes.resultCode == '6001') {
  134. uni.showToast({ icon: 'none', title: '取消支付' })
  135. setTimeout(() => { uni.switchTab({ url: '/pages/order/order' }) }, 2000)
  136. } else if (payRes.resultCode == '9000') {
  137. uni.showToast({ icon: 'none', title: '支付成功' })
  138. setTimeout(() => { uni.redirectTo({ url: '/another-tf/another-serve/paySuccessful/index?orderId=' + payInfo.orderId }) }, 2000)
  139. }
  140. },
  141. fail(err) {
  142. uni.showToast({ icon: 'none', title: '支付取消' })
  143. setTimeout(() => { uni.switchTab({ url: '/pages/order/order' }) }, 2000)
  144. }
  145. })
  146. } catch (e) {
  147. uni.showToast({ title: '支付宝支付异常', icon: 'none' })
  148. setTimeout(() => { uni.switchTab({ url: '/pages/order/order' }) }, 2000)
  149. } finally {
  150. uni.hideLoading()
  151. }
  152. } else if ([3, 4, 5].includes(payInfo.purchaseMode)) {
  153. try {
  154. const res = await gotoOrderPayApi(payInfo)
  155. uni.requestPayment({
  156. provider: 'alipay',
  157. orderInfo: res.data.tradeNo,
  158. success(payRes) {
  159. if (payRes.resultCode == '6001') {
  160. uni.showToast({ icon: 'none', title: '取消支付' })
  161. setTimeout(() => { uni.redirectTo({ url: '/user/otherServe/payment-completed/index?state=fail' }) }, 2000)
  162. } else if (payRes.resultCode == '9000') {
  163. uni.showToast({ icon: 'none', title: '支付成功' })
  164. setTimeout(() => { uni.redirectTo({ url: '/user/otherServe/payment-completed/index' }) }, 2000)
  165. }
  166. },
  167. fail(err) {
  168. uni.showToast({ icon: 'none', title: '支付取消' })
  169. setTimeout(() => { uni.redirectTo({ url: '/user/otherServe/payment-completed/index?state=fail' }) }, 2000)
  170. }
  171. })
  172. } catch (e) {
  173. uni.showToast({ title: '支付宝支付异常', icon: 'none' })
  174. setTimeout(() => { uni.redirectTo({ url: '/user/otherServe/payment-completed/index?state=fail' }) }, 2000)
  175. } finally {
  176. uni.hideLoading()
  177. }
  178. }
  179. }
  180. /**
  181. * 微信小程序拉起支付
  182. * @param payInfo
  183. * @return {Promise<void>}
  184. */
  185. async function mpWechatPay(payInfo) {
  186. if ([1, 2].includes(payInfo.purchaseMode)) {
  187. try {
  188. const res = await gotoOrderPayApi(payInfo)
  189. uni.requestPayment({
  190. provider: 'wxpay',
  191. timeStamp: res.data.timeStamp,
  192. nonceStr: res.data.nonceStr,
  193. package: res.data.package,
  194. signType: res.data.signType,
  195. paySign: res.data.paySign,
  196. success: async (payRes) => {
  197. // 拼团微信支付成功回调
  198. if (payInfo.collageId) await payOrderSuccessApi({ orderId: payInfo.orderId, collageId: payInfo.collageId })
  199. uni.showToast({ icon: 'none', title: '支付成功' })
  200. setTimeout(() => { uni.redirectTo({ url: '/another-tf/another-serve/paySuccessful/index?orderId=' + payInfo.orderId }) }, 2000)
  201. },
  202. fail(err) {
  203. uni.showToast({ icon: 'none', title: '用户取消支付' })
  204. setTimeout(() => { uni.switchTab({ url: '/pages/order/order' }) }, 2000)
  205. }
  206. })
  207. } catch (e) {
  208. uni.showToast({ title: '微信支付拉起失败', icon: 'none' })
  209. setTimeout(() => { uni.switchTab({ url: '/pages/order/order' }) }, 2000)
  210. }
  211. } else if ([3, 4, 5].includes(payInfo.purchaseMode)) {
  212. try {
  213. const res = await gotoOrderPayApi(payInfo)
  214. uni.requestPayment({
  215. provider: 'wxpay',
  216. timeStamp: res.data.timeStamp,
  217. nonceStr: res.data.nonceStr,
  218. package: res.data.package,
  219. signType: res.data.signType,
  220. paySign: res.data.paySign,
  221. success: async (payRes) => {
  222. // 拼团微信支付成功回调
  223. if (payInfo.collageId) await payOrderSuccessApi({ orderId: payInfo.orderId, collageId: payInfo.collageId })
  224. uni.showToast({ icon: 'none', title: '支付成功' })
  225. setTimeout(() => { uni.redirectTo({ url: '/user/otherServe/payment-completed/index' }) }, 2000)
  226. },
  227. fail(err) {
  228. uni.showToast({ icon: 'none', title: '用户取消支付' })
  229. setTimeout(() => { uni.redirectTo({ url: '/user/otherServe/payment-completed/index?state=fail' }) }, 2000)
  230. }
  231. })
  232. } catch (e) {
  233. uni.showToast({ title: '微信支付拉起失败', icon: 'none' })
  234. setTimeout(() => { uni.redirectTo({ url: '/user/otherServe/payment-completed/index?state=fail' }) }, 2000)
  235. }
  236. }
  237. }
  238. /**
  239. * App拉起微信支付
  240. * @param payInfo
  241. * @return {Promise<void>}
  242. */
  243. async function appWechatPay(payInfo) {
  244. if ([1, 2].includes(payInfo.purchaseMode)) {
  245. try {
  246. const res = await gotoOrderAppPayApi(payInfo)
  247. const obj = {
  248. appid: res.data.appId,
  249. noncestr: res.data.nonceStr,
  250. package: 'Sign=WXPay',
  251. prepayid: res.data.prepayId,
  252. timestamp: res.data.timeStamp,
  253. sign: res.data.paySign,
  254. partnerid: res.data.partnerId
  255. }
  256. uni.requestPayment({
  257. provider: 'wxpay',
  258. orderInfo: obj,
  259. success(payRes) {
  260. uni.showToast({ icon: 'none', title: '支付成功' })
  261. setTimeout(() => { uni.redirectTo({ url: '/another-tf/another-serve/paySuccessful/index?orderId=' + payInfo.orderId }) }, 2000)
  262. },
  263. fail(err) {
  264. uni.showToast({ icon: 'none', title: '用户取消支付' })
  265. setTimeout(() => { uni.switchTab({ url: '/pages/order/order' }) }, 2000)
  266. }
  267. })
  268. } catch (e) {
  269. uni.showToast({ title: 'APP拉起微信支付失败', icon: 'none' })
  270. setTimeout(() => { uni.switchTab({ url: '/pages/order/order' }) }, 2000)
  271. } finally {
  272. uni.hideLoading()
  273. }
  274. } else if ([3, 4, 5].includes(payInfo.purchaseMode)) {
  275. try {
  276. const res = await gotoOrderAppPayApi(payInfo)
  277. const obj = {
  278. appid: res.data.appId,
  279. noncestr: res.data.nonceStr,
  280. package: 'Sign=WXPay',
  281. prepayid: res.data.prepayId,
  282. timestamp: res.data.timeStamp,
  283. sign: res.data.paySign,
  284. partnerid: res.data.partnerId
  285. }
  286. uni.requestPayment({
  287. provider: 'wxpay',
  288. orderInfo: obj,
  289. success(payRes) {
  290. uni.showToast({ icon: 'none', title: '支付成功' })
  291. setTimeout(() => { uni.redirectTo({ url: '/user/otherServe/payment-completed/index' }) }, 2000)
  292. },
  293. fail(err) {
  294. uni.showToast({ icon: 'none', title: '用户取消支付' })
  295. uni.redirectTo({ url: '/user/otherServe/payment-completed/index?state=fail' })
  296. }
  297. })
  298. } catch (e) {
  299. uni.showToast({ title: 'APP拉起微信支付失败', icon: 'none' })
  300. setTimeout(() => { uni.redirectTo({ url: '/user/otherServe/payment-completed/index?state=fail' }) }, 2000)
  301. } finally {
  302. uni.hideLoading()
  303. }
  304. }
  305. }
  306. /**
  307. * 银行卡支付处理
  308. * @param data 结算返回的支付信息
  309. */
  310. async function bankCardPay(data, payType, type, otherArgs) {
  311. try {
  312. let res
  313. if ([1, 2].includes(payType)) {
  314. } else if (payType === 3) {
  315. // res = await payGotoH5SettlePayApi({ ...data, purchaseMode: payType })
  316. // uni.redirectTo({ url: '/user/otherServe/payment-completed/index' })
  317. } else if (payType === 4) {
  318. // res = await payGotoH5VoucherApi({ ...data, purchaseMode: payType })
  319. // uni.redirectTo({ url: '/user/otherServe/payment-completed/index' })
  320. }
  321. } catch (e) {
  322. uni.showToast({ title: '银行卡支付失败', icon: 'none' })
  323. setTimeout(() => {
  324. if ([1, 2].includes(payType)) {
  325. } else if ([3, 4, 5].includes(payType)) {
  326. uni.redirectTo({ url: '/user/otherServe/payment-completed/index?state=fail' })
  327. }
  328. }, 2000)
  329. } finally {
  330. uni.hideLoading()
  331. }
  332. }
  333. /**
  334. * 通联支付H5处理
  335. * @param data 结算返回的支付信息
  336. */
  337. async function h5TonglianPay(data, payType, type, otherArgs) {
  338. if (isInWx()) {
  339. await gotoOrderH5PayApi({
  340. ...data,
  341. purchaseMode: payType,
  342. ...otherArgs
  343. }).then((res) => {
  344. console.log(JSON.stringify(res.data))
  345. if (type) {
  346. uni.removeStorageSync(T_PAY_ORDER)
  347. uni.setStorageSync(T_PAY_ORDER, {
  348. type,
  349. TL_ORDER_NO: data.orderSn
  350. })
  351. }
  352. if (!res.data.package) { // 零元支付情况
  353. uni.redirectTo({ url: '/user/otherServe/payment-completed/index' })
  354. } else {
  355. const payData = JSON.parse(res.data.package)
  356. const form = document.createElement('form')
  357. form.setAttribute('action', res.data.mwebUrl)
  358. form.setAttribute('method', 'POST')
  359. let input
  360. for (const key in payData) {
  361. input = document.createElement('input')
  362. input.name = key
  363. input.value = payData[key]
  364. form.appendChild(input)
  365. }
  366. document.body.appendChild(form)
  367. form.submit()
  368. document.body.removeChild(form)
  369. }
  370. })
  371. .catch((e) => {
  372. console.log(e)
  373. uni.showToast({ title: '支付失败', icon: 'none' })
  374. setTimeout(() => {
  375. if ([1, 2].includes(payType)) {
  376. uni.switchTab({ url: '/pages/order/order' })
  377. } else if ([3, 4, 5].includes(payType)) {
  378. uni.redirectTo({ url: '/user/otherServe/payment-completed/index?state=fail' })
  379. }
  380. }, 2000)
  381. })
  382. .finally((e) => {
  383. uni.hideLoading()
  384. })
  385. } else {
  386. await getPayMiniProgramQueryApi({
  387. orderFormid: data.orderFormid, // 无此传参
  388. orderNo: data.orderSn || data.orderFormid,
  389. purchaseMode: payType,
  390. paymentMode: data.paymentMode,
  391. ...otherArgs
  392. }).then((res) => {
  393. console.log(JSON.stringify(res.data))
  394. if (res.code == 200) {
  395. if (type) {
  396. uni.removeStorageSync(T_PAY_ORDER)
  397. uni.setStorageSync(T_PAY_ORDER, {
  398. type,
  399. TL_ORDER_NO: data.orderSn
  400. })
  401. }
  402. if (res.data.isZeroOrder === '1') { // 零元支付情况
  403. uni.redirectTo({ url: '/user/otherServe/payment-completed/index' })
  404. } else {
  405. delete res.data.isZeroOrder
  406. let query = ''
  407. for (const key in res.data) {
  408. query += key + '=' + res.data[key] + '&'
  409. }
  410. location.href = `weixin://dl/business/?appid=wx3cef6c7325c38a45&path=pages/loading/loading&query=${query}orderNo=${data.orderSn}&userId=${getUserId()}`
  411. }
  412. }
  413. })
  414. .finally((e) => {
  415. uni.hideLoading()
  416. })
  417. }
  418. }
  419. /**
  420. * 通联支付webview套壳处理
  421. * @param data 结算返回的支付信息
  422. */
  423. async function wvTonglianPay(data, payType, type, otherArgs) {
  424. await getPayMiniProgramQueryApi({
  425. orderFormid: data.orderFormid, // 无此传参
  426. orderNo: data.orderSn || data.orderFormid,
  427. purchaseMode: payType,
  428. paymentMode: data.paymentMode,
  429. ...otherArgs
  430. }).then((res) => {
  431. console.log(JSON.stringify(res.data))
  432. if (res.code == 200) {
  433. if (type) {
  434. uni.removeStorageSync(T_PAY_ORDER)
  435. uni.setStorageSync(T_PAY_ORDER, {
  436. type,
  437. TL_ORDER_NO: data.orderSn
  438. })
  439. }
  440. if (res.data.isZeroOrder === '1') { // 零元支付情况
  441. uni.redirectTo({ url: '/user/otherServe/payment-completed/index' })
  442. } else {
  443. delete res.data.isZeroOrder
  444. let query = ''
  445. for (const key in res.data) {
  446. query += key + '=' + res.data[key] + '&'
  447. }
  448. wx.miniProgram.navigateTo({
  449. url: '/pages/loading/loading?' + query + 'orderNo=' + data.orderSn + '&userId=' + getUserId(),
  450. fail: () => {
  451. uni.switchTab({
  452. url: '/pages/order/order'
  453. })
  454. }
  455. })
  456. }
  457. }
  458. })
  459. .finally((e) => {
  460. uni.hideLoading()
  461. })
  462. }
  463. /**
  464. * 通联支付APP处理
  465. * @param data 结算返回的支付信息
  466. */
  467. async function appTonglianPay(data, payType, type, otherArgs) {
  468. await getPayMiniProgramQueryApi({
  469. orderFormid: data.orderFormid, // 无此传参
  470. orderNo: data.orderSn || data.orderFormid,
  471. purchaseMode: payType,
  472. paymentMode: data.paymentMode,
  473. ...otherArgs
  474. }).then((res) => {
  475. if (res.code == 200) {
  476. if (type) {
  477. uni.removeStorageSync(T_PAY_ORDER)
  478. uni.setStorageSync(T_PAY_ORDER, {
  479. type,
  480. TL_ORDER_NO: data.orderSn
  481. })
  482. }
  483. if (res.data.isZeroOrder === '1') { // 零元支付情况
  484. uni.redirectTo({ url: '/user/otherServe/payment-completed/index' })
  485. } else {
  486. delete res.data.isZeroOrder
  487. let query = ''
  488. for (const key in res.data) {
  489. query += key + '=' + res.data[key] + '&'
  490. }
  491. plus.share.getServices(function (result) {
  492. let sweixin = null
  493. for (const i in result) {
  494. if (result[i].id == 'weixin') {
  495. sweixin = result[i]
  496. }
  497. }
  498. if (sweixin) {
  499. sweixin.launchMiniProgram({
  500. id: 'gh_e64a1a89a0ad', // 微信小程序的原始ID("g_"开头的字符串)
  501. type: 0,
  502. path: 'pages/orderDetail/orderDetail?' + query
  503. })
  504. } else {
  505. uni.showToast({ title: '请先安装微信', icon: 'none' })
  506. }
  507. }, function (e) {
  508. uni.showToast({ title: '获取分享服务列表失败', icon: 'none' })
  509. console.log('获取分享服务列表失败:' + e.message)
  510. })
  511. }
  512. }
  513. })
  514. .finally((e) => {
  515. uni.hideLoading()
  516. })
  517. }
  518. /**
  519. * 惠市宝支付H5处理
  520. * @param data 结算返回的支付信息
  521. */
  522. async function h5HuiShiBaoPay(data, payType, type, otherArgs) {
  523. await gotoOrderH5PayApi({
  524. ...data,
  525. purchaseMode: payType,
  526. ...otherArgs
  527. }).then((res) => {
  528. console.log(JSON.stringify(res.data))
  529. if (type) {
  530. uni.removeStorageSync(T_PAY_ORDER)
  531. uni.setStorageSync(T_PAY_ORDER, {
  532. type,
  533. TL_ORDER_NO: data.orderSn
  534. })
  535. }
  536. if (!res.data.Cshdk_Url) {
  537. uni.redirectTo({ url: '/user/otherServe/payment-completed/index?state=fail' })
  538. } else {
  539. const payData = res.data
  540. const form = document.createElement('form')
  541. form.setAttribute('action', res.data.Cshdk_Url)
  542. form.setAttribute('method', 'POST')
  543. let input
  544. for (const key in payData) {
  545. input = document.createElement('input')
  546. input.name = key
  547. input.value = payData[key]
  548. form.appendChild(input)
  549. }
  550. document.body.appendChild(form)
  551. form.submit()
  552. document.body.removeChild(form)
  553. }
  554. })
  555. .catch((e) => {
  556. console.log(e)
  557. uni.showToast({ title: '支付失败', icon: 'none' })
  558. setTimeout(() => {
  559. if ([1, 2].includes(payType)) {
  560. uni.switchTab({ url: '/pages/order/order' })
  561. } else if ([3, 4, 5].includes(payType)) {
  562. uni.redirectTo({ url: '/user/otherServe/payment-completed/index?state=fail' })
  563. }
  564. }, 2000)
  565. })
  566. .finally((e) => {
  567. uni.hideLoading()
  568. })
  569. }
  570. /**
  571. * 惠市宝支付webview套壳处理
  572. * @param data 结算返回的支付信息
  573. */
  574. async function wvHuiShiBaoPay(data, payType, type, otherArgs) {
  575. // 方式一:
  576. if (typeof otherArgs.isSuccess === 'number') { // isSuccess、payType、orderId、collageId
  577. if (otherArgs.isSuccess) {
  578. if ([1, 2, 3, 4, 5].includes(payType)) {
  579. if (data.collageId) await payOrderSuccessApi({ orderId: data.orderId, collageId: data.collageId })
  580. uni.redirectTo({ url: '/user/otherServe/payment-completed/index' })
  581. }
  582. } else if ([1, 2, 3, 4, 5].includes(payType)) {
  583. uni.redirectTo({ url: '/user/otherServe/payment-completed/index?state=fail' })
  584. }
  585. } else {
  586. if (type) {
  587. uni.removeStorageSync(T_PAY_ORDER)
  588. uni.setStorageSync(T_PAY_ORDER, {
  589. type,
  590. TL_ORDER_NO: data.orderSn
  591. })
  592. }
  593. // 尝试一
  594. // uni.postMessage({
  595. // data: {
  596. // event: 'wvHuiShiBaoPay',
  597. // data,
  598. // payType,
  599. // type,
  600. // otherArgs,
  601. // jumpType: `wvHuiShiBaoPayTurn`
  602. // }
  603. // })
  604. // 尝试二
  605. // var popup = window.open(...popup)
  606. // popup.postMessage(
  607. // "The user is 'bob' and the password is 'secret'",
  608. // 'https://www.tuanfengkeji.cn'
  609. // )
  610. // popup.postMessage('hello there!', 'https://test.tuanfengkeji.cn')
  611. // function receiveMessage(event) {
  612. // if (event.origin !== 'https://test.tuanfengkeji.cn') return
  613. // }
  614. // window.addEventListener('message', receiveMessage, false)
  615. // 尝试三
  616. // parent.postMessage('aaaaa', '*')
  617. // 尝试四
  618. const res = uni.getStorageSync(T_STORAGE_KEY) || {}
  619. if (res.token) res.Authorization = res.token
  620. if (res.ssoUserInfo && res.ssoUserInfo.token) res['satoken-user'] = res.ssoUserInfo.token
  621. jumpToOtherProject({
  622. isInMiniProgram: store.state.app.isInMiniProgram,
  623. id: 'gh_c5b32d0f9bc9',
  624. appId: 'wx3cef6c7325c38a45',
  625. url: `pages/skip/skip`,
  626. programUrl: `pages/skip/skip`,
  627. toType: 'MP',
  628. query: `?type=wvHuiShiBaoPay&data=${encodeURIComponent(JSON.stringify({ data, payType, type, otherArgs, jumpType: `wvHuiShiBaoPayTurn`, Authorization: res.Authorization || '', satokenUser: res['satoken-user'] || '', baseUrl: ANOTHER_TF_INTERFACE }))}`,
  629. montageTerminal: [6, 5, 1, 2, 4]
  630. })
  631. uni.hideLoading()
  632. }
  633. // 方式二:
  634. // // , otherArgs = { stage: 'one' }
  635. // if (otherArgs.satge === 'one') {
  636. // delete otherArgs.satge
  637. // uni.postMessage({
  638. // data: {
  639. // event: 'wvHuiShiBaoPay',
  640. // data,
  641. // payType,
  642. // type,
  643. // otherArgs: { ...otherArgs, stage: 'two' },
  644. // jumpType: `wvHuiShiBaoPayTurn`
  645. // }
  646. // })
  647. // } else if (otherArgs.satge === 'two') {
  648. // // 考虑到跳转webview的该链接还需要让网页的isInMiniProgram重新赋值(或者在链接上的传参非常多),比较麻烦且可能会产生其它问题
  649. // delete otherArgs.satge
  650. // const code = otherArgs.code
  651. // const result = await getSessionKeyAppApi({ code })
  652. // await gotoOrderH5PayApi({
  653. // ...data,
  654. // purchaseMode: payType,
  655. // hsbSubPayType: '05',
  656. // extJsonStr: JSON.stringify({
  657. // 'Sub_Appid': 'wx3cef6c7325c38a45', // 小程序appId
  658. // 'Sub_Openid': result.data.wechatOpenId // 微信用户openId
  659. // }),
  660. // ...otherArgs
  661. // }).then((res) => {
  662. // console.log(JSON.stringify(res.data))
  663. // if (type) {
  664. // uni.removeStorageSync(T_PAY_ORDER)
  665. // uni.setStorageSync(T_PAY_ORDER, {
  666. // type,
  667. // TL_ORDER_NO: data.orderSn
  668. // })
  669. // }
  670. // if (!res.data.Cshdk_Url) {
  671. // uni.redirectTo({ url: '/user/otherServe/payment-completed/index?state=fail' })
  672. // } else {
  673. // uni.postMessage({
  674. // data: {
  675. // event: 'wvHuiShiBaoPay',
  676. // data,
  677. // payType,
  678. // type,
  679. // otherArgs: { ...otherArgs, stage: 'three' },
  680. // jumpType: `wvHuiShiBaoPayBack`
  681. // }
  682. // })
  683. // }
  684. // })
  685. // .catch((e) => {
  686. // console.log(e)
  687. // uni.showToast({ title: '支付失败', icon: 'none' })
  688. // setTimeout(() => {
  689. // if ([1, 2].includes(payType)) {
  690. // uni.switchTab({ url: '/pages/order/order' })
  691. // } else if ([3, 4, 5].includes(payType)) {
  692. // uni.redirectTo({ url: '/user/otherServe/payment-completed/index?state=fail' })
  693. // }
  694. // }, 2000)
  695. // })
  696. // .finally((e) => {
  697. // uni.hideLoading()
  698. // })
  699. // } else if (otherArgs.satge === 'three') {
  700. // delete otherArgs.satge
  701. // if ([1, 2].includes(payType)) {
  702. // if (otherArgs.isSuccess) {
  703. // if (data.collageId) await payOrderSuccessApi({ orderId: data.orderId, collageId: data.collageId })
  704. // uni.showToast({ icon: 'none', title: '支付成功' })
  705. // setTimeout(() => { uni.redirectTo({ url: '/another-tf/another-serve/paySuccessful/index?orderId=' + data.orderId }) }, 2000)
  706. // } else {
  707. // uni.showToast({ icon: 'none', title: '用户取消支付' })
  708. // setTimeout(() => { uni.switchTab({ url: '/pages/order/order' }) }, 2000)
  709. // }
  710. // } else if ([3, 4, 5].includes(payType)) {
  711. // if (otherArgs.isSuccess) {
  712. // if (data.collageId) await payOrderSuccessApi({ orderId: data.orderId, collageId: data.collageId })
  713. // uni.showToast({ icon: 'none', title: '支付成功' })
  714. // setTimeout(() => { uni.redirectTo({ url: '/user/otherServe/payment-completed/index' }) }, 2000)
  715. // } else {
  716. // uni.showToast({ icon: 'none', title: '用户取消支付' })
  717. // setTimeout(() => { uni.redirectTo({ url: '/user/otherServe/payment-completed/index?state=fail' }) }, 2000)
  718. // }
  719. // }
  720. // }
  721. }
  722. /**
  723. * 惠市宝支付微信小程序处理
  724. * @param data 结算返回的支付信息
  725. */
  726. function mpHuiShiBaoPay(data, payType, type, otherArgs) {
  727. return new Promise((resolve, reject) => {
  728. const failOperation = (failText = '支付失败') => {
  729. uni.showToast({ title: failText, icon: 'none' })
  730. setTimeout(() => {
  731. if ([1, 2].includes(payType)) {
  732. uni.switchTab({ url: '/pages/order/order' })
  733. } else if ([3, 4, 5].includes(payType)) {
  734. uni.redirectTo({ url: '/user/otherServe/payment-completed/index?state=fail' })
  735. }
  736. }, 2000)
  737. }
  738. uni.login({
  739. provider: 'weixin',
  740. success: (res) => {
  741. getSessionKeyAppApi({ code: res.code, _isShowToast: false })
  742. .then(async (res1) => {
  743. await gotoOrderH5PayApi({
  744. ...data,
  745. purchaseMode: payType,
  746. ...otherArgs,
  747. hsbSubPayType: '05',
  748. extJsonStr: JSON.stringify({
  749. 'Sub_Appid': 'wx3cef6c7325c38a45', // 小程序appId
  750. 'Sub_Openid': res1.data.wechatOpenId // 微信用户openId
  751. }),
  752. _isShowToast: false
  753. }).then((res2) => {
  754. if (type) {
  755. uni.removeStorageSync(T_PAY_ORDER)
  756. uni.setStorageSync(T_PAY_ORDER, {
  757. type,
  758. TL_ORDER_NO: data.orderSn
  759. })
  760. }
  761. if (res2.data.Rtn_Par_Data) {
  762. const paymentObj = JSON.parse(res2.data.Rtn_Par_Data)
  763. uni.requestPayment({
  764. provider: 'wxpay',
  765. timeStamp: paymentObj.timeStamp,
  766. nonceStr: paymentObj.nonceStr,
  767. package: paymentObj.package,
  768. signType: paymentObj.signType,
  769. paySign: paymentObj.paySign,
  770. success: async (res3) => {
  771. if (data.collageId) await payOrderSuccessApi({ orderId: data.orderId, collageId: data.collageId }) // 拼团微信支付成功回调
  772. uni.showToast({ icon: 'none', title: '支付成功' })
  773. setTimeout(() => { uni.redirectTo({ url: '/user/otherServe/payment-completed/index' }) }, 2000)
  774. resolve('支付成功')
  775. },
  776. fail(e) {
  777. console.log(e)
  778. failOperation('支付失败!' + e)
  779. reject('支付失败!' + e)
  780. }
  781. })
  782. } else {
  783. uni.hideLoading()
  784. failOperation('缺少支付信息')
  785. reject('缺少支付信息')
  786. }
  787. })
  788. .catch((e) => {
  789. uni.hideLoading()
  790. if (e.data) failOperation(`${e.data.message}-${e.data.errorData}`)
  791. else failOperation(`请求:${e.errMsg}`)
  792. reject(e)
  793. })
  794. })
  795. .catch((e) => {
  796. uni.hideLoading()
  797. if (e.data) failOperation(`${e.data.message}-${e.data.errorData}`)
  798. else failOperation(`请求:${e.errMsg}`)
  799. reject(e)
  800. })
  801. },
  802. fail: () => {
  803. uni.hideLoading()
  804. failOperation('鉴权失败!')
  805. reject('鉴权失败!')
  806. }
  807. })
  808. })
  809. }
  810. /**
  811. * 处理支付
  812. * @param submitResult 结算结果
  813. */
  814. // eslint-disable-next-line complexity
  815. export async function handleDoPay(submitResult, purchaseMode, type = 'DEFAULT', otherArgs = {}) {
  816. uni.showLoading({
  817. mask: true,
  818. title: '支付中...'
  819. })
  820. console.log(JSON.stringify(submitResult))
  821. if (purchaseMode) { // 1-商城 2-本地 3-入驻 4-代金券 5-地图红包
  822. const submitInfo = { ...submitResult, purchaseMode }
  823. if (submitInfo.paymentMode === 999) {
  824. await bankCardPay(submitResult, purchaseMode, type, otherArgs)
  825. } else if ([ 1 ].includes(submitInfo.paymentMode)) { // 微信支付
  826. if (isInWx()) {
  827. if (store.state.app.isInMiniProgram) {
  828. await payH5InWechat(submitInfo)
  829. } else {
  830. await payH5InWechat(submitInfo)
  831. }
  832. } else {
  833. // #ifdef H5
  834. await payH5InEquipment(submitInfo)
  835. // #endif
  836. // #ifdef APP
  837. await appWechatPay(submitInfo)
  838. // #endif
  839. // #ifdef MP-WEIXIN
  840. await mpWechatPay(submitInfo)
  841. // #endif
  842. // #ifdef MP-ALIPAY
  843. uni.hideLoading()
  844. uni.showToast({ title: '暂不支持在支付宝小程序使用微信支付', icon: 'none' })
  845. // #endif
  846. }
  847. } else if ([2, 3].includes(submitInfo.paymentMode)) { // 支付宝
  848. if (isInWx()) {
  849. if (store.state.app.isInMiniProgram) {
  850. uni.hideLoading()
  851. uni.showToast({ title: '暂不支持在此端使用支付宝支付', icon: 'none' })
  852. } else {
  853. uni.hideLoading()
  854. uni.showToast({ title: '暂不支持在此端使用支付宝支付', icon: 'none' })
  855. }
  856. } else {
  857. // #ifdef H5
  858. uni.hideLoading()
  859. uni.showToast({ title: '暂不支持在此端使用支付宝支付', icon: 'none' })
  860. // #endif
  861. // #ifdef APP
  862. uni.hideLoading()
  863. uni.showToast({ title: '暂不支持在此端使用支付宝支付', icon: 'none' })
  864. // #endif
  865. // #ifdef MP-WEIXIN
  866. uni.hideLoading()
  867. uni.showToast({ title: '暂不支持在此端使用支付宝支付', icon: 'none' })
  868. // #endif
  869. // #ifdef MP-ALIPAY
  870. await zhiAliPay(submitInfo)
  871. throw new Error('支付宝相关支付暂时只支持支付宝小程序')
  872. // #endif
  873. }
  874. } else if ([ 4 ].includes(submitInfo.paymentMode)) { // 通联支付
  875. if (isInWx()) {
  876. // if (store.state.app.isInMiniProgram) {
  877. // await wvTonglianPay(submitResult, purchaseMode, type, otherArgs)
  878. // } else {
  879. await h5TonglianPay(submitResult, purchaseMode, type, otherArgs)
  880. // }
  881. } else {
  882. // #ifdef H5
  883. await h5TonglianPay(submitResult, purchaseMode, type, otherArgs)
  884. // #endif
  885. // // #ifdef APP
  886. // await appTonglianPay(submitResult, purchaseMode, type, otherArgs)
  887. // // #endif
  888. // // #ifdef MP-WEIXIN
  889. // uni.hideLoading()
  890. // uni.showToast({ title: '暂不支持在微信小程序使用通联支付', icon: 'none' })
  891. // // #endif
  892. // // #ifdef MP-ALIPAY
  893. // uni.hideLoading()
  894. // uni.showToast({ title: '暂不支持在支付宝小程序使用通联支付', icon: 'none' })
  895. // // #endif
  896. }
  897. } else if ([5, 6, 7, 8].includes(submitInfo.paymentMode)) { // 佣金支付、平台余额支付、商家余额支付、消费金支付
  898. if (isInWx()) {
  899. if (store.state.app.isInMiniProgram) {
  900. await h5TonglianPay(submitResult, purchaseMode, type, otherArgs)
  901. } else {
  902. await h5TonglianPay(submitResult, purchaseMode, type, otherArgs)
  903. }
  904. } else {
  905. // #ifdef H5
  906. await h5TonglianPay(submitResult, purchaseMode, type, otherArgs)
  907. // #endif
  908. // #ifdef APP
  909. await h5TonglianPay(submitResult, purchaseMode, type, otherArgs)
  910. // #endif
  911. // #ifdef MP-WEIXIN
  912. await h5TonglianPay(submitResult, purchaseMode, type, otherArgs)
  913. // #endif
  914. // #ifdef MP-ALIPAY
  915. await h5TonglianPay(submitResult, purchaseMode, type, otherArgs)
  916. // #endif
  917. }
  918. } else if ([ 9 ].includes(submitInfo.paymentMode)) { // 惠市宝支付
  919. if (isInWx()) {
  920. if (store.state.app.isInMiniProgram) {
  921. await wvHuiShiBaoPay(submitResult, purchaseMode, type, otherArgs)
  922. } else {
  923. await h5HuiShiBaoPay(submitResult, purchaseMode, type, otherArgs)
  924. }
  925. } else {
  926. // #ifdef H5
  927. await h5HuiShiBaoPay(submitResult, purchaseMode, type, otherArgs)
  928. // await wvHuiShiBaoPay(submitResult, purchaseMode, type, otherArgs)
  929. // #endif
  930. // #ifdef APP
  931. await wvHuiShiBaoPay(submitResult, purchaseMode, type, otherArgs)
  932. // #endif
  933. // #ifdef MP-WEIXIN
  934. await mpHuiShiBaoPay(submitResult, purchaseMode, type, otherArgs)
  935. // #endif
  936. // #ifdef MP-ALIPAY
  937. uni.hideLoading()
  938. uni.showToast({ title: '暂不支持在支付宝小程序使用惠市宝支付', icon: 'none' })
  939. // #endif
  940. }
  941. }
  942. }
  943. }