-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
139 lines (124 loc) · 4.64 KB
/
main.py
File metadata and controls
139 lines (124 loc) · 4.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import os
import uuid
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
import httpx
from dotenv import load_dotenv
load_dotenv()
WOMPI_PUBLIC_KEY = os.getenv("WOMPI_PUBLIC_KEY", "YOUR_PUBLIC_KEY_HERE")
WOMPI_PRIVATE_KEY = os.getenv("WOMPI_PRIVATE_KEY", "YOUR_PRIVATE_KEY_HERE")
WOMPI_API_URL = "https://sandbox.wompi.co/v1/"
WOMPI_PAYOUTS_URL = "https://api.sandbox.payouts.wompi.co/v1/"
WOMPI_USER_PRINCIPAL_ID = os.getenv("WOMPI_USER_PRINCIPAL_ID", "your-user-principal-id")
WOMPI_X_API_KEY = os.getenv("WOMPI_X_API_KEY", "your-x-api-key")
app = FastAPI(title="Wompi API Consumer", description="A FastAPI app to interact with Wompi API.")
class CardTokenRequest(BaseModel):
number: str
cvc: str
exp_month: str
exp_year: str
card_holder: str
@app.post("/wompi/card-token")
async def create_card_token(card: CardTokenRequest):
url = WOMPI_API_URL + "tokens/cards"
data = {
"number": card.number,
"cvc": card.cvc,
"exp_month": card.exp_month,
"exp_year": card.exp_year,
"card_holder": card.card_holder
}
headers = {
"Authorization": f"Bearer {WOMPI_PUBLIC_KEY}",
"Content-Type": "application/json"
}
async with httpx.AsyncClient() as client:
response = await client.post(url, json=data, headers=headers)
if response.status_code != 201:
raise HTTPException(status_code=response.status_code, detail=response.text)
return response.json()
class Bank(BaseModel):
financial_institution_code: str
financial_institution_name: str
type: str
class PaymentRequest(BaseModel):
amount_in_cents: int
currency: str
customer_email: str
payment_method_type: str
reference: str
token: str
installments: int
@app.post("/transactions/credit-card")
async def create_transaction(payment: PaymentRequest):
url = WOMPI_API_URL + "transactions"
headers = {"Authorization": f"Bearer {WOMPI_PUBLIC_KEY}"}
data = {
"amount_in_cents": payment.amount_in_cents,
"currency": payment.currency,
"customer_email": payment.customer_email,
"payment_method": {
"type": payment.payment_method_type,
"token": payment.token,
"installments": payment.installments
},
"reference": payment.reference
}
async with httpx.AsyncClient() as client:
response = await client.post(url, json=data, headers=headers)
if response.status_code != 201:
raise HTTPException(status_code=response.status_code, detail=response.text)
return response.json()
@app.get("/wompi/banks")
async def list_banks():
"""
Lists available banks for PSE payments in Wompi
"""
url = WOMPI_API_URL + "pse/financial_institutions"
headers = {
"Authorization": f"Bearer {WOMPI_PUBLIC_KEY}",
"Content-Type": "application/json"
}
async with httpx.AsyncClient() as client:
response = await client.get(url, headers=headers)
if response.status_code != 200:
raise HTTPException(status_code=response.status_code, detail=response.text)
return response.json()
@app.get("/wompi/merchants/{merchant_id}/banks")
async def list_merchant_banks(merchant_id: str):
"""
Lists banks associated with a specific merchant
"""
url = f"{WOMPI_API_URL}merchants/{merchant_id}/payment_sources/banks"
headers = {
"Authorization": f"Bearer {WOMPI_PUBLIC_KEY}",
"Content-Type": "application/json"
}
async with httpx.AsyncClient() as client:
response = await client.get(url, headers=headers)
if response.status_code != 200:
raise HTTPException(status_code=response.status_code, detail=response.text)
return response.json()
@app.get("/wompi/payouts/banks")
async def list_payouts_banks():
"""
Lists available banks for payments using Wompi's Payouts API
Documentation: https://app.swaggerhub.com/apis-docs/wompi/Payouts/1.0.0/#/Resources/get_banks
"""
url = f"{WOMPI_PAYOUTS_URL}banks"
# Generate a unique idempotency key
idempotency_key = str(uuid.uuid4()).replace("-", "")[:64]
headers = {
"user-principal-id": WOMPI_USER_PRINCIPAL_ID,
"x-api-key": WOMPI_X_API_KEY,
"idempotency-key": idempotency_key,
"Content-Type": "application/json"
}
async with httpx.AsyncClient() as client:
response = await client.get(url, headers=headers)
if response.status_code != 200:
raise HTTPException(status_code=response.status_code, detail=response.text)
return response.json()
@app.get("/health")
async def health():
return {"status": "ok"}