-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path04_stripe_server_auto.py
59 lines (44 loc) · 1.48 KB
/
04_stripe_server_auto.py
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
from time import sleep
import stripe
import logfire
from fastapi import FastAPI
from fastapi.responses import JSONResponse
from opentelemetry.instrumentation.requests import RequestsInstrumentor
logfire.configure()
# you can get a testing key by just signing up for stripe
STRIPE_KEY = 'sk_test_...'
stripe.api_key = STRIPE_KEY
RequestsInstrumentor().instrument()
app = FastAPI()
logfire.instrument_fastapi(app)
logfire.info('Starting the app')
RequestsInstrumentor().instrument()
@app.post('/payments/{user_id:int}/complete/')
def hello(user_id: int):
amount, currency, payment_method = get_payment_details(user_id)
try:
intent = stripe.PaymentIntent.create(
amount=amount,
currency=currency,
payment_method=payment_method,
confirm=True,
return_url='https://example.com/return',
)
except stripe.CardError:
store_payment_failure(user_id)
return JSONResponse(content={'detail': 'Card error'}, status_code=400)
else:
store_payment_success(user_id, intent)
@logfire.instrument()
def get_payment_details(user_id: int) -> [int, str, str]:
sleep(0.2)
if user_id == 42:
return 20_00, 'usd', 'pm_card_visa'
else:
return 20_00, 'usd', 'pm_card_visa_chargeDeclinedInsufficientFunds'
@logfire.instrument()
def store_payment_success(user_id: int, _indent) -> None:
sleep(0.2)
@logfire.instrument()
def store_payment_failure(user_id: int) -> None:
sleep(0.2)