pay.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * 支付
  3. */
  4. export const pay = (result, self, success, fail) => {
  5. if (result.code === -10) {
  6. self.showError(result.msg);
  7. return false;
  8. }
  9. // 发起微信支付
  10. if (result.data.pay_type == 20) {
  11. //小程序支付
  12. // #ifdef MP-WEIXIN
  13. uni.requestPayment({
  14. provider: 'wxpay',
  15. timeStamp: result.data.payment.timeStamp,
  16. nonceStr: result.data.payment.nonceStr,
  17. package: 'prepay_id=' + result.data.payment.prepay_id,
  18. signType: 'MD5',
  19. paySign: result.data.payment.paySign,
  20. success: res => {
  21. paySuccess(result, self, success);
  22. },
  23. fail: res => {
  24. self.showError('订单未支付成功', () => {
  25. payError(result, fail, self);
  26. });
  27. },
  28. });
  29. // #endif
  30. //公众号支付
  31. // #ifdef H5
  32. if(self.isWeixin()){
  33. WeixinJSBridge.invoke('getBrandWCPayRequest', JSON.parse(result.data.payment),
  34. function(res) {
  35. if (res.err_msg == "get_brand_wcpay_request:ok") {
  36. paySuccess(result, self, success);
  37. } else if (res.err_msg == "get_brand_wcpay_request:cancel") {
  38. self.showSuccess('支付取消', () => {
  39. payError(result, fail, self);
  40. });
  41. } else {
  42. self.showError('订单未支付成功', () => {
  43. payError(result, fail, self);
  44. });
  45. }
  46. }
  47. );
  48. }else{
  49. //h5支付
  50. self.gotoPage('/pages/order/pay-h5/pay-h5?order_id='+result.data.order_id+'&order_type='+result.data.order_type);
  51. return ;
  52. }
  53. // #endif
  54. // #ifdef APP-PLUS
  55. //微信支付
  56. wxAppPay(result, self,success, fail);
  57. // #endif
  58. }
  59. // 余额支付
  60. if (result.data.pay_type == 10) {
  61. paySuccess(result, self, success);
  62. }
  63. // 支付宝支付
  64. if (result.data.pay_type == 30) {
  65. // #ifdef APP-PLUS
  66. aliAppPay(result, self,success, fail);
  67. // #endif
  68. // #ifdef H5
  69. self.gotoPage('/pages/order/alipay-h5/alipay-h5?order_id='+result.data.order_id+'&order_type='+result.data.order_type);
  70. // #endif
  71. }
  72. }
  73. /*跳到支付成功页*/
  74. function paySuccess(result, self, success) {
  75. if(success){
  76. success(result);
  77. return;
  78. }
  79. gotoSuccess(result, self);
  80. }
  81. /*跳到支付成功页*/
  82. function gotoSuccess(result, self) {
  83. self.gotoPage('/pages/order/pay-success/pay-success?order_id=' + result.data.order_id, 'reLaunch');
  84. }
  85. /*支付失败跳订单详情*/
  86. function payError(result, fail, self) {
  87. if(fail){
  88. fail(result);
  89. return;
  90. }
  91. self.gotoPage('/pages/order/order-detail?order_id=' + result.data.order_id, 'redirect');
  92. }
  93. function wxAppPay(result, self,success, fail){
  94. // 获取支付通道
  95. plus.payment.getChannels(function(channels) {
  96. self.channel = channels[0];
  97. console.log(self.channel);
  98. uni.requestPayment({
  99. provider: 'wxpay',
  100. orderInfo: result.data.payment,
  101. success(res) {
  102. paySuccess(result, self, success);
  103. },
  104. fail(error) {
  105. console.log(error);
  106. self.showError('订单未支付成功', () => {
  107. payError(result, fail);
  108. });
  109. },
  110. });
  111. }, function(e) {
  112. console.log("获取支付通道失败:" + e.message);
  113. });
  114. }
  115. function aliAppPay(result, self,success, fail){
  116. console.log(result.data.payment);
  117. uni.requestPayment({
  118. provider: 'alipay',
  119. orderInfo: result.data.payment,
  120. success(res) {
  121. paySuccess(result, self, success);
  122. },
  123. fail(error) {
  124. console.log(error);
  125. self.showError('订单未支付成功', () => {
  126. payError(result, fail, self);
  127. });
  128. },
  129. });
  130. }