1document.addEventListener("DOMContentLoaded", function(event) { 2 (function () { 3 var checkPaidInterval = null; 4 var wp_rest_base_url = LN_Paywall.rest_base; 5 var LN_Paywall_Spinner = 6 '<svg class="LNP_spinner" viewBox="0 0 50 50"><circle class="LNP_path" cx="25" cy="25" r="20" fill="none" stroke-width="5"></circle></svg>'; 7 var LN_Paywall_Copy = '<svg xmlns="http://www.w3.org/2000/svg" class="LNP_copy" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor"><path class="LNP_path" stroke-linecap="round" stroke-linejoin="round" d="M15.75 17.25v3.375c0 .621-.504 1.125-1.125 1.125h-9.75a1.125 1.125 0 01-1.125-1.125V7.875c0-.621.504-1.125 1.125-1.125H6.75a9.06 9.06 0 011.5.124m7.5 10.376h3.375c.621 0 1.125-.504 1.125-1.125V11.25c0-4.46-3.243-8.161-7.5-8.876a9.06 9.06 0 00-1.5-.124H9.375c-.621 0-1.125.504-1.125 1.125v3.5m7.5 10.375H9.375a1.125 1.125 0 01-1.125-1.125v-9.25m12 6.625v-1.875a3.375 3.375 0 00-3.375-3.375h-1.5a1.125 1.125 0 01-1.125-1.125v-1.5a3.375 3.375 0 00-3.375-3.375H9.75" /></svg>'; 8 9 function pay(invoice, options) 10 { 11 if (!window.webln) { 12 showQRCode(invoice, options); 13 return startWatchingForPayment(invoice); 14 } 15 return window.webln.enable() 16 .then( 17 function () { 18 window.webln 19 .sendPayment(invoice.payment_request) 20 .then( 21 (response) => { 22 window.LNP_CURRENT_PREIMAGE = 23 response.preimage || response.payment_preimage; 24 } 25 ) 26 .catch( 27 function (e) { 28 console.error(e); 29 showQRCode(invoice, options); 30 } 31 ); 32 return startWatchingForPayment(invoice); 33 } 34 ) 35 .catch( 36 function (err) { 37 console.error(err); 38 showQRCode(invoice, options); 39 return startWatchingForPayment(invoice); 40 } 41 ); 42 } 43 44 function startWatchingForPayment(invoice) 45 { 46 stopWatchingForPayment(); 47 return new Promise( 48 function (resolve, reject) { 49 checkPaidInterval = setInterval( 50 checkPaymentStatus(invoice, resolve), 51 2500 52 ); 53 } 54 ); 55 } 56 57 function stopWatchingForPayment() 58 { 59 window.LNP_CURRENT_PREIMAGE = null; 60 if (checkPaidInterval) { 61 clearTimeout(checkPaidInterval); 62 checkPaidInterval = null; 63 } 64 } 65 66 function checkPaymentStatus(invoice, callback) 67 { 68 if (!invoice || !invoice.token) { 69 console.log("Ligthning invoice missing"); 70 return; 71 } 72 return function () { 73 let body = { 74 token: invoice.token, 75 preimage: window.LNP_CURRENT_PREIMAGE, 76 t: Date.now() 77 }; 78 fetch( 79 wp_rest_base_url+'/invoices/verify', { 80 method: "POST", 81 credentials: "same-origin", 82 cache: "no-cache", 83 body: JSON.stringify(body), 84 headers: { 85 "Content-Type": "application/json", 86 }, 87 } 88 ).then( 89 function (response) { 90 if (response.ok) { 91 response.json().then( 92 function () { 93 stopWatchingForPayment(); 94 callback(invoice); 95 } 96 ); 97 } 98 } 99 ); 100 }; 101 } 102 103 function showQRCode(invoice, options) 104 { 105 options.target.innerHTML = `<div class="wp-lnp-qrcode"> 106 <a href="lightning:${invoice.payment_request 107 }">
107<img src="https://quickchart.io/chart?cht=qr&chs=200x200&chl=${ 108 invoice.payment_request 109 }"></a> 110 <br /> 111 <a href="lightning:${invoice.payment_request}">${invoice.payment_request.substr(0, 36)}...</a><span onClick="navigator.clipboard.writeText('${invoice.payment_request}');" class="wp-lnp-copy">${LN_Paywall_Copy}</span> 112 </div>`; 113 } 114 115 function requestPayment(params, options) 116 { 117 return fetch( 118 wp_rest_base_url+'/invoices', { 119 method: "POST", 120 credentials: "same-origin", 121 cache: "no-cache", 122 body: JSON.stringify(params), 123 headers: { 124 "Content-Type": "application/json", 125 }, 126 } 127 ) 128 .then( 129 function (resp) { 130 return resp.json(); 131 } 132 ) 133 .then( 134 function (invoice) { 135 if (!invoice || !invoice.payment_request) { 136 console.error("Failed to generate lightning invoice", invoice); 137 throw new Error("Failed to generate lightning invoice " + JSON.stringify(invoice)); 138 } 139 return pay(invoice, options); 140 } 141 ); 142 } 143 144 function initWeblnButtons() 145 { 146 var buttons = document.querySelectorAll('.wp-lnp-webln-button') 147 if (buttons.length === 0) { 148 return; 149 } 150 buttons.forEach( 151 function (button) { 152 button.addEventListener( 153 "click", function (e) { 154 e.preventDefault(); 155 this.setAttribute("disabled", ""); 156 157 var thankyouMessage = this.dataset.success || "Thanks!"; 158 var paymentRequestArgs = { amount: this.dataset.amount, currency: this.dataset.currency }; 159 var wrapper = this.closest(".wp-lnp-webln-button-wrapper"); 160 161 wrapper.innerHTML = '<div class="wp-lnp-webln-button-spinner">' + LN_Paywall_Spinner + '</div>'; 162 163 requestPayment( 164 paymentRequestArgs, 165 { target: wrapper } 166 ) 167 .then( 168 function (invoice) { 169 wrapper.innerHTML = '<div class="wp-lnp-webln-button-thanks">' + thankyouMessage + '</div>'; 170 } 171 ) 172 .catch( 173 function (e) { 174 console.error(e); 175 alert("Sorry, something went wrong."); 176 } 177 ); 178 } 179 ); 180 } 181 ); 182 } 183 184 initWeblnButtons(); 185 })(); 186});
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.