pay.js 2.0 KB

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