def new_agent(self, agent_name, agent_type, agent_price, agent_address, agent_protocol, agent_pin, agent_upline, pin): '''Add new agent''' bc = AgentNotifier() am = ManageAgentPrice(bc.dbconn, bc.cacheconn) new_id = am.newAgent(agent_name, agent_address, agent_type, agent_pin, agent_upline, config.AGENT_DEFAULT_MARKUP, agent_price, agent_protocol) if not new_id: return json.dumps({ 'success': 0, 'agent_id': '', }) bc.dbconn.commit() if am.registerProtocols(new_id, agent_protocol): bc.dbconn.commit() bc.writeNotifyOut(agent_protocol, 'add_agent_success', { 'agent_id': new_id, 'name': agent_name, 'pin': agent_pin, }) bc.dbconn.commit() return json.dumps({ 'success': 1, 'agent_id': new_id, })
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 depmut_execute(self, agent_id, type, amount, comment, pin): result = { 'success': 0, 'balance': 0, } try: int(amount) except: result['message'] = 'AMOUNT NOT VALID' return json.dumps(result) # agent must exist bc = AgentNotifier() c_agent = DBCache(const.AGENT_PREFIX, config.DEFAULT_EXPIRE, const.AGENT_SQL) c_agent.setConn(bc.dbconn, bc.cacheconn) #print c_agent.sGet(agentid) ag = c_agent.sGet(agent_id) if not ag: result['message'] = 'AGENT NOT REGISTERED' return json.dumps(result) dm = DepositMutation(20, bc.dbconn, bc.cacheconn) prev_bal = dm.getBalance(agent_id) if type.upper() == 'C': balance = dm.credit(agent_id, int(amount), comment) bc.writeNotifyOut(ag['default_protocol'], 'deposit_credit', { 'mutation': thousandSeparator(amount), 'balance_before': thousandSeparator(prev_bal), 'balance': thousandSeparator(balance), }) elif type.upper() == 'D': balance = dm.debit(agent_id, int(amount), comment) bc.writeNotifyOut(ag['default_protocol'], 'deposit_debit', { 'mutation': thousandSeparator(amount), 'balance_before': thousandSeparator(prev_bal), 'balance': thousandSeparator(balance), }) else: result['message'] = 'TYPE NOT RECOGNIZED' return json.dumps(result) if balance == NOT_ENOUGH_BALANCE: result['message'] = 'NOT ENOUGH BALANCE' return json.dumps(result) elif balance == LOCK_FAILED: result['message'] = 'SERVER BUSY' return json.dumps(result) bc.dbconn.commit() msg = '{0}-{1} Deposit: Rp. {2} Before: Rp. {3} After: Rp. {4} ({5}) ID:{6}'.\ format(ag['agent_id'], ag['agent_name'], thousandSeparator(amount), thousandSeparator(prev_bal), thousandSeparator(balance), comment, dm.last_id) for prot in ('ym://b_martian','ym://inileonard', 'ym://sridwan981'): bc.writeNotifyOut(prot, 'general_message', {'message': msg}) bc.dbconn.commit() result.update({ 'success': 1, 'message': 'SUCCESS', 'balance': balance, 'mutation_id': dm.last_id, }) return json.dumps(result)
def sendSMS(self, msisdn, msg=''): bc = AgentNotifier() bc.writeNotifyOut('sms://{0}'.format(msisdn), 'transaction_successful', {}) return json.dumps({'success': 1, 'reason': 'SUCCESS'})