From 6bc6b03a91dc14aef492d74a837cf099d06e7f10 Mon Sep 17 00:00:00 2001 From: kilian Date: Sun, 7 Dec 2025 18:54:20 +0100 Subject: [PATCH] fixed pydantic input validation error --- backend/src/configuration/environment.py | 3 +++ backend/src/payments/payment_handler.py | 8 ++++---- backend/src/payments/payment_router.py | 11 ++++++----- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/backend/src/configuration/environment.py b/backend/src/configuration/environment.py index a995c10..e75a409 100644 --- a/backend/src/configuration/environment.py +++ b/backend/src/configuration/environment.py @@ -21,3 +21,6 @@ class Environment : # Load paypal secrets paypal_id_sandbox = os.environ['PAYPAL_ID_SANDBOX'] paypal_key_sandbox = os.environ['PAYPAL_KEY_SANDBOX'] + paypal_id_prod = os.environ['PAYPAL_ID_PROD'] + paypal_key_prod = os.environ['PAYPAL_KEY_PROD'] + diff --git a/backend/src/payments/payment_handler.py b/backend/src/payments/payment_handler.py index 61f1bb7..c2177e2 100644 --- a/backend/src/payments/payment_handler.py +++ b/backend/src/payments/payment_handler.py @@ -43,7 +43,6 @@ class OrderRequest(BaseModel): items: list[Item] = Field(default_factory=list) total_price: float = None total_credits: int = None - supabase_client: SupabaseClient # @field_validator('basket') @@ -69,7 +68,7 @@ class OrderRequest(BaseModel): return v - def load_items_and_price(self): + def load_items_and_price(self, supabase: SupabaseClient): # This should be automatic upon initialization of the class """ Loads item details from database and calculates the total price as well as the total credits to be granted. @@ -78,7 +77,7 @@ class OrderRequest(BaseModel): self.total_price = 0 self.total_credits = 0 for basket_item in self.basket: - item = self.supabase_client.get_item(basket_item.item_id, self.currency) + item = supabase.get_item(basket_item.item_id, self.currency) self.items.append(item) self.total_price += item.unit_price * basket_item.quantity # increment price self.total_credits += item.unit_credits * basket_item.quantity # increment credit balance @@ -195,6 +194,7 @@ class PaypalClient: def order( self, order_request: OrderRequest, + supabase_client: SupabaseClient, return_url_success: str, return_url_failure: str ): @@ -209,7 +209,7 @@ class PaypalClient: """ # Fetch details of order from mart database and compute total credits and price - order_request.load_items_and_price() + order_request.load_items_and_price(supabase=supabase_client) # Prepare payload for post request to paypal API order_data = { diff --git a/backend/src/payments/payment_router.py b/backend/src/payments/payment_router.py index 8ae680c..434ac11 100644 --- a/backend/src/payments/payment_router.py +++ b/backend/src/payments/payment_router.py @@ -5,6 +5,7 @@ from fastapi.responses import RedirectResponse from .payment_handler import PaypalClient, OrderRequest from ..supabase.supabase import SupabaseClient +from ..structs.shop import BasketItem from ..cache import CreditCache, make_credit_cache_key @@ -22,11 +23,11 @@ logger = logging.getLogger(__name__) # TODO: add the return url in the API payload to redirect the user to the app. @router.post("/orders/new") def create_order( - user_id: str = Query(...), - basket: list = Query(...), - currency: str = Query(...), - return_url_success: str = Query('https://anydev.info/orders/success'), - return_url_failure: str = Query('https://anydev.info/orders/failed') + user_id: str = Query(...), + currency: str = Query(...), + return_url_success: str = Query('https://anydev.info/orders/success'), + return_url_failure: str = Query('https://anydev.info/orders/failed'), + basket: list[BasketItem] = Body(...), ): """ Creates a new PayPal order.