SatSale

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

satsale.js (4617B)


      1 // Payment logic, talks to satsale.py
      2 function payment(payment_data) {
      3     $('document').ready(function(){
      4         var payment_uuid;
      5         var invoiceData = {amount: payment_data.amount, currency: payment_data.currency, method: payment_data.method};
      6 
      7         // If a webhook URL is provided (woocommerce)
      8         if (payment_data.w_url) {
      9             invoiceData['w_url'] = payment_data.w_url
     10         }
     11 
     12         $.get("/api/createpayment", invoiceData).then(function(data) {
     13             invoice = data.invoice;
     14             payment_uuid = invoice.uuid;
     15 
     16             $('#address').text(invoice.address).html();
     17             $('#amount').text(invoice.btc_value).html();
     18             $('#amount_sats').text(Math.round(invoice.btc_value * 10**8)).html();
     19             $('#timer').text(Math.round(invoice.time_left)).html();
     20             $('#paymentDetails').show();
     21 
     22             if (invoice.btc_value >= invoice.onchain_dust_limit) {
     23                 $('#paymentMethodSwitchButton').show();
     24             }
     25 
     26             return payment_uuid
     27         }, function(data) {
     28             console.error(data["responseJSON"]["message"]);
     29             $('#error').show();
     30             $('#error_message').text(data["responseJSON"]["message"]).html();
     31             return "";
     32         }).then(function(payment_uuid) {
     33             if (payment_uuid != "") {
     34                 load_qr(payment_uuid);
     35                 document.getElementById('timerContainer').style.visibility = "visible";
     36 
     37                 // Pass payment uuid and the interval process to check_payment
     38                 var checkinterval = setInterval(function() {check_payment(payment_uuid, checkinterval, payment_data);}, 1000);
     39             }
     40         })
     41     });
     42 }
     43 
     44 function check_payment(payment_uuid, checkinterval, payment_data) {
     45     $.get("/api/checkpayment", {uuid: payment_uuid}).then(function(checkpayment_data) {
     46         payment_status = checkpayment_data.status;
     47         console.log(payment_status);
     48         if (payment_status.expired == 1) {
     49             $('#status').text("Payment expired.").html();
     50             document.getElementById('timerContainer').style.visibility = "hidden";
     51             clearInterval(checkinterval);
     52             return 1;
     53         }
     54 
     55         if (payment_status.payment_complete == 1) {
     56             $('#status').text("Payment confirmed.").html();
     57             document.getElementById('timerContainer').style.visibility = "hidden";
     58             complete_payment(payment_uuid, payment_data);
     59             // clearInterval(checkinterval);
     60             return 1;
     61         }
     62         else {
     63             if (payment_status.unconfirmed_paid > 0) {
     64                 $('#status').text("Discovered payment. Waiting for more confirmations...").html();
     65                 return 0;
     66             }
     67             else {
     68                 $('#status').text("Waiting for payment...").html();
     69                 return 0;
     70             }
     71         }
     72     });
     73 }
     74 
     75 function complete_payment(payment_uuid, payment_data) {
     76     var order_id = location.search.split('id=')[1];
     77     $.get("/api/completepayment", {uuid: payment_uuid, id: order_id}).then(function(payment_completion) {
     78         console.log(payment_completion);
     79         $('#status').text(payment_completion.message).html();
     80     });
     81     setTimeout(() => {  window.location.replace(payment_data.redirect);  }, 5000);
     82 }
     83 
     84 function load_qr(payment_uuid) {
     85     // Display QR code
     86     if (payment_uuid != null) {
     87         // Change image id to qr id
     88         document.getElementById('qrImage').className = "qr";
     89         // Insert image and link
     90         document.getElementById('qrClick').href = "/static/qr_codes/" + payment_uuid + ".png";
     91         document.getElementById('qrImage').src = "/static/qr_codes/" + payment_uuid + ".png";
     92     }
     93 }
     94 
     95 function replaceUrlParam(url, paramName, paramValue)
     96 {
     97     console.log(url);
     98     var href = new URL(url);
     99     href.searchParams.set(paramName, paramValue);
    100     window.location = href;
    101     return
    102 }
    103 
    104 // Payment timer, can't go below zero, update every second
    105 intervalTimer = setInterval(function () {
    106     var currentTime = document.getElementById('timer').innerHTML;
    107     if (currentTime <= 0) {
    108         currentTime = 1;
    109     }
    110     document.getElementById('timer').innerHTML = Math.round(currentTime - 1);
    111 }, 1000)
    112 
    113 // Copy text functions
    114 function copyText(text) {
    115   navigator.clipboard.writeText(text);
    116 }
    117 function copyTextFromElement(elementID) {
    118   let element = document.getElementById(elementID); //select the element
    119   let elementText = element.textContent; //get the text content from the element
    120   copyText(elementText); //use the copyText function below
    121   alert("Copied address:" + elementText)
    122 }