Browse Source

connect linepay

huai-sian 3 years ago
parent
commit
26d0cbb1d5
2 changed files with 48 additions and 29 deletions
  1. 48 28
      ArkCard-web/goto.js
  2. 0 1
      ArkCard-web/shop.html

+ 48 - 28
ArkCard-web/goto.js

@@ -74,7 +74,7 @@ function collection(collectuserid='') {
 // 購買頁面
 function shop(collectuserid='') {
   console.log('Call shop(' + collectuserid + ')'); // test
-
+  userId = collectuserid;
   $.ajax({
     method: "GET",
     url: "https://api.ptt.cx:8750/api/v1/line/shop/" + collectuserid + "",
@@ -111,14 +111,45 @@ function shop(collectuserid='') {
         <div class="text-end px-2">\
             <p class="text-start text-white mb-2">※一次只能購買一則NFT收藏品</p>\
             <button type="button" style="border:1px solid #fff; border-radius: 30px;" class="close btn text-white px-3">取消</button>\
-            <input style="border-radius: 30px; background:#fff; border: none;" class="send-btn px-3 ms-1 py-2" type="submit" value="購買">\
+            <span style="border-radius: 30px; background:#fff; border: none;" class="send-btn px-3 ms-1 py-2 btn span">購買</span>\
         </div>\
     </div>\
 </div>';
 
     $('.sec06-nft-mall').html(nftmall);
     $('.buybox').html(buybox);
+    $('.span').click(function(){
+      var nftid = $("input[name='nftid']:checked").val();
+    
+     // var data = `{"nftid":"` + nftid +`","userid":"` + userid +`"}`;
+      
+      //console.log(data); // test
+      
+      var url = `https://api.ptt.cx:8750/api/v1/linepay/request?line_id=${userId}&nft_id=${nftid}`;
+      console.log(url);
+      var xhr = new XMLHttpRequest();
+      xhr.open("POST", url, false);
+      
+      xhr.setRequestHeader("accept", "application/json");
+      xhr.setRequestHeader("Content-Type", "application/json");
+      
+      xhr.onreadystatechange = function () {
+        if (xhr.readyState === 4) {
+          console.log(xhr.status);
+          const res = JSON.parse(xhr.responseText);
+          if(device === 'desktop'){
+            location.replace(res.web);
+          } else {
+            location.replace(res.app);
+          }
+          alert("購買成功");
+        }
+      };
+      
+      xhr.send();
+    })
   });
+  
 }
 
 // Qrcode頁面
@@ -201,41 +232,30 @@ $(".collect-send").submit(function (e) {
   }).then(res => {
     console.log(res);
     window.location.href = './collect.html';
-    
   }).catch(err => {
     console.log(err);
   })
   
 });
 
-$(".nft-buy").submit(function (e) {
-  var nftid = $("input[name='nftid']:checked").val();
-  var userid = $('#userid').val();
+const getDeviceType = () => {
+  const ua = navigator.userAgent;
+  if (/(tablet|ipad|playbook|silk)|(android(?!.*mobi))/i.test(ua)) {
+    return "tablet";
+  }
+  if (
+    /Mobile|iP(hone|od)|Android|BlackBerry|IEMobile|Kindle|Silk-Accelerated|(hpw|web)OS|Opera M(obi|ini)/.test(
+      ua
+    )
+  ) {
+    return "mobile";
+  }
+  return "desktop";
+};
 
-  var data = `{"nftid":"` + nftid +`","userid":"` + userid +`"}`;
-  
-  console.log(data); // test
-  
-  var url = "https://api.ptt.cx:8750/api/v1/line/buy";
+const device = getDeviceType();
 
-  var xhr = new XMLHttpRequest();
-  xhr.open("POST", url, false);
-  
-  xhr.setRequestHeader("accept", "application/json");
-  xhr.setRequestHeader("Content-Type", "application/json");
-  
-  xhr.onreadystatechange = function () {
-    if (xhr.readyState === 4) {
-      console.log(xhr.status);
-      console.log(xhr.responseText);
 
-      alert("購買成功");
-      location.replace("shop.html");
-    }
-  };
-    
-  xhr.send(data);
-});
 
 function copyEvent(id) {
   var str = document.getElementById(id);

+ 0 - 1
ArkCard-web/shop.html

@@ -37,7 +37,6 @@
                     <h1 class="secn-title ps-2">NFT商品</h1>
                 </div>
                 <p class="text-start mb-2">※點選您喜歡的nft商品即可購買</p>
-
                 <div class="sec06-nft-mall row row-cols-1 row-cols-md-4 g-4">
                     <!-- <div class="col">
                         <div class="card h-100">