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