Better form amount parsing
This commit is contained in:
parent
21067e3b1d
commit
d19c990039
1 changed files with 14 additions and 7 deletions
21
main.py
21
main.py
|
@ -22,18 +22,25 @@ async def welcome(request: Request):
|
||||||
return templates.TemplateResponse(request=request, name="welcome.html.j2")
|
return templates.TemplateResponse(request=request, name="welcome.html.j2")
|
||||||
|
|
||||||
|
|
||||||
@app.post("/", response_class=RedirectResponse)
|
@app.post("/")
|
||||||
async def pay(request: Request, amount: Annotated[int, Form()]):
|
async def pay(request: Request, amount: Annotated[str, Form()]):
|
||||||
# Amount is in cents. Depositing less than a euro does not really make sense, so interpret this
|
# Normalize input.
|
||||||
# as full Euro's.
|
if "," in amount:
|
||||||
if amount < 100:
|
amount = amount.replace(",", ".")
|
||||||
amount *= 100
|
if "." not in amount:
|
||||||
|
amount = f"{amount}.00"
|
||||||
|
# Conversion.
|
||||||
|
[i, f] = amount.split(".")
|
||||||
|
cents = int(i) * 100 + int(f)
|
||||||
|
# Sanity checks.
|
||||||
|
if cents < 10_00 or 150_00 < cents:
|
||||||
|
raise HTTPException(status_code=400, detail=f"invalid amount: {amount}")
|
||||||
|
|
||||||
payment = mollie_client.payments.create(
|
payment = mollie_client.payments.create(
|
||||||
{
|
{
|
||||||
"amount": {
|
"amount": {
|
||||||
"currency": "EUR",
|
"currency": "EUR",
|
||||||
"value": f"{amount // 100}.{amount % 100:02}",
|
"value": f"{cents // 100}.{cents % 100:02}",
|
||||||
},
|
},
|
||||||
"description": "Bitlair bar tegoed",
|
"description": "Bitlair bar tegoed",
|
||||||
"redirectUrl": f"{public_url}/return",
|
"redirectUrl": f"{public_url}/return",
|
||||||
|
|
Loading…
Add table
Reference in a new issue