def h2o_unlimited(self):
     if settings.TEST_MODE:
         self.transaction.add_transaction_step(self.step, self.carrier, SUCCESS,
                                               'Requesting recharge from h20 in TEST mode')
         return False
     try:
         self.transaction.add_transaction_step(self.step, self.carrier, SUCCESS, 'Logging into H2O website')
         auth_result = login_h2o(self.carrieradmin)
         if auth_result['valid']:
             s = auth_result['session']
         else:
             raise Exception(auth_result['error'])
         r = s.get('https://www.h2odealer.com/mainCtrl.php?page=do_recharge&carrier_type=GSM&min=%s&pin=%s' % (
         self.transaction.autorefill.phone_number, self.transaction.pin))
         logger.debug('H2O page %s' % r.text)
         message = 'Undefined result. Please, check result at Dealer site'
         page = r.text
         status = int(page.split('var ret_code = \'')[1].split('\';')[0])
         if status >= 0:
             message = 'Successful refill. Expiration %s' % page.split('var exp = \'')[1].split('\';')[0]
         elif status < 0:
             raise Exception(page.split('var err_note = \'')[1].split('\';')[0])
         self.transaction.add_transaction_step(self.step, self.carrier, SUCCESS, u'%s' % message)
         result = mdn_status(self.transaction.autorefill.phone_number, s)
         if result['status'] >= 0:
             details = '%s|%s|%s|%s|%s|%s|%s' % (
             result['plan_name'], result['expiration'], result['available'], result['available_balance'],
             result['data_balance'], result['card'], result['card_balance'])
         else:
             details = result['message']
         self.transaction.add_transaction_step(self.step, self.carrier, SUCCESS, details)
     except Exception, e:
         logger.error("Exception: %s. Trace: %s." % (e, traceback.format_exc(limit=10)))
         raise Exception(u'%s' % e)
 def envie_mobile(self):
     self.transaction.add_transaction_step(self.step, self.carrier, SUCCESS, 'Logging into ENVIE MOBILE website')
     auth_result = login_h2o(self.carrieradmin)
     if auth_result['valid']:
         s = auth_result['session']
     else:
         raise Exception(auth_result['error'])
     r = s.get('https://www.h2odealer.com/mainCtrl.php?page=do_recharge_env&carrier_type=CDMA&min=%s&pin=%s' % (
     self.transaction.autorefill.phone_number, self.transaction.pin))
     logger.debug('ENVIE page %s' % r.text)
     message = 'Undefined result. Please, check result at Dealer site'
     page = r.text
     status = int(page.split('var ret_code = \'')[1].split('\';')[0])
     if status >= 0:
         message = 'Successful refill. Expiration %s' % page.split('var exp = \'')[1].split('\';')[0]
     elif status < 0:
         raise Exception(page.split('var err_note = \'')[1].split('\';')[0])
     self.transaction.add_transaction_step(self.step, self.carrier, SUCCESS, u'%s' % message)
def get_mdn_number(phone_number, company):
    from models import CarrierAdmin
    answer = {
        'valid': True,
        'valid_for_schedule': False,
        'carrier': '',
        'error': '',
        'mdn_status': '',
        'plan': '',
        'schedule': '',
        'renewal_date': ''
    }
    log_title = '[Verify carrier for %s]\n' % phone_number
    log_message = ''
    #PagePlus
    carrier_admin_page_plus = CarrierAdmin.objects.filter(
        company=company,
        carrier__name__icontains='PAGE PLUS CELLULAR').get()
    auth_pageplus = dealer_page_plus.login_pageplus(carrier_admin_page_plus.username, carrier_admin_page_plus.password)
    if auth_pageplus['valid']:
        result_pageplus = dealer_page_plus.verify_pageplus(phone_number, auth_pageplus['browser'])
        log_message += "[PagePlus: %s]" % create_log_message_for_verify_carrier(result_pageplus)
        if result_pageplus['valid']:
            answer = result_pageplus
            Log.objects.create(company=company, note="%s%s" % (log_title, log_message.replace('\n', ' ')))
            return answer
        else:
            answer['valid'] = False
            answer['error'] += '[PagePlus:%s]\n' % result_pageplus['error']
    else:
        answer['valid'] = False
        answer['error'] += "[PagePlus:%s]\n" % auth_pageplus['error']
        log_message += "\n[RedPocket: %s]" % auth_pageplus['error']
    #RedPocket
    carrier_admin_red_pocket = CarrierAdmin.objects.filter(company=company, carrier__name__icontains='RED POCKET').get()
    auth_redpocket = dealer_red_pocket.log_in_red_pocket(carrier_admin_red_pocket)
    if auth_redpocket['valid']:
        result_redpocket = dealer_red_pocket.verify_carrier(phone_number, auth_redpocket['session'])
        log_message += "\n[RedPocket: %s]" % create_log_message_for_verify_carrier(result_redpocket)
        if result_redpocket['valid']:
            answer = result_redpocket
            Log.objects.create(company=company, note="%s%s" % (log_title, log_message.replace('\n', ' ')))
            return answer
        else:
            answer['valid'] = False
            answer['error'] += '[RedPocket:%s]\n' % result_redpocket['error']
    else:
        answer['valid'] = False
        answer['error'] += "[RedPocket:%s]\n" % auth_redpocket['error']
        log_message += "\n[RedPocket: %s]" % auth_redpocket['error']
    #H2O
    carrier_admin_h2o = CarrierAdmin.objects.filter(company=company, carrier__name__icontains='H2O UNLIMITED').get()
    auth_h2o = dealer_h2o.login_h2o(carrier_admin_h2o)
    if auth_h2o['valid']:
        result_h2o = dealer_h2o.verify_carrier(phone_number, auth_h2o['session'])
        log_message += "\n[H2O: %s]" % create_log_message_for_verify_carrier(result_h2o)
        if result_h2o['valid']:
            answer = result_h2o
            Log.objects.create(company=company, note="%s%s" % (log_title, log_message.replace('\n', ' ')))
            return answer
        else:
            answer['valid'] = False
            answer['error'] += '[H2O:%s]\n' % result_h2o['error']
    else:
        answer['valid'] = False
        answer['error'] += "[H2O:%s]\n" % auth_h2o['error']
        log_message += "\n[H2O: %s]" % auth_h2o['error']
    #AirVoice
    result_airvoice = dealer_airvoice.verify_carrier(phone_number)
    log_message += "\n[Airvoice: %s]" % create_log_message_for_verify_carrier(result_airvoice)
    if result_airvoice['valid']:
        answer = result_airvoice
        Log.objects.create(company=company, note="%s%s" % (log_title, log_message.replace('\n', ' ')))
        return answer
    else:
        answer['valid'] = False
        answer['error'] += "[AirVoice:%s]\n" % result_airvoice['error']
    #ApprovedLink
    result_approvedlink = dealer_pvc_approvedlink.verify_carrier(phone_number)
    log_message += "\n[ApprovedLink: %s]" % create_log_message_for_verify_carrier(result_approvedlink)
    if result_approvedlink['valid']:
        answer = result_approvedlink
    else:
        answer['valid'] = False
        answer['error'] += "[ApprovedLink:%s]\n" % result_approvedlink['error']
    Log.objects.create(company=company, note="%s%s" % (log_title, log_message.replace('\n', ' ')))
    return answer