pay.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. const WXAPI = require('apifm-wxapi')
  2. /**
  3. * type: order 支付订单 recharge 充值 paybill 优惠买单
  4. * data: 扩展数据对象,用于保存参数
  5. */
  6. function wxpay(type, money, orderId, redirectUrl, data) {
  7. const postData = {
  8. token: wx.getStorageSync('token'),
  9. money: money,
  10. remark: "在线充值",
  11. }
  12. if (type === 'order') {
  13. postData.remark = "支付订单 :" + orderId;
  14. postData.nextAction = {
  15. type: 0,
  16. id: orderId
  17. };
  18. }
  19. if (type === 'paybill') {
  20. postData.remark = "优惠买单 :" + data.money;
  21. postData.nextAction = {
  22. type: 4,
  23. uid: wx.getStorageSync('uid'),
  24. money: data.money
  25. };
  26. }
  27. if (type === 'fxsBuy') {
  28. postData.remark = "购买分销资格";
  29. postData.nextAction = {
  30. type: 13
  31. };
  32. }
  33. postData.payName = postData.remark;
  34. if (postData.nextAction) {
  35. postData.nextAction = JSON.stringify(postData.nextAction);
  36. }
  37. WXAPI.wxpay(postData).then(function (res) {
  38. if (res.code == 0) {
  39. // 发起支付
  40. wx.requestPayment({
  41. timeStamp: res.data.timeStamp,
  42. nonceStr: res.data.nonceStr,
  43. package: res.data.package,
  44. signType: res.data.signType,
  45. paySign: res.data.paySign,
  46. fail: function (aaa) {
  47. console.error(aaa)
  48. wx.showToast({
  49. title: '支付失败:' + aaa
  50. })
  51. },
  52. success: function () {
  53. // 提示支付成功
  54. wx.showToast({
  55. title: '支付成功'
  56. })
  57. wx.redirectTo({
  58. url: redirectUrl
  59. });
  60. }
  61. })
  62. } else {
  63. wx.showModal({
  64. title: '出错了',
  65. content: JSON.stringify(res),
  66. showCancel: false
  67. })
  68. }
  69. })
  70. }
  71. module.exports = {
  72. wxpay: wxpay
  73. }