Skip to content

Commit 558b34a

Browse files
committed
More configs
- optionnaly serving merchantid_domain_association: this is required to be served to validate the domain with apple/stripe and should be done on infra but given the simplicity we're using just this app and the proxy. The proxy cannot serve this via a conf snippet as the string is too long. - matching min/max amounts and currencies with the app and making those configurable
1 parent bbae5e2 commit 558b34a

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

donation-api/src/donation_api/constants.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,19 @@ class Constants:
1212
stripe_webhook_secret: str = os.getenv("STRIPE_WEBHOOK_SECRET") or ""
1313
stripe_webhook_sender_ips: list[str] = field(default_factory=list)
1414
stripe_webhook_testing_ips: list[str] = field(default_factory=list)
15+
alllowed_currencies: list[str] = field(default_factory=list)
16+
merchantid_domain_association: str = (
17+
os.getenv("MERCHANTID_DOMAIN_ASSOCIATION") or ""
18+
)
1519

16-
stripe_minimal_amount: float = 1.0
17-
stripe_maximum_amount: float = 1000000
20+
stripe_minimal_amount: int = int(os.getenv("STRIPE_MINIMAL_AMOUNT") or "5")
21+
stripe_maximum_amount: int = int(os.getenv("STRIPE_MAXIMUM_AMOUNT") or "999999")
1822

1923
def __post_init__(self):
24+
self.alllowed_currencies = (
25+
os.getenv("ALLOWED_CURRENCIES") or "USD|EUR|CHF"
26+
).split("|")
27+
2028
self.stripe_webhook_testing_ips = os.getenv(
2129
"STRIPE_WEBHOOK_TESTING_IPS", ""
2230
).split("|")

donation-api/src/donation_api/entrypoint.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
from fastapi import FastAPI
44
from fastapi.middleware.cors import CORSMiddleware
5-
from fastapi.responses import RedirectResponse
5+
from fastapi.responses import PlainTextResponse, RedirectResponse
66

77
from donation_api import stripe
88
from donation_api.__about__ import __description__, __title__, __version__
9+
from donation_api.constants import conf
910

1011
PREFIX = "/v1"
1112

@@ -22,6 +23,16 @@ async def _():
2223
"""Redirect to root of latest version of the API"""
2324
return RedirectResponse(f"{PREFIX}/", status_code=HTTPStatus.PERMANENT_REDIRECT)
2425

26+
# could be done on infra ; this is a handy shortcut
27+
if conf.merchantid_domain_association:
28+
29+
@app.get("/.well-known/apple-developer-merchantid-domain-association")
30+
async def _():
31+
"""Used to validate domain ownership with apple/stripe"""
32+
return PlainTextResponse(
33+
conf.merchantid_domain_association, status_code=HTTPStatus.OK
34+
)
35+
2536
api = FastAPI(
2637
title=__title__,
2738
description=__description__,

donation-api/src/donation_api/stripe.py

+8
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ async def check_config():
9696
if not conf.stripe_webhook_sender_ips:
9797
errors.append("Missing Stripe IPs")
9898

99+
if not conf.alllowed_currencies:
100+
errors.append("Missing currencies list")
101+
99102
if errors:
100103
raise HTTPException(
101104
status_code=HTTPStatus.INTERNAL_SERVER_ERROR, detail="\n".join(errors)
@@ -124,6 +127,11 @@ async def create_payment_intent(pi_payload: PaymentIntentRequest):
124127
status_code=HTTPStatus.BAD_REQUEST,
125128
detail="Currency doesnt look like a currency",
126129
)
130+
if pi_payload.currency not in conf.alllowed_currencies:
131+
raise HTTPException(
132+
status_code=HTTPStatus.BAD_REQUEST,
133+
detail="Currency not supported",
134+
)
127135

128136
if (
129137
pi_payload.amount < conf.stripe_minimal_amount

0 commit comments

Comments
 (0)