多租户商城-商户小程序端
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.

339 lines
10 KiB

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. console.log('进入微信支付')
  182. try {
  183. const res = await NET.request(API.gotoPay, payInfo, 'POST')
  184. uni.requestPayment({
  185. provider: 'wxpay',
  186. timeStamp: res.data.timeStamp,
  187. nonceStr: res.data.nonceStr,
  188. package: res.data.package,
  189. signType: res.data.signType,
  190. paySign: res.data.paySign,
  191. success: async (payRes) => {
  192. // 拼团微信支付成功回调
  193. if (payInfo.collageId) {
  194. await NET.request(API.paySuccess, {
  195. orderId: payInfo.orderId,
  196. collageId: payInfo.collageId
  197. }, 'POST')
  198. }
  199. uni.showToast({
  200. icon: 'none',
  201. title: '支付成功'
  202. })
  203. //console.log(submitResult.orderId, 'order Id')
  204. uni.navigateTo({
  205. url: '/pages_category_page1/orderModule/paySuccessful?orderId=' + payInfo.orderId
  206. })
  207. },
  208. fail: function (err) {
  209. uni.showToast({
  210. icon: 'none',
  211. title: '用户取消支付'
  212. })
  213. uni.navigateTo({
  214. url: '/pages_category_page1/orderModule/index?type=1'
  215. })
  216. }
  217. })
  218. } catch (e) {
  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. // #endif
  229. // #ifdef APP-PLUS
  230. /**
  231. * App拉起微信支付
  232. * @param payInfo
  233. * @return {Promise<void>}
  234. */
  235. async function appWechatPay(payInfo) {
  236. try {
  237. const res = await NET.request(API.gotoAppPay, payInfo, 'POST')
  238. const obj = {
  239. appid: res.data.appId,
  240. noncestr: res.data.nonceStr,
  241. package: 'Sign=WXPay',
  242. prepayid: res.data.prepayId,
  243. timestamp: res.data.timeStamp,
  244. sign: res.data.paySign,
  245. partnerid: res.data.partnerId
  246. }
  247. uni.requestPayment({
  248. provider: 'wxpay',
  249. orderInfo: obj,
  250. success: function (payRes) {
  251. uni.showToast({
  252. icon: 'none',
  253. title: '支付成功'
  254. })
  255. uni.navigateTo({
  256. url: '/pages_category_page1/orderModule/paySuccessful?orderId=' + payInfo.orderId
  257. })
  258. },
  259. fail: function (err) {
  260. uni.showToast({
  261. icon: 'none',
  262. title: '用户取消支付'
  263. })
  264. uni.navigateTo({
  265. url: '/pages_category_page1/orderModule/index?type=1'
  266. })
  267. }
  268. })
  269. } catch (e) {
  270. console.log(e)
  271. uni.showToast({
  272. title: 'APP拉起微信支付失败',
  273. icon: 'none'
  274. })
  275. uni.navigateTo({
  276. url: '/pages_category_page1/orderModule/index?type=1'
  277. })
  278. } finally {
  279. uni.hideLoading()
  280. }
  281. }
  282. // #endif
  283. /**
  284. * 处理支付
  285. * @param submitResult 结算结果
  286. */
  287. export async function handleDoPay(submitResult) {
  288. uni.showLoading({
  289. mask: true,
  290. title: '支付中...',
  291. })
  292. console.log('进入支付')
  293. const {paymentMode} = submitResult
  294. if (paymentMode === 1) {
  295. // 微信支付
  296. // #ifdef APP-PLUS
  297. await appWechatPay(submitResult)
  298. // #endif
  299. // #ifdef MP-WEIXIN
  300. console.log('进入微信支付')
  301. await wechatPay(submitResult)
  302. // #endif
  303. // #ifdef H5
  304. await h5Pay(submitResult)
  305. // #endif
  306. } else if ([2, 3].includes(paymentMode)) {
  307. // 支付宝
  308. // #ifdef MP-ALIPAY
  309. await aliPay(submitResult)
  310. // #endif
  311. // #ifndef MP-ALIPAY
  312. // await appWechatPay(submitResult,this.orderId)
  313. console.error("支付宝相关支付暂时只支持支付宝小程序")
  314. throw new Error('支付宝相关支付暂时只支持支付宝小程序')
  315. // #endif
  316. }
  317. setTimeout(()=>{
  318. uni.navigateTo({
  319. url: '/pages_category_page1/orderModule/index?type=2'
  320. })
  321. },1000)
  322. /* uni.navigateTo({
  323. url: '/pages_category_page1/orderModule/index?type=2'
  324. })*/
  325. // uni.hideLoading()
  326. }