2023-02-08 07:47:38 +01:00
|
|
|
<template id="lnurl-withdraw-template">
|
2023-11-30 10:17:23 +01:00
|
|
|
<div v-if="display" class="mt-4" id="NFC">
|
|
|
|
<div v-if="nfcErrorMessage" class="alert alert-danger" v-text="nfcErrorMessage"></div>
|
|
|
|
<template v-if="isV2">
|
|
|
|
<button class="btn btn-secondary rounded-pill w-100" type="button" id="PayByNFC"
|
|
|
|
:disabled="nfcScanning || submitting" v-on:click="handleClick">{{btnText}}</button>
|
|
|
|
</template>
|
|
|
|
<template v-else>
|
|
|
|
<bp-loading-button>
|
|
|
|
<button class="action-button" style="margin: 0 45px;width:calc(100% - 90px) !important" :disabled="nfcScanning || submitting" v-on:click="handleClick" id="PayByNFC"
|
|
|
|
:class="{ 'action-button': nfcSupported, 'btn btn-text w-100': !nfcSupported }">
|
2023-02-21 21:06:36 +01:00
|
|
|
<span class="button-text">{{btnText}}</span>
|
|
|
|
<div class="loader-wrapper">
|
|
|
|
@await Html.PartialAsync("~/Views/UIInvoice/Checkout-Spinner.cshtml")
|
|
|
|
</div>
|
|
|
|
</button>
|
|
|
|
</bp-loading-button>
|
2023-11-30 10:17:23 +01:00
|
|
|
</template>
|
|
|
|
</div>
|
2023-02-08 07:47:38 +01:00
|
|
|
</template>
|
2023-06-26 16:45:34 +02:00
|
|
|
<script>
|
2023-02-08 07:47:38 +01:00
|
|
|
Vue.component("lnurl-withdraw-checkout", {
|
|
|
|
template: "#lnurl-withdraw-template",
|
2023-02-20 00:49:10 +01:00
|
|
|
props: {
|
|
|
|
model: Object,
|
2023-11-30 10:17:23 +01:00
|
|
|
isV2: Boolean,
|
|
|
|
nfcSupported: Boolean,
|
|
|
|
nfcScanning: Boolean,
|
|
|
|
nfcErrorMessage: String
|
2023-02-20 00:49:10 +01:00
|
|
|
},
|
2023-02-08 07:47:38 +01:00
|
|
|
computed: {
|
2023-03-27 18:28:53 +02:00
|
|
|
display () {
|
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)
|
2023-03-28 12:36:32 +02:00
|
|
|
const isAvailable = activePaymentMethodId === 'BTC_LNURLPAY' || (
|
2023-02-22 07:53:14 +01:00
|
|
|
// Unified QR/BIP21 case
|
|
|
|
(activePaymentMethodId === 'BTC' && isUnified && lnurlwAvailable) ||
|
|
|
|
// Lightning with LNURL available
|
|
|
|
(activePaymentMethodId === 'BTC_LightningLike' && lnurlwAvailable))
|
2023-11-30 10:17:23 +01:00
|
|
|
return isAvailable && (this.nfcSupported || this.testFallback)
|
2023-02-22 07:53:14 +01:00
|
|
|
},
|
2023-03-28 12:36:32 +02:00
|
|
|
testFallback () {
|
2023-11-30 10:17:23 +01:00
|
|
|
return !this.nfcSupported && window.location.search.match('lnurlwtest=(1|true)')
|
2023-02-08 07:47:38 +01:00
|
|
|
},
|
2023-03-27 18:28:53 +02:00
|
|
|
btnText () {
|
2023-11-30 10:17:23 +01:00
|
|
|
if (this.nfcSupported) {
|
2023-03-27 18:28:53 +02:00
|
|
|
if (this.submitting) {
|
|
|
|
return this.isV2 ? this.$t('submitting_nfc') : 'Submitting NFC …'
|
2023-11-30 10:17:23 +01:00
|
|
|
} else if (this.nfcScanning) {
|
2023-03-27 18:28:53 +02:00
|
|
|
return this.isV2 ? this.$t('scanning_nfc') : 'Scanning NFC …'
|
|
|
|
} else {
|
|
|
|
return this.isV2 ? this.$t('pay_by_nfc') : 'Pay by NFC'
|
|
|
|
}
|
2023-02-20 00:49:10 +01:00
|
|
|
} else {
|
|
|
|
return this.isV2 ? this.$t('pay_by_lnurl') : 'Pay by LNURL-Withdraw'
|
|
|
|
}
|
2023-02-08 07:47:38 +01:00
|
|
|
}
|
|
|
|
},
|
2023-03-27 18:28:53 +02:00
|
|
|
data () {
|
2023-02-08 07:47:38 +01:00
|
|
|
return {
|
2023-02-20 00:49:10 +01:00
|
|
|
url: @Safe.Json(Context.Request.GetAbsoluteUri(Url.Action("SubmitLNURLWithdrawForInvoice", "NFC"))),
|
2023-02-21 21:06:36 +01:00
|
|
|
amount: 0,
|
2023-11-30 10:17:23 +01:00
|
|
|
submitting: false
|
2023-02-08 07:47:38 +01:00
|
|
|
}
|
|
|
|
},
|
2023-11-30 10:17:23 +01:00
|
|
|
beforeMount () {
|
|
|
|
this.$root.$on('read-nfc-data', this.sendData)
|
2023-03-27 18:28:53 +02:00
|
|
|
},
|
2023-03-28 12:36:32 +02:00
|
|
|
beforeDestroy () {
|
2023-11-30 10:17:23 +01:00
|
|
|
this.$root.$off('read-nfc-data')
|
2023-03-28 12:36:32 +02:00
|
|
|
},
|
2023-02-08 07:47:38 +01:00
|
|
|
methods: {
|
2023-03-27 18:28:53 +02:00
|
|
|
async handleClick () {
|
2023-11-30 10:17:23 +01:00
|
|
|
if (this.nfcSupported) {
|
|
|
|
this.$emit('start-nfc-scan')
|
2023-03-27 18:28:53 +02:00
|
|
|
} else {
|
2023-02-20 00:49:10 +01:00
|
|
|
if (this.model.isUnsetTopUp) {
|
2023-03-27 18:28:53 +02:00
|
|
|
this.handleUnsetTopUp()
|
|
|
|
if (!this.amount) {
|
2023-02-08 07:47:38 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2023-03-27 18:28:53 +02:00
|
|
|
const lnurl = prompt("Enter LNURL-Withdraw")
|
|
|
|
if (lnurl) {
|
|
|
|
await this.sendData(lnurl)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
handleUnsetTopUp () {
|
|
|
|
const amountStr = prompt("How many sats do you want to pay?")
|
|
|
|
if (amountStr) {
|
|
|
|
try {
|
|
|
|
this.amount = parseInt(amountStr)
|
|
|
|
} catch {
|
2023-11-30 10:17:23 +01:00
|
|
|
alert("Please provide a valid number amount in sats")
|
2023-03-27 18:28:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
},
|
2023-11-30 10:17:23 +01:00
|
|
|
async sendData (data) {
|
|
|
|
this.submitting = true
|
|
|
|
this.$emit('handle-nfc-data')
|
2023-07-24 15:57:24 +02:00
|
|
|
|
2023-02-21 21:06:36 +01:00
|
|
|
// Post LNURL-Withdraw data to server
|
2023-11-30 10:17:23 +01:00
|
|
|
const body = JSON.stringify({ lnurl: data, invoiceId: this.model.invoiceId, amount: this.amount })
|
2023-02-21 21:06:36 +01:00
|
|
|
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()
|
2023-11-30 10:17:23 +01:00
|
|
|
const action = response.ok ? 'handle-nfc-result' : 'handle-nfc-error'
|
|
|
|
this.$emit(action, result)
|
2023-02-21 21:06:36 +01:00
|
|
|
} catch (error) {
|
2023-11-30 10:17:23 +01:00
|
|
|
this.$emit('handle-nfc-error', error)
|
2023-02-21 21:06:36 +01:00
|
|
|
}
|
2023-11-30 10:17:23 +01:00
|
|
|
this.submitting = false
|
2023-02-08 07:47:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
</script>
|