mirror of
https://github.com/lnbits/lnbits.git
synced 2025-09-26 11:56:16 +02:00
Stuck getting user to create satspay charge from tipjar
This commit is contained in:
@@ -8,7 +8,14 @@ from typing import Optional
|
|||||||
from lnbits.db import SQLITE
|
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"""
|
"""Create a new Tip"""
|
||||||
await db.execute(
|
await db.execute(
|
||||||
"""
|
"""
|
||||||
@@ -22,10 +29,10 @@ async def create_tip(data: createTip) -> Tip:
|
|||||||
)
|
)
|
||||||
VALUES (?, ?, ?, ?, ?, ?)
|
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"
|
assert tip, "Newly created tip couldn't be retrieved"
|
||||||
return tip
|
return tip
|
||||||
|
|
||||||
@@ -62,7 +69,7 @@ async def create_tipjar(data: createTipJar) -> TipJar:
|
|||||||
async def get_tipjar(tipjar_id: int) -> Optional[TipJar]:
|
async def get_tipjar(tipjar_id: int) -> Optional[TipJar]:
|
||||||
"""Return a tipjar by ID"""
|
"""Return a tipjar by ID"""
|
||||||
row = await db.fetchone("SELECT * FROM tipjar.TipJars WHERE id = ?", (tipjar_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]:
|
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(
|
rows = await db.fetchall(
|
||||||
"SELECT * FROM tipjar.TipJars WHERE wallet = ?", (wallet_id,)
|
"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:
|
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]:
|
async def get_tip(tip_id: str) -> Optional[Tip]:
|
||||||
"""Return a Tip"""
|
"""Return a Tip"""
|
||||||
row = await db.fetchone("SELECT * FROM tipjar.Tips WHERE id = ?", (tip_id,))
|
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]:
|
async def get_tips(wallet_id: str) -> Optional[list]:
|
||||||
"""Return all Tips assigned to wallet_id"""
|
"""Return all Tips assigned to wallet_id"""
|
||||||
rows = await db.fetchall("SELECT * FROM tipjar.Tips WHERE wallet = ?", (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:
|
async def delete_tip(tip_id: str) -> None:
|
||||||
|
@@ -66,21 +66,23 @@
|
|||||||
sats: '',
|
sats: '',
|
||||||
message: ''
|
message: ''
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
methods: {
|
methods: {
|
||||||
Invoice: function () {
|
Invoice: function () {
|
||||||
var self = this
|
var self = this
|
||||||
|
console.log('{{ tipjar }}')
|
||||||
axios
|
axios
|
||||||
.post('/tipjar/api/v1/tips', {
|
.post('/tipjar/api/v1/tips', {
|
||||||
tipjar: {{ tipjar }},
|
tipjar: '{{ tipjar }}',
|
||||||
name: self.tipDialog.data.name,
|
name: self.tipDialog.data.name,
|
||||||
sats: self.tipDialog.data.sats,
|
sats: self.tipDialog.data.sats,
|
||||||
message: self.tipDialog.data.message
|
message: self.tipDialog.data.message
|
||||||
})
|
})
|
||||||
.then(function (response) {
|
.then(function (response) {
|
||||||
|
console.log(response.data)
|
||||||
self.redirect_url = response.data.redirect_url
|
self.redirect_url = response.data.redirect_url
|
||||||
console.log(self.redirect_url)
|
console.log(self.redirect_url)
|
||||||
window.location.href = self.redirect_url
|
window.location.href = self.redirect_url
|
||||||
|
@@ -32,10 +32,11 @@ async def index(request: Request, user: User = Depends(check_user_exists)):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@tipjar_ext.route("/{id}")
|
@tipjar_ext.get("/{tipjar_id}")
|
||||||
async def tip(request: Request, id: str = Query(None)):
|
async def tip(request: Request, tipjar_id: int = Query(None)):
|
||||||
"""Return the donation form for the Tipjar corresponding to id"""
|
"""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:
|
if not tipjar:
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=HTTPStatus.NOT_FOUND, detail="TipJar does not exist."
|
status_code=HTTPStatus.NOT_FOUND, detail="TipJar does not exist."
|
||||||
|
@@ -34,25 +34,30 @@ from .models import createTipJar, createTips, createTip
|
|||||||
async def api_create_tipjar(data: createTipJar):
|
async def api_create_tipjar(data: createTipJar):
|
||||||
"""Create a tipjar, which holds data about how/where to post tips"""
|
"""Create a tipjar, which holds data about how/where to post tips"""
|
||||||
try:
|
try:
|
||||||
tipjar = await create_tipjar(**data)
|
tipjar = await create_tipjar(data)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise HTTPException(status_code=HTTPStatus.INTERNAL_SERVER_ERROR, detail=str(e))
|
raise HTTPException(status_code=HTTPStatus.INTERNAL_SERVER_ERROR, detail=str(e))
|
||||||
|
|
||||||
return tipjar.dict()
|
return tipjar.dict()
|
||||||
|
|
||||||
|
|
||||||
|
async def user_from_wallet(wallet: WalletTypeInfo = Depends(get_key_type)):
|
||||||
|
return wallet.wallet.user
|
||||||
|
|
||||||
|
|
||||||
@tipjar_ext.post("/api/v1/tips")
|
@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"""
|
"""Take data from tip form and return satspay charge"""
|
||||||
sats = data.sats
|
sats = data.sats
|
||||||
message = data.get("message", "")[:144]
|
message = data.message
|
||||||
if not message:
|
if not message:
|
||||||
message = "No message"
|
message = "No message"
|
||||||
tipjar_id = data.tipjar
|
tipjar_id = data.tipjar
|
||||||
tipjar = await get_tipjar(tipjar_id)
|
tipjar = await get_tipjar(tipjar_id)
|
||||||
|
|
||||||
webhook = tipjar.webhook
|
webhook = tipjar.webhook
|
||||||
charge_details = await get_charge_details(tipjar.id)
|
charge_details = await get_charge_details(tipjar.id)
|
||||||
name = data.get("name", "")[:25]
|
name = data.name
|
||||||
# Ensure that description string can be split reliably
|
# Ensure that description string can be split reliably
|
||||||
name = name.replace('"', "''")
|
name = name.replace('"', "''")
|
||||||
if not name:
|
if not name:
|
||||||
@@ -60,18 +65,21 @@ async def api_create_tip(data: createTips, dataCreateTip: createTip):
|
|||||||
description = f'"{name}": {message}'
|
description = f'"{name}": {message}'
|
||||||
|
|
||||||
charge = await create_charge(
|
charge = await create_charge(
|
||||||
amount=sats,
|
data={
|
||||||
webhook=webhook,
|
"amount": sats,
|
||||||
description=description,
|
"webhook": webhook,
|
||||||
**charge_details,
|
"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}"}
|
return {"redirect_url": f"/satspay/{charge.id}"}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user