pay.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /**
  2. * Created by 黄诚天 on 2017-11-03.
  3. */
  4. // var proId;
  5. // var amount;
  6. // var payWay;//0支付宝 1微信 2银联
  7. // var uuid = "10041166";
  8. const userAgent = navigator.userAgent;
  9. const isMobile = /(iPhone|iPad|Opera Mini|Android.*Mobile|NetFront|PSP|BlackBerry|Windows Phone)/ig.test(userAgent);
  10. var orderInterval;
  11. var pay =function (proId, amount, payWay, uuid) {
  12. var customAmount = $("#custom-amount" + proId).val();
  13. console.log(customAmount);
  14. if ($.trim(customAmount) != "") {
  15. var reg = "^(([0-9]+\.[0-9]{1,2})|([0-9]*[1-9][0-9]*\.[0-9]{1,2})|([0-9]*[1-9][0-9]*))$";
  16. var patt = new RegExp(reg);
  17. var r = patt.test(customAmount);
  18. if (r) {
  19. if (parseFloat(customAmount) < 10000000) {
  20. amount = customAmount;
  21. } else {
  22. alert("您输入的金额过大,请不要超过一千万");
  23. return;
  24. }
  25. } else {
  26. alert('输入金额有误,请正确输入金额且最多保留两位小数!');
  27. return;
  28. }
  29. }
  30. var yuanIndex = amount.toString().indexOf("元");
  31. if (yuanIndex > 0) {
  32. amount = amount.toString().substring(0, yuanIndex);
  33. }
  34. //获取支付方式
  35. if(payWay == "支付宝") {
  36. aliPay(proId, amount, payWay, uuid);
  37. } else if (payWay == "微信支付") {
  38. wxPay(proId, amount, payWay, uuid);
  39. } else if (payWay == "网银支付") {
  40. unionPay();
  41. }
  42. }
  43. var wxPay = function (proId, amount, payWay, uuid) {
  44. var jsonStr = getJson(proId, amount, payWay, uuid);
  45. //公众号支付js模板 需引入http://res.wx.qq.com/open/js/jweixin-1.0.0.js
  46. //对浏览器的UserAgent进行正则匹配,不含有微信独有标识的则为其他浏览器
  47. //var userAgent = navigator.userAgent;
  48. if (userAgent.match(/MicroMessenger/i) == 'MicroMessenger') {
  49. //公众号支付逻辑(微信浏览器)
  50. webPay(jsonStr);
  51. // $.ajax({
  52. // url : "/wxpay/userAdvanced",
  53. // type : "GET",
  54. // async: false,
  55. // success : function() {
  56. // webPay(jsonStr);
  57. // },
  58. // error : function() {
  59. // alert("请求openid失败");
  60. // }
  61. // });
  62. }else{
  63. //H5支付逻辑
  64. var url = "/wxpay/pcPay";
  65. var dataType = "html"
  66. if (isMobile) {
  67. url = "/wxpay/wapPay";
  68. dataType = "json"
  69. }
  70. $.ajax({
  71. type : "POST",
  72. url : url,
  73. dataType : dataType,
  74. async: false,
  75. //contentType : 'application/json;charset=UTF-8',
  76. data: {
  77. jsonStr : jsonStr
  78. },
  79. success : function(res){
  80. var resJsonObj=JSON.parse(res);
  81. if (resJsonObj.error != null) {
  82. alert(resJsonObj.error);
  83. return;
  84. }
  85. if (isMobile) {
  86. window.location.href = resJsonObj.mwebUrl;
  87. } else {
  88. document.cookie = 'qrcodeUrl='+encodeURIComponent(resJsonObj.qrcodeUrl);
  89. window.location.href = "http://lj.ubtob.com/project#/donateQrcode/" + resJsonObj.outTradeNo;
  90. }
  91. },
  92. error : function() {
  93. alert('系统异常,请联系客服!');
  94. }
  95. });
  96. }
  97. }
  98. var aliPay = function (proId, amount, payWay, uuid) {
  99. var jsonStr = getJson(proId, amount, payWay, uuid);
  100. var url = "/alipay/pcPay";
  101. if (isMobile) {
  102. url = "/alipay/wapPay";
  103. }
  104. $.ajax({
  105. type : "POST",
  106. url : url,
  107. dataType : "html",
  108. //contentType : "application/json;charset=UTF-8",
  109. async: false,
  110. data: {
  111. jsonStr : jsonStr
  112. },
  113. success : function(data) {
  114. document.write(data);
  115. },
  116. error : function() {
  117. alert('系统异常,请联系客服!');
  118. }
  119. });
  120. }
  121. var unionPay = function () {
  122. }
  123. /*公众号支付*/
  124. var webPay = function (jsonStr) {
  125. // var xmlHttpRequest;
  126. //
  127. // $(function(){
  128. // if(window.XMLHttpRequest){
  129. // xmlHttpRequest=new XMLHttpRequest();
  130. // }else{
  131. // xmlHttpRequest=new ActiveXObject("Microsoft.XMLHTTP");
  132. // }
  133. // //xmlHttpRequest.responseType = 'json';
  134. // //xmlHttpRequest.setRequestHeader('Content-type','application/x-www-form-urlencoded');
  135. // xmlHttpRequest.onreadystatechange = back;
  136. // xmlHttpRequest.open('POST','/wxpay/webPay',true);
  137. // xmlHttpRequest.send('jsonStr=' + jsonStr);
  138. // function back() {
  139. // if(xmlHttpRequest.status == 200 || xmlHttpRequest.readyState == 4){
  140. // alert(xmlHttpRequest.responseText);
  141. // var resJsonObj=JSON.parse(xmlHttpRequest.responseText);
  142. // if (resJsonObj.error != null) {
  143. // alert(resJsonObj.error);
  144. // return;
  145. // }
  146. //
  147. // alert("0");
  148. //
  149. // if (typeof WeixinJSBridge == "undefined") {
  150. // alert("1");
  151. // if (document.addEventListener) {
  152. // document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
  153. // } else if (document.attachEvent) {
  154. // document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
  155. // document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
  156. // }
  157. // } else {
  158. // alert("2");
  159. // onBridgeReady(resJsonObj);
  160. // }
  161. // }
  162. // }
  163. //
  164. // });
  165. $.ajax({
  166. url : '/wxpay/webPay',
  167. method : 'post',
  168. dataType : 'json',
  169. async: false,
  170. contentType: 'application/x-www-form-urlencoded',
  171. data: {
  172. jsonStr : jsonStr
  173. },
  174. success : function(res) {
  175. if (res.error != null) {
  176. alert(res.error);
  177. return;
  178. }
  179. if (typeof WeixinJSBridge == "undefined") {
  180. alert("2");
  181. if (document.addEventListener) {
  182. document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
  183. } else if (document.attachEvent) {
  184. document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
  185. document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
  186. }
  187. } else {
  188. onBridgeReady(res);
  189. }
  190. },
  191. error : function() {
  192. alert('系统异常,请联系客服!');
  193. }
  194. });
  195. // $http.post({
  196. // url : '/wxpay/webPay',
  197. // dataType : 'jsonp',
  198. // async: false,
  199. // contentType: 'application/x-www-form-urlencoded',
  200. // data: {
  201. // jsonStr : jsonStr
  202. // },
  203. // success: function() {
  204. // alert('系统异常!');
  205. // },
  206. // error : function() {
  207. // alert('系统异常,请联系客服!');
  208. // }
  209. // }).success();
  210. }
  211. var getJson = function (proId, amount, payWay, uuid) {
  212. if (typeof uuid == 'undefined') {
  213. return jsonStr = "{proId:" + proId + "," +
  214. "amount:" + amount + "," +
  215. "way:" + "\"" + payWay + "\"" +
  216. "}";
  217. } else {
  218. return jsonStr = "{proId:" + proId + "," +
  219. "amount:" + amount + "," +
  220. "way:" + "\"" + payWay + "\"" + "," +
  221. "uuid:" + uuid +
  222. "}";
  223. }
  224. }
  225. function onBridgeReady(resJsonObj){
  226. WeixinJSBridge.invoke('getBrandWCPayRequest', {
  227. "appId" : resJsonObj.appId, //公众号名称,由商户传入       
  228. "timeStamp" : resJsonObj.timeStamp, //时间戳,自1970年以来的秒数       
  229. "nonceStr" : resJsonObj.nonceStr, //随机串       
  230. "package" : resJsonObj.package,
  231. "signType" : resJsonObj.signType, //微信签名方式:       
  232. "paySign" : resJsonObj.paySign
  233. //微信签名   
  234. },
  235. function (res) {
  236. // 使用以上方式判断前端返回,微信团队郑重提示:res.err_msg将在用户支付成功后返回 ok,但并不保证它绝对可靠。
  237. if (res.err_msg == 'get_brand_wcpay_request:ok') {
  238. alert("支付成功");
  239. //window.location.href = "${returnUrl}";
  240. }else if(res.err_msg == 'get_brand_wcpay_request:cancel'){
  241. alert("取消支付");
  242. }else{
  243. alert("支付失败");
  244. } // 使用以上方式判断前端返回,微信团队郑重提示:res.err_msg将在用户支付成功后返回 ok,但并不保证它绝对可靠。
  245. });
  246. }