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') active_plan = Subscription.get_plan_by_id(plan) # Guard against an invalid or missing plan. if active_plan is None and request.method == 'GET': 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: flash(_('Awesome, thanks for subscribing!'), 'success') else: flash(_('You must enable Javascript for this request.'), 'warn') return redirect(url_for('user.settings')) return render_template('billing/payment_method.jinja2', form=form, plan=active_plan)
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.payment_id 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(): if not current_user.subscription: return redirect(url_for('billing.pricing')) 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() 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: flash(_('Your subscription has been updated.'), 'success') return redirect(url_for('user.settings')) return render_template('billing/pricing.jinja2', form=form, plans=settings.STRIPE_PLANS, active_plan=active_plan)
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_last4 = str(current_user.credit_card.last4) 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, 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.'), 'warn') return redirect(url_for('user.settings')) return render_template('billing/payment_method.jinja2', form=form, plan=active_plan, card_last4=card_last4)
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 cancelled for %(user)s.', user=user.name), 'success') else: flash(_('No subscription was cancelled, something went wrong.'), 'error') return redirect(url_for('admin.users'))
def cancel(): if not current_user.subscription: flash(_('You do not have an active subscription.'), 'error') return redirect(url_for('user.settings')) form = CancelSubscriptionForm() if form.validate_on_submit(): subscription = Subscription() cancelled = subscription.cancel(user=current_user) if cancelled: flash(_( 'Sorry to see you go, your subscription has been cancelled.'), 'success') return redirect(url_for('user.settings')) return render_template('billing/cancel.jinja2', form=form)
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() 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: flash(_('Your subscription has been updated.'), 'success') return redirect(url_for('user.settings')) return render_template('billing/pricing.jinja2', form=form, plans=settings.STRIPE_PLANS, active_plan=active_plan)