pay.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. // #ifndef APP-PLUS
  2. //const jweixin = require('/lib/jweixin.js');
  3. import jweixin from '/lib/jweixin.js';
  4. // #endif
  5. import dever from './core';
  6. import html from './html';
  7. var pay = {
  8. init: function(doc) {
  9. html.init(doc);
  10. }
  11. // 支付
  12. payment: function(type, order_no, param) {
  13. // 回跳页面地址
  14. if (type == 'alipay_app' || type == 'wechat_app' || type == 'wechat_xcx') {
  15. this.appPayment(type, param, function(res) {
  16. uni.redirectTo({
  17. url: '/pages/payment/callback?order_no=' + order_no
  18. })
  19. }, function(err) {
  20. tool.msg('支付异常,请重下单重试');
  21. tool.payBack();
  22. });
  23. return;
  24. }
  25. if (type == 'alipay_wap' || type == 'wechat_wap') {
  26. html.insert(param);
  27. uni.redirectTo({
  28. url: '/pages/payment/callback?order_no=' + order_no
  29. })
  30. return;
  31. }
  32. if (type == 'wechat_gzh') {
  33. this.wxJSApiPay(param, (res) => {
  34. tool.msg('支付成功', 'success');
  35. uni.redirectTo({
  36. url: '/pages/payment/callback?order_no=' + order_no
  37. })
  38. }, (err) => {
  39. tool.msg('支付异常,请重下单重试');
  40. tool.payBack();
  41. });
  42. return;
  43. }
  44. if (type == 'balance' && param.code == 1) {
  45. uni.redirectTo({
  46. url: '/pages/payment/callback?order_no=' + order_no
  47. })
  48. return;
  49. }
  50. },
  51. // app支付
  52. appPayment: function(type, param, callback, errorCallback) {
  53. let paymentType;
  54. switch (type) {
  55. // 微信APP
  56. case 'wechat_app':
  57. paymentType = 'wxpay'
  58. break;
  59. // 微信小程序
  60. case 'wechat_xcx':
  61. paymentType = 'wxpay'
  62. break;
  63. // 阿里APP支付
  64. case 'alipay_app':
  65. paymentType = 'alipay'
  66. break;
  67. }
  68. uni.requestPayment({
  69. provider: paymentType,
  70. orderInfo: param,
  71. success: function(res) {
  72. if (callback) {
  73. callback(res);
  74. }
  75. },
  76. fail: function(err) {
  77. console.log('fail:' + JSON.stringify(err));
  78. if (errorCallback) {
  79. errorCallback(err)
  80. }
  81. }
  82. });
  83. },
  84. // 微信公众号支付
  85. wxJSApiPay: function(data, callback, errorCallback) {
  86. jweixin.config({
  87. debug: false, // 开启调试模式
  88. appId: data.appId, // 必填,公众号的唯一标识
  89. timestamp: data.timeStamp, // 必填,生成签名的时间戳
  90. nonceStr: data.nonceStr, // 必填,生成签名的随机串
  91. signature: data.paySign, // 必填,签名,见附录1
  92. jsApiList: ['checkJsApi', 'startRecord', 'stopRecord', 'translateVoice', 'scanQRCode',
  93. 'openCard'
  94. ] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
  95. });
  96. jweixin.ready(function() {
  97. jweixin.checkJsApi({
  98. jsApiList: ['checkJsApi', 'startRecord', 'stopRecord', 'translateVoice',
  99. 'scanQRCode', 'openCard'
  100. ],
  101. success: (res) => {},
  102. fail: function(res) {
  103. tool.msg('支付呼起失败');
  104. }
  105. });
  106. jweixin.chooseWXPay({
  107. timestamp: data.timeStamp,
  108. nonceStr: data.nonceStr,
  109. package: data.package,
  110. signType: data.signType,
  111. paySign: data.paySign,
  112. success: (res) => {
  113. tool.msg(res);
  114. // 前端判断返回方式,微信团队郑重提示:不保证绝对可靠,切记!
  115. if (res.errMsg == 'chooseWXPay:ok' && callback) {
  116. callback(res);
  117. } else {
  118. tool.msg('支付失败,请重试');
  119. }
  120. },
  121. cancel: (r) => {
  122. tool.msg('支付已取消');
  123. },
  124. fail: (err) => {
  125. if (errorCallback) {
  126. errorCallback(err)
  127. }
  128. }
  129. });
  130. });
  131. // config信息验证失败会执行error函数,
  132. jweixin.error(function(res) {
  133. console.log('jweixin.error失败:', res)
  134. });
  135. }
  136. }
  137. module.exports = pay;
  138. /*
  139. const pay = {
  140. //拉起支付
  141. pay : function(result, location, error) {
  142. var self = this;
  143. if (!error) {
  144. if (location.indexOf('?') == -1) {
  145. var fix = '?';
  146. } else {
  147. var fix = '&';
  148. }
  149. location = location + fix + 'order_id=' + result.order_id;
  150. }
  151. if (result.type == 'test') {
  152. self.alert('支付成功','none', function() {
  153. self.location(location, 'to');
  154. })
  155. } else if (self.source == 'h5') {
  156. uni.hideLoading();
  157. uni.showLoading({title: '跳转中', mask: true});
  158. self.html(result.order);
  159. } else if (self.source == 'app') {
  160. self.appPayment(result.type, result.order, function() {
  161. self.location(location, 'to');
  162. }, function() {
  163. if (!error) {
  164. self.location(location + '&error=1', 'to');
  165. } else {
  166. self.alert(error);
  167. }
  168. });
  169. }
  170. },
  171. //app支付
  172. appPayment : function(type, order, callback, errorCallback) {
  173. uni.requestPayment({
  174. provider: type,
  175. orderInfo: order, //微信、支付宝订单数据
  176. success: function (res) {
  177. //console.log('success:' + JSON.stringify(res));
  178. callback(res);
  179. },
  180. fail: function (err) {
  181. //console.log('fail:' + JSON.stringify(err));
  182. errorCallback(err)
  183. }
  184. });
  185. },
  186. }*/