better endpoint for payments
Some checks failed
Build and deploy the backend to staging / Build and push image (pull_request) Failing after 51s
Build and deploy the backend to staging / Deploy to staging (pull_request) Has been skipped
Run linting on the backend code / Build (pull_request) Successful in 3m8s
Run testing on the backend code / Build (pull_request) Failing after 3m56s

This commit is contained in:
2025-10-19 23:22:42 +02:00
parent d9724ff07d
commit 9c930996c7
4 changed files with 48 additions and 40 deletions

View File

@@ -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.

View File

@@ -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"]

View File

@@ -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 :