From 9c930996c74ec5d716f50f870eb474c1dfbba7b5 Mon Sep 17 00:00:00 2001 From: kilian Date: Sun, 19 Oct 2025 23:22:42 +0200 Subject: [PATCH] better endpoint for payments --- backend/src/payments/payment_router.py | 12 +++---- backend/src/payments/test.py | 30 +++++++++++------- backend/src/tests/test_payment.py | 38 +++++++++++------------ backend/src/tests/test_trip_generation.py | 8 ++--- 4 files changed, 48 insertions(+), 40 deletions(-) diff --git a/backend/src/payments/payment_router.py b/backend/src/payments/payment_router.py index 048678c..890535d 100644 --- a/backend/src/payments/payment_router.py +++ b/backend/src/payments/payment_router.py @@ -1,7 +1,7 @@ import logging from typing import Literal -from fastapi import APIRouter, HTTPException +from fastapi import APIRouter, Query, Body from ..payments import PaypalClient, OrderRequest from ..supabase.supabase import SupabaseClient from ..cache import CreditCache, make_credit_cache_key @@ -21,11 +21,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, - basket: list, - currency: Literal['CHF', 'EUR', 'USD'], - return_url_success: str = 'https://anydev.info', - return_url_failure: str = 'https://anydev.info' + user_id: str = Query(...), + basket: list = Query(...), + currency: str = Query(...), + return_url_success: str = Query('https://anydev.info'), + return_url_failure: str = Query('https://anydev.info') ): """ Creates a new PayPal order. diff --git a/backend/src/payments/test.py b/backend/src/payments/test.py index 2bc10b6..e2ca9cc 100644 --- a/backend/src/payments/test.py +++ b/backend/src/payments/test.py @@ -1,17 +1,19 @@ +#%% import requests import json +import os +from dotenv import load_dotenv -from ..configuration.environment import Environment - +# username and password +load_dotenv(override=True) +username = os.environ['PAYPAL_ID_SANDBOX'] +password = os.environ['PAYPAL_KEY_SANDBOX'] # DOCUMENTATION AT : https://developer.paypal.com/api/rest/requests/ -# username and password -username = Environment.paypal_id_sandbox -password = Environment.paypal_key_sandbox - +#%% ######## STEP 1: Validation ######## # url for validation post request validation_url = "https://api-m.sandbox.paypal.com/v1/oauth2/token" @@ -32,6 +34,7 @@ print(validation_response.text) access_token = json.loads(validation_response.text)["access_token"] +#%% ######## STEP 2: Create Order ######## # url for post request order_url = "https://api-m.sandbox.paypal.com/v2/checkout/orders" @@ -79,15 +82,20 @@ order_response = requests.post( auth=(username, password) ) -# todo check status code + try except -print(order_response.text) -order_id = json.loads(order_response.text)["id"] +# Send the redirect link to the user +# print(order_response.json()) +for link_obj in order_response.json()['links']: + if link_obj['rel'] == 'approve': + forward_to_user_link = link_obj['href'] +print(f'Follow this link to proceed to payment: {forward_to_user_link}') +# order_id = json.loads(order_response.text)["id"] +#%% ######## STEP 3: capture payment # url for post request capture_url = f"https://api-m.sandbox.paypal.com/v2/checkout/orders/{order_id}/capture" -capture_url_prod = f"https://api-m.paypal.com/v2/checkout/orders/{order_id}/capture" +# capture_url_prod = f"https://api-m.paypal.com/v2/checkout/orders/{order_id}/capture" capture_response = requests.post( url=capture_url, @@ -96,6 +104,6 @@ capture_response = requests.post( ) # todo check status code + try except -print(capture_response.text) +print(capture_response.json()) # order_id = json.loads(response.text)["id"] diff --git a/backend/src/tests/test_payment.py b/backend/src/tests/test_payment.py index 1f7c155..7a141fd 100644 --- a/backend/src/tests/test_payment.py +++ b/backend/src/tests/test_payment.py @@ -4,6 +4,12 @@ from fastapi.testclient import TestClient import pytest from ..main import app +from ..supabase.supabase import SupabaseClient + + +# Create a supabase client +supabase = SupabaseClient() + @pytest.fixture(scope="module") @@ -12,24 +18,7 @@ def client(): return TestClient(app) -@pytest.mark.parametrize( - "location,status_code", - [ - ([45.7576485, 4.8330241], 200), # Lyon, France - ([41.4020572, 2.1818985], 200), # Barcelona, Spain - ([59.3293, 18.0686], 200), # Stockholm, Sweden - ([43.6532, -79.3832], 200), # Toronto, Canada - ([38.7223, -9.1393], 200), # Lisbon, Portugal - ([6.5244, 3.3792], 200), # Lagos, Nigeria - ([17.3850, 78.4867], 200), # Hyderabad, India - ([30.0444, 31.2357], 200), # Cairo, Egypt - ([50.8503, 4.3517], 200), # Brussels, Belgium - ([35.2271, -80.8431], 200), # Charlotte, USA - ([10.4806, -66.9036], 200), # Caracas, Venezuela - ([9.51074, -13.71118], 200), # Conakry, Guinea - ] -) -def test_nearby(client, location, status_code): # pylint: disable=redefined-outer-name +def test_nearby(client): # pylint: disable=redefined-outer-name """ Test n°1 : Verify handling of invalid input. @@ -37,7 +26,18 @@ def test_nearby(client, location, status_code): # pylint: disable=redefined-o client: request: """ - response = client.post(f"/get-nearby/landmarks/{location[0]}/{location[1]}") + response = client.post( + url=f"/orders/new/", + json={ + 'user_id': supabase.SUPABASE_TEST_USER_ID + 'basket': { + + } + 'currency': + 'return_url_success': + 'return_url_failure': + } + ) suggestions = response.json() # checks : diff --git a/backend/src/tests/test_trip_generation.py b/backend/src/tests/test_trip_generation.py index 881f50f..523f586 100644 --- a/backend/src/tests/test_trip_generation.py +++ b/backend/src/tests/test_trip_generation.py @@ -56,8 +56,8 @@ def test_trip(client, request, sightseeing, shopping, nature, max_time_minute, s "preferences": prefs.model_dump(), "start": start_coords, "end": end_coords, - } - ) + } + ) assert response.status_code == 200 landmarks = response.json() @@ -71,8 +71,8 @@ def test_trip(client, request, sightseeing, shopping, nature, max_time_minute, s "landmarks": landmarks, "start": start, "end": end, - } - ) + } + ) assert response.status_code == 200 # Increment the user balance again