wxPay.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. const wx = require('weixin-js-sdk');
  2. export default {
  3. weixinPay(data, jumpUrl) {
  4. wx.config({
  5. debug: false,
  6. appId: data.appId,
  7. timestamp: data.timeStamp,
  8. nonceStr: data.nonceStr,
  9. signature: data.paySign,
  10. jsApiList: ['chooseWXPay']
  11. });
  12. return new Promise((resolve, reject) => {
  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. resolve({ code: 0, msg: '成功' });
  22. if (e.errMsg === 'chooseWXPay:ok' && jumpUrl) {
  23. window.location.href = jumpUrl;
  24. }
  25. },
  26. cancel() {
  27. resolve({ code: 1, msg: '取消' });
  28. },
  29. fail(err) {
  30. reject({ code: 2, msg: err.errMsg.split(':')[1] || '支付失败!' });
  31. },
  32. complete(e) {
  33. if (e.errMsg !== 'chooseWXPay:ok') {
  34. reject({ code: 2, msg: '支付失败!' });
  35. }
  36. }
  37. });
  38. });
  39. wx.error((err) => {
  40. reject({ code: 2, msg: '支付失败!' });
  41. });
  42. });
  43. }
  44. };