2023-02-08 07:47:38 +01:00
|
|
|
@using BTCPayServer.Abstractions.Extensions
|
|
|
|
@using BTCPayServer.Abstractions.TagHelpers
|
|
|
|
<template id="lnurl-withdraw-template">
|
2023-02-20 00:49:10 +01:00
|
|
|
<template v-if="display">
|
2023-02-21 21:06:36 +01:00
|
|
|
<div class="mt-4">
|
|
|
|
<p id="CheatSuccessMessage" class="alert alert-success text-break" v-if="successMessage" v-text="successMessage"></p>
|
|
|
|
<p id="CheatErrorMessage" class="alert alert-danger text-break" v-if="errorMessage" v-text="errorMessage"></p>
|
|
|
|
<button v-if="isV2" class="btn btn-secondary rounded-pill w-100" type="button"
|
|
|
|
:disabled="scanning || submitting" v-on:click="startScan" :id="btnId"
|
|
|
|
:class="{ 'loading': scanning || submitting, 'text-secondary': !supported }">{{btnText}}</button>
|
|
|
|
<bp-loading-button v-else>
|
|
|
|
<button class="action-button" style="margin: 0 45px;width:calc(100% - 90px) !important"
|
|
|
|
:disabled="scanning || submitting" v-on:click="startScan" :id="btnId"
|
|
|
|
:class="{ 'loading': scanning || submitting, 'action-button': supported, 'btn btn-text w-100': !supported }">
|
|
|
|
<span class="button-text">{{btnText}}</span>
|
|
|
|
<div class="loader-wrapper">
|
|
|
|
@await Html.PartialAsync("~/Views/UIInvoice/Checkout-Spinner.cshtml")
|
|
|
|
</div>
|
|
|
|
</button>
|
|
|
|
</bp-loading-button>
|
|
|
|
</div>
|
2023-02-20 00:49:10 +01:00
|
|
|
</template>
|
2023-02-08 07:47:38 +01:00
|
|
|
</template>
|
|
|
|
<script type="text/javascript">
|
|
|
|
Vue.component("lnurl-withdraw-checkout", {
|
|
|
|
template: "#lnurl-withdraw-template",
|
2023-02-20 00:49:10 +01:00
|
|
|
props: {
|
|
|
|
model: Object,
|
|
|
|
isV2: Boolean
|
|
|
|
},
|
2023-02-08 07:47:38 +01:00
|
|
|
computed: {
|
2023-02-20 00:49:10 +01:00
|
|
|
display: function () {
|
2023-02-22 07:53:14 +01:00
|
|
|
const {
|
|
|
|
onChainWithLnInvoiceFallback: isUnified,
|
|
|
|
paymentMethodId: activePaymentMethodId,
|
|
|
|
availableCryptos: availablePaymentMethods,
|
|
|
|
invoiceBitcoinUrl: paymentUrl
|
|
|
|
} = this.model
|
|
|
|
const lnurlwAvailable =
|
|
|
|
// Either we have LN or LNURL available directly
|
|
|
|
!!availablePaymentMethods.find(pm => ['BTC_LNURLPAY', 'BTC_LightningLike'].includes(pm.paymentMethodId)) ||
|
|
|
|
// Or the BIP21 payment URL flags Lightning support
|
|
|
|
!!paymentUrl.match(/lightning=ln/i)
|
|
|
|
return activePaymentMethodId === 'BTC_LNURLPAY' || (
|
|
|
|
// Unified QR/BIP21 case
|
|
|
|
(activePaymentMethodId === 'BTC' && isUnified && lnurlwAvailable) ||
|
|
|
|
// Lightning with LNURL available
|
|
|
|
(activePaymentMethodId === 'BTC_LightningLike' && lnurlwAvailable))
|
|
|
|
},
|
|
|
|
btnId: function () {
|
|
|
|
return this.supported ? 'PayByNFC' : 'PayByLNURL'
|
2023-02-08 07:47:38 +01:00
|
|
|
},
|
2023-02-20 00:49:10 +01:00
|
|
|
btnText: function () {
|
|
|
|
if (this.supported) {
|
|
|
|
return this.isV2 ? this.$t('pay_by_nfc') : 'Pay by NFC (LNURL-Withdraw)'
|
|
|
|
} else {
|
|
|
|
return this.isV2 ? this.$t('pay_by_lnurl') : 'Pay by LNURL-Withdraw'
|
|
|
|
}
|
2023-02-08 07:47:38 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
data: function () {
|
|
|
|
return {
|
2023-02-20 00:49:10 +01:00
|
|
|
url: @Safe.Json(Context.Request.GetAbsoluteUri(Url.Action("SubmitLNURLWithdrawForInvoice", "NFC"))),
|
2023-02-08 07:47:38 +01:00
|
|
|
supported: ('NDEFReader' in window && window.self === window.top),
|
|
|
|
scanning: false,
|
|
|
|
submitting: false,
|
|
|
|
readerAbortController: null,
|
2023-02-21 21:06:36 +01:00
|
|
|
amount: 0,
|
|
|
|
successMessage: null,
|
|
|
|
errorMessage: null
|
2023-02-08 07:47:38 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
startScan: async function () {
|
|
|
|
try {
|
|
|
|
if (this.scanning || this.submitting) {
|
|
|
|
return;
|
|
|
|
}
|
2023-02-20 00:49:10 +01:00
|
|
|
if (this.model.isUnsetTopUp) {
|
2023-02-08 07:47:38 +01:00
|
|
|
const amountStr = prompt("How many sats do you want to pay?")
|
2023-02-21 21:06:36 +01:00
|
|
|
if (amountStr) {
|
2023-02-08 07:47:38 +01:00
|
|
|
try {
|
|
|
|
this.amount = parseInt(amountStr)
|
|
|
|
} catch {
|
|
|
|
alert("Please provide a valid number amount in sats");
|
|
|
|
}
|
2023-02-21 21:06:36 +01:00
|
|
|
} else {
|
2023-02-08 07:47:38 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const self = this;
|
|
|
|
self.submitting = false;
|
|
|
|
self.scanning = true;
|
|
|
|
if (!this.supported) {
|
2023-02-21 21:06:36 +01:00
|
|
|
const result = prompt("Enter LNURL-Withdraw");
|
2023-02-08 07:47:38 +01:00
|
|
|
if (result) {
|
2023-02-21 21:06:36 +01:00
|
|
|
await self.sendData.bind(self)(result);
|
2023-02-08 07:47:38 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
self.scanning = false;
|
|
|
|
}
|
|
|
|
ndef = new NDEFReader()
|
|
|
|
self.readerAbortController = new AbortController()
|
|
|
|
await ndef.scan({signal: self.readerAbortController.signal})
|
|
|
|
|
|
|
|
ndef.addEventListener('readingerror', () => {
|
|
|
|
self.scanning = false;
|
|
|
|
self.readerAbortController.abort()
|
|
|
|
})
|
|
|
|
|
2023-02-21 21:06:36 +01:00
|
|
|
ndef.addEventListener('reading', async ({message, serialNumber}) => {
|
2023-02-08 07:47:38 +01:00
|
|
|
//Decode NDEF data from tag
|
|
|
|
const record = message.records[0]
|
|
|
|
const textDecoder = new TextDecoder('utf-8')
|
|
|
|
const lnurl = textDecoder.decode(record.data)
|
|
|
|
|
|
|
|
//User feedback, show loader icon
|
|
|
|
self.scanning = false;
|
2023-02-21 21:06:36 +01:00
|
|
|
await self.sendData.bind(self)(lnurl);
|
2023-02-08 07:47:38 +01:00
|
|
|
})
|
2023-02-21 21:06:36 +01:00
|
|
|
} catch (e) {
|
2023-02-08 07:47:38 +01:00
|
|
|
self.scanning = false;
|
|
|
|
self.submitting = false;
|
|
|
|
}
|
|
|
|
},
|
2023-02-21 21:06:36 +01:00
|
|
|
sendData: async function (lnurl) {
|
2023-02-08 07:47:38 +01:00
|
|
|
this.submitting = true;
|
2023-02-21 21:06:36 +01:00
|
|
|
this.successMessage = null;
|
|
|
|
this.errorMessage = null;
|
|
|
|
|
|
|
|
// Post LNURL-Withdraw data to server
|
|
|
|
const body = JSON.stringify({ lnurl, invoiceId: this.model.invoiceId, amount: this.amount })
|
|
|
|
const opts = { method: 'POST', headers: { 'Content-Type': 'application/json' }, body }
|
|
|
|
const response = await fetch(this.url, opts)
|
|
|
|
|
|
|
|
// Handle response
|
|
|
|
try {
|
|
|
|
const result = await response.text()
|
|
|
|
if (response.ok) {
|
|
|
|
this.successMessage = result;
|
|
|
|
} else {
|
|
|
|
this.errorMessage = result;
|
2023-02-08 07:47:38 +01:00
|
|
|
}
|
2023-02-21 21:06:36 +01:00
|
|
|
} catch (error) {
|
|
|
|
this.errorMessage = error;
|
|
|
|
}
|
|
|
|
this.scanning = false;
|
|
|
|
this.submitting = false;
|
|
|
|
if (this.readerAbortController) {
|
|
|
|
this.readerAbortController.abort()
|
2023-02-08 07:47:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
</script>
|