def subscription_billing_cycle_change(request, form_class=ChangeBillingForm, extra_context=None): context = {} customer = request.META['customer'] context['customer'] = customer if not request.META['has_at_least_premium']: return redirect(reverse("subscription_change")) plan = get_subscription_type(customer.subscription.plan.id) if request.method == 'POST': form = form_class(plan, customer.subscription.plan.interval, request.POST) if form.is_valid(): token = form.cleaned_data['stripe_token'] card = None if token: card = token billing_cycle = form.cleaned_data['billing_cycle'] customer.update_subscription(plan=s.SUBSCRIPTION_UIDS[plan][billing_cycle][1], card=card) customer.save() return redirect("%s%s" % (reverse('employer_account'), "?msg=changed_billing&tab=subscription")) else: form = form_class(plan, customer.subscription.plan.interval) # TODO - allow for the price computation to be more flexible (beyond just an upgrade # from a monthly subscription to an annual subscription premium_annual_subscription_id = s.SUBSCRIPTION_UIDS['premium']['year'][1] premium_monthly_subscription_id = s.SUBSCRIPTION_UIDS['premium']['month'][1] annual_plan = stripe.Plan.retrieve(premium_annual_subscription_id) annual_plan_amount = annual_plan.amount monthly_plan = stripe.Plan.retrieve(premium_monthly_subscription_id) monthly_plan_amount = monthly_plan.amount refund = monthly_plan_amount * (1-(time() - customer.subscription['start'])/(customer.subscription['current_period_end'] - customer.subscription['start'])) context['refund'] = refund context['annual_plan_amount'] = annual_plan_amount context['prorated_amount'] = annual_plan_amount - refund context['form'] = form context.update(extra_context or {}) return context
def subscription_cancel(request, extra_context=None): context = {} customer = request.META['customer'] if request.method == "POST": customer.cancel_subscription(at_period_end=True) return redirect("%s%s" % (reverse("employer_account"), "?msg=subscription-cancelled&tab=subscription")) else: context['customer'] = customer context['premium_subscription_uids'] = map(lambda x: x[1], s.SUBSCRIPTION_UIDS['premium'].values()) context['feature_template'] = "subscription_features_%s.html" % get_subscription_type(customer.subscription.plan.id) return context