def singleTopup(product, dest, order, mt): if not mt: self.productError(p['protocol'], product['product_id'], agent_id, dest) return dest = sanitizeMSISDN(dest) if not re.match('^[\w#]+$', dest): self.writeNotifyOut(prot, 'command_invalid', {}) print 'invalid command by {0}'.format(agent_id) return productcode = product['product_id'] operator_id = product['operator_id'] product_type = product['type'] agent_name = verify_agent['agent']['agent_name'] if '{0}.{1}'.format(productcode, dest) in self.parsed: print 'reject same {0}.{1} for {2} ({3})'.\ format(productcode, dest, agent_name, p['protocol']) self.log.info('reject same {0}.{1} for {2} ({3})'.\ format(productcode, dest, agent_name, p['protocol'])) return tmp = datetime.now() start_date = datetime(tmp.year, tmp.month, tmp.day, 0, 0, 0, 0) end_date = datetime(tmp.year, tmp.month, tmp.day, 23, 59, 59) cursor = self.dbconn.cursor(MySQLdb.cursors.DictCursor) cursor.execute('SELECT `product_id`,`status`,`order`,' \ '`references`,`transaction_datetime` ' \ 'FROM `transaction` ' \ 'WHERE `transaction_datetime`>=%s and `transaction_datetime`<=%s ' \ 'AND `agent_id`=%s and `msisdn_destination`=%s ' \ 'AND `product_id`=%s and `order`=%s order by `transaction_id` DESC LIMIT 1', (start_date, end_date, agent_id, dest, productcode, order)) r = cursor.fetchone() if r: #return status topup_status = getStatusMsg(r['status']) st = int(r['status']) if st == const.TR_RETRIED: ref = '' elif st >= const.TR_EXECUTED and st < const.TR_FAILED_HLR: ref = 'Ref:{0}'.format(r['references']) else: ref = '' self.writeNotifyOut(p['protocol'], 'transaction_exist', { 'product_id': self.productIdAndOrder(r['product_id'], r['order']), 'dest': dest, 'timestamp': r['transaction_datetime'].\ strftime('%d/%m/%y %H:%M:%S'), 'status': topup_status, 'ref': ref, }) else: # write new topup request self.to_db.append((nin_id, datetime.now(), agent_id, productcode, p['protocol'], dest, order, const.TR_WAIT, agent_name, operator_id, mt, product_type)) print '{0}.{1} for {2} ({3}) mt {4}'.\ format(productcode, dest, agent_name, p['protocol'], mt) self.log.info('{0}.{1} for {2} ({3}) mt {4}'.\ format(productcode, dest, agent_name, p['protocol'], mt)) self.parsed.append('{0}.{1}'.format(productcode, dest)) cursor.close()
def _sd_daftar(self, p, ag): c = self.dbconn.cursor(MySQLdb.cursors.DictCursor) sql = '''SELECT `msisdn` FROM `rs` WHERE `msisdn`=%s''' msisdn = sanitizeMSISDN(p['msisdn']) c.execute(sql, (msisdn,)) if c.fetchone(): self.writeNotifyOut(p['protocol'], 'rs_exist', {'msisdn': p['msisdn']}) return sql = '''SELECT `device_id` FROM `sd` WHERE `sd_id`=%s''' c.execute(sql, (ACTIVE_SD,)) x = c.fetchone() if not x: print 'INVALID SD' return sd_dev = self.c_device.get(self.dbconn, self.cacheconn, x['device_id']) if not sd_dev: print 'INVALID DEVICE_ID FOR SD' return msg = SD_DAFTAR.format(msisdn=msisdn, pin=sd_dev['pin'], device_id=sd_dev['device_id']) leaf_server = '{0}@{2}/{1}'.format(config.LEAFSERVER, sd_dev['server_id'], config.MSG_SERVER) self.sendMessage(leaf_server, 'JBDV', msg, commit=False) sql = '''INSERT INTO `rs` (`msisdn`,`sd_id`,`name`,`address`,`agent_id`,`status`) VALUES (%(msisdn)s,%(sd_id)s,%(name)s,%(address)s,%(agent_id)s,%(status)s)''' c.execute(sql, { 'msisdn': msisdn, 'sd_id': ACTIVE_SD, 'name': p['name'], 'address': p['address'], 'agent_id': ag['agent_id'], 'status': 9, }) self.writeNotifyOut(p['protocol'], 'begin_rs_register', {'msisdn': msisdn})
def th_unlock_rs(self, msisdn, pin): msisdn = sanitizeMSISDN(msisdn) msg = '{device_id},0,ussd://*897*3*4*{msisdn}*{pin}#' bc = AgentNotifier() self.thUSSD(bc, msg, msisdn) sql = '''SELECT `status` FROM `rs` WHERE `msisdn`=%s''' o,n = self.wait(bc, sql, msisdn) if o == n: return json.dumps({'success': 0, 'reason': 'TIMEOUT'}) return json.dumps({'success': 1, 'reason': ''})
def th_saldo_rs(self, msisdn, pin): msisdn = sanitizeMSISDN(msisdn) msg = '{device_id},0,ussd://*897*5*{msisdn}*{pin}#' bc = AgentNotifier() self.thUSSD(bc, msg, msisdn) sql = '''SELECT `balance` FROM `rs` WHERE `msisdn`=%s''' o,n = self.wait(bc, sql, msisdn) if o == n: return json.dumps({'success': 0, 'reason': 'TIMEOUT'}) return json.dumps({'success': 1, 'reason': '', 'notifyid': int(n),})
def web_trx(self, agent_id, product, msisdn, pin): bc = AgentNotifier() ma = ManageAgent(bc.dbconn, bc.cacheconn) hlrmap = HLRMap(bc.dbconn, bc.cacheconn) x = ma.verifyAgent(agent_id, '1234', True, False) if x['code'] in (agent.AGST_NOTFOUND, agent.AGST_NOTACTIVE): return json.dumps({'success': 0, 'reason': 'Agent not registered / not active'}) prod_c = DBCache(const.PRODUCT_PREFIX, config.DEFAULT_EXPIRE, const.PRODUCT_SQL) prod = prod_c.get(bc.dbconn, bc.cacheconn, product) if not prod: return json.dumps({'success': 0, 'reason': 'Invalid product id'}) msisdn = sanitizeMSISDN(msisdn) method = firstMethod(hlrmap, prod, msisdn) tran_id = self.topup(bc, x['agent'], msisdn, prod, method) return json.dumps({'success': 1, 'reason': '', 'tranid': tran_id})
def _3sakti_t(self, p, ag): try: am = int(p['amount']) except: self.writeNotifyOut(p['protocol'], 'amount_not_number', {'amount': p['amount']}) return c = self.dbconn.cursor(MySQLdb.cursors.DictCursor) sql = '''SELECT * FROM `devices` WHERE `operator_id`=13 LIMIT 1''' c.execute(sql) sd_dev = c.fetchone() # sd_dev = self.c_device.get(self.dbconn, self.cacheconn, ch['device_id']) if not sd_dev: self.writeNotifyOut(p['protocol'], 'topup_fail', {}) print 'INVALID DEVICE_ID' return if am < 10000 or am > 100000000: self.writeNotifyOut(p['protocol'], 'amount_not_number', {'amount': p['amount']}) msisdn = sanitizeMSISDN(p['msisdn']) if 'order' not in p: p['order'] = 1 prod_id = '3S*{0}'.format(thousandSeparator(am)) if self.checkTrx(p['protocol'], ag['agent_id'], prod_id, p['order'], msisdn): return cur_bal = self.dm.getBalance(ag['agent_id']) if cur_bal < am: self.writeNotifyOut(p['protocol'], 'agent_not_enough_balance', {'product_id': am, 'dest': msisdn, 'balance': cur_bal,}) return new_deposit = self.dm.debit(ag['agent_id'], am, 'transfer ke {0}'.format(msisdn)) # new_deposit = 999 sql = '''INSERT INTO `transaction` (`method`,`agent_id`,`agent_name`, `reg_protocol`,`deposit`,`base_price`,`sell_price`,`profit`, `product_id`,`operator_product_id`,`hlr_id`,`operator_id`, `msisdn_destination`,`order`,`transaction_datetime`,`device_id`, `status`) VALUES (%(method)s,%(agent_id)s,%(agent_name)s, %(reg_protocol)s,%(deposit)s,%(base_price)s,%(sell_price)s,%(profit)s, %(product_id)s,%(operator_product_id)s,%(hlr_id)s,%(operator_id)s, %(msisdn_destination)s,%(order)s,%(transaction_datetime)s,%(device_id)s, %(status)s)''' c.execute(sql, { 'method': 'CH', 'agent_id': ag['agent_id'], 'agent_name': ag['agent_name'], 'reg_protocol': p['protocol'], 'deposit': new_deposit, 'base_price': am, 'sell_price': am, 'profit': 0, 'product_id': prod_id, 'operator_product_id': prod_id, 'hlr_id': 0, 'operator_id': 13, 'msisdn_destination': msisdn, 'order': p['order'], 'transaction_datetime': datetime.now(), 'device_id': sd_dev['device_id'], 'status': const.TR_INPROGRESS, }) template = '{device_id},0,sms://089611223344,trf.{msisdn}.{amount}.{pin}' msg = template.format(msisdn=msisdn, amount=p['amount'], pin=sd_dev['pin'], device_id=sd_dev['device_id']) leaf_server = '{0}@{2}/{1}'.format(config.LEAFSERVER, sd_dev['server_id'], config.MSG_SERVER) self.sendMessage(leaf_server, 'JBDV', msg, commit=False) print msg
def _sd_transfer(self, p, ag, prod='t'): try: am = int(p['amount']) except: self.writeNotifyOut(p['protocol'], 'amount_not_number', {'amount': p['amount']}) return min_am = 1 if prod == 't': min_am = 10000 if am < min_am or am > 100000000: self.writeNotifyOut(p['protocol'], 'amount_not_number', {'amount': p['amount']}) return msisdn = sanitizeMSISDN(p['msisdn']) if 'order' not in p: p['order'] = 1 sql = '''SELECT `rs`.`msisdn`,`rs`.`status`,`sd`.`device_id` FROM `rs` INNER JOIN `sd` ON `sd`.`sd_id` = `rs`.`sd_id` WHERE `rs`.`msisdn`=%s''' c = self.dbconn.cursor(MySQLdb.cursors.DictCursor) c.execute(sql, (msisdn,)) ch = c.fetchone() if not ch: self.writeNotifyOut(p['protocol'], 'rs_not_registered', {'msisdn': msisdn}) return if int(ch['status']) == 9: self.writeNotifyOut(p['protocol'], 'rs_not_registered', {'msisdn': msisdn}) return if int(ch['status']) == 0: self.writeNotifyOut(p['protocol'], 'rs_suspended', {'msisdn': msisdn}) return sd_dev = self.c_device.get(self.dbconn, self.cacheconn, ch['device_id']) if not sd_dev: self.writeNotifyOut(p['protocol'], 'topup_fail', {}) print 'INVALID DEVICE_ID' return if prod == 't': prod_id = 'S#{0}'.format(thousandSeparator(am)) else: prod_id = '{0}#{1}'.format(prod.upper(), am) am = self.price[prod] * am if self.checkTrx(p['protocol'], ag['agent_id'], prod_id, p['order'], msisdn): return cur_bal = self.dm.getBalance(ag['agent_id']) if cur_bal < am: self.writeNotifyOut(p['protocol'], 'agent_not_enough_balance', {'product_id': am, 'dest': msisdn, 'balance': cur_bal,}) return new_deposit = self.dm.debit(ag['agent_id'], am, 'transfer ke {0}'.format(msisdn)) sql = '''INSERT INTO `transaction` (`method`,`agent_id`,`agent_name`, `reg_protocol`,`deposit`,`base_price`,`sell_price`,`profit`, `product_id`,`operator_product_id`,`hlr_id`,`operator_id`, `msisdn_destination`,`order`,`transaction_datetime`,`device_id`, `status`) VALUES (%(method)s,%(agent_id)s,%(agent_name)s, %(reg_protocol)s,%(deposit)s,%(base_price)s,%(sell_price)s,%(profit)s, %(product_id)s,%(operator_product_id)s,%(hlr_id)s,%(operator_id)s, %(msisdn_destination)s,%(order)s,%(transaction_datetime)s,%(device_id)s, %(status)s)''' c.execute(sql, { 'method': 'CH', 'agent_id': ag['agent_id'], 'agent_name': ag['agent_name'], 'reg_protocol': p['protocol'], 'deposit': new_deposit, 'base_price': am, 'sell_price': am, 'profit': 0, 'product_id': prod_id, 'operator_product_id': prod_id, 'hlr_id': 0, 'operator_id': 4, 'msisdn_destination': msisdn, 'order': p['order'], 'transaction_datetime': datetime.now(), 'device_id': sd_dev['device_id'], 'status': const.TR_INPROGRESS, }) template = self.tran_parse[prod] msg = template.format(msisdn=msisdn, amount=p['amount'], pin=sd_dev['pin'], device_id=sd_dev['device_id']) leaf_server = '{0}@{2}/{1}'.format(config.LEAFSERVER, sd_dev['server_id'], config.MSG_SERVER) self.sendMessage(leaf_server, 'JBDV', msg, commit=False)
def th_reset_pin(self, msisdn, pin): msisdn = sanitizeMSISDN(msisdn) msg = '{device_id},0,ussd://*897*9*{msisdn}*{pin}#' bc = AgentNotifier() self.thUSSD(bc, msg, msisdn) return json.dumps({'success': 1, 'reason': ''})
def topup(self, bc, agent, reqid, msisdn, product): hlrmap = HLRMap(bc.dbconn, bc.cacheconn) msisdn = sanitizeMSISDN(msisdn) db = bc.dbconn c = db.cursor(MySQLdb.cursors.DictCursor) sql = '''INSERT INTO `transaction` ( `notify_in_id_request`,`transaction_datetime`, `agent_id`,`product_id`,`reg_protocol`,`msisdn_destination`, `order`,`status`,`agent_name`,`operator_id`,`method`,`type`) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)''' protocol = 'rest://{ip}'.format(ip=cherrypy.request.remote.ip) ts = datetime.now() param = (0, ts, agent['agent_id'], product['product_id'], protocol, msisdn, 1, const.TR_WAIT, agent['agent_name'], product['operator_id'], firstMethod(hlrmap, product, msisdn), product['type']) c.execute(sql, param) db.commit() c.execute('SELECT LAST_INSERT_ID() AS `lid`') lid = c.fetchone()['lid'] sql = '''INSERT INTO `h2h_server` (`userid`,`reqid`,`msisdn`,`product`, `req_time`,`tran_id`) VALUES (%s,%s,%s,%s,%s,%s)''' param = (agent['agent_id'], reqid, msisdn, product['product_id'], ts, lid) c.execute(sql, param) db.commit() c.execute('SELECT LAST_INSERT_ID() AS `h2h_id`') h2h_id = c.fetchone()['h2h_id'] tr = self.waitStatus(bc, lid, 60) if not tr: tr = { 'status': const.TR_INPROGRESS, 'product_id': product['product_id'], 'msisdn_destination': msisdn, } elif int(lid) != int(tr['transaction_id']): c.execute('SELECT LAST_INSERT_ID() AS `h2h_id`') h2h_id = c.fetchone()['h2h_id'] sql = '''UPDATE `h2h_server` SET `tran_id`=%s WHERE `id`=%s''' c.execute(sql, (tr['transaction_id'], h2h_id)) db.commit() st = self.translateStatus(tr['status']) msg_normal = ('transaction_fail', 'transaction_successful') msg_unit = ('transaction_fail_unit', 'transaction_successful_unit') if int(product['type']) == 3: mymsg = msg_unit else: mymsg = msg_normal if st in (0, 3, 4, 5): # tr['deposit'] = int(tr['deposit']) + int(tr['sell_price']) msg = bc.buildMessage(mymsg[0], bc.buildSubTopupFail(tr, False)) elif st == 1: msg = bc.buildMessage(mymsg[1], bc.buildSubTopupSucess(tr)) elif st == 2: msg = bc.buildMessage('transaction_in_progress', { 'product_id': tr['product_id'], 'dest': tr['msisdn_destination'], }) return { 'reqid': reqid, 'status': st, 'message': msg, 'timestamp': ts.strftime('%y%m%d%H%M%S'), }