def add_credit_card(request, account_type): """ Adds a credit card and subscribes to the account type. """ username = request.subdomain # Ensure that we cannot edit another user's account type: if username != request.user.username or not request.user.is_authenticated(): raise Http404 if account_type != settings.PREMIUM_ACCOUNT_NAME and account_type != settings.PROFESSIONAL_ACCOUNT_NAME: raise Http404 user = request.user customer = user.customer stripe_customer = stripe.Customer.retrieve(customer.stripe_customer_id) if stripe_customer.active_card: last_4 = stripe_customer.active_card.last4 else: last_4 = None profile = user.get_profile() error = None if request.method == 'POST': form = CardForm(request.POST) if form.is_valid(): stripe_customer.card = form.cleaned_data['stripe_token'] error = save_stripe_customer(stripe_customer) if error is None: stripe_customer.update_subscription(plan=account_type, prorate=False) if account_type == settings.PREMIUM_ACCOUNT_NAME: customer.account_limit = settings.PREMIUM_IMAGE_LIMIT customer.max_file_size = settings.PREMIUM_MAX_FILE_SIZE else: customer.account_limit = settings.PROFESSIONAL_IMAGE_LIMIT customer.max_file_size = settings.PROFESSIONAL_MAX_FILE_SIZE customer.save() variables = RequestContext(request, {'username': username, 'customer': customer, 'profile': profile, 'account_type': account_type} ) return render_to_response('accounts/change_account_success.html', variables) else: form = CardForm() now = datetime.datetime.now() variables = RequestContext(request, {'publishable': settings.STRIPE_PUBLISHABLE, 'soon': soon(), 'months': range(1, 13), 'years': range(now.year, (now.year + 15)), 'username': username, 'customer': customer, 'profile': profile, 'last_4': last_4, 'error': error} ) return render_to_response('accounts/credit_card_form.html', variables)
def change_credit_card(request): """ Allows the user to update their credit card information """ username = request.subdomain # Ensure that we cannot edit another user's account type: if username != request.user.username or not request.user.is_authenticated(): raise Http404 user = request.user customer = user.customer stripe_customer = stripe.Customer.retrieve(customer.stripe_customer_id) if stripe_customer.active_card: last_4 = stripe_customer.active_card.last4 else: last_4 = None profile = user.get_profile() error = None if request.method == 'POST': form = CardForm(request.POST) if form.is_valid(): stripe_customer.card = form.cleaned_data['stripe_token'] error = save_stripe_customer(stripe_customer) if error is None: variables = RequestContext(request, {'username': username, 'customer': customer, 'profile': profile} ) return render_to_response('accounts/update_card_success.html', variables) else: form = CardForm() now = datetime.datetime.now() variables = RequestContext(request, {'publishable': settings.STRIPE_PUBLISHABLE, 'soon': soon(), 'months': range(1, 13), 'years': range(now.year, (now.year + 15)), 'username': username, 'customer': customer, 'profile': profile, 'last_4': last_4, 'error': error} ) return render_to_response('accounts/credit_card_form.html', variables)
def register_user(request, account_type): """ Registers a new user to the site. Works for free and paid accounts. """ if account_type != settings.PROFESSIONAL_ACCOUNT_NAME and account_type != settings.PREMIUM_ACCOUNT_NAME and account_type != settings.FREE_ACCOUNT_NAME: raise Http404 card_form_valid = True error = None paid = (account_type != settings.FREE_ACCOUNT_NAME) if request.method == 'POST': user_form = RegistrationForm(request.POST) if account_type != settings.FREE_ACCOUNT_NAME: card_form = CardForm(request.POST) card_form_valid = card_form.is_valid() if user_form.is_valid() and card_form_valid: user = User.objects.create_user( username=user_form.cleaned_data['username'], password=user_form.cleaned_data['password1'], email=user_form.cleaned_data['email'] ) # Now, populate the profile from the form. profile = user.get_profile() # profile.field_1 = form.cleaned_data['field_1'] profile.save() # Get the customer record that was automatically created customer = user.customer if account_type == settings.PROFESSIONAL_ACCOUNT_NAME: customer.account_limit = settings.PROFESSIONAL_IMAGE_LIMIT elif account_type == settings.PREMIUM_ACCOUNT_NAME: customer.account_limit = settings.PREMIUM_IMAGE_LIMIT else: customer.account_limit = settings.FREE_IMAGE_LIMIT # Accessing the stripe_customer attribute creates the stripe customer stripe_customer = customer.stripe_customer stripe_customer.email = user.email if account_type != settings.FREE_ACCOUNT_NAME: stripe_customer.card = card_form.cleaned_data['stripe_token'] # If there's an error with the credit card, delete everything # and display the error message on the form. Clearly a hacky way # to do this, and something I'll clean up later. error = save_stripe_customer(stripe_customer) if error is None: stripe_customer.update_subscription(plan=account_type) customer.save() user = authenticate(username=request.POST['username'], password=request.POST['password1']) login(request, user) return HttpResponseRedirect('/accounts/profile/') else: stripe_customer.delete() user.delete() else: user_form = RegistrationForm() now = datetime.datetime.now() variables = RequestContext(request, {'user_form': user_form, 'publishable': settings.STRIPE_PUBLISHABLE, 'soon': soon(), 'months': range(1, 13), 'years': range(now.year, (now.year + 15)), 'error': error, 'account_type': account_type, 'paid': paid} ) return render_to_response('registration/register.html', variables)