payUtil.js 7.8 KB

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