wxPay.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. const wx = require('weixin-js-sdk');
  2. export default {
  3. wexinPay(data, jumpUrl) {
  4. return new Promise(r => {
  5. wx.config({
  6. debug: false,
  7. appId: data.appId,
  8. timestamp: data.timeStamp,
  9. nonceStr: data.nonceStr,
  10. signature: data.paySign,
  11. jsApiList: ['chooseWXPay']
  12. });
  13. wx.ready(() => {
  14. wx.chooseWXPay({
  15. timestamp: data.timeStamp, //这个字段是为字符串后端返回时需检查
  16. nonceStr: data.nonceStr,
  17. package: data.packageValue,
  18. signType: data.signType,
  19. paySign: data.paySign,
  20. success(res) {
  21. r({
  22. code: 0,
  23. msg: "成功"
  24. });
  25. },
  26. cancel() {
  27. r({
  28. code: 1,
  29. msg: "取消"
  30. });
  31. },
  32. fail(err) {
  33. r({
  34. code: 2,
  35. msg: err.errMsg.split(':')[1] || '支付失败!'
  36. });
  37. },
  38. // 无论失败成功都会执行
  39. complete(e) {
  40. // e.errMsg三种状态 1.chooseWXPay:ok 支付成功 2: chooseWXPay:cancel 支付取消 3:chooseWXPay:fail 支付失败
  41. if (e.errMsg === 'chooseWXPay:ok') {
  42. if (jumpUrl) {
  43. window.location.href = jumpUrl
  44. } else {
  45. window.location.reload()
  46. }
  47. }
  48. }
  49. });
  50. });
  51. wx.error(function(err) {
  52. r({
  53. code: 2,
  54. msg: '支付失败!'
  55. });
  56. });
  57. })
  58. }
  59. }