mirror of
https://github.com/lnbits/lnbits.git
synced 2025-09-24 11:03:03 +02:00
Finish donation posting API flow
This commit is contained in:
@@ -5,6 +5,9 @@ from ..satspay.crud import delete_charge
|
|||||||
|
|
||||||
import httpx
|
import httpx
|
||||||
|
|
||||||
|
from http import HTTPStatus
|
||||||
|
from quart import jsonify
|
||||||
|
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
|
||||||
from lnbits.helpers import urlsafe_short_hash
|
from lnbits.helpers import urlsafe_short_hash
|
||||||
@@ -28,11 +31,12 @@ async def get_charge_details(service_id):
|
|||||||
|
|
||||||
async def create_donation(
|
async def create_donation(
|
||||||
id: str,
|
id: str,
|
||||||
name: str,
|
|
||||||
cur_code: str,
|
cur_code: str,
|
||||||
sats: int,
|
sats: int,
|
||||||
amount: float,
|
amount: float,
|
||||||
service: int,
|
service: int,
|
||||||
|
name: str = "Anonymous",
|
||||||
|
message: str = "",
|
||||||
posted: bool = False,
|
posted: bool = False,
|
||||||
) -> Donation:
|
) -> Donation:
|
||||||
await db.execute(
|
await db.execute(
|
||||||
@@ -40,17 +44,19 @@ async def create_donation(
|
|||||||
INSERT INTO Donations (
|
INSERT INTO Donations (
|
||||||
id,
|
id,
|
||||||
name,
|
name,
|
||||||
|
message,
|
||||||
cur_code,
|
cur_code,
|
||||||
sats,
|
sats,
|
||||||
amount,
|
amount,
|
||||||
service,
|
service,
|
||||||
posted
|
posted
|
||||||
)
|
)
|
||||||
VALUES (?, ?, ?)
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
||||||
""",
|
""",
|
||||||
(
|
(
|
||||||
id,
|
id,
|
||||||
name,
|
name,
|
||||||
|
message,
|
||||||
cur_code,
|
cur_code,
|
||||||
sats,
|
sats,
|
||||||
amount,
|
amount,
|
||||||
@@ -61,19 +67,51 @@ async def create_donation(
|
|||||||
return await get_donation(id)
|
return await get_donation(id)
|
||||||
|
|
||||||
|
|
||||||
async def post_donation(donation_id: str) -> bool:
|
async def post_donation(donation_id: str) -> tuple:
|
||||||
donation = await get_donation(donation_id)
|
donation = await get_donation(donation_id)
|
||||||
|
if not donation:
|
||||||
|
return (
|
||||||
|
jsonify({"message": "Donation not found!"}),
|
||||||
|
HTTPStatus.BAD_REQUEST
|
||||||
|
)
|
||||||
if donation.posted:
|
if donation.posted:
|
||||||
return False
|
return (
|
||||||
|
jsonify({"message": "Donation has already been posted!"}),
|
||||||
|
HTTPStatus.BAD_REQUEST
|
||||||
|
)
|
||||||
service = await get_service(donation.service)
|
service = await get_service(donation.service)
|
||||||
servicename = service.servicename
|
if service.servicename == "Streamlabs":
|
||||||
if servicename == "Streamlabs":
|
url = "https://streamlabs.com/api/v1.0/donations"
|
||||||
pass
|
data = {
|
||||||
|
"name": donation.name,
|
||||||
|
"message": donation.message,
|
||||||
|
"identifier": "LNbits",
|
||||||
|
"amount": donation.amount,
|
||||||
|
"currency": donation.cur_code.upper(),
|
||||||
|
"access_token": service.token,
|
||||||
|
}
|
||||||
|
async with httpx.AsyncClient() as client:
|
||||||
|
response = await client.post(url, data=data)
|
||||||
|
print(response.json())
|
||||||
|
status = [s for s in list(HTTPStatus) if s == response.status_code][0]
|
||||||
|
elif service.servicename == "StreamElements":
|
||||||
|
return (
|
||||||
|
jsonify({"message": "StreamElements not yet supported!"}),
|
||||||
|
HTTPStatus.BAD_REQUEST
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
return (
|
||||||
|
jsonify({"message": "Unsopported servicename"}),
|
||||||
|
HTTPStatus.BAD_REQUEST
|
||||||
|
)
|
||||||
await db.execute(
|
await db.execute(
|
||||||
"UPDATE Donations SET posted = 1 WHERE id = ?",
|
"UPDATE Donations SET posted = 1 WHERE id = ?",
|
||||||
(donation_id,)
|
(donation_id,)
|
||||||
)
|
)
|
||||||
return True
|
return (
|
||||||
|
jsonify(response.json()),
|
||||||
|
status
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
async def create_service(
|
async def create_service(
|
||||||
|
@@ -22,6 +22,7 @@ async def m001_initial(db):
|
|||||||
CREATE TABLE IF NOT EXISTS Donations (
|
CREATE TABLE IF NOT EXISTS Donations (
|
||||||
id TEXT PRIMARY KEY,
|
id TEXT PRIMARY KEY,
|
||||||
name TEXT NOT NULL,
|
name TEXT NOT NULL,
|
||||||
|
message TEXT NOT NULL,
|
||||||
cur_code TEXT NOT NULL,
|
cur_code TEXT NOT NULL,
|
||||||
sats INT NOT NULL,
|
sats INT NOT NULL,
|
||||||
amount FLOAT NOT NULL,
|
amount FLOAT NOT NULL,
|
||||||
|
@@ -5,6 +5,7 @@ from typing import NamedTuple, Optional
|
|||||||
class Donation(NamedTuple):
|
class Donation(NamedTuple):
|
||||||
id: str
|
id: str
|
||||||
name: str
|
name: str
|
||||||
|
message: str
|
||||||
cur_code: str
|
cur_code: str
|
||||||
sats: int
|
sats: int
|
||||||
amount: float
|
amount: float
|
||||||
|
@@ -133,19 +133,8 @@ async def api_post_donation():
|
|||||||
data = await request.get_json(force=True)
|
data = await request.get_json(force=True)
|
||||||
donation_id = data.get("id", "No ID")
|
donation_id = data.get("id", "No ID")
|
||||||
charge = await get_charge(donation_id)
|
charge = await get_charge(donation_id)
|
||||||
print(charge)
|
|
||||||
if charge and charge.paid:
|
if charge and charge.paid:
|
||||||
print("This endpoint works!")
|
return await post_donation(donation_id)
|
||||||
if await post_donation(donation_id):
|
|
||||||
return (
|
|
||||||
jsonify({"message": "Posted!"}),
|
|
||||||
HTTPStatus.OK
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
return (
|
|
||||||
jsonify({"message": "Already posted!"}),
|
|
||||||
HTTPStatus.BAD_REQUEST
|
|
||||||
)
|
|
||||||
else:
|
else:
|
||||||
return (
|
return (
|
||||||
jsonify({"message": "Not a paid charge!"}),
|
jsonify({"message": "Not a paid charge!"}),
|
||||||
|
Reference in New Issue
Block a user