| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277 |
- /**
- * Created by 黄诚天 on 2017-11-03.
- */
- // var proId;
- // var amount;
- // var payWay;//0支付宝 1微信 2银联
- // var uuid = "10041166";
- const userAgent = navigator.userAgent;
- const isMobile = /(iPhone|iPad|Opera Mini|Android.*Mobile|NetFront|PSP|BlackBerry|Windows Phone)/ig.test(userAgent);
- var orderInterval;
- var pay =function (proId, amount, payWay, uuid) {
- var customAmount = $("#custom-amount" + proId).val();
- console.log(customAmount);
- if ($.trim(customAmount) != "") {
- var reg = "^(([0-9]+\.[0-9]{1,2})|([0-9]*[1-9][0-9]*\.[0-9]{1,2})|([0-9]*[1-9][0-9]*))$";
- var patt = new RegExp(reg);
- var r = patt.test(customAmount);
- if (r) {
- if (parseFloat(customAmount) < 10000000) {
- amount = customAmount;
- } else {
- alert("您输入的金额过大,请不要超过一千万");
- return;
- }
- } else {
- alert('输入金额有误,请正确输入金额且最多保留两位小数!');
- return;
- }
- }
- var yuanIndex = amount.toString().indexOf("元");
- if (yuanIndex > 0) {
- amount = amount.toString().substring(0, yuanIndex);
- }
- //获取支付方式
- if(payWay == "支付宝") {
- aliPay(proId, amount, payWay, uuid);
- } else if (payWay == "微信支付") {
- wxPay(proId, amount, payWay, uuid);
- } else if (payWay == "网银支付") {
- unionPay();
- }
- }
- var wxPay = function (proId, amount, payWay, uuid) {
- var jsonStr = getJson(proId, amount, payWay, uuid);
- //公众号支付js模板 需引入http://res.wx.qq.com/open/js/jweixin-1.0.0.js
- //对浏览器的UserAgent进行正则匹配,不含有微信独有标识的则为其他浏览器
- //var userAgent = navigator.userAgent;
- if (userAgent.match(/MicroMessenger/i) == 'MicroMessenger') {
- //公众号支付逻辑(微信浏览器)
- webPay(jsonStr);
- // $.ajax({
- // url : "/wxpay/userAdvanced",
- // type : "GET",
- // async: false,
- // success : function() {
- // webPay(jsonStr);
- // },
- // error : function() {
- // alert("请求openid失败");
- // }
- // });
- }else{
- //H5支付逻辑
- var url = "/wxpay/pcPay";
- var dataType = "html"
- if (isMobile) {
- url = "/wxpay/wapPay";
- dataType = "json"
- }
- $.ajax({
- type : "POST",
- url : url,
- dataType : dataType,
- async: false,
- //contentType : 'application/json;charset=UTF-8',
- data: {
- jsonStr : jsonStr
- },
- success : function(res){
- var resJsonObj=JSON.parse(res);
- if (resJsonObj.error != null) {
- alert(resJsonObj.error);
- return;
- }
- if (isMobile) {
- window.location.href = resJsonObj.mwebUrl;
- } else {
- document.cookie = 'qrcodeUrl='+encodeURIComponent(resJsonObj.qrcodeUrl);
- window.location.href = "http://lj.ubtob.com/project#/donateQrcode/" + resJsonObj.outTradeNo;
- }
- },
- error : function() {
- alert('系统异常,请联系客服!');
- }
- });
- }
- }
- var aliPay = function (proId, amount, payWay, uuid) {
- var jsonStr = getJson(proId, amount, payWay, uuid);
- var url = "/alipay/pcPay";
- if (isMobile) {
- url = "/alipay/wapPay";
- }
- $.ajax({
- type : "POST",
- url : url,
- dataType : "html",
- //contentType : "application/json;charset=UTF-8",
- async: false,
- data: {
- jsonStr : jsonStr
- },
- success : function(data) {
- document.write(data);
- },
- error : function() {
- alert('系统异常,请联系客服!');
- }
- });
- }
- var unionPay = function () {
- }
- /*公众号支付*/
- var webPay = function (jsonStr) {
- // var xmlHttpRequest;
- //
- // $(function(){
- // if(window.XMLHttpRequest){
- // xmlHttpRequest=new XMLHttpRequest();
- // }else{
- // xmlHttpRequest=new ActiveXObject("Microsoft.XMLHTTP");
- // }
- // //xmlHttpRequest.responseType = 'json';
- // //xmlHttpRequest.setRequestHeader('Content-type','application/x-www-form-urlencoded');
- // xmlHttpRequest.onreadystatechange = back;
- // xmlHttpRequest.open('POST','/wxpay/webPay',true);
- // xmlHttpRequest.send('jsonStr=' + jsonStr);
- // function back() {
- // if(xmlHttpRequest.status == 200 || xmlHttpRequest.readyState == 4){
- // alert(xmlHttpRequest.responseText);
- // var resJsonObj=JSON.parse(xmlHttpRequest.responseText);
- // if (resJsonObj.error != null) {
- // alert(resJsonObj.error);
- // return;
- // }
- //
- // alert("0");
- //
- // if (typeof WeixinJSBridge == "undefined") {
- // alert("1");
- // if (document.addEventListener) {
- // document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
- // } else if (document.attachEvent) {
- // document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
- // document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
- // }
- // } else {
- // alert("2");
- // onBridgeReady(resJsonObj);
- // }
- // }
- // }
- //
- // });
- $.ajax({
- url : '/wxpay/webPay',
- method : 'post',
- dataType : 'json',
- async: false,
- contentType: 'application/x-www-form-urlencoded',
- data: {
- jsonStr : jsonStr
- },
- success : function(res) {
- if (res.error != null) {
- alert(res.error);
- return;
- }
- if (typeof WeixinJSBridge == "undefined") {
- alert("2");
- if (document.addEventListener) {
- document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
- } else if (document.attachEvent) {
- document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
- document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
- }
- } else {
- onBridgeReady(res);
- }
- },
- error : function() {
- alert('系统异常,请联系客服!');
- }
- });
- // $http.post({
- // url : '/wxpay/webPay',
- // dataType : 'jsonp',
- // async: false,
- // contentType: 'application/x-www-form-urlencoded',
- // data: {
- // jsonStr : jsonStr
- // },
- // success: function() {
- // alert('系统异常!');
- // },
- // error : function() {
- // alert('系统异常,请联系客服!');
- // }
- // }).success();
- }
- var getJson = function (proId, amount, payWay, uuid) {
- if (typeof uuid == 'undefined') {
- return jsonStr = "{proId:" + proId + "," +
- "amount:" + amount + "," +
- "way:" + "\"" + payWay + "\"" +
- "}";
- } else {
- return jsonStr = "{proId:" + proId + "," +
- "amount:" + amount + "," +
- "way:" + "\"" + payWay + "\"" + "," +
- "uuid:" + uuid +
- "}";
- }
- }
- function onBridgeReady(resJsonObj){
- WeixinJSBridge.invoke('getBrandWCPayRequest', {
- "appId" : resJsonObj.appId, //公众号名称,由商户传入
- "timeStamp" : resJsonObj.timeStamp, //时间戳,自1970年以来的秒数
- "nonceStr" : resJsonObj.nonceStr, //随机串
- "package" : resJsonObj.package,
- "signType" : resJsonObj.signType, //微信签名方式:
- "paySign" : resJsonObj.paySign
- //微信签名
- },
- function (res) {
- // 使用以上方式判断前端返回,微信团队郑重提示:res.err_msg将在用户支付成功后返回 ok,但并不保证它绝对可靠。
- if (res.err_msg == 'get_brand_wcpay_request:ok') {
- alert("支付成功");
- //window.location.href = "${returnUrl}";
- }else if(res.err_msg == 'get_brand_wcpay_request:cancel'){
- alert("取消支付");
- }else{
- alert("支付失败");
- } // 使用以上方式判断前端返回,微信团队郑重提示:res.err_msg将在用户支付成功后返回 ok,但并不保证它绝对可靠。
- });
- }
|