Stuck getting user to create satspay charge from tipjar

This commit is contained in:
benarc 2021-10-20 11:51:48 +01:00
parent 40f3e8b210
commit 14ebf72255
4 changed files with 45 additions and 27 deletions

View File

@ -8,7 +8,14 @@ from typing import Optional
from lnbits.db import SQLITE
async def create_tip(data: createTip) -> Tip:
async def create_tip(
id: int,
wallet: str,
message: str,
name: str,
sats: int,
tipjar: str,
) -> Tip:
"""Create a new Tip"""
await db.execute(
"""
@ -22,10 +29,10 @@ async def create_tip(data: createTip) -> Tip:
)
VALUES (?, ?, ?, ?, ?, ?)
""",
(data.id, data.wallet, data.name, data.message, data.sats, data.tipjar),
(id, wallet, name, message, sats, tipjar),
)
tip = await get_tip(data.id)
tip = await get_tip(id)
assert tip, "Newly created tip couldn't be retrieved"
return tip
@ -62,7 +69,7 @@ async def create_tipjar(data: createTipJar) -> TipJar:
async def get_tipjar(tipjar_id: int) -> Optional[TipJar]:
"""Return a tipjar by ID"""
row = await db.fetchone("SELECT * FROM tipjar.TipJars WHERE id = ?", (tipjar_id,))
return TipJar.from_row(row) if row else None
return TipJar(**row) if row else None
async def get_tipjars(wallet_id: str) -> Optional[list]:
@ -70,7 +77,7 @@ async def get_tipjars(wallet_id: str) -> Optional[list]:
rows = await db.fetchall(
"SELECT * FROM tipjar.TipJars WHERE wallet = ?", (wallet_id,)
)
return [TipJar.from_row(row) for row in rows] if rows else None
return [TipJar(**row) for row in rows] if rows else None
async def delete_tipjar(tipjar_id: int) -> None:
@ -84,13 +91,13 @@ async def delete_tipjar(tipjar_id: int) -> None:
async def get_tip(tip_id: str) -> Optional[Tip]:
"""Return a Tip"""
row = await db.fetchone("SELECT * FROM tipjar.Tips WHERE id = ?", (tip_id,))
return Tip.from_row(row) if row else None
return Tip(**row) if row else None
async def get_tips(wallet_id: str) -> Optional[list]:
"""Return all Tips assigned to wallet_id"""
rows = await db.fetchall("SELECT * FROM tipjar.Tips WHERE wallet = ?", (wallet_id,))
return [Tip.from_row(row) for row in rows] if rows else None
return [Tip(**row) for row in rows] if rows else None
async def delete_tip(tip_id: str) -> None:

View File

@ -66,21 +66,23 @@
sats: '',
message: ''
}
},
}
}
},
methods: {
Invoice: function () {
var self = this
console.log('{{ tipjar }}')
axios
.post('/tipjar/api/v1/tips', {
tipjar: {{ tipjar }},
tipjar: '{{ tipjar }}',
name: self.tipDialog.data.name,
sats: self.tipDialog.data.sats,
message: self.tipDialog.data.message
})
.then(function (response) {
console.log(response.data)
self.redirect_url = response.data.redirect_url
console.log(self.redirect_url)
window.location.href = self.redirect_url

View File

@ -32,10 +32,11 @@ async def index(request: Request, user: User = Depends(check_user_exists)):
)
@tipjar_ext.route("/{id}")
async def tip(request: Request, id: str = Query(None)):
@tipjar_ext.get("/{tipjar_id}")
async def tip(request: Request, tipjar_id: int = Query(None)):
"""Return the donation form for the Tipjar corresponding to id"""
tipjar = await get_tipjar(id)
tipjar = await get_tipjar(tipjar_id)
print(tipjar_id)
if not tipjar:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="TipJar does not exist."

View File

@ -34,25 +34,30 @@ from .models import createTipJar, createTips, createTip
async def api_create_tipjar(data: createTipJar):
"""Create a tipjar, which holds data about how/where to post tips"""
try:
tipjar = await create_tipjar(**data)
tipjar = await create_tipjar(data)
except Exception as e:
raise HTTPException(status_code=HTTPStatus.INTERNAL_SERVER_ERROR, detail=str(e))
return tipjar.dict()
async def user_from_wallet(wallet: WalletTypeInfo = Depends(get_key_type)):
return wallet.wallet.user
@tipjar_ext.post("/api/v1/tips")
async def api_create_tip(data: createTips, dataCreateTip: createTip):
async def api_create_tip(data: createTips):
"""Take data from tip form and return satspay charge"""
sats = data.sats
message = data.get("message", "")[:144]
message = data.message
if not message:
message = "No message"
tipjar_id = data.tipjar
tipjar = await get_tipjar(tipjar_id)
webhook = tipjar.webhook
charge_details = await get_charge_details(tipjar.id)
name = data.get("name", "")[:25]
name = data.name
# Ensure that description string can be split reliably
name = name.replace('"', "''")
if not name:
@ -60,18 +65,21 @@ async def api_create_tip(data: createTips, dataCreateTip: createTip):
description = f'"{name}": {message}'
charge = await create_charge(
amount=sats,
webhook=webhook,
description=description,
**charge_details,
data={
"amount": sats,
"webhook": webhook,
"description": description,
**charge_details,
},
)
await create_tip(
id=charge.id,
wallet=tipjar.wallet,
message=message,
name=name,
sats=data.sats,
tipjar=data.tipjar,
)
dataCreateTip.id = charge.id
dataCreateTip.wallet = tipjar.wallet
dataCreateTip.message = message
dataCreateTip.name = name
dataCreateTip.sats = data.sats
dataCreateTip.tipjar = data.tipjar
await create_tip(dataCreateTip)
return {"redirect_url": f"/satspay/{charge.id}"}