def __init__(self, user, practice_group, post_to_update=False, *args, **kwargs): """ Takes in a user to figure out whether a vault id exists or not etc. @post_to_update: if set to True, then form contents are meant to be posted to Braintree, otherwise its implied this form is meant for rendering to the user, hence initialize with braintree data (if any). """ self.__user = user self.__user_vault = UserVault.objects.get_user_vault_instance_or_none(user) self.__practice_group = practice_group if not post_to_update and self.__user_vault and not args: logging.debug('Looking up payment info for vault_id: %s' % self.__user_vault.vault_id) try: response = Customer.find(self.__user_vault.vault_id) info = response.credit_cards[0] initial = { 'name': info.cardholder_name, 'cc_number': info.masked_number, 'expiration_month': int(info.expiration_month), 'expiration_year': info.expiration_year, 'zip_code': info.billing_address.postal_code, } super(UserCCDetailsForm, self).__init__(initial=initial, *args, **kwargs) except Exception, e: logging.error('Was not able to get customer from vault. %s' % e) super(UserCCDetailsForm, self).__init__(initial={'name': '%s %s' % (user.first_name, user.last_name)}, *args, **kwargs)
def save(self, request, 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, e: logging.error('Was not able to get customer from vault. %s' % e) self.__user_vault.delete() # delete the stale instance from our db
def payments_billing(request, template="django_braintree/payments_billing.html"): """ Renders both the past payments that have occurred on the users credit card, but also their CC information on file (if any) """ d = {} if request.method == "POST": # Credit Card is being changed/updated by the user form = UserCCDetailsForm(request.user, True, request.POST) if form.is_valid(): response = form.save() if response.is_success: messages.add_message(request, messages.SUCCESS, "Your credit card information has been securely saved.") return HttpResponseJson() else: return HttpResponseJson(success=False, errors=[BAD_CC_ERROR_MSG]) return HttpResponseJson(success=False, data={"form": form_errors_serialize(form)}) else: if UserVault.objects.is_in_vault(request.user): try: response = Customer.find(UserVault.objects.get_user_vault_instance_or_none(request.user).vault_id) d["current_cc_info"] = response.credit_cards[0] except Exception, e: logging.error("Unable to get vault information for user from braintree. %s" % e) d["cc_form"] = UserCCDetailsForm(request.user)
def payments_billing(request, template='django_braintree/pay.html'): """ Renders both the past payments that have occurred on the users credit card, but also their CC information on file (if any) """ d = {} if request.method == 'POST': # Credit Card is being changed/updated by the user form = UserCCDetailsForm(request.user, True, request.POST) if form.is_valid(): response = form.save()# actually ,here ,we create a vault print response.is_success print response if response.is_success: messages.add_message(request, messages.SUCCESS, 'Your credit card information has been securely saved.') return JsonResponse() else: return JsonResponse(success=False, errors=[BAD_CC_ERROR_MSG]) return JsonResponse(success=False, data={'form': form_errors_serialize(form)}) else: if UserVault.objects.is_in_vault(request.user): try: response = Customer.find(UserVault.objects.get_user_vault_instance_or_none(request.user).vault_id) d['current_cc_info'] = response.credit_cards[0] print d['current_cc_info'] except Exception, e: logging.error('Unable to get vault information for user from braintree. %s' % e) d['cc_form'] = UserCCDetailsForm(request.user)
def payments_billing(request, template='django_braintree/payments_billing.html'): """ Renders both the past payments that have occurred on the users credit card, but also their CC information on file (if any) """ d = {} if request.method == 'POST': # Credit Card is being changed/updated by the user form = UserCCDetailsForm(request.user, True, request.POST) if form.is_valid(): response = form.save() if response.is_success: messages.add_message( request, messages.SUCCESS, 'Your credit card information has been securely saved.') return JsonResponse() else: return JsonResponse(success=False, errors=[BAD_CC_ERROR_MSG]) return JsonResponse(success=False, data={'form': form_errors_serialize(form)}) else: if UserVault.objects.is_in_vault(request.user): try: response = Customer.find( UserVault.objects.get_user_vault_instance_or_none( request.user).vault_id) d['current_cc_info'] = response.credit_cards[0] except Exception, e: logging.error( 'Unable to get vault information for user from braintree. %s' % e) d['cc_form'] = UserCCDetailsForm(request.user)
def charge(self, amount): """ Charges the users credit card, with he passed $amount, if they are in the vault. Returns the payment_log instance or None (if charge fails etc.) """ try: customer = Customer.find(self.vault_id) result = Transaction.sale( { 'amount': amount.quantize(Decimal('.01')), "payment_method_token": customer.credit_cards[0].token, 'customer_id': self.vault_id, "options": { "submit_for_settlement": True } } ) if result.is_success: # create a payment log payment_log = PaymentLog.objects.create(user=self.user, amount=amount, transaction_id=result.transaction.id) print "sucess" return payment_log else: raise Exception('Logical error in CC transaction') except Exception: return None logging.error('Failed to charge $%s to user: %s with vault_id: %s' % (amount, self.user, self.vault_id))
def save(self, request, 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, e: logging.error('Was not able to get customer from vault. %s' % e) self.__user_vault.delete( ) # delete the stale instance from our db
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
def __init__(self, user, practice_group, post_to_update=False, *args, **kwargs): """ Takes in a user to figure out whether a vault id exists or not etc. @post_to_update: if set to True, then form contents are meant to be posted to Braintree, otherwise its implied this form is meant for rendering to the user, hence initialize with braintree data (if any). """ self.__user = user self.__user_vault = UserVault.objects.get_user_vault_instance_or_none( user) self.__practice_group = practice_group if not post_to_update and self.__user_vault and not args: logging.debug('Looking up payment info for vault_id: %s' % self.__user_vault.vault_id) try: response = Customer.find(self.__user_vault.vault_id) info = response.credit_cards[0] initial = { 'name': info.cardholder_name, 'cc_number': info.masked_number, 'expiration_month': int(info.expiration_month), 'expiration_year': info.expiration_year, 'zip_code': info.billing_address.postal_code, } super(UserCCDetailsForm, self).__init__(initial=initial, *args, **kwargs) except Exception, e: logging.error('Was not able to get customer from vault. %s' % e) super(UserCCDetailsForm, self).__init__(initial={ 'name': '%s %s' % (user.first_name, user.last_name) }, *args, **kwargs)
'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
def payments_billing(request, template='genbilling/payments_billing.html'): context = get_context(request) check_for_duplicate = True mhluser = MHLUser.objects.get(pk=request.user.pk) try: ostaff = OfficeStaff.objects.get(user=mhluser) omgr = Office_Manager.objects.get(user=ostaff, practice=ostaff.current_practice) except ObjectDoesNotExist: return err403(request) if (omgr.manager_role != 2): return err403(request) #per michael - office manager can manage mulyi location in SAME GROUP ONLY #this is enforced via set up of user accounts and practicesby Docotor com staff! # practice_group = omgr.practice.practice_group practice_group = omgr.practice.get_parent_org() if (practice_group is None): return err403(request) #for release 0.1 check if THIS practice group has account already, #if yes, ONLY original user can change account try: account = Account.objects.get(practice_group_new=practice_group) except ObjectDoesNotExist: check_for_duplicate = False if (check_for_duplicate and account.owner != request.user): context['errors'] = DUPLICATE_CC_ERROR_MSG return render_to_response(template, context, context_instance=RequestContext(request)) context['cc_form_post_url'] = TransparentRedirect.url() #see if this update or create if (check_for_duplicate): # account exist user_vault = UserVault.objects.get_user_vault_instance_or_none( request.user) try: response = Customer.find(user_vault.vault_id) info = response.credit_cards[0] context['current_cc_info'] = True context['name'] = info.cardholder_name context['cc_number'] = info.masked_number context['expiration_date'] = info.expiration_date context['zip_code'] = info.billing_address.postal_code except Exception: context['errors'] = "Unable to retrieve your payment instrument "\ "info, please notify [email protected]." return render_to_response(template, context, context_instance=RequestContext(request)) form = BraintreeForm(request.user, practice_group) # Generate the tr_data field; this MUST be called! if (settings.IS_DEV == True): call_back_url = '//%s%s' % ( settings.SERVER_ADDRESS, reverse('MHLogin.genbilling.views.payments_billing_callback')) else: abs_uri = '://'.join( [settings.SERVER_PROTOCOL, settings.SERVER_ADDRESS]) url = reverse('MHLogin.genbilling.views.payments_billing_callback') call_back_url = urljoin(abs_uri, url) if (check_for_duplicate): # account exist tr_data = Customer.tr_data_for_update( { "customer_id": user_vault.vault_id, "customer": { "credit_card": { "options": { "update_existing_token": info.token } } } }, call_back_url) else: tr_data = Customer.tr_data_for_create( {"customer": { "company": practice_group.description }}, call_back_url) context['tr_data'] = tr_data return render_to_response(template, context, context_instance=RequestContext(request))