def add_customer_profile(self): """Add the customer profile on payment gateway """ if self.id and (not self.customer_profile_id): # check if this user already has a customer profile, # if so, no need to create a new one. cp_id_list = RecurringPayment.objects.filter( user=self.user).exclude(customer_profile_id='').values_list( 'customer_profile_id', flat=True) if cp_id_list: self.customer_profile_id = cp_id_list[0] self.save() else: # create a customer profile on payment gateway cp = CIMCustomerProfile() d = {'email': self.user.email, 'customer_id': str(self.id)} success, response_d = cp.create(**d) print success, response_d if success: self.customer_profile_id = response_d[ 'customer_profile_id'] self.save() else: if response_d["message_code"] == 'E00039': p = re.compile( 'A duplicate record with ID (\d+) already exists.', re.I) match = p.search(response_d['message_text']) if match: self.customer_profile_id = match.group(1) self.save()
def add_customer_profile(self): """Add the customer profile on payment gateway """ if self.id and (not self.customer_profile_id): # check if this user already has a customer profile, # if so, no need to create a new one. cp_id_list = RecurringPayment.objects.filter(user=self.user).exclude( customer_profile_id='' ).values_list('customer_profile_id', flat=True) if cp_id_list: self.customer_profile_id = cp_id_list[0] self.save() else: # create a customer profile on payment gateway cp = CIMCustomerProfile() d = {'email': self.user.email, 'customer_id': str(self.id)} success, response_d = cp.create(**d) print success, response_d if success: self.customer_profile_id = response_d['customer_profile_id'] self.save() else: if response_d["message_code"] == 'E00039': p = re.compile('A duplicate record with ID (\d+) already exists.', re.I) match = p.search(response_d['message_text']) if match: self.customer_profile_id = match.group(1) self.save()
def manage_payment_info( request, recurring_payment_id, template_name="recurring_payments/authnet/cim_manage_payment_iframe2.html" ): """ Add or edit payment info. """ rp = get_object_or_404(RecurringPayment, pk=recurring_payment_id) gateway_error = False # only admin or user self can access this page if not request.user.profile.is_superuser and request.user.id <> rp.user.id: raise Http403 if hasattr(settings, 'AUTHNET_CIM_TEST_MODE') and settings.AUTHNET_CIM_TEST_MODE: test_mode = 'true' form_post_url = "https://test.authorize.net/profile/manage" else: test_mode = 'false' form_post_url = "https://secure.authorize.net/profile/manage" if not rp.customer_profile_id: # customer_profile is not available yet for this customer, create one now cp = CIMCustomerProfile() d = { 'email': rp.user.email, 'description': rp.description, 'customer_id': str(rp.id) } success, response_d = cp.create(**d) if success: rp.customer_profile_id = response_d['customer_profile_id'] rp.save() else: gateway_error = True #payment_profiles = PaymentProfile.objects.filter(recurring_payment=rp, status=1, status_detail='active') # get the token from payment gateway for this customer (customer_profile_id=4356210) token = "" if not gateway_error: hosted_profile_page = CIMHostedProfilePage(rp.customer_profile_id) success, response_d = hosted_profile_page.get() if not success: gateway_error = True else: token = response_d['token'] return render_to_response(template_name, { 'token': token, 'test_mode': test_mode, 'form_post_url': form_post_url, 'rp': rp, 'gateway_error': gateway_error }, context_instance=RequestContext(request))
def manage_payment_info(request, recurring_payment_id, template_name="recurring_payments/authnet/cim_manage_payment_iframe2.html"): """ Add or edit payment info. """ rp = get_object_or_404(RecurringPayment, pk=recurring_payment_id) gateway_error = False # only admin or user self can access this page if not request.user.profile.is_superuser and request.user.id <> rp.user.id: raise Http403 if hasattr(settings, 'AUTHNET_CIM_TEST_MODE') and settings.AUTHNET_CIM_TEST_MODE: test_mode = 'true' form_post_url = "https://test.authorize.net/profile/manage" else: test_mode = 'false' form_post_url = "https://secure.authorize.net/profile/manage" if not rp.customer_profile_id: # customer_profile is not available yet for this customer, create one now cp = CIMCustomerProfile() d = {'email': rp.user.email, 'description': rp.description, 'customer_id': str(rp.id)} success, response_d = cp.create(**d) if success: rp.customer_profile_id = response_d['customer_profile_id'] rp.save() else: gateway_error = True #payment_profiles = PaymentProfile.objects.filter(recurring_payment=rp, status=1, status_detail='active') # get the token from payment gateway for this customer (customer_profile_id=4356210) token = "" if not gateway_error: hosted_profile_page = CIMHostedProfilePage(rp.customer_profile_id) success, response_d = hosted_profile_page.get() if not success: gateway_error = True else: token = response_d['token'] return render_to_response(template_name, {'token': token, 'test_mode': test_mode, 'form_post_url': form_post_url, 'rp': rp, 'gateway_error': gateway_error }, context_instance=RequestContext(request))
def delete_customer_profile(self): """Delete the customer profile on payment gateway """ if self.customer_profile_id: has_other_rps = RecurringPayment.objects.filter( customer_profile_id=self.customer_profile_id ).exclude(id=self.id).exists() if not has_other_rps: cim_customer_profile = CIMCustomerProfile(self.customer_profile_id) cim_customer_profile.delete() # delete payment profile belonging to this recurring payment PaymentProfile.objects.filter(customer_profile_id=self.customer_profile_id).delete() return True return False
def populate_payment_profile(self, *args, **kwargs): """ Check payment gateway for the payment profiles for this customer and store the payment profile info locally Returns a list of valid and a list of invalid customer_payment_profile_ids Specifically, 1) get a list of payment_profiles from gateway 2) validate each one, if not valid, remove from the list 3) if customer_payment_profile_id is not valid, delete it from database - keep only the valid ones on local. """ valid_cim_payment_profile_ids = [] invalid_cim_payment_profile_ids = [] if not self.customer_profile_id: self.add_customer_profile() # return immediately -there is no payment profile yet return [], [] validation_mode = kwargs.get('validation_mode') customer_profile = CIMCustomerProfile(self.customer_profile_id) success, response_d = customer_profile.get() if success: if response_d['profile'].has_key('payment_profiles'): # 1) get a list of payment_profiles from gateway cim_payment_profiles = response_d['profile'][ 'payment_profiles'] if not type(cim_payment_profiles) is list: cim_payment_profiles = list([cim_payment_profiles]) # 2) validate payment_profile, if not valid, remove it from the list if validation_mode: for cim_payment_profile in cim_payment_profiles: customer_payment_profile_id = cim_payment_profile[ 'customer_payment_profile_id'] is_valid = False # validate this payment profile cpp = CIMCustomerPaymentProfile( self.customer_profile_id, customer_payment_profile_id, ) success, response_d = cpp.validate( validation_mode=validation_mode) # convert direct_response string into a dict response_dict = direct_response_dict( response_d['direct_response']) response_code = response_dict.get('response_code', '') response_reason_code = response_dict.get( 'response_reason_code', '') if all([ success, response_code == '1', response_reason_code == '1' ]): is_valid = True if not is_valid: invalid_cim_payment_profile_ids.append( cim_payment_profile[ 'customer_payment_profile_id']) # remove it from the list cim_payment_profiles.remove(cim_payment_profile) else: valid_cim_payment_profile_ids.append( cim_payment_profile[ 'customer_payment_profile_id']) list_cim_payment_profile_ids = [ cim_payment_profile['customer_payment_profile_id'] for cim_payment_profile in cim_payment_profiles ] if not validation_mode: valid_cim_payment_profile_ids.extend( list_cim_payment_profile_ids) # 3) if customer_payment_profile_id is not valid, # delete it from database - keep only the valid ones on local payment_profiles = PaymentProfile.objects.filter( customer_profile_id=self.customer_profile_id) for pp in payment_profiles: if pp.payment_profile_id not in list_cim_payment_profile_ids: pp.delete() for cim_payment_profile in cim_payment_profiles: customer_payment_profile_id = cim_payment_profile[ 'customer_payment_profile_id'] # check if already exists, if not, insert to payment profiles table payment_profiles = PaymentProfile.objects.filter( payment_profile_id=customer_payment_profile_id) if not payment_profiles: payment_profile = PaymentProfile( customer_profile_id=self.customer_profile_id, payment_profile_id=customer_payment_profile_id, creator=self.user, owner=self.user, creator_username=self.user.username, owner_username=self.user.username, ) payment_profile.save() else: payment_profile = payment_profiles[0] if cim_payment_profile['payment'].has_key('credit_card') and \ cim_payment_profile['payment']['credit_card'].has_key('card_number'): card_num = cim_payment_profile['payment'][ 'credit_card']['card_number'][-4:] if payment_profile.card_num <> card_num: payment_profile.card_num = card_num payment_profile.save() return valid_cim_payment_profile_ids, invalid_cim_payment_profile_ids
def populate_payment_profile(self, *args, **kwargs): """ Check payment gateway for the payment profiles for this customer and store the payment profile info locally Returns a list of valid and a list of invalid customer_payment_profile_ids Specifically, 1) get a list of payment_profiles from gateway 2) validate each one, if not valid, remove from the list 3) if customer_payment_profile_id is not valid, delete it from database - keep only the valid ones on local. """ valid_cim_payment_profile_ids = [] invalid_cim_payment_profile_ids = [] if not self.customer_profile_id: self.add_customer_profile() # return immediately -there is no payment profile yet return [], [] validation_mode = kwargs.get('validation_mode') customer_profile = CIMCustomerProfile(self.customer_profile_id) success, response_d = customer_profile.get() if success: if response_d['profile'].has_key('payment_profiles'): # 1) get a list of payment_profiles from gateway cim_payment_profiles = response_d['profile']['payment_profiles'] if not type(cim_payment_profiles) is list: cim_payment_profiles = list([cim_payment_profiles]) # 2) validate payment_profile, if not valid, remove it from the list if validation_mode: for cim_payment_profile in cim_payment_profiles: customer_payment_profile_id = cim_payment_profile['customer_payment_profile_id'] is_valid = False # validate this payment profile cpp = CIMCustomerPaymentProfile(self.customer_profile_id, customer_payment_profile_id, ) success, response_d = cpp.validate(validation_mode=validation_mode) # convert direct_response string into a dict response_dict = direct_response_dict(response_d['direct_response']) response_code = response_dict.get('response_code', '') response_reason_code = response_dict.get('response_reason_code', '') if all([success, response_code == '1', response_reason_code == '1']): is_valid = True if not is_valid: invalid_cim_payment_profile_ids.append(cim_payment_profile['customer_payment_profile_id']) # remove it from the list cim_payment_profiles.remove(cim_payment_profile) else: valid_cim_payment_profile_ids.append(cim_payment_profile['customer_payment_profile_id']) list_cim_payment_profile_ids = [cim_payment_profile['customer_payment_profile_id'] for cim_payment_profile in cim_payment_profiles] if not validation_mode: valid_cim_payment_profile_ids.extend(list_cim_payment_profile_ids) # 3) if customer_payment_profile_id is not valid, # delete it from database - keep only the valid ones on local payment_profiles = PaymentProfile.objects.filter(customer_profile_id=self.customer_profile_id) for pp in payment_profiles: if pp.payment_profile_id not in list_cim_payment_profile_ids: pp.delete() for cim_payment_profile in cim_payment_profiles: customer_payment_profile_id = cim_payment_profile['customer_payment_profile_id'] # check if already exists, if not, insert to payment profiles table payment_profiles = PaymentProfile.objects.filter( payment_profile_id=customer_payment_profile_id) if not payment_profiles: payment_profile = PaymentProfile(customer_profile_id=self.customer_profile_id, payment_profile_id=customer_payment_profile_id, creator=self.user, owner=self.user, creator_username= self.user.username, owner_username = self.user.username, ) payment_profile.save() else: payment_profile = payment_profiles[0] if cim_payment_profile['payment'].has_key('credit_card') and \ cim_payment_profile['payment']['credit_card'].has_key('card_number'): card_num = cim_payment_profile['payment']['credit_card']['card_number'][-4:] if payment_profile.card_num <> card_num: payment_profile.card_num = card_num payment_profile.save() return valid_cim_payment_profile_ids, invalid_cim_payment_profile_ids