payUtil.js 10 KB

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