def queue_import_customers_from_usaepay(company_id, user_id): if not settings.TEST_MODE: from ppars.apps.price.models import SellingPriceLevel user = User.objects.get(id=user_id) company = CompanyProfile.objects.get(id=company_id) level_price = SellingPriceLevel.objects.get(level='1') message = '' added = 0 exists = 0 not_added = 0 found = 0 try: if company.usaepay_username and company.usaepay_password: payload = { 'username': company.usaepay_username, 'password': company.usaepay_password, 'stamp': '4159eb01db85b605b2616f384f903f84a477b1de' } s = requests.Session() r = s.post('https://secure.usaepay.com/login', data=payload) if r.url not in ['https://secure.usaepay.com/console/']: raise Exception( "Failed to login to USAePay, please check credentials") r = s.get( 'https://secure.usaepay.com/console/billing?limitstart=0&limit=2000&filter=&sortkey=&sortdir=&level=&type=' ) # r = s.post('https://sandbox.usaepay.com/login', data=payload) # if r.url not in ['https://sandbox.usaepay.com/console/']: # raise Exception("Failed to login to USAePay, please check credentials") # r = s.get('https://sandbox.usaepay.com/console/billing?limitstart=0&limit=2000&filter=&sortkey=&sortdir=&level=&type=') soup2 = BeautifulSoup(r.text) forms = soup2.findAll('form') usaepay_customers = [] for form in forms: if form.get('name') == 'custs': inputs = soup2.findAll('input') for obj in inputs: if obj.get('name') == 'sel[]': usaepay_customers.append(obj.get('value')) # for a in form.findAll('a'): # if 'javascript:editCustomer' in a.get('href'): # usaepay_customers.append(int(a.get('href').replace('javascript:editCustomer(\'', '').replace('\')', ''))) found = len(usaepay_customers) system_customers = Customer.objects.filter(company=company) for customer in system_customers: if customer.usaepay_customer_id: if customer.usaepay_customer_id in usaepay_customers: usaepay_customers.remove( customer.usaepay_customer_id) exists = found - len(usaepay_customers) if exists + Customer.objects.filter(company=company).count( ) > company.customer_limit and company.customer_limit != 0: raise Exception( 'Customer limit has been reached. Please contact administrator.' ) if usaepay_customers: for usaepay_customer in usaepay_customers: if company.usaepay_source_key and company.usaepay_pin: try: token = company.usaepay_authorization() client = SoapClient(wsdl=settings.USAEPAY_WSDL, trace=False, ns=False) response = client.getCustomer( CustNum=usaepay_customer, Token=token) result = response['getCustomerReturn'] if result: first_name = '' last_name = '' enabled = False city = '' zip = '' state = '' address = '' primary_email = '' creditcard = '' usaepay_custid = '' company_name = '' pns = [] logger.debug('Notes "%s"' % result['Notes']) if 'Notes' in result and result['Notes']: pns = extract_phone_numbers_from_notes( result['Notes']) if 'Enabled' in result and result[ 'Enabled']: enabled = result['Enabled'] if 'BillingAddress' in result: if 'City' in result[ 'BillingAddress'] and result[ 'BillingAddress']['City']: city = result['BillingAddress'][ 'City'].strip() if 'Zip' in result[ 'BillingAddress'] and result[ 'BillingAddress']['Zip']: zip = result['BillingAddress'][ 'Zip'].strip() if 'FirstName' in result[ 'BillingAddress'] and result[ 'BillingAddress'][ 'FirstName']: first_name = result[ 'BillingAddress'][ 'FirstName'].strip() if 'LastName' in result[ 'BillingAddress'] and result[ 'BillingAddress'][ 'LastName']: last_name = result[ 'BillingAddress'][ 'LastName'].strip() if 'Company' in result[ 'BillingAddress'] and result[ 'BillingAddress'][ 'Company']: company_name = result[ 'BillingAddress'][ 'Company'].strip() logger.debug( 'Phone "%s"' % result['BillingAddress']['Phone']) if 'Phone' in result[ 'BillingAddress'] and result[ 'BillingAddress']['Phone']: for n in extract_phone_numbers_from_notes( result['BillingAddress'] ['Phone'] ): #.strip().replace('-', '').replace(' ', '') if n not in pns: pns.append(n) if 'State' in result[ 'BillingAddress'] and result[ 'BillingAddress']['State']: state = result['BillingAddress'][ 'State'].strip() if 'Street' in result[ 'BillingAddress'] and result[ 'BillingAddress']['Street']: address = result['BillingAddress'][ 'Street'].strip() if 'Email' in result[ 'BillingAddress'] and result[ 'BillingAddress']['Email']: primary_email = result[ 'BillingAddress'][ 'Email'].strip() if 'PaymentMethods' in result: if len(result['PaymentMethods']) > 0: if 'item' in result[ 'PaymentMethods'][0]: item = result[ 'PaymentMethods'][0][ 'item'] creditcard = str( item.CardNumber) if 'CustomerID' in result and result[ 'CustomerID']: p = result['CustomerID'] for token in [ ', ', '. ', ' ', ',', '.' ]: p = p.replace(token, '|') p = p.replace('|', ', ').strip() if ',' != p[-1:]: p = '%s,' % p usaepay_custid = p new_customer = Customer.objects.create( usaepay_customer_id=usaepay_customer, usaepay_custid=usaepay_custid, user=user, company=company, charge_getaway='U', charge_type='CC', save_to_usaepay=True, creditcard=creditcard, first_name=first_name or company_name or str(usaepay_customer), last_name=last_name or str(usaepay_customer), enabled=enabled, city=city, zip=zip, selling_price_level=level_price, state=state, address=address, primary_email=primary_email, ) for number in pns: PhoneNumber.create( company=company, customer=new_customer, number=number) added = added + 1 message = '<a href="%s">%s</a><br/>%s' % ( reverse('customer_update', args=[new_customer.id]), new_customer, message) if message: message = 'This is customer from USAePay. Please check them out. %s' % ( message) # messages.add_message(request, messages.SUCCESS, '%s' % message) except Exception, e: not_added = not_added + 1 message = 'Customer with ID "%s" did`t added. Error:%s<br/>%s' % ( usaepay_customer, e, message) else: message = 'no USAePay tokens for API requests' # messages.add_message(request, messages.ERROR, '%s' % message) else: message = 'All USAePay users already exist in your system' else: message = 'no USAePay username/password for API requests' # messages.add_message(request, messages.ERROR, '%s' % message) message = ( '%s customers added, %s customers exists, %s not added of %s<br/><br/>%s' % (added, exists, not_added, found, message))