Skip to content

Commit

Permalink
feat: add fastrack checkout redirect (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
dni authored Aug 29, 2024
1 parent 2afc898 commit 5c57c9e
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 5 deletions.
4 changes: 3 additions & 1 deletion crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,12 @@ async def create_charge(
time,
amount,
zeroconf,
fasttrack,
balance,
extra,
custom_css
)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
""",
(
charge_id,
Expand All @@ -77,6 +78,7 @@ async def create_charge(
data.time,
data.amount,
data.zeroconf,
data.fasttrack,
0,
data.extra,
data.custom_css,
Expand Down
12 changes: 12 additions & 0 deletions migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,3 +197,15 @@ async def m013_add_setting_webhook(db):
await db.execute("UPDATE satspay.settings SET webhook_method = 'GET'")
except OperationalError:
pass


async def m014_fasttrack_to_charge(db):
"""
Add 'fasttrack' column to charge for allowing fasttrack checkout without 0conf
"""
try:
await db.execute(
"ALTER TABLE satspay.charges ADD COLUMN fasttrack BOOLEAN DEFAULT FALSE"
)
except OperationalError:
pass
13 changes: 12 additions & 1 deletion models.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class CreateCharge(BaseModel):
time: int = Query(..., ge=1)
amount: Optional[int] = Query(None, ge=1)
zeroconf: bool = Query(False)
fasttrack: bool = Query(False)
custom_css: Optional[str] = Query(None)
currency: str = Query(None)
currency_amount: Optional[float] = Query(None)
Expand All @@ -47,6 +48,7 @@ class Charge(BaseModel):
time: int
amount: int
zeroconf: bool
fasttrack: bool
balance: int
pending: Optional[int] = 0
timestamp: datetime
Expand All @@ -60,6 +62,13 @@ def add_extra(self, extra: dict):
old_extra = json.loads(self.extra) if self.extra else {}
self.extra = json.dumps({**old_extra, **extra})

@property
def paid_fasttrack(self):
"""
ignore the pending status if fasttrack is enabled tell the frontend its paid
"""
return (self.pending or 0) >= self.amount and self.fasttrack or self.paid

@property
def public(self):
public_keys = [
Expand All @@ -72,6 +81,7 @@ def public(self):
"time",
"amount",
"zeroconf",
"fasttrack",
"balance",
"pending",
"timestamp",
Expand All @@ -81,7 +91,8 @@ def public(self):
]
c = {k: v for k, v in self.dict().items() if k in public_keys}
c["timestamp"] = self.timestamp.isoformat()
if self.paid:
c["paid"] = self.paid_fasttrack
if self.paid_fasttrack:
c["completelink"] = self.completelink
return c

Expand Down
1 change: 1 addition & 0 deletions static/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ new Vue({
onchain: false,
onchainwallet: '',
zeroconf: false,
fasttrack: false,
lnbits: false,
description: '',
custom_css: '',
Expand Down
6 changes: 4 additions & 2 deletions tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,12 @@ async def send_success_websocket(charge: Charge):
for listener in listeners:
await listener.send_json(
{
"paid": charge.paid,
"paid": charge.paid_fasttrack,
"balance": charge.balance,
"pending": charge.pending,
"completelink": charge.completelink if charge.paid else None,
"completelink": (
charge.completelink if charge.paid_fasttrack else None
),
}
)

Expand Down
18 changes: 17 additions & 1 deletion templates/satspay/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ <h6 class="text-subtitle1 q-my-none">
:options="walletLinks"
label="Onchain Wallet"
></q-select>
<q-item tag="label" v-ripple>
<q-item tag="label" v-ripple v-if="!formDialogCharge.data.fasttrack">
<q-item-section avatar top>
<q-checkbox v-model="formDialogCharge.data.zeroconf" />
</q-item-section>
Expand All @@ -469,6 +469,22 @@ <h6 class="text-subtitle1 q-my-none">
</q-item-label>
</q-item-section>
</q-item>
<q-item tag="label" v-ripple v-if="!formDialogCharge.data.zeroconf">
<q-item-section avatar top>
<q-checkbox v-model="formDialogCharge.data.fasttrack" />
</q-item-section>
<q-item-section>
<q-item-label
>Fastrack redirect in checkout without 0-conf.</q-item-label
>
<q-item-label caption>
If enabled, the user will be redirected from the checkout page
once the onchain payment is detected (0-conf). Unlike 0-conf it
will wait for the onchain to be confirmed to send a webhook with
charge is paid.
</q-item-label>
</q-item-section>
</q-item>
</div>

<q-select
Expand Down

0 comments on commit 5c57c9e

Please sign in to comment.