Skip to content

Commit

Permalink
feat: add resend webhook endpoint and button (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
dni authored Aug 28, 2024
1 parent 6aa6bdc commit ef5eb5e
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 50 deletions.
25 changes: 21 additions & 4 deletions static/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ new Vue({
field: 'id'
},
{
name: 'description',
name: 'name',
align: 'left',
label: 'Title',
field: 'description'
label: 'Name',
field: 'name'
},
{
name: 'timeLeft',
Expand Down Expand Up @@ -351,6 +351,24 @@ new Vue({
}
})
},
sendWebhook: function (chargeId) {
LNbits.api
.request(
'GET',
`/satspay/api/v1/charge/webhook/${chargeId}`,
this.g.user.wallets[0].adminkey
)
.then(response => {
console.log(response)
this.$q.notify({
message: 'Webhook sent',
color: 'positive'
})
})
.catch(err => {
LNbits.utils.notifyApiError(err)
})
},
checkChargeBalance: function (chargeId) {
LNbits.api
.request(
Expand All @@ -366,7 +384,6 @@ new Vue({
const index = this.chargeLinks.findIndex(c => c.id === chargeId)
this.chargeLinks[index] = mapCharge(charge, this.chargeLinks[index])
if (charge.paid) {
LNbits.utils.notify('Charge paid')
this.$q.notify({
message: 'Charge paid',
color: 'positive'
Expand Down
2 changes: 1 addition & 1 deletion static/js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const mapCharge = (obj, oldObj = {}) => {
charge.displayUrl = ['/satspay/', obj.id].join('')
charge.expanded = oldObj.expanded || false
charge.extra =
charge.extra && charge.extra instanceof String
charge.extra && typeof charge.extra == 'string'
? JSON.parse(charge.extra)
: charge.extra
const now = new Date().getTime() / 1000
Expand Down
80 changes: 35 additions & 45 deletions templates/satspay/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ <h5 class="text-subtitle1 q-my-none">Charges</h5>
<template v-slot:header="props">
<q-tr :props="props">
<q-th auto-width></q-th>
<q-th auto-width>Status </q-th>
<q-th auto-width>Status</q-th>
<q-th auto-width>Title</q-th>
<q-th auto-width>Time Left (hh:mm)</q-th>
<q-th auto-width>Time To Pay (hh:mm)</q-th>
Expand Down Expand Up @@ -155,12 +155,12 @@ <h5 class="text-subtitle1 q-my-none">Charges</h5>
>
</q-badge>
</q-td>
<q-td key="description" :props="props" :class="">
<q-td key="name" :props="props">
<a
:href="props.row.displayUrl"
target="_blank"
style="color: unset; text-decoration: none"
>{{props.row.description}}</a
>{{props.row.name}}</a
>
</q-td>
<q-td key="timeLeft" :props="props" :class="">
Expand Down Expand Up @@ -200,31 +200,22 @@ <h5 class="text-subtitle1 q-my-none">Charges</h5>
</q-tr>
<q-tr v-show="props.row.expanded" :props="props">
<q-td colspan="100%">
<div
v-if="props.row.onchainwallet"
class="row items-center q-mt-md q-mb-lg"
>
<div class="col-2 q-pr-lg">Onchain Wallet:</div>
<div class="col-4 q-pr-lg">
<div style="padding: 12px">
<div>ID: {{props.row.id}}</div>
<div>Description: {{props.row.description}}</div>
<div v-if="props.row.onchainwallet">
Onchain Wallet:
{{getOnchainWalletName(props.row.onchainwallet)}}
</div>
</div>
<div
v-if="props.row.lnbitswallet"
class="row items-center q-mt-md q-mb-lg"
>
<div class="col-2 q-pr-lg">LNbits Wallet:</div>
<div class="col-4 q-pr-lg">
<div v-if="props.row.lnbitswallet">
LNbits Wallet:
{{getLNbitsWalletName(props.row.lnbitswallet)}}
</div>
</div>

<div
v-if="props.row.completelink || props.row.completelinktext"
class="row items-center q-mt-md q-mb-lg"
>
<div class="col-2 q-pr-lg">Completed Link:</div>
<div class="col-4 q-pr-lg">
<div
v-if="props.row.completelink || props.row.completelinktext"
>
Completed Link:
<a
class="text-secondary"
:href="props.row.completelink"
Expand All @@ -234,13 +225,8 @@ <h5 class="text-subtitle1 q-my-none">Charges</h5>
props.row.completelink}}</a
>
</div>
</div>
<div
v-if="props.row.webhook"
class="row items-center q-mt-md q-mb-lg"
>
<div class="col-2 q-pr-lg">Webhook:</div>
<div class="col-4 q-pr-lg">
<div v-if="props.row.webhook">
Webhook:
<a
class="text-secondary"
:href="props.row.webhook"
Expand All @@ -249,52 +235,55 @@ <h5 class="text-subtitle1 q-my-none">Charges</h5>
>{{props.row.webhook}}</a
>
</div>
<div class="col-4 q-pr-lg">
<div v-if="props.row.webhook">
Webhook Response:
<q-badge
v-if="props.row.webhook_message"
v-if="props.row.extra.webhook_message"
@click="showWebhookResponseDialog(props.row.extra.webhook_response)"
color="blue"
class="cursor-pointer"
>
{{props.row.webhook_message }}
{{props.row.extra.webhook_message }}
</q-badge>
<span v-else>no response yet</span>
</div>
</div>
<div class="row items-center q-mt-md q-mb-lg">
<div class="col-2 q-pr-lg">ID:</div>
<div class="col-4 q-pr-lg">{{props.row.id}}</div>
</div>
<div class="row items-center q-mt-md q-mb-lg">
<div class="col-2 q-pr-lg"></div>
<div class="col-6 q-pr-lg">
<div class="row">
<q-btn
unelevated
outline
type="a"
:href="props.row.displayUrl"
target="_blank"
class="float-left q-mr-lg"
class="float-left q-mr-md q-mt-md"
>Details</q-btn
>
<q-btn
unelevated
outline
icon="refresh"
@click="checkChargeBalance(props.row.id)"
class="float-left q-mr-lg"
class="float-left q-mr-md q-mt-md"
v-if="!props.row.paid"
>Check charge balance</q-btn
>
<q-btn
unelevated
outline
icon="refresh"
@click="sendWebhook(props.row.id)"
class="float-left q-mr-md q-mt-md"
v-if="props.row.paid"
>Resend Webhook</q-btn
>
<q-btn
unelevated
color="pink"
icon="cancel"
@click="deleteChargeLink(props.row.id)"
class="float-left q-mr-md q-mt-md"
>Delete</q-btn
>
</div>
<div class="col-4"></div>
<div class="col-2 q-pr-lg"></div>
</div>
</q-td>
</q-tr>
Expand Down Expand Up @@ -375,6 +364,7 @@ <h6 class="text-subtitle1 q-my-none">
</q-card-section>
</q-card>
</div>

<q-dialog v-model="formDialogCharge.show" position="top">
<q-card class="q-pa-lg q-pt-xl lnbits__dialog-card">
<q-form @submit="sendFormDataCharge" class="q-gutter-md">
Expand Down
20 changes: 20 additions & 0 deletions views_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
update_satspay_settings,
)
from .helpers import (
call_webhook,
check_charge_balance,
fetch_onchain_address,
fetch_onchain_config_network,
Expand Down Expand Up @@ -147,6 +148,25 @@ async def api_charge_check_balance(charge_id: str) -> Charge:
return charge


@satspay_api_router.get(
"/api/v1/charge/webhook/{charge_id}", dependencies=[Depends(require_admin_key)]
)
async def api_charge_webhook(charge_id: str) -> Charge:
charge = await get_charge(charge_id)
if not charge:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="Charge does not exist."
)
if not charge.webhook:
raise HTTPException(
status_code=HTTPStatus.BAD_REQUEST, detail="No webhook set."
)
resp = await call_webhook(charge)
charge.add_extra(resp)
charge = await update_charge(charge)
return charge


@satspay_api_router.delete(
"/api/v1/charge/{charge_id}", dependencies=[Depends(require_admin_key)]
)
Expand Down

0 comments on commit ef5eb5e

Please sign in to comment.