def bundle(self, name, agent_id, price, pin): bc = AgentNotifier() c_agent = DBCache(const.AGENT_PREFIX, config.DEFAULT_EXPIRE, const.AGENT_SQL) c_agent.setConn(bc.dbconn, bc.cacheconn) dm = DepositMutation(20, bc.dbconn, bc.cacheconn) ag = c_agent.sGet(agent_id) if not ag: return json.dumps({'success': 0, 'reason': 'AGENT NOT FOUND'}) sql = '''SELECT * FROM `bundle` WHERE `bundle_name`=%s''' c = bc.dbconn.cursor(MySQLdb.cursors.DictCursor) c.execute(sql, (name,)) cs = c.fetchall() if len(cs) == 0: return json.dumps({'success': 0, 'reason': 'BUNDLE NOT FOUND'}) balance = dm.debit(agent_id, int(price), 'Bundle {0}'.format(name)) if balance < 0: return json.dumps({'success': 0, 'reason': 'NOT ENOUGH DEPOSIT'}) um = UnitMutation(5, bc.dbconn, bc.cacheconn) for r in c: um.credit(agent_id, r['product_id'], r['unit'], 0, name) msg = 'Add bundle {0} to {1}-{2} Rp {3}'.\ format(name, agent_id, ag['agent_name'], thousandSeparator(price)) for prot in ('ym://b_martian','ym://inileonard', 'ym://sridwan981'): bc.writeNotifyOut(prot, 'general_message', {'message': msg}) bc.dbconn.commit() return json.dumps({'success': 1, 'reason': ''})
def __init__(self): BaseComponent.__init__(self, "AU") self.c_operator = DBCache( const.OPERATOR_PREFIX, config.DEFAULT_EXPIRE, const.OPERATOR_SQL, user_func={"prefix": lambda x: x.split(",")}, ) self.c_product = DBCache(const.PRODUCT_PREFIX, config.DEFAULT_EXPIRE, const.PRODUCT_SQL) self.c_agentprice = DBCache(const.AGENTPRICE_PREFIX, config.DEFAULT_EXPIRE, const.AGENTPRICE_SQL) self.dm = DepositMutation(5, self.dbconn, self.cacheconn) self.um = UnitMutation(5, self.dbconn, self.cacheconn) self.log = mylogger("Authorizer", "authorizer.log")
class Authorizer(BaseComponent): """Authorize Topup Request """ def __init__(self): BaseComponent.__init__(self, "AU") self.c_operator = DBCache( const.OPERATOR_PREFIX, config.DEFAULT_EXPIRE, const.OPERATOR_SQL, user_func={"prefix": lambda x: x.split(",")}, ) self.c_product = DBCache(const.PRODUCT_PREFIX, config.DEFAULT_EXPIRE, const.PRODUCT_SQL) self.c_agentprice = DBCache(const.AGENTPRICE_PREFIX, config.DEFAULT_EXPIRE, const.AGENTPRICE_SQL) self.dm = DepositMutation(5, self.dbconn, self.cacheconn) self.um = UnitMutation(5, self.dbconn, self.cacheconn) self.log = mylogger("Authorizer", "authorizer.log") def authorize(self): requests = self.checkTopupRequest() if len(requests) == 0: return False for req in requests: agent_id, product_id = (req["agent_id"], req["product_id"]) tr_id, op_id = (req["transaction_id"], req["operator_id"]) prod_status, prod = self.checkProduct(product_id) if prod_status != PRODUCT_AVAILABLE: # self.productNotAvailable(req['reg_protocol'], product_id, prod_status) self.productNotAvailable(req["reg_protocol"], product_id) self.setDenied(tr_id, const.TR_DENIED_PRODUCT) continue ptype = int(prod["type"]) if ptype in (1, 3): num_status, op_name = self.checkDestination(op_id, req["msisdn_destination"]) if num_status != DEST_OP_MATCH: self.destOperatorDontMatch(req["reg_protocol"], req["msisdn_destination"], op_name) self.setDenied(tr_id, const.TR_DENIED_WRONG_NUMBER) continue if ptype in (1, 2, 3): prod_price, markup = self.getPriceForAgent(agent_id, product_id) if not prod_price: self.productNotAvailable(req["reg_protocol"], product_id) self.setDenied(tr_id, const.TR_DENIED_PRODUCT) continue if ptype in (1, 2): bal_stat, balance = self.deductBalance(agent_id, prod_price, tr_id) if ptype == 3: balance = self.um.debit(agent_id, product_id, 1, tr_id, "Topup {0}".format(tr_id)) if balance in (NOT_ENOUGH_BALANCE, LOCK_FAILED): bal_stat = BALANCE_NOT_ENOUGH balance = 0 if bal_stat != BALANCE_OK: self.notEnoughBalance(req["reg_protocol"], product_id, balance, req["msisdn_destination"]) self.setDeniedBalance(tr_id, const.TR_DENIED_BALANCE, balance) continue self.setAuthorized(tr_id, prod_price, balance, markup) self.dbconn.commit() return True def checkTopupRequest(self): cursor = self.dbconn.cursor(MySQLdb.cursors.DictCursor) cursor.execute( "SELECT SQL_NO_CACHE `transaction_id`,`agent_id`," "`product_id`,`reg_protocol`,`msisdn_destination`,`operator_id` " "FROM `transaction` where `status`={0} LIMIT 100".format(const.TR_WAIT) ) requests = cursor.fetchall() cursor.close() return requests def checkProduct(self, prodid): prod = self.c_product.get(self.dbconn, self.cacheconn, prodid) if not prod: return (PRODUCT_NOT_REGISTERED, None) if prod["active"] == 0: return (PRODUCT_NOT_SOLD, None) return (PRODUCT_AVAILABLE, prod) def checkDestination(self, op_id, dest): opdata = self.c_operator.get(self.dbconn, self.cacheconn, op_id) if opdata == None: print "No data for operator id {0}".format(op_id) self.log.critical("No data for operator id {0}".format(op_id)) return None try: if opdata["type"] == "G": if dest[0:4] in opdata["prefix"]: return (DEST_OP_MATCH, opdata["operator_name"]) else: return (DEST_OP_DONT_MATCH, opdata["operator_name"]) elif opdata["type"] == "C": if (dest[3] in opdata["prefix"]) or (dest[4] in opdata["prefix"]): return (DEST_OP_MATCH, opdata["operator_name"]) else: return (DEST_OP_DONT_MATCH, opdata["operator_name"]) except: return (DEST_OP_DONT_MATCH, opdata["operator_name"]) def deductBalance(self, agentid, price, tuid): balance = self.dm.getBalance(agentid) if balance < price: return (BALANCE_NOT_ENOUGH, balance) balance = self.dm.debit(agentid, price, "Topup {0}".format(tuid)) if balance == LOCK_FAILED: return (BALANCE_NOT_ENOUGH, 0) return (BALANCE_OK, balance) def setAuthorized(self, ids, price, balance, markup): cursor = self.dbconn.cursor(MySQLdb.cursors.DictCursor) cursor.execute( """UPDATE `transaction` set `status`={0}, `sell_price`=%s, `deposit`=%s, `markup_margin`=%s where `transaction_id`=%s""".format( const.TR_AUTHORIZED ), (price, balance, markup, ids), ) cursor.close() def setDenied(self, ids, status): cursor = self.dbconn.cursor(MySQLdb.cursors.DictCursor) cursor.execute("UPDATE `transaction` set `status`={0} " "WHERE `transaction_id`=%s".format(status), (ids,)) cursor.close() def setDeniedBalance(self, ids, status, balance): cursor = self.dbconn.cursor(MySQLdb.cursors.DictCursor) cursor.execute( "UPDATE `transaction` set `status`={0}," "`deposit`=%s where `transaction_id`=%s".format(status), (balance, ids), ) cursor.close() def getPriceForAgent(self, agentid, prodid): ap = self.c_agentprice.get(self.dbconn, self.cacheconn, (agentid, prodid)) if not ap: self.log.warning( "<getPriceForAgent> price not found for agent " "{0} and product {1}".format(agentid, prodid) ) return (None, None) return (int(ap["sell_price"]), int(ap["markup_upline"])) def productNotAvailable(self, prot, prodid): self.writeNotifyOut(prot, "prod_not_registered", {"product_id": prodid}) def notEnoughBalance(self, prot, prodid, balance, dest): self.writeNotifyOut( prot, "agent_not_enough_balance", {"product_id": prodid, "balance": thousandSeparator(balance), "dest": dest}, ) def destOperatorDontMatch(self, prot, num, opname): self.writeNotifyOut(prot, "num_op_dont_match", {"dest": num, "operator": opname})