def pre_save(self, model_instance, add): if not add and not self.update_on_save: return getattr(model_instance, self.name) random_hash = md5_hash(max_length=self.hash_length) setattr(model_instance, self.name, random_hash) return random_hash
def __init__(self, update_on_save=False, hash_length=None, *args, **kwargs): # TODO: args & kwargs serve no purpose but to make django evolution to work self.update_on_save = update_on_save self.hash_length = hash_length super(fields.CharField, self).__init__( max_length=128, unique=True, blank=False, null=False, db_index=True, default=md5_hash(max_length=self.hash_length))
def save(self, prepend_vault_id=''): """ Adds or updates a users CC to the vault. @prepend_vault_id: any string to prepend all vault id's with in case the same braintree account is used by multiple projects/apps. """ assert self.is_valid() cc_details_map = { # cc details 'number': self.cleaned_data['cc_number'], 'cardholder_name': self.cleaned_data['name'], 'expiration_date': '%s/%s' %\ (self.cleaned_data['expiration_month'], self.cleaned_data['expiration_year']), 'cvv': self.cleaned_data['cvv'], 'billing_address': { 'postal_code': self.cleaned_data['zip_code'], } } if self.__user_vault: try: # get customer info, its credit card and then update that credit card response = Customer.find(self.__user_vault.vault_id) cc_info = response.credit_cards[0] return CreditCard.update(cc_info.token, params=cc_details_map) except Exception as e: logging.error('Was not able to get customer from vault. %s' % e) self.__user_vault.delete() # delete the stale instance from our db # in case the above updating fails or user was never in the vault new_customer_vault_id = '%s%s' % (prepend_vault_id, md5_hash()[:24]) respone = Customer.create({ # creating a customer, but we really just want to store their CC details 'id': new_customer_vault_id, # vault id, uniquely identifies customer. We're not caring about tokens (used for storing multiple CC's per user) 'credit_card': cc_details_map }) if respone.is_success: # save a new UserVault instance UserVault.objects.create(user=self.__user, vault_id=new_customer_vault_id) return respone
'cardholder_name': self.cleaned_data['name'], 'expiration_date': '%s/%s' %\ (self.cleaned_data['expiration_month'], self.cleaned_data['expiration_year']), 'cvv': self.cleaned_data['cvv'], 'billing_address': { 'postal_code': self.cleaned_data['zip_code'], } } if self.__user_vault: try: # get customer info, its credit card and then update that credit card response = Customer.find(self.__user_vault.vault_id) cc_info = response.credit_cards[0] return CreditCard.update(cc_info.token, params=cc_details_map) except Exception, e: logging.error('Was not able to get customer from vault. %s' % e) self.__user_vault.delete() # delete the stale instance from our db # in case the above updating fails or user was never in the vault new_customer_vault_id = '%s%s' % (prepend_vault_id, md5_hash()[:24]) respone = Customer.create({ # creating a customer, but we really just want to store their CC details 'id': new_customer_vault_id, # vault id, uniquely identifies customer. We're not caring about tokens (used for storing multiple CC's per user) 'credit_card': cc_details_map }) if respone.is_success: # save a new UserVault instance UserVault.objects.create(user=self.__user, vault_id=new_customer_vault_id) return respone