pay.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import request from '@/common/request.js'
  2. /**
  3. * type: order 支付订单 recharge paybill 优惠买单
  4. * data: 扩展数据对象,用于保存参数
  5. */
  6. module.exports = {
  7. wxpay: function(type, money, orderid, redirectUrl) {
  8. request.post('pay', {
  9. orderid: orderid,
  10. money: money,
  11. type: type
  12. }).then(res => {
  13. uni.showLoading({
  14. title: '正在支付中...'
  15. })
  16. if (res.errno == 0) {
  17. // #ifdef MP-WEIXIN
  18. // 发起支付
  19. uni.requestPayment({
  20. 'timeStamp': res.data.timeStamp,
  21. 'nonceStr': res.data.nonceStr,
  22. 'package': res.data.package,
  23. 'signType': 'MD5',
  24. 'paySign': res.data.paySign,
  25. fail: function(err) {
  26. console.log(err.errMsg);
  27. if (err.errMsg == 'requestPayment:fail cancel'){
  28. uni.hideLoading();
  29. uni.showModal({
  30. title: '提示',
  31. content: '取消支付',
  32. showCancel: false,
  33. success: function(res) {
  34. }
  35. })
  36. }else{
  37. uni.hideLoading();
  38. uni.showModal({
  39. title: '支付失败',
  40. content: JSON.stringify(err),
  41. showCancel: false,
  42. success: function(res) {
  43. }
  44. })
  45. }
  46. },
  47. success: function() {
  48. uni.hideLoading();
  49. // 提示支付成功
  50. uni.showToast({
  51. title: "支付成功",
  52. duration: 2000,
  53. icon: 'success_no_circle',
  54. size: "10"
  55. })
  56. uni.redirectTo({
  57. url: redirectUrl
  58. });
  59. }
  60. })
  61. // #endif
  62. // #ifdef H5
  63. WeixinJSBridge.invoke('getBrandWCPayRequest', {
  64. "appId": res.data.appId, //公众号名称,由商户传入
  65. "timeStamp": res.data.timeStamp, //时间戳
  66. "nonceStr": res.data.nonceStr, //随机串
  67. "package": res.data.package, //扩展包
  68. "signType": 'MD5', //微信签名方式:MD5
  69. "paySign": res.data.paySign //微信签名
  70. }, function(respay) {
  71. if (respay.err_msg === "get_brand_wcpay_request:ok") {
  72. uni.redirectTo({
  73. url: redirectUrl
  74. });
  75. //that.getData();
  76. } else if (respay.err_msg === "get_brand_wcpay_request:cancel") {
  77. uni.showToast({
  78. title: "取消支付",
  79. icon: "none",
  80. duration: 2000
  81. });
  82. } else if (respay.err_msg === "get_brand_wcpay_request:fail") {
  83. uni.showToast({
  84. title: "支付失败",
  85. icon: "none",
  86. duration: 2000
  87. })
  88. }
  89. }, function(err) {
  90. uni.showToast({
  91. title: res.data.msg,
  92. icon: "none",
  93. duration: 2000
  94. });
  95. });
  96. // #endif
  97. } else {
  98. console.log('支付出错')
  99. uni.showModal({
  100. title: '出错了',
  101. content: res.code + ':' + res.msg + ':' + res.data,
  102. showCancel: false,
  103. success: function(res) {
  104. }
  105. })
  106. }
  107. })
  108. }
  109. }