def bulk_delete(cls, ids): """ Override the general bulk_delete method because we need to delete them one at a time while also deleting them on Stripe. :param ids: List of ids to be deleted :type ids: list :return: int """ delete_count = 0 for id in ids: user = User.query.get(id) if user is None: continue if user.subscription is None: user.delete() else: subscription = Subscription() cancelled = subscription.cancel(user=user) # If successful, delete it locally. if cancelled: user.delete() delete_count += 1 return delete_count
def update_payment_method(): if not current_user.credit_card: flash('You do not have a payment method on file.', 'error') return redirect(url_for('user.settings')) active_plan = Subscription.get_plan_by_id(current_user.subscription.plan) card = current_user.credit_card stripe_key = current_app.config.get('STRIPE_PUBLISHABLE_KEY') form = CreditCardForm(stripe_key=stripe_key, plan=active_plan, name=current_user.name) if form.validate_on_submit(): subscription = Subscription() updated = subscription.update_payment_method( user=current_user, credit_card=card, name=request.form.get('name'), token=request.form.get('stripe_token')) if updated: flash('Your payment method has been updated.', '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=active_plan, card_last4=str(card.last4))
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 cancel(): form = CancelSubscriptionForm() if form.validate_on_submit(): # Cancel the user's subscription if current_user.subscription: subscription = Subscription() canceled = subscription.cancel(user=current_user) else: # If there is no subscription, then delete the user canceled = True if canceled: # Get the user's email email = current_user.email # Clear the cache. mailbox_id = current_user.mailbox_id if cache.get(mailbox_id): cache.delete(mailbox_id) from app.blueprints.parse.models.mailbox import Mailbox # Delete the credentials from MG for mailbox in Mailbox.query.filter_by(user_email=email).all(): delete_inbox(mailbox.mailbox_id) # Delete all emails, rules and mailboxes belonging to the user. from app.blueprints.user.tasks import delete_all delete_all.delay(email, mailbox_id) # Delete the user. from app.blueprints.billing.tasks import delete_users ids = [current_user.id] delete_users(ids) # Send a cancellation email. from app.blueprints.user.tasks import send_cancel_email send_cancel_email.delay(email) flash('Sorry to see you go! Your subscription has been canceled.', 'success') return redirect(url_for('user.login')) return render_template('billing/cancel.html', form=form)
def users_cancel_subscription(): form = UserCancelSubscriptionForm() if form.validate_on_submit(): user = User.query.get(request.form.get('id')) if user: subscription = Subscription() if subscription.cancel(user): flash('Subscription has been canceled for {0}.' .format(user.name), 'success') else: flash('No subscription was canceled, something went wrong.', 'danger') return redirect(url_for('admin.users'))
def cancel(): from app.blueprints.base.functions import print_traceback try: form = CancelSubscriptionForm() if form.validate_on_submit(): # If there is no subscription, then delete the user canceled = True # Cancel the user's subscription if current_user.subscription: subscription = Subscription() canceled = subscription.cancel(user=current_user) if canceled: # Set the user to inactive from app.blueprints.base.functions import set_inactive set_inactive(current_user) logout_user() # Get the user's email # email = current_user.email # Delete the user. # from app.blueprints.billing.tasks import delete_users # ids = [current_user.id] # Delete the user # delete_users.delay(ids) # current_user.delete() # Send a cancellation email. from app.blueprints.user.tasks import send_cancel_email # send_cancel_email.delay(email) flash( 'Sorry to see you go! Your subscription has been canceled.', 'success') return redirect(url_for('user.signup')) return render_template('billing/cancel.html', form=form) except Exception as e: print_traceback(e) return redirect(url_for('user.settings'))
def cancel(): form = CancelSubscriptionForm() if form.validate_on_submit(): # Cancel the user's subscription if current_user.subscription: subscription = Subscription() canceled = subscription.cancel(user=current_user) else: # If there is no subscription, then delete the user canceled = True if canceled: # Get the user's email email = current_user.email # Delete the user. from app.blueprints.billing.tasks import delete_users, delete_auth from app.blueprints.api.models.app_auths import AppAuthorization ids = [current_user.id] # Delete the app auths for id in ids: auths = [ x.id for x in AppAuthorization.query.filter( AppAuthorization.user_id == id).all() ] delete_auth.delay(auths) # Delete the user delete_users.delay(ids) # Send a cancellation email. from app.blueprints.user.tasks import send_cancel_email send_cancel_email.delay(email) flash('Sorry to see you go! Your subscription has been canceled.', 'success') return redirect(url_for('user.login')) return render_template('billing/cancel.html', form=form)
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)
def subscriptions(db): """ Create subscription fixtures. :param db: Pytest fixture :return: SQLAlchemy database session """ subscriber = User.find_by_identity('*****@*****.**') if subscriber: subscriber.delete() db.session.query(Subscription).delete() params = { 'role': 'admin', 'email': '*****@*****.**', 'name': 'Subby', 'password': '******', 'payment_id': 'cus_000' } subscriber = User(**params) # The account needs to be commit before we can assign a subscription to it. db.session.add(subscriber) db.session.commit() # Create a subscription. params = { 'user_id': subscriber.id, 'plan': 'gold' } subscription = Subscription(**params) db.session.add(subscription) # Create a credit card. params = { 'user_id': subscriber.id, 'brand': 'Visa', 'last4': '4242', 'exp_date': datetime.date(2015, 6, 1) } credit_card = CreditCard(**params) db.session.add(credit_card) db.session.commit() return db
def update(): try: current_plan = current_user.subscription.plan active_plan = Subscription.get_plan_by_id(current_plan) new_plan = Subscription.get_new_plan(request.form.keys()) plan = Subscription.get_plan_by_id(new_plan) # Guard against an invalid, missing or identical plan. is_same_plan = new_plan == active_plan['id'] if ((new_plan is not None and plan is None) or is_same_plan) and \ request.method == 'POST': return redirect(url_for('billing.update')) form = UpdateSubscriptionForm( coupon_code=current_user.subscription.coupon) if form.validate_on_submit(): subscription = Subscription() updated = subscription.update( user=current_user, coupon=request.form.get('coupon_code'), plan=plan.get('id')) if updated: from app.blueprints.billing.billing_functions import change_limits change_limits(current_user, plan, active_plan) from app.blueprints.user.tasks import send_plan_change_email send_plan_change_email.delay(current_user.email, plan) flash( 'Your plan has been updated. Changes will take effect immediately.', 'success') return redirect(url_for('user.dashboard')) return render_template('billing/pricing.html', form=form, plans=settings.STRIPE_PLANS, active_plan=active_plan) except Exception as e: print_traceback(e) flash( 'There was an error. We weren\'t able to change your plan at this time.', 'error') return redirect(url_for('user.dashboard'))
def update(): current_plan = current_user.subscription.plan active_plan = Subscription.get_plan_by_id(current_plan) new_plan = Subscription.get_new_plan(request.form.keys()) plan = Subscription.get_plan_by_id(new_plan) # Guard against an invalid, missing or identical plan. is_same_plan = new_plan == active_plan['id'] if ((new_plan is not None and plan is None) or is_same_plan) and \ request.method == 'POST': return redirect(url_for('billing.update')) form = UpdateSubscriptionForm(coupon_code=current_user.subscription.coupon) if form.validate_on_submit(): subscription = Subscription() updated = subscription.update(user=current_user, coupon=request.form.get('coupon_code'), plan=plan.get('id')) if updated: if new_plan is not None: # Set the user's current mailbox to the oldest mailbox from app.blueprints.parse.models.mailbox import Mailbox mailbox = Mailbox.query.filter( Mailbox.user_email == current_user.email).order_by( Mailbox.created_on.asc()).first() if mailbox: current_user.mailbox_id = mailbox.mailbox_id # Set the mailbox and email limits accordingly mailbox_limit = 10 if new_plan == 'hobby' else 40 if new_plan == 'startup'\ else 60 if new_plan == 'professional' else 100 if new_plan == 'enterprise'\ else 100 if new_plan == 'developer' else 0 email_limit = 400 if new_plan == 'hobby' else 2000 if new_plan == 'startup'\ else 5000 if new_plan == 'professional' else 15000 if new_plan == 'enterprise'\ else 20000 if new_plan == 'developer' else 0 # Adjust the mailboxes by deleting # the most recent mailboxes and emails from app.blueprints.user.tasks import adjust_mailboxes if plan['amount'] < active_plan['amount']: current_user.email_count, current_user.mailbox_count = adjust_mailboxes( current_user.email, current_user.mailbox_id, current_user.email_count, mailbox_limit, email_limit) current_user.mailbox_limit, current_user.email_limit = mailbox_limit, email_limit current_user.save() flash( 'Your plan has been updated. Changes will take effect immediately.', 'success') return redirect(url_for('user.settings')) return render_template('billing/pricing.html', form=form, plans=settings.STRIPE_PLANS, active_plan=active_plan)