第三方支付對接開發

weixin_33912246發表於2018-04-09
  1 <script type="text/javascript">    
  2 
  3     var myTimer;
  4     var strcode = 'HTTPS://QR.ALIPAY.COM/FKX06368SFHS1MACJD0L03';
  5 
  6     function timer(intDiff) {
  7         myTimer = window.setInterval(function () {
  8             var day = 0,
  9                 hour = 0,
 10                 minute = 0,
 11                 second = 0;//時間預設值
 12             if (intDiff > 0) {
 13                 day = Math.floor(intDiff / (60 * 60 * 24));
 14                 hour = Math.floor(intDiff / (60 * 60)) - (day * 24);
 15                 minute = Math.floor(intDiff / 60) - (day * 24 * 60) - (hour * 60);
 16                 second = Math.floor(intDiff) - (day * 24 * 60 * 60) - (hour * 60 * 60) - (minute * 60);
 17             }
 18             if (minute <= 9) minute = '0' + minute;
 19             if (second <= 9) second = '0' + second;
 20             $('#hour_show').html('<s id="h"></s>' + hour + '時');
 21             $('#minute_show').html('<s></s>' + minute + '分');
 22             $('#second_show').html('<s></s>' + second + '秒');
 23             if (hour <= 0 && minute <= 0 && second <= 0) {
 24                 qrcode_timeout();
 25                 clearInterval(myTimer);
 26             }
 27             intDiff--;
 28             
 29             if (strcode != ""){
 30                 checkdata();
 31             }
 32             
 33         }, 2000);
 34     }
 35 
 36     function checkdata(){
 37         $.post(
 38             "/getresult",
 39             {
 40                 paysapi_id : "babc6773eba8a7ae22c13d3a",
 41             },
 42             function(data){
 43                 if (data.code > 0){
 44                     window.clearInterval(timer);
 45                     $("#show_qrcode").attr("src","images/pay_ok.png");
 46                     $("#use").remove();
 47                     $("#money").text("支付成功");
 48                     $("#msg").html("<h1>即將返回商家頁</h1>");
 49                     if (isMobile() == 1){
 50                         $(".paybtn").html('<a href="' + data.url + '" class="btn btn-primary">返回商家頁</a>');
 51                         setTimeout(function(){
 52                             // window.location = data.url;
 53                             location.replace(data.url)
 54                         }, 3000);
 55                     }else{
 56                         $("#msg").html("<h1>即將<a href='https://www.paysapi.com/'>跳轉</a>回商家頁</h1>");
 57                         setTimeout(function(){
 58                             // window.location = data.url;
 59                             location.replace(data.url)
 60                         }, 3000);
 61                     }
 62                     
 63                 }
 64             }
 65         );
 66     }
 67 
 68     function qrcode_timeout(){
 69         $('#show_qrcode').attr("src","images/qrcode_timeout.png");
 70         $("#use").hide();
 71         $('#msg').html("<h1>請重新整理本頁</h1>");
 72         
 73     }
 74 
 75     function isWeixin() { 
 76         var ua = window.navigator.userAgent.toLowerCase(); 
 77         if (ua.match(/MicroMessenger/i) == 'micromessenger') { 
 78             return 1;
 79         } else { 
 80             return 0;
 81         } 
 82     }
 83 
 84     function isMobile() {
 85         var ua = navigator.userAgent.toLowerCase();
 86         _long_matches = 'googlebot-mobile|android|avantgo|blackberry|blazer|elaine|hiptop|ip(hone|od)|kindle|midp|mmp|mobile|o2|opera mini|palm( os)?|pda|plucker|pocket|psp|smartphone|symbian|treo|up\.(browser|link)|vodafone|wap|windows ce; (iemobile|ppc)|xiino|maemo|fennec';
 87         _long_matches = new RegExp(_long_matches);
 88         _short_matches = '1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|e\-|e\/|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(di|rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|xda(\-|2|g)|yas\-|your|zeto|zte\-';
 89         _short_matches = new RegExp(_short_matches);
 90         if (_long_matches.test(ua)) {
 91             return 1;
 92         }
 93         user_agent = ua.substring(0, 4);
 94         if (_short_matches.test(user_agent)) {
 95             return 1;
 96         }
 97         return 0;
 98     }
 99     //本地生成二維碼
100     function showCodeImage(){
101         var qrcode = $('#qrcode').qrcode({  
102             text: 'HTTPS://QR.ALIPAY.COM/FKX06368SFHS1MACJD0L03',  
103             width: 200,  
104             height: 200,
105         }).hide();  
106         //新增文字  
107         var outTime = '過期時間:2018-04-08 23:09:22';//過期時間
108         var canvas = qrcode.find('canvas').get(0);  
109         var oldCtx = canvas.getContext('2d');  
110         var imgCanvas = document.getElementById('imgCanvas');  
111         var ctx = imgCanvas.getContext('2d');  
112         ctx.fillStyle = 'white';  
113         ctx.fillRect(0,0,310,270);  
114         ctx.putImageData(oldCtx.getImageData(0, 0, 200, 200), 55, 20);  
115         //ctx.stroke = 3;  
116         ctx.textBaseline = 'middle';  
117         ctx.textAlign = 'center';  
118         ctx.font ="15px Arial";  
119         ctx.fillStyle = '#00c800';
120         ctx.strokeStyle = '#00c800'
121         ctx.fillText(outTime, imgCanvas.width / 2, 235 );  
122         ctx.strokeText(outTime, imgCanvas.width / 2, 235);  
123 
124         var about = '過期後請勿支付,不自動到賬'; 
125         ctx.fillText(about, imgCanvas.width / 2, 260 );  
126         ctx.strokeText(about, imgCanvas.width / 2, 260);  
127 
128         imgCanvas.style.display = 'none';  
129         $('#show_qrcode').attr('src', imgCanvas.toDataURL('image/png')).css({  
130             width: 310,height:270  
131         }); 
132         // $('#downloadbtn').attr('href', imgCanvas.toDataURL('image/png'));
133     }
134 
135     $().ready(function(){
136         //預設6分鐘過期
137         timer("300");
138         var istype = "1";
139         var suremoney = "1";
140         var uaa = navigator.userAgent;
141         var isiOS = !!uaa.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios終端
142         if (isMobile() == 1){
143             if (isWeixin() == 1 && istype == 2){
144                 //微信內建瀏覽器+微信支付
145                 $("#showtext").text("長按二維碼識別");
146             } else{
147                 //其他手機瀏覽器+支付寶支付
148                 if (isWeixin() == 0 && istype == 1){
149                     $(".paybtn").attr('style','');
150                     var goPay = '<span id="goPay"> <span>';
151                     //給A標籤中的文字新增一個能被jQuery捕獲的元素
152                     $('#alipaybtn').append(goPay);
153                     //模擬點選A標籤中的文字
154                     $('#goPay').click();
155 
156                     $('#msg').html("<h1>支付完成後,請返回此頁</h1>");
157                     $(".qrcode-img-wrapper").remove();
158                     $(".tip").remove();
159                     $(".foot").remove();                                      
160 
161                     //$(location).attr('href', 'HTTPS://QR.ALIPAY.COM/FKX06368SFHS1MACJD0L03');
162                 } else {
163                     if (isWeixin() == 0 && istype == 2){
164                         //其他手機瀏覽器+微信支付
165                         //IOS的排除掉
166                         if (isiOS){
167                             // showCodeImage();
168 
169                             $('.iospayweixinbtn').attr('style','padding-top: 15px;');
170                         }else{
171                             $(".payweixinbtn").attr('style','padding-top: 15px;');
172                         }                    
173                         $("#showtext").html("請儲存二維碼到手機<br>微信掃一掃點右上角-從相簿選取");
174                     }
175                 }
176             }
177         }
178 
179 
180         if (isiOS){
181             $('#show_qrcode').css({width: 310,height:310});  
182         }else{
183             var show_expire_time = '1523200162';
184             if(show_expire_time!='0'){
185                 if (document.getElementById("imgCanvas").getContext){
186                     try {
187                         showCodeImage();
188                     } catch (error) {  
189                         $('#show_qrcode').attr('src', "http://mobile.qq.com/qrcode?url=HTTPS://QR.ALIPAY.COM/FKX06368SFHS1MACJD0L03"); 
190                     }
191                 }else{
192                     $('#show_qrcode').attr('src', "http://mobile.qq.com/qrcode?url=HTTPS://QR.ALIPAY.COM/FKX06368SFHS1MACJD0L03"); 
193                     // $('#downloadbtn').attr('href', "http://mobile.qq.com/qrcode?url=HTTPS://QR.ALIPAY.COM/FKX06368SFHS1MACJD0L03");
194                 }
195             }else{
196                 $('#show_qrcode').attr('src', "http://mobile.qq.com/qrcode?url=HTTPS://QR.ALIPAY.COM/FKX06368SFHS1MACJD0L03");     
197                 // $('#downloadbtn').attr('href', "http://mobile.qq.com/qrcode?url=HTTPS://QR.ALIPAY.COM/FKX06368SFHS1MACJD0L03");
198             }
199         }
200         
201     });
202 
203 
204 
205    </script>

 

相關文章