fixed pydantic input validation error
Some checks failed
Build and deploy the backend to staging / Build and push image (pull_request) Failing after 2m58s
Build and deploy the backend to staging / Deploy to staging (pull_request) Has been skipped
Run linting on the backend code / Build (pull_request) Failing after 2m20s
Run testing on the backend code / Build (pull_request) Failing after 2m25s

This commit is contained in:
2025-12-07 18:54:20 +01:00
parent a9641f4a1c
commit 6bc6b03a91
3 changed files with 13 additions and 9 deletions

View File

@@ -21,3 +21,6 @@ class Environment :
# Load paypal secrets # Load paypal secrets
paypal_id_sandbox = os.environ['PAYPAL_ID_SANDBOX'] paypal_id_sandbox = os.environ['PAYPAL_ID_SANDBOX']
paypal_key_sandbox = os.environ['PAYPAL_KEY_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']

View File

@@ -43,7 +43,6 @@ class OrderRequest(BaseModel):
items: list[Item] = Field(default_factory=list) items: list[Item] = Field(default_factory=list)
total_price: float = None total_price: float = None
total_credits: int = None total_credits: int = None
supabase_client: SupabaseClient
# @field_validator('basket') # @field_validator('basket')
@@ -69,7 +68,7 @@ class OrderRequest(BaseModel):
return v 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 # 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. 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_price = 0
self.total_credits = 0 self.total_credits = 0
for basket_item in self.basket: 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.items.append(item)
self.total_price += item.unit_price * basket_item.quantity # increment price self.total_price += item.unit_price * basket_item.quantity # increment price
self.total_credits += item.unit_credits * basket_item.quantity # increment credit balance self.total_credits += item.unit_credits * basket_item.quantity # increment credit balance
@@ -195,6 +194,7 @@ class PaypalClient:
def order( def order(
self, self,
order_request: OrderRequest, order_request: OrderRequest,
supabase_client: SupabaseClient,
return_url_success: str, return_url_success: str,
return_url_failure: str return_url_failure: str
): ):
@@ -209,7 +209,7 @@ class PaypalClient:
""" """
# Fetch details of order from mart database and compute total credits and price # 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 # Prepare payload for post request to paypal API
order_data = { order_data = {

View File

@@ -5,6 +5,7 @@ from fastapi.responses import RedirectResponse
from .payment_handler import PaypalClient, OrderRequest from .payment_handler import PaypalClient, OrderRequest
from ..supabase.supabase import SupabaseClient from ..supabase.supabase import SupabaseClient
from ..structs.shop import BasketItem
from ..cache import CreditCache, make_credit_cache_key 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. # TODO: add the return url in the API payload to redirect the user to the app.
@router.post("/orders/new") @router.post("/orders/new")
def create_order( def create_order(
user_id: str = Query(...), user_id: str = Query(...),
basket: list = Query(...), currency: str = Query(...),
currency: str = Query(...), return_url_success: str = Query('https://anydev.info/orders/success'),
return_url_success: str = Query('https://anydev.info/orders/success'), return_url_failure: str = Query('https://anydev.info/orders/failed'),
return_url_failure: str = Query('https://anydev.info/orders/failed') basket: list[BasketItem] = Body(...),
): ):
""" """
Creates a new PayPal order. Creates a new PayPal order.