btcpayserver/BTCPayServer/Views/Shared/NFC/CheckoutEnd.cshtml

131 lines
5.1 KiB
Text
Raw Normal View History

<template id="lnurl-withdraw-template">
<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 }">
<span class="button-text">{{btnText}}</span>
<div class="loader-wrapper">
@await Html.PartialAsync("~/Views/UIInvoice/Checkout-Spinner.cshtml")
</div>
</button>
</bp-loading-button>
</template>
</div>
</template>
2023-06-26 16:45:34 +02:00
<script>
Vue.component("lnurl-withdraw-checkout", {
template: "#lnurl-withdraw-template",
props: {
model: Object,
isV2: Boolean,
nfcSupported: Boolean,
nfcScanning: Boolean,
nfcErrorMessage: String
},
computed: {
display () {
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' || (
// Unified QR/BIP21 case
(activePaymentMethodId === 'BTC' && isUnified && lnurlwAvailable) ||
// Lightning with LNURL available
(activePaymentMethodId === 'BTC_LightningLike' && lnurlwAvailable))
return isAvailable && (this.nfcSupported || this.testFallback)
},
2023-03-28 12:36:32 +02:00
testFallback () {
return !this.nfcSupported && window.location.search.match('lnurlwtest=(1|true)')
},
btnText () {
if (this.nfcSupported) {
if (this.submitting) {
return this.isV2 ? this.$t('submitting_nfc') : 'Submitting NFC …'
} else if (this.nfcScanning) {
return this.isV2 ? this.$t('scanning_nfc') : 'Scanning NFC …'
} else {
return this.isV2 ? this.$t('pay_by_nfc') : 'Pay by NFC'
}
} else {
return this.isV2 ? this.$t('pay_by_lnurl') : 'Pay by LNURL-Withdraw'
}
}
},
data () {
return {
url: @Safe.Json(Context.Request.GetAbsoluteUri(Url.Action("SubmitLNURLWithdrawForInvoice", "NFC"))),
amount: 0,
submitting: false
}
},
beforeMount () {
this.$root.$on('read-nfc-data', this.sendData)
},
2023-03-28 12:36:32 +02:00
beforeDestroy () {
this.$root.$off('read-nfc-data')
2023-03-28 12:36:32 +02:00
},
methods: {
async handleClick () {
if (this.nfcSupported) {
this.$emit('start-nfc-scan')
} else {
if (this.model.isUnsetTopUp) {
this.handleUnsetTopUp()
if (!this.amount) {
return;
}
}
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 {
alert("Please provide a valid number amount in sats")
}
}
return false
},
async sendData (data) {
this.submitting = true
this.$emit('handle-nfc-data')
// Post LNURL-Withdraw data to server
const body = JSON.stringify({ lnurl: data, 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()
const action = response.ok ? 'handle-nfc-result' : 'handle-nfc-error'
this.$emit(action, result)
} catch (error) {
this.$emit('handle-nfc-error', error)
}
this.submitting = false
}
}
});
</script>