def test_subscribe_customer_to_second_plan(self) -> None: with self.assertRaisesRegex(BillingError, 'subscribing with existing subscription'): do_subscribe_customer_to_plan(self.example_user("iago"), mock_customer_with_subscription(), self.stripe_plan_id, self.quantity, 0)
def initial_upgrade(request: HttpRequest) -> HttpResponse: user = request.user if Customer.objects.filter(realm=user.realm).exists(): return HttpResponseRedirect(reverse('zilencer.views.billing_home')) if request.method == 'POST': customer = do_create_customer_with_payment_source(user, request.POST['stripeToken']) # TODO: the current way this is done is subject to tampering by the user. seat_count = int(request.POST['seat_count']) if seat_count < 1: raise AssertionError('seat_count is less than 1') do_subscribe_customer_to_plan( customer=customer, stripe_plan_id=Plan.objects.get(nickname=request.POST['plan']).stripe_plan_id, seat_count=seat_count, # TODO: billing address details are passed to us in the request; # use that to calculate taxes. tax_percent=0) # TODO: check for errors and raise/send to frontend return HttpResponseRedirect(reverse('zilencer.views.billing_home')) context = { 'publishable_key': STRIPE_PUBLISHABLE_KEY, 'email': user.email, 'seat_count': get_seat_count(user.realm), 'plan': "Zulip Premium", 'nickname_monthly': Plan.CLOUD_MONTHLY, 'nickname_annual': Plan.CLOUD_ANNUAL, } # type: Dict[str, Any] return render(request, 'zilencer/upgrade.html', context=context)
def test_subscribe_customer_to_second_plan( self, mock_retrieve_customer: mock.Mock) -> None: with self.assertRaisesRegex( AssertionError, "Customer already has an active subscription."): do_subscribe_customer_to_plan(stripe.Customer.retrieve(), self.stripe_plan_id, self.quantity, 0)
def initial_upgrade(request: HttpRequest) -> HttpResponse: if not settings.DEVELOPMENT: return render(request, "404.html") user = request.user error_message = "" if Customer.objects.filter(realm=user.realm).exists(): return HttpResponseRedirect(reverse('zilencer.views.billing_home')) if request.method == 'POST': plan = request.POST['plan'] if plan not in [Plan.CLOUD_ANNUAL, Plan.CLOUD_MONTHLY]: billing_logger.warning( "Tampered plan during realm upgrade. user: %s, realm: %s (%s)." % (user.id, user.realm.id, user.realm.string_id)) error_message = "Something went wrong. Please contact [email protected]" try: seat_count = int( unsign_string(request.POST['signed_seat_count'], request.POST['salt'])) except signing.BadSignature: billing_logger.warning( "Tampered seat count during realm upgrade. user: %s, realm: %s (%s)." % (user.id, user.realm.id, user.realm.string_id)) error_message = "Something went wrong. Please contact [email protected]" if not error_message: stripe_customer = do_create_customer_with_payment_source( user, request.POST['stripeToken']) do_subscribe_customer_to_plan( stripe_customer=stripe_customer, stripe_plan_id=Plan.objects.get(nickname=plan).stripe_plan_id, seat_count=seat_count, # TODO: billing address details are passed to us in the request; # use that to calculate taxes. tax_percent=0) # TODO: check for errors and raise/send to frontend return HttpResponseRedirect(reverse('zilencer.views.billing_home')) seat_count = get_seat_count(user.realm) signed_seat_count, salt = sign_string(str(seat_count)) context = { 'publishable_key': STRIPE_PUBLISHABLE_KEY, 'email': user.email, 'seat_count': seat_count, 'signed_seat_count': signed_seat_count, 'salt': salt, 'plan': "Zulip Premium", 'nickname_monthly': Plan.CLOUD_MONTHLY, 'nickname_annual': Plan.CLOUD_ANNUAL, 'error_message': error_message, } # type: Dict[str, Any] return render(request, 'zilencer/upgrade.html', context=context)
def initial_upgrade(request: HttpRequest) -> HttpResponse: if not settings.DEVELOPMENT: return render(request, "404.html") user = request.user error_message = "" if Customer.objects.filter(realm=user.realm).exists(): return HttpResponseRedirect(reverse('zilencer.views.billing_home')) if request.method == 'POST': plan = request.POST['plan'] if plan not in [Plan.CLOUD_ANNUAL, Plan.CLOUD_MONTHLY]: billing_logger.warning("Tampered plan during realm upgrade. user: %s, realm: %s (%s)." % (user.id, user.realm.id, user.realm.string_id)) error_message = "Something went wrong. Please contact [email protected]" try: seat_count = int(unsign_string(request.POST['signed_seat_count'], request.POST['salt'])) except signing.BadSignature: billing_logger.warning("Tampered seat count during realm upgrade. user: %s, realm: %s (%s)." % (user.id, user.realm.id, user.realm.string_id)) error_message = "Something went wrong. Please contact [email protected]" if not error_message: stripe_customer = do_create_customer_with_payment_source(user, request.POST['stripeToken']) do_subscribe_customer_to_plan( stripe_customer=stripe_customer, stripe_plan_id=Plan.objects.get(nickname=plan).stripe_plan_id, seat_count=seat_count, # TODO: billing address details are passed to us in the request; # use that to calculate taxes. tax_percent=0) # TODO: check for errors and raise/send to frontend return HttpResponseRedirect(reverse('zilencer.views.billing_home')) seat_count = get_seat_count(user.realm) signed_seat_count, salt = sign_string(str(seat_count)) context = { 'publishable_key': STRIPE_PUBLISHABLE_KEY, 'email': user.email, 'seat_count': seat_count, 'signed_seat_count': signed_seat_count, 'salt': salt, 'plan': "Zulip Premium", 'nickname_monthly': Plan.CLOUD_MONTHLY, 'nickname_annual': Plan.CLOUD_ANNUAL, 'error_message': error_message, } # type: Dict[str, Any] return render(request, 'zilencer/upgrade.html', context=context)
def test_subscribe_customer_to_second_plan(self) -> None: with self.assertRaises(BillingError) as context: do_subscribe_customer_to_plan(mock_customer_with_subscription(), self.stripe_plan_id, self.quantity, 0) self.assertEqual(context.exception.description, 'subscribing with existing subscription')
def test_subscribe_customer_to_second_plan(self) -> None: with self.assertRaisesRegex(BillingError, 'subscribing with existing subscription'): do_subscribe_customer_to_plan(self.example_user("iago"), mock_customer_with_subscription(), self.stripe_plan_id, self.quantity, 0)
def test_subscribe_customer_to_second_plan(self, mock_customer_with_active_subscription: mock.Mock) -> None: with self.assertRaisesRegex(AssertionError, "Customer already has an active subscription."): do_subscribe_customer_to_plan(stripe.Customer.retrieve(), # type: ignore # Mocked out function call self.stripe_plan_id, self.quantity, 0)
def test_subscribe_customer_to_second_plan(self, mock_retrieve_customer: mock.Mock) -> None: with self.assertRaisesRegex(AssertionError, "Customer already has an active subscription."): do_subscribe_customer_to_plan(stripe.Customer.retrieve(), # type: ignore # Mocked out function call self.stripe_plan_id, self.quantity, 0)