def create(): try: if current_user.subscription: flash('You already have an active subscription.', 'info') return redirect(url_for('user.dashboard')) plan = request.args.get('plan') subscription_plan = Subscription.get_plan_by_id(plan) # Guard against an invalid or missing plan. if subscription_plan is None and request.method == 'GET': flash('Sorry, that plan did not exist.', 'error') return redirect(url_for('billing.pricing')) stripe_key = current_app.config.get('STRIPE_PUBLISHABLE_KEY') form = CreditCardForm(stripe_key=stripe_key, plan=plan) if form.validate_on_submit(): subscription = Subscription() created = subscription.create( user=current_user, name=request.form.get('name'), plan=request.form.get('plan'), coupon=request.form.get('coupon_code'), token=request.form.get('stripe_token')) if created: from app.blueprints.billing.billing_functions import signup_limits signup_limits(current_user, subscription.plan) current_user.trial = False current_user.save() from app.blueprints.user.tasks import send_plan_signup_email send_plan_signup_email.delay(current_user.email, subscription.plan) flash('Your account has been upgraded!', 'success') else: flash('You must enable JavaScript for this request.', 'warning') return redirect(url_for('user.dashboard')) return render_template('billing/payment_method.html', form=form, plan=subscription_plan) except Exception as e: print_traceback(e) flash( 'There was an error. We weren\'t able to subscribe you to a plan at this time.', 'error') return redirect(url_for('user.dashboard'))
def create(): if current_user.subscription: flash('You already have an active subscription.', 'info') return redirect(url_for('user.settings')) plan = request.args.get('plan') subscription_plan = Subscription.get_plan_by_id(plan) # Guard against an invalid or missing plan. if subscription_plan is None and request.method == 'GET': flash('Sorry, that plan did not exist.', 'error') return redirect(url_for('billing.pricing')) stripe_key = current_app.config.get('STRIPE_PUBLISHABLE_KEY') form = CreditCardForm(stripe_key=stripe_key, plan=plan) if form.validate_on_submit(): subscription = Subscription() created = subscription.create(user=current_user, name=request.form.get('name'), plan=request.form.get('plan'), coupon=request.form.get('coupon_code'), token=request.form.get('stripe_token')) if created: if request.form.get('plan') is not None: plan = request.form.get('plan') # Set the mailbox and email limits accordingly current_user.mailbox_limit = 1 if plan == 'hobby' else 10 if plan == 'startup' else 40 if plan == 'professional' else 100 if plan == 'enterprise' else 0 current_user.email_limit = 400 if plan == 'hobby' else 2000 if plan == 'startup' else 5000 if plan == 'professional' else 15000 if plan == 'enterprise' else 0 current_user.save() flash('Your account has been upgraded!', 'success') else: flash('You must enable JavaScript for this request.', 'warning') return redirect(url_for('user.settings')) return render_template('billing/payment_method.html', form=form, plan=subscription_plan)