Skip to content

Commit

Permalink
First test version of subscription plan API...
Browse files Browse the repository at this point in the history
  • Loading branch information
freQniK committed Nov 3, 2023
1 parent 454ebf4 commit 3f8caef
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 9 deletions.
138 changes: 130 additions & 8 deletions meile_plan_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import scrtxxs


VERSION=20231024.0250

app = Flask(__name__)
Expand All @@ -28,9 +29,9 @@
HotWalletAddress = scrtxxs.WalletAddress
keyring_passphrase = scrtxxs.HotWalletPW

DBdir = '/home/' + str(pwd.getpwuid(os.getuid())[0]) + '/dbs'
WalletLogDIR = '/home/' + str(pwd.getpwuid(os.getuid())[0]) + '/Logs'
DBFile = 'sqlite:///' + DBdir + '/dvpn_stripe.sqlite'
DBdir = scrtxxs.dbDIR
WalletLogDIR = scrtxxs.LogDIR
DBFile = 'sqlite:///' + DBdir + '/meile_plan.sqlite'


# SQLAlchemy Configurations
Expand Down Expand Up @@ -130,16 +131,137 @@ def GetDBCursor():
conn = mysql.connect()
return conn.cursor()

def GetPlanCostDenom(uuid):

query = "SELECT plan_price, plan_denom FROM meile_plans;"

c = GetDBCursor()
c.execute(query)
plan411 = c.fetchone()

return plan411['plan_price'], plan411['plan_denom']

def CheckRenewalStatus(subid, wallet):

query = f"SELECT subscription_id, subscription_date FROM meile_subscriptions WHERE wallet={wallet} AND subscription_id = {subid}"
c = GetDBCursor()
c.execute(query)

results = c.fetchone()

if results['subscription_date'] and results['subscription_id']:
return True,results['subscription_date']
else:
return False, None

@app.route('/v1/add', methods=['POST'])
@auth.login_required
def add_wallet_to_plan():
wallet = request.json.get('wallet')
plan_uuid = request.json.get('uuid') # plan ID, we should have 4 or 5 plans. Will be a UUID.
duration = request.json.get('duration') # duration of plan subscription, in months
sub_id = request.json.get('subid')
status = False
renewal = False
try:
wallet = request.json.get('wallet')
plan_id = int(request.json.get('planid')) # plan ID, we should have 4 or 5 plans. Will be a UUID.
duration = int(request.json.get('duration')) # duration of plan subscription, in months
sub_id = int(request.json.get('subid')) # subscription ID of plan
uuid = request.json.get('uuid') # uuid of subscription
amt_paid = int(request.json.get('amt'))
denom = int(request.json.get('denom'))
except Exception as e:
print(str(e))
status = False
tx = None
message = "Not all POST values were present. Please try submitting your request again."
PlanTX = {'status' : status, 'wallet' : wallet, 'planid' : plan_id, 'id' : sub_id, 'duration' : duration, 'tx' : tx, 'message' : message, 'expires' : None}
print(PlanTX)
return jsonify(PlanTX)

cost, denom = GetPlanCostDenom(uuid)

if not cost or not denom:
status = False
message = "No plan found in Database. Wallet not added to non-existing plan"
tx = "None"
PlanTX = {'status' : status, 'wallet' : wallet, 'planid' : plan_id, 'id' : sub_id, 'duration' : duration, 'tx' : tx, 'message' : message, 'expires' : None}
print(PlanTX)
return jsonify(PlanTX)

renewal,subscription_date = CheckRenewalStatus(sub_id, wallet)

now = datetime.now()
plan_expirary = now + relativedelta(months=+duration)
expires = now + relativedelta(months=+duration)


WalletLogFile = os.path.join(WalletLogDIR, "meile_plan.log")
add_to_plan_cmd = '%s tx vpn subscription allocate --from "%s" --gas-prices "0.3udvpn" --node "%s" --keyring-dir "%s" --keyring-backend "file" --chain-id "%s" --yes %s "%s" %d' % (scrtxxs.sentinelhub,
scrtxxs.WalletName,
scrtxxs.RPC,
scrtxxs.KeyringDIR,
scrtxxs.CHAINID,
sub_id,
wallet,
scrtxxs.BYTES)




print(add_to_plan_cmd)
try:
ofile = open(WalletLogFile, 'ab+')

child = pexpect.run(add_to_plan_cmd)
child.logfile = ofile

child.expect("Enter .*")
child.sendline(keyring_passphrase)
child.expect(pexpect.EOF)


ofile.flush()
ofile.close()
ofile.close()
with open(WalletLogFile ,'r+') as rfile:
last_line = rfile.readlines()[-1]
if 'txhash' in last_line:
tx = last_line.split(':')[-1].rstrip().lstrip()
print(f"{wallet} added to plan: {sub_id}, plan_id: {plan_id}, {duration} months, hash: {tx}")
else:
tx = 'none'

rfile.close()
status = True
message = "Success."
except Exception as e:
print(str(e))
status = False
message = "Error adding wallet to plan. Please contact support@mathnodes.com for assistance."
expires = None
if renewal and subscription_date is not None:
query = '''
UPDATE meile_subscriptions
SET uuid = "%s", wallet = "%s", subscription_id = %d, plan_id = %d, amt_paid = %d, amt_denom = "%s", subscribe_date = "%s", subscription_duration = %d, expires = "%s"
WHERE wallet = "%s" AND subscription_id = %d
''' % (uuid, wallet, sub_id, plan_id, amt_paid, denom, subscription_date, duration, str(expires), wallet, sub_id)

else:
query = '''
INSERT INTO meile_subscriptions (uuid, wallet, subscription_id, plan_id, amt_paid, amt_denom, subscribe_date, subscription_duration, expires)
VALUES("%s", "%s", %d, %d, %d, "%s", "%s", %d, "%s")
''' % (uuid, wallet, sub_id, plan_id, amt_paid, denom, str(now), duration, str(expires))


print("Updating Subscription Table...")
try:
UpdateDBTable(query)
except Exception as e:
print(str(e))
status = False
message = "Error updating subscription table. Please contact support@mathnodes.com for more information."
tx = None
expires = None

PlanTX = {'status' : status, 'wallet' : wallet, 'planid' : plan_id, 'id' : sub_id, 'duration' : duration, 'tx' : tx, 'message' : message, 'expires' : expires}
return jsonify(PlanTX)


@app.route('/v1/plans', methods=['GET'])
Expand Down
17 changes: 17 additions & 0 deletions scrtxxs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import pwd
import os

WalletAddress = "sent..."
WalletName = "Name"
HotWalletPW = "password"
SQLAlchemyScrtKey = 'sql_scrt_key'
MySQLUsername = 'username'
MySQLPassword = 'password'
MySQLDB = 'db_name'
BYTES = 1073741824000 # 1 TB
RPC = "https://rpc.mathnodes.com:443"
CHAINID = "sentinelhub-2"
KeyringDIR = "/home/" + str(pwd.getpwuid(os.getuid())[0])
LogDIR = '/home/' + str(pwd.getpwuid(os.getuid())[0]) + '/Logs'
dbDIR = '/home/' + str(pwd.getpwuid(os.getuid())[0]) + '/dbs'
sentinelhub = "/home/" + str(pwd.getpwuid(os.getuid())[0]) + "/go/bin/sentinelhub"
5 changes: 4 additions & 1 deletion sql/plan_db.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
--- Create Meile Plan Table

CREATE TABLE meile_plans (uuid VARCHAR(100), subscription_id UNSIGNED BIGINT, plan_id UNSIGNED BIGINT, plan_name VARACHAR(256), plan_price UNSIGNED BIGINT, plan_denom VARCHAR(6), PRIMARY KEY(uuid))
CREATE TABLE meile_plans (uuid VARCHAR(100), subscription_id UNSIGNED BIGINT, plan_id UNSIGNED BIGINT, plan_name VARACHAR(256), plan_price UNSIGNED BIGINT, plan_denom VARCHAR(100), PRIMARY KEY(uuid))

--- Create Meile Subscriber Table
CREATE TABLE meile_subscriptions (id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, uuid VARCHAR(100), wallet VARCHAR(100), subscription_id UNSIGNED BIGINT, plan_id UNSIGNED BIGINT, amt_paid DECIMAL(24,12), amt_denom VARCHAR(100), subscribe_date TIMSTAMP, subscription_duration UNSIGNED SMALLINT, expires TIMESTAMP, PRIMARY KEY(id))

0 comments on commit 3f8caef

Please sign in to comment.