forked from V07D/bitmessage-email-gateway
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathwalletnotify.py
executable file
·123 lines (109 loc) · 4.23 KB
/
walletnotify.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
#!/usr/bin/python2.7
import jsonrpclib
import MySQLdb
import sys
import hashlib
import datetime
import decimal
from lib.config import BMConfig
from lib.mysql import BMMySQL
from lib.bmlogging import BMLogging
from lib.user import GWUser
from lib.bmapi import BMAPI
from lib.sendbm import SendBMTemplate
from warnings import filterwarnings
def parse_configfile(fname):
ret = {
'rpcport': 8332,
'rpcuser': '',
'rpcpassword': '',
'rpcconnect': '127.0.0.1'
}
from os.path import expanduser
fname = expanduser(fname)
with open(fname) as f:
for line in f.read().splitlines():
try:
key, val = line.split("=")
except:
continue
if not key:
continue
if key in ret:
ret[key] = val
return ret
if len(sys.argv) < 3:
print "Must specify coin (BTC or DRK) and txid"
sys.exit()
coin = sys.argv[1]
txid = sys.argv[2]
if coin == 'BTC':
cfg = parse_configfile('~/.bitcoin/bitcoin.conf')
elif coin == 'DRK':
cfg = parse_configfile('~/.dash/dash.conf')
else:
print "Unknown coin " + coin
sys.exit()
client = jsonrpclib.Server('http://' + cfg['rpcuser'] + ":" + cfg['rpcpassword'] + "@" + cfg['rpcconnect'] + ":" + str(cfg['rpcport']) + "/")
txinfo = client.gettransaction(txid, True)
BMLogging()
cur = BMMySQL().conn().cursor(MySQLdb.cursors.DictCursor)
while BMAPI().conn() is False:
print "Failure connecting to API, sleeping..."
time.sleep(random.random()+0.5)
filterwarnings('ignore', category = MySQLdb.Warning)
#amount = txinfo['amount']
for detail in txinfo['details']:
if detail['category'] == "receive":
print detail['address'] + " -> " + str(detail['amount'])
cur.execute ( """INSERT IGNORE INTO payment (address, coin, txid, amount, confirmations) VALUES (%s, %s, %s, %s, %s)""",
(detail['address'], coin, txid, detail['amount'], txinfo['confirmations']))
if txinfo['confirmations'] == 0:
# select from invoice
cur.execute ("SELECT amount, paid, type, payer, sendercharge_id FROM invoice WHERE address = %s AND coin = %s", (detail['address'], coin))
for row in cur.fetchall():
invoice = row
# fetch
totalpaid = 0
cur.execute ("SELECT amount FROM payment WHERE address = %s AND coin = %s AND confirmations = 0", (detail['address'], coin))
for row in cur.fetchall():
payment = row
totalpaid += payment['amount'] # fixme floating rounding problems?
# fetch
if invoice['paid'] == None or invoice['paid'] <= datetime.datetime(1971, 1, 1) and totalpaid >= invoice['amount']:
cur.execute ("UPDATE invoice SET paid = NOW() WHERE address = %s AND coin = %s ", (detail['address'], coin))
if invoice['type'] == 0:
incomingamount = decimal.Decimal(str(detail['amount']))
if incomingamount < invoice['amount']:
months = 0
else:
months = incomingamount / invoice['amount']
print "Extending for " + str(months) + " months"
userdata = GWUser(bm = invoice['payer'])
if not hasattr(userdata, "exp"):
# user was deleted
continue
datefrom = userdata.exp if userdata.exp > datetime.date.today() else datetime.date.today()
cur.execute ("UPDATE user SET cansend = 1, exp = IF(exp < CURDATE(),DATE_ADD(CURDATE(), INTERVAL " +
str(months) + " MONTH),DATE_ADD(exp, INTERVAL " + str(months) +
" MONTH)) WHERE bm = %s", invoice['payer']);
userdata = GWUser(bm = invoice['payer'])
dateuntil = userdata.exp
SendBMTemplate(
sender = BMAPI().get_address(BMConfig().get("bmgateway", "bmgateway", "registration_address_label")),
recipient = invoice['payer'],
template = "invoice",
addmaps = {
'btcamount': str(incomingamount),
'service': 'Subscription for ' + userdata.email + ' from ' + datefrom.strftime("%B %-d %Y") +
' until ' + dateuntil.strftime("%B %-d %Y"),
'email': userdata.email
})
elif invoice['type'] == 1:
# find in combo table and allow
cur.execute ("UPDATE sendercharge SET status = 1 WHERE id = %s", (invoice['sendercharge_id']))
elif invoice['type'] == 2:
# notify user that payment is incoming
pass
pass
cur.close()