def test_extract_current_subscription(self) -> None: self.assertIsNone(extract_current_subscription(mock_create_customer())) subscription = extract_current_subscription( mock_customer_with_subscription()) self.assertEqual(subscription["id"][:4], "sub_") self.assertIsNone( extract_current_subscription( mock_customer_with_canceled_subscription()))
def test_extract_current_subscription(self, mock_create_customer: mock.Mock, mock_retrieve_customer: mock.Mock) -> None: # Only the most basic test. In particular, doesn't include testing with a # canceled subscription, because we don't have a fixture for it. customer_without_subscription = stripe.Customer.create() # type: ignore # Mocked out function call self.assertIsNone(extract_current_subscription(customer_without_subscription)) customer_with_subscription = stripe.Customer.retrieve() # type: ignore # Mocked out function call subscription = extract_current_subscription(customer_with_subscription) self.assertEqual(subscription["id"][:4], "sub_")
def billing_home(request: HttpRequest) -> HttpResponse: user = request.user customer = Customer.objects.filter(realm=user.realm).first() if customer is None: return HttpResponseRedirect(reverse('zilencer.views.initial_upgrade')) if not customer.has_billing_relationship: return HttpResponseRedirect(reverse('zilencer.views.initial_upgrade')) if not user.is_realm_admin and not user.is_billing_admin: context = {'admin_access': False} # type: Dict[str, Any] return render(request, 'zilencer/billing.html', context=context) context = {'admin_access': True} stripe_customer = stripe_get_customer(customer.stripe_customer_id) subscription = extract_current_subscription(stripe_customer) if subscription: plan_name = PLAN_NAMES[Plan.objects.get( stripe_plan_id=subscription.plan.id).nickname] seat_count = subscription.quantity # Need user's timezone to do this properly renewal_date = '{dt:%B} {dt.day}, {dt.year}'.format( dt=timestamp_to_datetime(subscription.current_period_end)) upcoming_invoice = stripe_get_upcoming_invoice( customer.stripe_customer_id) renewal_amount = subscription.plan.amount * subscription.quantity / 100. prorated_credits = 0 prorated_charges = upcoming_invoice.amount_due / 100. - renewal_amount if prorated_charges < 0: prorated_credits = -prorated_charges # nocoverage -- no way to get here yet prorated_charges = 0 # nocoverage # Can only get here by subscribing and then downgrading. We don't support downgrading # yet, but keeping this code here since we will soon. else: # nocoverage plan_name = "Zulip Free" seat_count = 0 renewal_date = '' renewal_amount = 0 prorated_credits = 0 prorated_charges = 0 payment_method = None if stripe_customer.default_source is not None: payment_method = "Card ending in %(last4)s" % { 'last4': stripe_customer.default_source.last4 } context.update({ 'plan_name': plan_name, 'seat_count': seat_count, 'renewal_date': renewal_date, 'renewal_amount': '{:,.2f}'.format(renewal_amount), 'payment_method': payment_method, 'prorated_charges': '{:,.2f}'.format(prorated_charges), 'prorated_credits': '{:,.2f}'.format(prorated_credits), }) return render(request, 'zilencer/billing.html', context=context)
def billing_home(request: HttpRequest) -> HttpResponse: user = request.user customer = Customer.objects.filter(realm=user.realm).first() if customer is None: return HttpResponseRedirect(reverse('zilencer.views.initial_upgrade')) if not customer.has_billing_relationship: return HttpResponseRedirect(reverse('zilencer.views.initial_upgrade')) if not user.is_realm_admin and not user.is_billing_admin: context = {'admin_access': False} # type: Dict[str, Any] return render(request, 'zilencer/billing.html', context=context) context = {'admin_access': True} stripe_customer = stripe_get_customer(customer.stripe_customer_id) subscription = extract_current_subscription(stripe_customer) prorated_charges = stripe_customer.account_balance if subscription: plan_name = PLAN_NAMES[Plan.objects.get(stripe_plan_id=subscription.plan.id).nickname] seat_count = subscription.quantity # Need user's timezone to do this properly renewal_date = '{dt:%B} {dt.day}, {dt.year}'.format( dt=timestamp_to_datetime(subscription.current_period_end)) upcoming_invoice = stripe_get_upcoming_invoice(customer.stripe_customer_id) renewal_amount = subscription.plan.amount * subscription.quantity prorated_charges += upcoming_invoice.total - renewal_amount # Can only get here by subscribing and then downgrading. We don't support downgrading # yet, but keeping this code here since we will soon. else: # nocoverage plan_name = "Zulip Free" seat_count = 0 renewal_date = '' renewal_amount = 0 prorated_credits = 0 if prorated_charges < 0: # nocoverage prorated_credits = -prorated_charges prorated_charges = 0 payment_method = None if stripe_customer.default_source is not None: payment_method = "Card ending in %(last4)s" % {'last4': stripe_customer.default_source.last4} context.update({ 'plan_name': plan_name, 'seat_count': seat_count, 'renewal_date': renewal_date, 'renewal_amount': '{:,.2f}'.format(renewal_amount / 100.), 'payment_method': payment_method, 'prorated_charges': '{:,.2f}'.format(prorated_charges / 100.), 'prorated_credits': '{:,.2f}'.format(prorated_credits / 100.), }) return render(request, 'zilencer/billing.html', context=context)
def billing_home(request: HttpRequest) -> HttpResponse: user = request.user customer = Customer.objects.filter(realm=user.realm).first() if customer is None: return HttpResponseRedirect(reverse('zilencer.views.initial_upgrade')) if not user.is_realm_admin and not user == customer.billing_user: context = {'admin_access': False} # type: Dict[str, Any] return render(request, 'zilencer/billing.html', context=context) context = {'admin_access': True} stripe_customer = get_stripe_customer(customer.stripe_customer_id) subscription = extract_current_subscription(stripe_customer) if subscription: plan_name = PLAN_NAMES[Plan.objects.get(stripe_plan_id=subscription.plan.id).nickname] seat_count = subscription.quantity # Need user's timezone to do this properly renewal_date = '{dt:%B} {dt.day}, {dt.year}'.format( dt=timestamp_to_datetime(subscription.current_period_end)) upcoming_invoice = get_upcoming_invoice(customer.stripe_customer_id) renewal_amount = subscription.plan.amount * subscription.quantity / 100. prorated_credits = 0 prorated_charges = upcoming_invoice.amount_due / 100. - renewal_amount if prorated_charges < 0: prorated_credits = -prorated_charges # nocoverage -- no way to get here yet prorated_charges = 0 # nocoverage else: # nocoverage -- no way to get here yet plan_name = "Zulip Free" seat_count = 0 renewal_date = '' renewal_amount = 0 prorated_credits = 0 prorated_charges = 0 payment_method = None if stripe_customer.default_source is not None: payment_method = "Card ending in %(last4)s" % {'last4': stripe_customer.default_source.last4} context.update({ 'plan_name': plan_name, 'seat_count': seat_count, 'renewal_date': renewal_date, 'renewal_amount': '{:,.2f}'.format(renewal_amount), 'payment_method': payment_method, 'prorated_charges': '{:,.2f}'.format(prorated_charges), 'prorated_credits': '{:,.2f}'.format(prorated_credits), }) return render(request, 'zilencer/billing.html', context=context)
def test_extract_current_subscription(self) -> None: self.assertIsNone(extract_current_subscription(mock_create_customer())) subscription = extract_current_subscription(mock_customer_with_subscription()) self.assertEqual(subscription["id"][:4], "sub_") self.assertIsNone(extract_current_subscription(mock_customer_with_canceled_subscription()))