-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
138 lines (117 loc) · 3.09 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
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
from Crypto.Cipher import DES3
from fastapi import FastAPI, Header, HTTPException, status
from pydantic import BaseModel
from typing import Union
import base64
class Request(BaseModel):
accountid: str
amount: str
authmodel: str
bvn: Union[str, None] = None
cardname: Union[str, None] = None
cardno: str
cardphonenumber: str
cardphonetype: Union[str, None] = None
cardzip: Union[str, None] = None
chargetoken: str
country: str
currency: str
cvv: str
email: str
expirymonth: str
expiryyear: str
institution: Union[str, None] = None
institutionid: Union[str, None] = None
mcc: str
merchantaddress: str
merchantid: str
merchantname: str
mpgsmid: str
password: Union[str, None] = None
pin: str
publictoken: str
pwcMerchantId: str
responseurl: str
transactionreference: str
trxreference: str
vpcmerchant: str
vpcrouting: str
class EncryptedResponse(BaseModel):
accountid: str
amount: str
authmodel: str
bvn: Union[str, None] = None
cardname: Union[str, None] = None
cardno: str
cardphonenumber: str
cardphonetype: Union[str, None] = None
cardzip: Union[str, None] = None
chargetoken: str
country: str
currency: str
cvv: str
email: str
expirymonth: str
expiryyear: str
institution: Union[str, None] = None
institutionid: Union[str, None] = None
mcc: str
merchantaddress: str
merchantid: str
merchantname: str
mpgsmid: str
password: Union[str, None] = None
pin: str
publictoken: str
pwcMerchantId: str
responseurl: str
transactionreference: str
trxreference: str
vpcmerchant: str
vpcrouting: str
app = FastAPI()
IntegerValueKeyArray = [
"accountid",
"amount",
"cardno",
"cardphonenumber",
"chargetoken",
"cvv",
"expirymonth",
"expiryyear",
"mcc",
"pin",
"pwcMerchantId",
"bvn",
"cardzip",
"vpcrouting"
]
def encrypt_data(key, value):
blockSize = 8
padDiff = blockSize - (len(value) % blockSize)
cipher = DES3.new(key, DES3.MODE_ECB)
value = "{}{}".format(value, "".join(chr(padDiff) * padDiff))
test = value.encode('utf-8')
encrypted = base64.b64encode(cipher.encrypt(test)).decode("utf-8")
return encrypted
@app.get("/")
async def root():
return {"message": "Endpoint is up"}
@app.post("/encrypt")
async def formatRequest(request:Request, Token: str = Header(...)):
if not Token:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail = f'Please enter your Encryption key.'
)
req = request.dict()
for key in IntegerValueKeyArray:
data = req[key]
if req[key] is None:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail = f'{key} cannot be null. please pass a valid value.'
)
req[key] = encrypt_data(Token, data)
encrypted_response = EncryptedResponse(**req)
return encrypted_response