多租户商城-商户小程序端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

389 lines
12 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. /**
  2. * @FileDescription:
  3. * @Author: kahu
  4. * @Date: 2022/11/2
  5. * @LastEditors: kahu
  6. * @LastEditTime: 2022/11/2
  7. */
  8. const NET = require('./request')
  9. const API = require('../config/api')
  10. // #ifdef H5
  11. const jweixin = require('jweixin-module')
  12. /**
  13. * 普通H5处理
  14. * @param payInfo 结算返回的支付信息
  15. */
  16. async function payH5InEquipment(payInfo) {
  17. try {
  18. const res = await NET.request(API.gotoH5Pay, payInfo, 'POST')
  19. location.replace(res.data.mwebUrl)
  20. } catch (e) {
  21. this.submitActive = true
  22. uni.showToast({
  23. title: '支付失败',
  24. icon: 'none'
  25. })
  26. uni.navigateTo({
  27. url: '/pages_category_page1/orderModule/index?type=1'
  28. })
  29. } finally {
  30. uni.hideLoading()
  31. }
  32. }
  33. /**
  34. * 微信内H5处理
  35. * @param payInfo 结算返回的支付信息
  36. * @param orderId 订单ID
  37. */
  38. async function payH5InWechat(payInfo) {
  39. payInfo.paymentMode = 1
  40. const res = await NET.request(API.gotoPay, payInfo, 'POST')
  41. jweixin.config({
  42. debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
  43. appId: res.data.appId, // 必填,公众号的唯一标识
  44. timestamp: res.data.timeStamp, // 必填,生成签名的时间戳
  45. nonceStr: res.data.nonceStr, // 必填,生成签名的随机串
  46. signature: res.data.paySign, // 必填,签名,见附录1
  47. jsApiList: ['chooseWXPay'] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
  48. });
  49. jweixin.ready(function () {
  50. jweixin.checkJsApi({
  51. jsApiList: ['chooseWXPay'], // 需要检测的JS接口列表,所有JS接口列表见附录2,
  52. success: function (res) {
  53. },
  54. fail: function (res) {
  55. }
  56. });
  57. jweixin.chooseWXPay({
  58. timestamp: res.data
  59. .timeStamp, // 支付签名时间戳,注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
  60. nonceStr: res.data.nonceStr, // 支付签名随机串,不长于 32 位
  61. package: res.data
  62. .package, // 统一支付接口返回的prepay_id参数值,提交格式如:prepay_id=***)
  63. signType: res.data.signType, // 签名方式,默认为'SHA1',使用新版支付需传入'MD5'
  64. paySign: res.data.paySign, // 支付签名
  65. success: function (res) {
  66. // 支付成功后的回调函数
  67. uni.showToast({
  68. icon: 'none',
  69. title: '支付成功'
  70. })
  71. uni.navigateTo({
  72. url: '/pages_category_page1/orderModule/paySuccessful?orderId=' + payInfo.orderId
  73. })
  74. },
  75. cancel: function (r) {
  76. uni.showToast({
  77. icon: 'none',
  78. title: '用户取消支付'
  79. })
  80. uni.navigateTo({
  81. url: '/pages_category_page1/orderModule/index?type=1'
  82. })
  83. },
  84. fail: function (res) {
  85. uni.showToast({
  86. icon: 'none',
  87. title: '微信内支付错误'
  88. })
  89. uni.navigateTo({
  90. url: '/pages_category_page1/orderModule/index?type=1'
  91. })
  92. }
  93. });
  94. });
  95. jweixin.error(function (res) {
  96. uni.showToast({
  97. icon: 'none',
  98. title: '微信内支付加载失败',
  99. duration: 3000
  100. });
  101. uni.navigateTo({
  102. url: '/pages_category_page1/orderModule/index?type=1'
  103. })
  104. });
  105. }
  106. /**
  107. * H5拉起支付
  108. * @param payInfo 结算返回的支付信息
  109. */
  110. async function h5Pay(payInfo) {
  111. let ua = navigator.userAgent.toLowerCase();
  112. if (ua.match(/MicroMessenger/i) == "micromessenger") {
  113. await payH5InWechat(payInfo)
  114. } else {
  115. await payH5InEquipment(payInfo)
  116. }
  117. }
  118. // #endif
  119. // #ifdef MP-ALIPAY
  120. /**
  121. * 支付宝小程序拉起支付
  122. * @param payInfo 结算返回的支付信息
  123. * @return {Promise<void>}
  124. */
  125. async function aliPay(payInfo) {
  126. try {
  127. const res = await NET.request(API.gotoPay, payInfo, 'POST')
  128. uni.requestPayment({
  129. provider: 'alipay',
  130. orderInfo: res.data.tradeNo,
  131. success: function (payRes) {
  132. if (payRes.resultCode == '6001') {
  133. uni.showToast({
  134. icon: 'none',
  135. title: '取消支付'
  136. })
  137. uni.navigateTo({
  138. url: '/pages_category_page1/orderModule/index?type=1'
  139. })
  140. }
  141. if (payRes.resultCode == '9000') {
  142. uni.showToast({
  143. icon: 'none',
  144. title: '支付成功'
  145. })
  146. uni.navigateTo({
  147. url: '/pages_category_page1/orderModule/paySuccessful?orderId=' + orderId
  148. })
  149. }
  150. },
  151. fail: function (err) {
  152. uni.showToast({
  153. icon: 'none',
  154. title: '支付取消'
  155. })
  156. uni.navigateTo({
  157. url: '/pages_category_page1/orderModule/index?type=1'
  158. })
  159. }
  160. });
  161. } catch (e) {
  162. uni.showToast({
  163. title: '支付宝支付异常',
  164. icon: 'none'
  165. })
  166. uni.navigateTo({
  167. url: '/pages_category_page1/orderModule/index?type=1'
  168. })
  169. } finally {
  170. uni.hideLoading()
  171. }
  172. }
  173. // #endif
  174. // #ifdef MP-WEIXIN
  175. /**
  176. * 微信小程序拉起支付
  177. * @param payInfo
  178. * @return {Promise<void>}
  179. */
  180. async function wechatPay(payInfo) {
  181. try {
  182. const res = await NET.request(API.gotoPay, payInfo, 'POST')
  183. console.log(res)
  184. // WeixinJSBridge.invoke(
  185. // 'getBrandWCPayRequest', {
  186. // "appId":res.data.appId,
  187. // "timeStamp":res.data.timeStamp,
  188. // "nonceStr":res.data.nonceStr,
  189. // "package":res.data.package,
  190. // "signType":res.data.signType,
  191. // "paySign":res.data.paySign
  192. // },
  193. // async function(res){
  194. // if(res.err_msg == "get_brand_wcpay_request:ok" ){
  195. // // 拼团微信支付成功回调
  196. // if (payInfo.collageId) {
  197. // await NET.request(API.paySuccess, {
  198. // orderId: payInfo.orderId,
  199. // collageId: payInfo.collageId
  200. // }, 'POST')
  201. // }
  202. // uni.showToast({
  203. // icon: 'none',
  204. // title: '支付成功'
  205. // })
  206. // //console.log(submitResult.orderId, 'order Id')
  207. // uni.navigateTo({
  208. // url: '/pages_category_page1/orderModule/paySuccessful?orderId=' + payInfo.orderId
  209. // })
  210. // } else if(res.err_msg == "get_brand_wcpay_request:cancel" ){
  211. // uni.showToast({
  212. // icon: 'none',
  213. // title: '用户取消支付'
  214. // })
  215. // uni.navigateTo({
  216. // url: '/pages_category_page1/orderModule/index?type=1'
  217. // })
  218. // } else {
  219. // uni.showToast({
  220. // title: '微信支付拉起失败',
  221. // icon: 'none'
  222. // })
  223. // uni.navigateTo({
  224. // url: '/pages_category_page1/orderModule/index?type=1'
  225. // })
  226. // }
  227. // });
  228. // get_brand_wcpay_request:ok 支付成功
  229. // get_brand_wcpay_request:cancel 支付过程中用户取消
  230. // get_brand_wcpay_request:fail 支付失败
  231. uni.requestPayment({
  232. provider: 'wxpay',
  233. timeStamp: res.data.timeStamp,
  234. nonceStr: res.data.nonceStr,
  235. package: res.data.package,
  236. signType: res.data.signType,
  237. paySign: res.data.paySign,
  238. success: async (payRes) => {
  239. // 拼团微信支付成功回调
  240. if (payInfo.collageId) {
  241. await NET.request(API.paySuccess, {
  242. orderId: payInfo.orderId,
  243. collageId: payInfo.collageId
  244. }, 'POST')
  245. }
  246. uni.showToast({
  247. icon: 'none',
  248. title: '支付成功'
  249. })
  250. //console.log(submitResult.orderId, 'order Id')
  251. uni.navigateTo({
  252. url: '/pages_category_page1/orderModule/paySuccessful?orderId=' + payInfo.orderId
  253. })
  254. },
  255. fail: function (err) {
  256. console.log(err)
  257. uni.showToast({
  258. icon: 'none',
  259. title: '用户取消支付'
  260. })
  261. uni.navigateTo({
  262. url: '/pages_category_page1/orderModule/index?type=1'
  263. })
  264. }
  265. })
  266. } catch (e) {
  267. console.log(e)
  268. uni.showToast({
  269. title: '微信支付拉起失败',
  270. icon: 'none'
  271. })
  272. uni.navigateTo({
  273. url: '/pages_category_page1/orderModule/index?type=1'
  274. })
  275. }
  276. }
  277. // #endif
  278. // #ifdef APP-PLUS
  279. /**
  280. * App拉起微信支付
  281. * @param payInfo
  282. * @return {Promise<void>}
  283. */
  284. async function appWechatPay(payInfo) {
  285. try {
  286. const res = await NET.request(API.gotoAppPay, payInfo, 'POST')
  287. const obj = {
  288. appid: res.data.appId,
  289. noncestr: res.data.nonceStr,
  290. package: 'Sign=WXPay',
  291. prepayid: res.data.prepayId,
  292. timestamp: res.data.timeStamp,
  293. sign: res.data.paySign,
  294. partnerid: res.data.partnerId
  295. }
  296. uni.requestPayment({
  297. provider: 'wxpay',
  298. orderInfo: obj,
  299. success: function (payRes) {
  300. uni.showToast({
  301. icon: 'none',
  302. title: '支付成功'
  303. })
  304. uni.navigateTo({
  305. url: '/pages_category_page1/orderModule/paySuccessful?orderId=' + payInfo.orderId
  306. })
  307. },
  308. fail: function (err) {
  309. uni.showToast({
  310. icon: 'none',
  311. title: '用户取消支付'
  312. })
  313. uni.navigateTo({
  314. url: '/pages_category_page1/orderModule/index?type=1'
  315. })
  316. }
  317. })
  318. } catch (e) {
  319. uni.showToast({
  320. title: 'APP拉起微信支付失败',
  321. icon: 'none'
  322. })
  323. uni.navigateTo({
  324. url: '/pages_category_page1/orderModule/index?type=1'
  325. })
  326. } finally {
  327. uni.hideLoading()
  328. }
  329. }
  330. // #endif
  331. /**
  332. * 处理支付
  333. * @param submitResult 结算结果
  334. */
  335. export async function handleDoPay(submitResult) {
  336. uni.showLoading({
  337. mask: true,
  338. title: '支付中...',
  339. })
  340. const {paymentMode} = submitResult
  341. if (paymentMode === 1) {
  342. // 微信支付
  343. // #ifdef APP-PLUS
  344. await appWechatPay(submitResult)
  345. // #endif
  346. // #ifdef MP-WEIXIN
  347. await wechatPay(submitResult)
  348. // #endif
  349. // #ifdef H5
  350. await h5Pay(submitResult)
  351. // #endif
  352. } else if ([2, 3].includes(paymentMode)) {
  353. // 支付宝
  354. // #ifdef MP-ALIPAY
  355. await aliPay(submitResult)
  356. // #endif
  357. // #ifndef MP-ALIPAY
  358. // await appWechatPay(submitResult,this.orderId)
  359. console.error("支付宝相关支付暂时只支持支付宝小程序")
  360. throw new Error('支付宝相关支付暂时只支持支付宝小程序')
  361. // #endif
  362. }
  363. //#ifndef H5
  364. // setTimeout(()=>{
  365. // uni.navigateTo({
  366. // url: '/pages_category_page1/orderModule/index?type=2'
  367. // })
  368. // },1000)
  369. //#endif
  370. /* uni.navigateTo({
  371. url: '/pages_category_page1/orderModule/index?type=2'
  372. })*/
  373. // uni.hideLoading()
  374. }