Fixed paywall, unnecessary key check added

This commit is contained in:
benarc 2022-02-17 09:23:33 +00:00
parent 0764f4fdf1
commit 3c5fac3874
2 changed files with 54 additions and 50 deletions

View file

@ -77,8 +77,8 @@
mixins: [windowMixin], mixins: [windowMixin],
data: function () { data: function () {
return { return {
userAmount: {{ paywall.amount }}, userAmount: '{{ paywall.amount }}',
paywallAmount: {{ paywall.amount }}, paywallAmount: '{{ paywall.amount }}',
paymentReq: null, paymentReq: null,
redirectUrl: null, redirectUrl: null,
paymentDialog: { paymentDialog: {
@ -89,7 +89,9 @@
}, },
computed: { computed: {
amount: function () { amount: function () {
return (this.paywallAmount > this.userAmount) ? this.paywallAmount : this.userAmount return this.paywallAmount > this.userAmount
? this.paywallAmount
: this.userAmount
} }
}, },
methods: { methods: {
@ -102,48 +104,55 @@
}, },
createInvoice: function () { createInvoice: function () {
var self = this var self = this
console.log(this.amount) LNbits.api
axios .request(
.post( 'POST',
'/paywall/api/v1/paywalls/{{ paywall.id }}/invoice', '/paywall/api/v1/paywalls/invoice/{{ paywall.id }}',
{amount: self.amount} 'filler',
{
amount: self.amount
}
) )
.then(function (response) { .then(function (response) {
if (response.data) {
self.paymentReq = response.data.payment_request.toUpperCase() self.paymentReq = response.data.payment_request.toUpperCase()
self.paymentDialog.dismissMsg = self.$q.notify({ self.paymentDialog.dismissMsg = self.$q.notify({
timeout: 0, timeout: 0,
message: 'Waiting for payment...' message: 'Waiting for payment...'
}) })
self.paymentDialog.checker = setInterval(function () { self.paymentDialog.checker = setInterval(function () {
axios LNbits.api
.post( .request(
'/paywall/api/v1/paywalls/{{ paywall.id }}/check_invoice', 'POST',
'/paywall/api/v1/paywalls/check_invoice/{{ paywall.id }}',
'filler',
{payment_hash: response.data.payment_hash} {payment_hash: response.data.payment_hash}
) )
.then(function (res) { .then(function (response) {
if (res.data.paid) { if (response.data) {
if (response.data.paid) {
self.cancelPayment() self.cancelPayment()
self.redirectUrl = res.data.url self.redirectUrl = response.data.url
if (res.data.remembers) { if (response.data.remembers) {
self.$q.localStorage.set( self.$q.localStorage.set(
'lnbits.paywall.{{ paywall.id }}', 'lnbits.paywall.{{ paywall.id }}',
res.data.url response.data.url
) )
} }
self.$q.notify({ self.$q.notify({
type: 'positive', type: 'positive',
message: 'Payment received!', message: 'Payment received!',
icon: null icon: null
}) })
} }
}
}) })
.catch(function (error) { .catch(function (error) {
LNbits.utils.notifyApiError(error) LNbits.utils.notifyApiError(error)
}) })
}, 2000) }, 2000)
}
}) })
.catch(function (error) { .catch(function (error) {
LNbits.utils.notifyApiError(error) LNbits.utils.notifyApiError(error)

View file

@ -52,20 +52,17 @@ async def api_paywall_delete(
raise HTTPException(status_code=HTTPStatus.NO_CONTENT) raise HTTPException(status_code=HTTPStatus.NO_CONTENT)
@paywall_ext.post("/api/v1/paywalls/{paywall_id}/invoice") @paywall_ext.post("/api/v1/paywalls/invoice/{paywall_id}")
async def api_paywall_create_invoice( async def api_paywall_create_invoice(
paywall_id,
data: CreatePaywallInvoice, data: CreatePaywallInvoice,
wallet: WalletTypeInfo = Depends(get_key_type), paywall_id: str = Query(None)
): ):
paywall = await get_paywall(paywall_id) paywall = await get_paywall(paywall_id)
if data.amount < paywall.amount: if data.amount < paywall.amount:
raise HTTPException( raise HTTPException(
status_code=HTTPStatus.BAD_REQUEST, status_code=HTTPStatus.BAD_REQUEST,
detail=f"Minimum amount is {paywall.amount} sat.", detail=f"Minimum amount is {paywall.amount} sat.",
) )
try: try:
amount = data.amount if data.amount > paywall.amount else paywall.amount amount = data.amount if data.amount > paywall.amount else paywall.amount
payment_hash, payment_request = await create_invoice( payment_hash, payment_request = await create_invoice(
@ -80,15 +77,14 @@ async def api_paywall_create_invoice(
return {"payment_hash": payment_hash, "payment_request": payment_request} return {"payment_hash": payment_hash, "payment_request": payment_request}
@paywall_ext.post("/api/v1/paywalls/{paywall_id}/check_invoice") @paywall_ext.post("/api/v1/paywalls/check_invoice/{paywall_id}")
async def api_paywal_check_invoice(data: CheckPaywallInvoice, paywall_id): async def api_paywal_check_invoice(data: CheckPaywallInvoice, paywall_id: str = Query(None)):
paywall = await get_paywall(paywall_id) paywall = await get_paywall(paywall_id)
payment_hash = data.payment_hash payment_hash = data.payment_hash
if not paywall: if not paywall:
raise HTTPException( raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="Paywall does not exist." status_code=HTTPStatus.NOT_FOUND, detail="Paywall does not exist."
) )
try: try:
status = await check_invoice_status(paywall.wallet, payment_hash) status = await check_invoice_status(paywall.wallet, payment_hash)
is_paid = not status.pending is_paid = not status.pending
@ -101,5 +97,4 @@ async def api_paywal_check_invoice(data: CheckPaywallInvoice, paywall_id):
await payment.set_pending(False) await payment.set_pending(False)
return {"paid": True, "url": paywall.url, "remembers": paywall.remembers} return {"paid": True, "url": paywall.url, "remembers": paywall.remembers}
return {"paid": False} return {"paid": False}