コード例 #1
0
ファイル: admin.py プロジェクト: sridwan/meong
 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})
コード例 #2
0
ファイル: TopupParser.py プロジェクト: sridwan/meong
 def _topup(self, nin_id, agent_id, p):
     def getStatusMsg(status):
         if status < const.TR_EXECUTED:
             return 'DALAM PROSES'
         elif status == const.TR_RETRIED:
             return 'DALAM PROSES'
         elif status >= const.TR_EXECUTED and status < const.TR_FAILED_HLR:
             return 'BERHASIL'
         return 'GAGAL'
     
     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()
     # verify agent
     prot = p['protocol']
     verify_agent = self.agent_manager.verifyAgent(agent_id, p['pin'])
     if verify_agent['code'] != AGST_FOUND:
         raise Error(verify_agent['code'])
     topup_params = p['topup_param'].split(self.rx_separator)
     pcode = topup_params[0].upper()
     # go topup
     product = self.finalCheck(prot, pcode, agent_id, topup_params)
     if not product:
         return
     l = len(topup_params)
     if l == 2:  # single topup
         method = firstMethod(self.hlrmap, product, topup_params[1])
         singleTopup(product, topup_params[1], 1, method)
         return
     elif l == 3:  # repeat topup
         try:
             order = int(topup_params[1])
         except:
             raise Error(WRONG_REQ_FORMAT)
         method = firstMethod(self.hlrmap, product, topup_params[2])
         singleTopup(product, topup_params[2], order, method)
         return
     elif l > 3 and l % 2 == 0:  # multiple topups
         tmp = [(topup_params[x], topup_params[x+1]) for x in range(l) if x%2 == 0]
         print tmp
         self.log.info(tmp)
         for pcode, dnum in tmp:
             pcode = pcode.upper()
             product = self.finalCheck(prot, pcode, agent_id, dnum)
             if not product:
                 return
             method = firstMethod(self.hlrmap, product, dnum)
             if not product:
                 self.productError(prot, pcode, agent_id, dnum)
                 continue
             singleTopup(product, dnum, 1, method)
         return
     else:
         raise Error(WRONG_REQ_FORMAT)
コード例 #3
0
ファイル: H2HServer.py プロジェクト: sridwan/meong
 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'),
            }