Beispiel #1
0
    def acctounting(self):
        if not self.account:
            return logger.error(
                "[Acct] Received an accounting update request but user[%s] not exists"% self.request.account_number)  

        ticket = Storage(**self.request)
        _datetime = datetime.datetime.now() 
        online = self.get_online(ticket.nas_addr,ticket.acct_session_id)    
        if not online:
            session_time = ticket.acct_session_time 
            stop_time = _datetime.strftime( "%Y-%m-%d %H:%M:%S")
            start_time = (_datetime - datetime.timedelta(seconds=int(session_time))).strftime( "%Y-%m-%d %H:%M:%S")
            ticket.acct_start_time = start_time
            ticket.acct_stop_time = stop_time
            ticket.start_source= STATUS_TYPE_STOP
            ticket.stop_source = STATUS_TYPE_STOP
            self.add_ticket(ticket)
        else:
            self.del_online(ticket.nas_addr,ticket.acct_session_id)
            ticket.acct_start_time = online.acct_start_time
            ticket.acct_stop_time= _datetime.strftime( "%Y-%m-%d %H:%M:%S")
            ticket.start_source = online.start_source
            ticket.stop_source = STATUS_TYPE_STOP
            self.add_ticket(ticket)

        self.billing(online)
        logger.info('%s Accounting stop request, remove online'% self.account.account_number)
    def acctounting(self):
        if not self.account:
            return logger.error(
                "[Acct] Received an accounting update request but user[%s] not exists"
                % self.request.account_number)

        ticket = Storage(**self.request)
        online = self.get_online(ticket.nas_addr, ticket.acct_session_id)
        if not online:
            sessiontime = ticket.acct_session_time
            updatetime = datetime.datetime.now()
            _starttime = updatetime - datetime.timedelta(seconds=sessiontime)
            online = Storage(
                account_number=self.account.account_number,
                nas_addr=ticket.nas_addr,
                acct_session_id=ticket.acct_session_id,
                acct_start_time=_starttime.strftime("%Y-%m-%d %H:%M:%S"),
                framed_ipaddr=ticket.framed_ipaddr,
                mac_addr=ticket.mac_addr,
                nas_port_id=ticket.nas_port_id,
                billing_times=ticket.acct_session_time,
                input_total=self.get_input_total(),
                output_total=self.get_output_total(),
                start_source=STATUS_TYPE_UPDATE)
            self.add_online(online)

        self.billing(online)
        logger.info('%s Accounting update request, update online' %
                    self.account.account_number)
Beispiel #3
0
    def bill_botimes(self,online, product):
        #买断时长
        logger.info('%s > Buyout long time billing ' % self.account.account_number)
        time_length = self.get_user_time_length()
        sessiontime = self.request.acct_session_time
        billing_times = online.billing_times
        acct_times = sessiontime - billing_times
        user_time_length = time_length - acct_times
        if user_time_length < 0 :
            user_time_length = 0

        self.update_billing(Storage(
            account_number = online.account_number,
            nas_addr = online.nas_addr,
            acct_session_id = online.acct_session_id,
            acct_start_time = online.acct_start_time,
            acct_session_time = self.request.acct_session_time,
            input_total = self.get_input_total(),
            output_total = self.get_output_total(),
            acct_times = acct_times,
            acct_flows = 0,
            acct_fee = 0,
            actual_fee = 0,
            balance = 0,
            time_length = user_time_length,
            flow_length = 0,
            is_deduct = 1,
            create_time = datetime.datetime.now().strftime( "%Y-%m-%d %H:%M:%S")
        ))
    
        if user_time_length == 0 :
            self.disconnect(online)
Beispiel #4
0
 def get_request(self, online):
     session_time = (datetime.datetime.now() - datetime.datetime.strptime(
         online.acct_start_time, "%Y-%m-%d %H:%M:%S")).total_seconds()
     return Storage(account_number=online.account_number,
                    mac_addr=online.mac_addr,
                    nas_addr=online.nas_addr,
                    nas_port=0,
                    service_type='',
                    framed_ipaddr=online.framed_ipaddr,
                    framed_netmask='',
                    nas_class='',
                    session_timeout=0,
                    calling_station_id='',
                    acct_status_type=STATUS_TYPE_STOP,
                    acct_input_octets=0,
                    acct_output_octets=0,
                    acct_session_id=online.acct_session_id,
                    acct_session_time=session_time,
                    acct_input_packets=0,
                    acct_output_packets=0,
                    acct_terminate_cause='',
                    acct_input_gigawords=0,
                    acct_output_gigawords=0,
                    event_timestamp=datetime.datetime.now().strftime(
                        "%Y-%m-%d %H:%M:%S"),
                    nas_port_type='',
                    nas_port_id=online.nas_port_id)
Beispiel #5
0
 def __init__(self, dbengine=None, cache=None, aes=None, request=None):
     self.dbengine = dbengine
     self.cache = cache
     self.aes = aes
     self.request = Storage(request)
     self.account = self.get_account_by_username(
         self.request.account_number)
Beispiel #6
0
 def fetch_result():
     table = models.TrProductAttr.__table__
     with self.dbengine.begin() as conn:
         vals = conn.execute(table.select().where(
             table.c.product_id == product_id).where(
                 table.c.attr_type == 1))
         return vals and [Storage(val.items()) for val in vals] or []
Beispiel #7
0
 def new_ticket(online):
     _datetime = datetime.datetime.now()
     _starttime = datetime.datetime.strptime(online.acct_start_time,"%Y-%m-%d %H:%M:%S")
     session_time = (_datetime - _starttime).seconds
     stop_time = _datetime.strftime( "%Y-%m-%d %H:%M:%S")
     ticket = Storage()
     ticket.account_number = online.account_number,
     ticket.acct_session_id = online.acct_session_id,
     ticket.acct_start_time = online.acct_start_time,
     ticket.nas_addr = online.nas_addr,
     ticket.framed_ipaddr = online.framed_ipaddr,
     ticket.acct_session_time = session_time,
     ticket.acct_stop_time = stop_time,
     return ticket
Beispiel #8
0
def parse_form_request(api_secret, request):
    """
        >>> parse_form_request("123456",{"nonce": 1451122677, "msg": "helllo", "code": 0, "sign": "DB30F4D1112C20DFA736F65458F89C64"})
        <Storage {'nonce': 1451122677, 'msg': 'helllo', 'code': 0, 'sign': 'DB30F4D1112C20DFA736F65458F89C64'}>
    """
    if not check_sign(api_secret, request):
        raise ValueError(u"message sign error")

    return Storage(request)
Beispiel #9
0
def parse_request(api_secret, reqbody, dec_func=False):
    """
        >>> parse_request("123456",'{"nonce": 1451122677, "msg": "helllo", "code": 0, "sign": "DB30F4D1112C20DFA736F65458F89C64"}')
        <Storage {u'nonce': 1451122677, u'msg': u'helllo', u'code': 0, u'sign': u'DB30F4D1112C20DFA736F65458F89C64'}>
    """
    try:
        if type(reqbody) == type(dict):
            return self.parse_form_request(reqbody)

        if callable(dec_func):
            req_msg = json.loads(dec_func(reqbody))
        else:
            req_msg = json.loads(reqbody)
    except Exception as err:
        raise ValueError(u"parse params error")

    if not check_sign(api_secret, req_msg):
        raise ValueError(u"message sign error")

    return Storage(req_msg)
Beispiel #10
0
 def bill_pptimes(self,online,product):
     # 预付费时长
     logger.info('%s > Prepaid long time billing ' % self.account.account_number)
     user_balance = self.get_user_balance()
     sessiontime = decimal.Decimal(self.request.acct_session_time)
     billing_times = decimal.Decimal(online.billing_times)
     acct_times = sessiontime - billing_times
     fee_price = decimal.Decimal(product['fee_price'])
     usedfee = acct_times/decimal.Decimal(3600) * fee_price
     usedfee = actual_fee = int(usedfee.to_integral_value())
     balance = user_balance - usedfee
     
     if balance < 0 :  
         balance = 0
         actual_fee = user_balance
         
     self.update_billing(Storage(
         account_number = online.account_number,
         nas_addr = online.nas_addr,
         acct_session_id = online.acct_session_id,
         acct_start_time = online.acct_start_time,
         acct_session_time = self.request.acct_session_time,
         input_total = self.get_input_total(),
         output_total = self.get_output_total(),
         acct_times = int(acct_times.to_integral_value()),
         acct_flows = 0,
         acct_fee = usedfee,
         actual_fee = actual_fee,
         balance = balance,
         time_length = 0,
         flow_length = 0,
         is_deduct = 1,
         create_time = datetime.datetime.now().strftime( "%Y-%m-%d %H:%M:%S")
     ))
     
     if balance == 0 :
         self.disconnect(online)
Beispiel #11
0
    def acctounting(self):
        if self.is_online(self.request.nas_addr, self.request.acct_session_id):
            return logger.error('online %s is exists' %
                                self.request.acct_session_id)

        if not self.account:
            return logger.error('user %s not exists' %
                                self.request.account_number)

        online = Storage(account_number=self.request.account_number,
                         nas_addr=self.request.nas_addr,
                         acct_session_id=self.request.acct_session_id,
                         acct_start_time=datetime.datetime.now().strftime(
                             "%Y-%m-%d %H:%M:%S"),
                         framed_ipaddr=self.request.framed_ipaddr,
                         mac_addr=self.request.mac_addr,
                         nas_port_id=self.request.nas_port_id,
                         billing_times=0,
                         input_total=0,
                         output_total=0,
                         start_source=STATUS_TYPE_START)
        self.add_online(online)
        logger.info('%s Accounting start request, add new online' %
                    online.account_number)
Beispiel #12
0
        resp = yield requests.post(gateway, data=urlencode(params))
        veryfy_result = yield resp.content()

        defer.returnValue(veryfy_result.lower().strip() == 'true')


if __name__ == '__main__':
    from radiuslib.storage import Storage
    settings = Storage(
        ALIPAY_KEY='234234',
        ALIPAY_INPUT_CHARSET='utf-8',
        # 合作身份者ID,以2088开头的16位纯数字
        ALIPAY_PARTNER='234',
        # 签约支付宝账号或卖家支付宝帐户
        ALIPAY_SELLER_EMAIL='*****@*****.**',
        ALIPAY_SIGN_TYPE='MD5',
        # 付完款后跳转的页面(同步通知) 要用 http://格式的完整路径,不允许加?id=123这类自定义参数
        ALIPAY_RETURN_URL='',
        # 交易过程中服务器异步通知的页面 要用 http://格式的完整路径,不允许加?id=123这类自定义参数
        ALIPAY_NOTIFY_URL='',
        ALIPAY_SHOW_URL='',
        # 访问模式,根据自己的服务器是否支持ssl访问,若支持请选择https;若不支持请选择http
        ALIPAY_TRANSPORT='https')
    alipay = AliPay(settings)
    params = {}
    params['service'] = 'create_direct_pay_by_user'
    params['payment_type'] = '1'
    params['aaaa'] = u"好"
    print alipay.make_request_url(**params)
    print alipay.create_direct_pay_by_user('2323525', u"阿士大夫", u"啥打法是否", 0.01)
Beispiel #13
0
 def fetch_result():
     table = models.TrProduct.__table__
     with self.dbengine.begin() as conn:
         val = conn.execute(
             table.select().where(table.c.id == product_id)).first()
         return val and Storage(val.items()) or None
Beispiel #14
0
 def fetch_result():
     table = models.TrAccount.__table__
     with self.dbengine.begin() as conn:
         val = conn.execute(table.select().where(
             table.c.account_number == username)).first()
         return val and Storage(val.items()) or None