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
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:
@@ -1,7 +1,7 @@
|
|||||||
import logging
|
import logging
|
||||||
from typing import Literal
|
from typing import Literal
|
||||||
|
|
||||||
from fastapi import APIRouter, HTTPException
|
from fastapi import APIRouter, Query, Body
|
||||||
from ..payments import PaypalClient, OrderRequest
|
from ..payments import PaypalClient, OrderRequest
|
||||||
from ..supabase.supabase import SupabaseClient
|
from ..supabase.supabase import SupabaseClient
|
||||||
from ..cache import CreditCache, make_credit_cache_key
|
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.
|
# 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,
|
user_id: str = Query(...),
|
||||||
basket: list,
|
basket: list = Query(...),
|
||||||
currency: Literal['CHF', 'EUR', 'USD'],
|
currency: str = Query(...),
|
||||||
return_url_success: str = 'https://anydev.info',
|
return_url_success: str = Query('https://anydev.info'),
|
||||||
return_url_failure: str = 'https://anydev.info'
|
return_url_failure: str = Query('https://anydev.info')
|
||||||
):
|
):
|
||||||
"""
|
"""
|
||||||
Creates a new PayPal order.
|
Creates a new PayPal order.
|
||||||
|
|||||||
@@ -1,17 +1,19 @@
|
|||||||
|
#%%
|
||||||
import requests
|
import requests
|
||||||
import json
|
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/
|
# 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 ########
|
######## STEP 1: Validation ########
|
||||||
# url for validation post request
|
# url for validation post request
|
||||||
validation_url = "https://api-m.sandbox.paypal.com/v1/oauth2/token"
|
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"]
|
access_token = json.loads(validation_response.text)["access_token"]
|
||||||
|
|
||||||
|
|
||||||
|
#%%
|
||||||
######## STEP 2: Create Order ########
|
######## STEP 2: Create Order ########
|
||||||
# url for post request
|
# url for post request
|
||||||
order_url = "https://api-m.sandbox.paypal.com/v2/checkout/orders"
|
order_url = "https://api-m.sandbox.paypal.com/v2/checkout/orders"
|
||||||
@@ -79,15 +82,20 @@ order_response = requests.post(
|
|||||||
auth=(username, password)
|
auth=(username, password)
|
||||||
)
|
)
|
||||||
|
|
||||||
# todo check status code + try except
|
# Send the redirect link to the user
|
||||||
print(order_response.text)
|
# print(order_response.json())
|
||||||
order_id = json.loads(order_response.text)["id"]
|
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
|
######## STEP 3: capture payment
|
||||||
# url for post request
|
# url for post request
|
||||||
capture_url = f"https://api-m.sandbox.paypal.com/v2/checkout/orders/{order_id}/capture"
|
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(
|
capture_response = requests.post(
|
||||||
url=capture_url,
|
url=capture_url,
|
||||||
@@ -96,6 +104,6 @@ capture_response = requests.post(
|
|||||||
)
|
)
|
||||||
|
|
||||||
# todo check status code + try except
|
# todo check status code + try except
|
||||||
print(capture_response.text)
|
print(capture_response.json())
|
||||||
# order_id = json.loads(response.text)["id"]
|
# order_id = json.loads(response.text)["id"]
|
||||||
|
|
||||||
|
|||||||
@@ -4,6 +4,12 @@ from fastapi.testclient import TestClient
|
|||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from ..main import app
|
from ..main import app
|
||||||
|
from ..supabase.supabase import SupabaseClient
|
||||||
|
|
||||||
|
|
||||||
|
# Create a supabase client
|
||||||
|
supabase = SupabaseClient()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(scope="module")
|
@pytest.fixture(scope="module")
|
||||||
@@ -12,24 +18,7 @@ def client():
|
|||||||
return TestClient(app)
|
return TestClient(app)
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
def test_nearby(client): # pylint: disable=redefined-outer-name
|
||||||
"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
|
|
||||||
"""
|
"""
|
||||||
Test n°1 : Verify handling of invalid input.
|
Test n°1 : Verify handling of invalid input.
|
||||||
|
|
||||||
@@ -37,7 +26,18 @@ def test_nearby(client, location, status_code): # pylint: disable=redefined-o
|
|||||||
client:
|
client:
|
||||||
request:
|
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()
|
suggestions = response.json()
|
||||||
|
|
||||||
# checks :
|
# checks :
|
||||||
|
|||||||
@@ -56,8 +56,8 @@ def test_trip(client, request, sightseeing, shopping, nature, max_time_minute, s
|
|||||||
"preferences": prefs.model_dump(),
|
"preferences": prefs.model_dump(),
|
||||||
"start": start_coords,
|
"start": start_coords,
|
||||||
"end": end_coords,
|
"end": end_coords,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
landmarks = response.json()
|
landmarks = response.json()
|
||||||
@@ -71,8 +71,8 @@ def test_trip(client, request, sightseeing, shopping, nature, max_time_minute, s
|
|||||||
"landmarks": landmarks,
|
"landmarks": landmarks,
|
||||||
"start": start,
|
"start": start,
|
||||||
"end": end,
|
"end": end,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
|
|
||||||
# Increment the user balance again
|
# Increment the user balance again
|
||||||
|
|||||||
Reference in New Issue
Block a user