feat: Add promo codes
All checks were successful
Build Docker / Build Image (push) Successful in 21s
All checks were successful
Build Docker / Build Image (push) Successful in 21s
This commit is contained in:
74
payments.py
74
payments.py
@@ -29,6 +29,10 @@ else:
|
||||
with open(path+'used.json', 'w') as f:
|
||||
json.dump(used, f, indent=4)
|
||||
|
||||
if not os.path.exists(path+'promo.json'):
|
||||
with open(path+'promo.json', 'w') as f:
|
||||
json.dump([], f, indent=4)
|
||||
|
||||
HNSaddress = os.getenv('ADDRESS')
|
||||
|
||||
names = {
|
||||
@@ -37,7 +41,7 @@ names = {
|
||||
}
|
||||
|
||||
|
||||
def generate_payment(name,email,mobile,address,country,cart,hns):
|
||||
def generate_payment(name,email,mobile,address,country,cart,hns,promo=False):
|
||||
# Generate a payment object
|
||||
payment_id = generate_payment_id()
|
||||
if payment_id == "ERROR":
|
||||
@@ -50,6 +54,11 @@ def generate_payment(name,email,mobile,address,country,cart,hns):
|
||||
if item['name'] in names:
|
||||
item['name'] = names[item['name']]
|
||||
|
||||
if promo:
|
||||
promo = promo["id"]
|
||||
usePromo(promo)
|
||||
|
||||
|
||||
payment = {
|
||||
"name": name,
|
||||
"email": email,
|
||||
@@ -59,6 +68,7 @@ def generate_payment(name,email,mobile,address,country,cart,hns):
|
||||
"hns": hns,
|
||||
"ID": finalPrice,
|
||||
"cart": cart,
|
||||
"promo": promo,
|
||||
"status": "Pending"
|
||||
}
|
||||
finalPriceDolarydoo = float(finalPrice) * 1000000
|
||||
@@ -134,6 +144,68 @@ def generate_payment_id():
|
||||
|
||||
if id > 99:
|
||||
return "ERROR"
|
||||
|
||||
|
||||
def getPromo(code):
|
||||
with open(path+'promo.json', 'r') as f:
|
||||
promos = json.load(f)
|
||||
for promo in promos:
|
||||
if promo['id'] == code:
|
||||
return promo
|
||||
return False
|
||||
|
||||
|
||||
def getPromos():
|
||||
with open(path+'promo.json', 'r') as f:
|
||||
promos = json.load(f)
|
||||
return promos
|
||||
|
||||
def addPromo(id, constant, percent, uses):
|
||||
with open(path+'promo.json', 'r') as f:
|
||||
promos = json.load(f)
|
||||
|
||||
for promo in promos:
|
||||
if promo['id'] == id:
|
||||
return False
|
||||
|
||||
|
||||
promos.append({
|
||||
"id": id,
|
||||
"constant": constant,
|
||||
"percent": percent,
|
||||
"uses": uses
|
||||
})
|
||||
with open(path+'promo.json', 'w') as f:
|
||||
json.dump(promos, f, indent=4)
|
||||
return True
|
||||
|
||||
def deletePromo(id):
|
||||
with open(path+'promo.json', 'r') as f:
|
||||
promos = json.load(f)
|
||||
for promo in promos:
|
||||
if promo['id'] == id:
|
||||
promos.remove(promo)
|
||||
with open(path+'promo.json', 'w') as f:
|
||||
json.dump(promos, f, indent=4)
|
||||
return True
|
||||
return False
|
||||
|
||||
def usePromo(id):
|
||||
with open(path+'promo.json', 'r') as f:
|
||||
promos = json.load(f)
|
||||
for promo in promos:
|
||||
if promo['id'] == id:
|
||||
if promo['uses'] == "1":
|
||||
promos.remove(promo)
|
||||
elif promo['uses'] == "-1":
|
||||
return True
|
||||
|
||||
uses = int(promo['uses'])
|
||||
uses -= 1
|
||||
promo['uses'] = str(uses)
|
||||
with open(path+'promo.json', 'w') as f:
|
||||
json.dump(promos, f, indent=4)
|
||||
return True
|
||||
|
||||
if __name__ == '__main__':
|
||||
for i in range(10):
|
||||
|
||||
Reference in New Issue
Block a user