Files
anyway/backend/src/payments/test.py
kilian ab03cee3e3
Some checks failed
Build and deploy the backend to staging / Build and push image (pull_request) Failing after 50s
Build and deploy the backend to staging / Deploy to staging (pull_request) Has been skipped
Run testing on the backend code / Build (pull_request) Failing after 2m32s
Run linting on the backend code / Build (pull_request) Successful in 2m39s
strong base for payment handling
2025-10-08 17:30:07 +02:00

102 lines
2.6 KiB
Python

import requests
import json
from ..configuration.environment import Environment
# 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"
validation_url_prod = "https://api-m.paypal.com/v1/oauth2/token"
# payload for the post request
validation_data = {'grant_type': 'client_credentials'}
# pass the request
validation_response = requests.post(
url=validation_url,
data=validation_data,
auth=(username, password)
)
# todo check status code + try except. Status code 201 ?
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"
order_url_prod = "https://api-m.paypal.com/v2/checkout/orders"
# payload for the request
order_data = {
"intent": "CAPTURE",
"purchase_units": [
{
"items": [
{
"name": "AnyWay Credits",
"description": "50 pack of credits",
"quantity": 1,
"unit_amount": {
"currency_code": "CHF",
"value": "5.00"
}
}
],
"amount": {
"currency_code": "CHF",
"value": "5.00",
"breakdown": {
"item_total": {
"currency_code": "CHF",
"value": "5.00"
}
}
}
}
],
"application_context": {
"return_url": "https://anydev.info",
"cancel_url": "https://anydev.info"
}
}
order_response = requests.post(
url=order_url,
headers={"Authorization": f"Bearer {access_token}"}, ## need access token here?
json=order_data,
auth=(username, password)
)
# todo check status code + try except
print(order_response.text)
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_response = requests.post(
url=capture_url,
json={},
auth=(username, password)
)
# todo check status code + try except
print(capture_response.text)
# order_id = json.loads(response.text)["id"]