1
This commit is contained in:
parent
f9b1308522
commit
a5fe2c79b0
@ -90,6 +90,7 @@ CREATE TABLE `confirmed_order` (
|
||||
`idx` int(11) NOT NULL AUTO_INCREMENT COMMENT '自增id',
|
||||
`orderid` varchar(60) DEFAULT '' COMMENT '订单id',
|
||||
`status` int(11) NOT NULL DEFAULT '0' COMMENT '0: 未确认 1:已确认',
|
||||
`confirm_time` int(11) NOT NULL DEFAULT '0' COMMENT '确认时间',
|
||||
`createtime` int(11) NOT NULL DEFAULT '0' COMMENT '创建时间',
|
||||
PRIMARY KEY (`idx`),
|
||||
KEY `orderid` (`orderid`)
|
||||
|
@ -10,9 +10,26 @@ import urllib.request
|
||||
import tornado.ioloop
|
||||
import tornado.web
|
||||
|
||||
mysql_cluster_json = json.loads(open('../config/pay_backend.mysql.cluster.json', 'r').read())
|
||||
notifyapi_json = json.loads(open('../config/pay_backend.notifyapi.json', 'r').read())
|
||||
last_idx = 0
|
||||
confirmed_order_list = []
|
||||
|
||||
def info(msg):
|
||||
print(str(datetime.datetime.now()) + '[INFO] ' + msg, flush = True)
|
||||
|
||||
def getMysqlConn(accountid):
|
||||
hash_code = binascii.crc32(accountid.encode())
|
||||
assert(hash_code >= 0)
|
||||
mysql_conf = mysql_cluster_json[hash_code % len(mysql_cluster)]
|
||||
return pymysql.connect(host = mysql_conf['host'],
|
||||
port = mysql_conf['port'],
|
||||
user = mysql_conf['user'],
|
||||
passwd = mysql_conf['passwd'],
|
||||
db = 'paydb',
|
||||
charset = 'utf8'
|
||||
)
|
||||
|
||||
def md5Sign(params, secret, timestamp, connstr = '&', secret_connstr = ':'):
|
||||
params_str = ''
|
||||
for key in sorted(params.keys()):
|
||||
@ -33,22 +50,63 @@ def getDaySeconds(time_val, incdays):
|
||||
info(dayseconds)
|
||||
return dayseconds
|
||||
|
||||
def sendNotify(conf, sendtime):
|
||||
def fetchConfirmedOrderList():
|
||||
try:
|
||||
info('sendNotify start')
|
||||
params = {'key' : 'kingsome'}
|
||||
secret = 'fc38349c5d084e920925e614c420be9f'
|
||||
conn = getMysqlConn('')
|
||||
cursor = conn.cursor()
|
||||
cursor.execute('SELECT sp_pay_result, accountid, itemid, price, orderid, gameid '
|
||||
'FROM orderinfo WHERE orderid="%s";' % conn.escape_string(orderid))
|
||||
except:
|
||||
return
|
||||
|
||||
def sendOneOrder(idx, orderid):
|
||||
try:
|
||||
conn = getMysqlConn('')
|
||||
cursor = conn.cursor()
|
||||
cursor.execute('SELECT sp_pay_result, accountid, itemid, price, orderid, gameid, status '
|
||||
'FROM orderinfo WHERE orderid="%s";' % conn.escape_string(orderid))
|
||||
row = cursor.fetchone()
|
||||
if (not row) or (row[0] != '1'):
|
||||
return True
|
||||
if row[6] == '0':
|
||||
timestamp = time.time()
|
||||
params = {
|
||||
'account_id' : row[1],
|
||||
'orderid' : row[4],
|
||||
'itemid' : row[2],
|
||||
'amount' : row[3],
|
||||
}
|
||||
secret = 'fc38349c5d084e920925e614c420be9f'
|
||||
md5signstr = md5Sign(params, secret, timestamp)
|
||||
url = conf['notify_url'] + '×tamp=' + str(timestamp) + '&sign=' + md5signstr
|
||||
req = urllib.request.Request(url)
|
||||
data = urllib.request.urlopen(req).read()
|
||||
info('sendNotify end')
|
||||
return True
|
||||
except:
|
||||
return False
|
||||
|
||||
def sendNotify(conf, sendtime):
|
||||
try:
|
||||
if len(confirmed_order_list) <= 0:
|
||||
fetchConfirmedOrderList();
|
||||
handled_cout = 0
|
||||
while len(confirmed_order_list) > 0 and handled_cout < 50:
|
||||
confirmed_order = confirmed_order_list[0]
|
||||
if sendOneOrder(confirmed_order['idx'], confirmed_order['orderid']):
|
||||
confirmed_order_list.pop(0)
|
||||
handled_cout += 1
|
||||
else:
|
||||
break
|
||||
except Exception as e:
|
||||
info('sendNotifu error: ' + str(e))
|
||||
|
||||
#进入下一次循环
|
||||
tornado.ioloop.IOLoop.current().call_at(getDaySeconds(time.time(), 1) + sendtime,
|
||||
if len(confirmed_order_list) > 0:
|
||||
tornado.ioloop.IOLoop.current().call_at(time.time() + 1,
|
||||
lambda : sendNotify(conf, sendtime))
|
||||
else:
|
||||
tornado.ioloop.IOLoop.current().call_at(time.time() + sendtime,
|
||||
lambda : sendNotify(conf, sendtime))
|
||||
|
||||
class SelfCheckingHandler(tornado.web.RequestHandler):
|
||||
@ -73,6 +131,6 @@ if __name__ == "__main__":
|
||||
app = make_app()
|
||||
app.listen(conf['listen_port'])
|
||||
|
||||
tornado.ioloop.IOLoop.current().call_at(getDaySeconds(time.time(), 1) + conf['notifytime'],
|
||||
tornado.ioloop.IOLoop.current().call_at(time.time()+ conf['notifytime'],
|
||||
lambda : sendNotify(conf, conf['notifytime']))
|
||||
tornado.ioloop.IOLoop.current().start()
|
||||
|
Loading…
x
Reference in New Issue
Block a user