-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
109 lines (87 loc) · 2.78 KB
/
main.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
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
from itertools import count
from time import time
from urllib import request
from uuid import uuid4
from fastapi import FastAPI
from datetime import datetime
from api import create_new_task, paytm_api_call, transaction_status
from db import get_request_from_db, store_new_request_to_db
from functions import generate_qr
from models import DrinkType, NewteaModel, Paytm_api_call, QR_Res_Model, TeaRequests
app = FastAPI()
MERCHANT_ID = '123456'
POS_ID = "lajksdkljdfhj"
@app.get("/")
def read_root():
return {"Hello": "World"}
@app.post("/tea/new")
def newtea(tea:NewteaModel):
# Calculate Date
newdata = TeaRequests(
key = uuid4().hex,
time = datetime.now(),
machine_number= tea.machine_number,
amount=tea.amount,
status="New Request",
config=tea.config,
type = tea.type,
orderId = uuid4().hex,
quantity= tea.count
)
# Store request in db
store_new_request_to_db(newdata)
# Check Status and Levels
if tea.config :
if tea.config.water_level < 10:
return 'Not Enough Water'
if tea.type == DrinkType.TEA and tea.config.tea_powder < 10:
return 'Not Enough Tea Powder'
if tea.type == DrinkType.COFFEE and tea.config.coffee_power < 10:
return 'Not Enough Coffee Powder'
# Call paytm api
data = Paytm_api_call(
mid = MERCHANT_ID,
orderId = newdata.orderId,
amount = newdata.amount,
businessType="UPI_QR_CODE",
posId=POS_ID
)
result = paytm_api_call(data)
# Check result status
if result.resultInfo.resultStatus != 'SUCCESS':
return "API Generation Failed"
# Update status in DB
newdata.status = "QR GENERATED"
store_new_request_to_db(newdata)
# Create new task to poll txn resul api.
create_new_task()
# return qr response
qr, length = generate_qr(result.qrData)
return {
'qr' : qr,
'size' : length,
'request_id': newdata.key
}
@app.get("/payment/status/{request_id}")
def get_payment_status(request_id:str):
request = get_request_from_db(request_id)
# request exists?
if request is None:
return "Request Not Found"
# request status check
# Paytm api call
transaction = transaction_status(MERCHANT_ID,request.orderId)
# update request status
request.status = transaction.resultInfo.resultStatus
store_new_request_to_db(request)
if transaction.resultInfo.resultStatus == 'TXN_SUCCESS':
return {
'amount' : transaction.txnAmount,
'status' : "SUCCESS"
}
if transaction.resultInfo.resultStatus == 'PENDING':
return {
'amount' : transaction.txnAmount,
'status' : "PENDING"
}
return False