pay.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. var app = getApp();
  2. var util = require('../../utils/util.js');
  3. var api = require('../../config/api.js');
  4. Page({
  5. data: {
  6. orderIds: [],
  7. actualPrice: 0.00,
  8. isMergePay: '',
  9. payType: 'wxGlobal'
  10. },
  11. onLoad: function (options) {
  12. // 页面初始化 options为页面跳转所带来的参数
  13. this.setData({
  14. orderIds: options.orderIds,
  15. actualPrice: options.actualPrice,
  16. isMergePay: options.isMergePay
  17. })
  18. console.log(this.data.orderIds);
  19. },
  20. onReady: function () {
  21. },
  22. onShow: function () {
  23. // 页面显示
  24. },
  25. onHide: function () {
  26. // 页面隐藏
  27. },
  28. onUnload: function () {
  29. // 页面关闭
  30. },
  31. //改变支付方式
  32. changePayType: function (e) {
  33. console.log(e.currentTarget.id);
  34. this.setData({
  35. payType: e.currentTarget.id
  36. });
  37. },
  38. //向服务请求支付参数
  39. requestPayParam() {
  40. let that = this;
  41. wx.showLoading({
  42. title: '加载中...',
  43. });
  44. console.log(this.data.payType);
  45. let url = '';
  46. if (this.data.payType == 'wx') {
  47. url = api.PayPrepayId;
  48. } else if (this.data.payType == 'pingan') {
  49. url = api.Payorder;
  50. } else if (this.data.payType == 'wxGlobal') {
  51. url = api.GlobalPayorder;
  52. }
  53. // 测试领取优惠券
  54. // wx.redirectTo({
  55. // url: '/pages/payResult/payResult?status=1&orderId=' + that.data.orderId,
  56. // })
  57. // todo
  58. util.request(url, {
  59. orderIds: that.data.orderIds, payType: 1,
  60. isMergePay: that.data.isMergePay
  61. }).then(function (res) {
  62. wx.hideLoading();
  63. if (res.errno === 0) {
  64. let payParam = res.data;
  65. wx.requestPayment({
  66. 'appId': payParam.appId,
  67. 'timeStamp': payParam.timeStamp,
  68. 'nonceStr': payParam.nonceStr,
  69. 'package': payParam.package,
  70. 'signType': 'RSA',
  71. 'paySign': payParam.paySign,
  72. 'success': function (res) {
  73. wx.redirectTo({
  74. url: '/pages/payResult/payResult?status=1&orderIds=' + that.data.orderIds,
  75. })
  76. },
  77. 'fail': function (res) {
  78. console.log(res)
  79. wx.redirectTo({
  80. url: '/pages/payResult/payResult?status=0&orderIds=' + that.data.orderIds,
  81. })
  82. }
  83. })
  84. }else {
  85. // util.showErrorToast(res.errmsg)
  86. wx.showModal({
  87. title: '',
  88. content: res.errmsg,
  89. showCancel: false
  90. });
  91. }
  92. });
  93. },
  94. startPay(e) {
  95. this.requestPayParam();
  96. }
  97. })