Merge modifications for more separate backend functions #69
| @@ -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 : | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user