Beispiel #1
0
    def test_sign_string(self) -> None:
        string = "abc"
        signed_string, salt = sign_string(string)
        self.assertEqual(string, unsign_string(signed_string, salt))

        with self.assertRaises(signing.BadSignature):
            unsign_string(signed_string, "randomsalt")
Beispiel #2
0
    def test_sign_string(self) -> None:
        string = "abc"
        signed_string, salt = sign_string(string)
        self.assertEqual(string, unsign_string(signed_string, salt))

        with self.assertRaises(signing.BadSignature):
            unsign_string(signed_string, "randomsalt")
Beispiel #3
0
    def setUp(self) -> None:
        self.token = 'token'
        # The values below should be copied from stripe_fixtures.json
        self.stripe_customer_id = 'cus_D7OT2jf5YAtZQL'
        self.customer_created = 1529990750
        self.stripe_plan_id = 'plan_D7Nh2BtpTvIzYp'
        self.subscription_created = 1529990751
        self.quantity = 8

        self.signed_seat_count, self.salt = sign_string(str(self.quantity))
        Plan.objects.create(nickname=Plan.CLOUD_ANNUAL, stripe_plan_id=self.stripe_plan_id)
Beispiel #4
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)
Beispiel #5
0
    def setUp(self) -> None:
        self.token = 'token'
        # The values below should be copied from stripe_fixtures.json
        self.stripe_customer_id = 'cus_D7OT2jf5YAtZQL'
        self.customer_created = 1529990750
        self.stripe_plan_id = 'plan_D7Nh2BtpTvIzYp'
        self.subscription_created = 1529990751
        self.quantity = 8

        self.signed_seat_count, self.salt = sign_string(str(self.quantity))
        Plan.objects.create(nickname=Plan.CLOUD_ANNUAL, stripe_plan_id=self.stripe_plan_id)
Beispiel #6
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)
Beispiel #7
0
def initial_upgrade(request: HttpRequest) -> HttpResponse:
    if not settings.BILLING_ENABLED:
        return render(request, "404.html")

    user = request.user
    error_message = ""
    error_description = ""  # only used in tests

    customer = Customer.objects.filter(realm=user.realm).first()
    if customer is not None and customer.has_billing_relationship:
        return HttpResponseRedirect(reverse('zilencer.views.billing_home'))

    if request.method == 'POST':
        try:
            plan, seat_count = unsign_and_check_upgrade_parameters(
                user, request.POST['plan'], request.POST['signed_seat_count'],
                request.POST['salt'])
            process_initial_upgrade(user, plan, seat_count,
                                    request.POST['stripeToken'])
        except BillingError as e:
            error_message = e.message
            error_description = e.description
        except Exception as e:
            billing_logger.exception("Uncaught exception in billing: %s" %
                                     (e, ))
            error_message = BillingError.CONTACT_SUPPORT
        else:
            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,
        'cloud_monthly_price': 8,
        'cloud_annual_price': 80,
        'cloud_annual_price_per_month': 6.67,
    }  # type: Dict[str, Any]
    response = render(request, 'zilencer/upgrade.html', context=context)
    response['error_description'] = error_description
    return response
Beispiel #8
0
def initial_upgrade(request: HttpRequest) -> HttpResponse:
    if not settings.BILLING_ENABLED:
        return render(request, "404.html")

    user = request.user
    error_message = ""
    error_description = ""  # only used in tests

    customer = Customer.objects.filter(realm=user.realm).first()
    if customer is not None and customer.has_billing_relationship:
        return HttpResponseRedirect(reverse('zilencer.views.billing_home'))

    if request.method == 'POST':
        try:
            plan, seat_count = unsign_and_check_upgrade_parameters(
                user, request.POST['plan'], request.POST['signed_seat_count'], request.POST['salt'])
            process_initial_upgrade(user, plan, seat_count, request.POST['stripeToken'])
        except BillingError as e:
            error_message = e.message
            error_description = e.description
        except Exception as e:
            billing_logger.exception("Uncaught exception in billing: %s" % (e,))
            error_message = BillingError.CONTACT_SUPPORT
        else:
            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,
        'cloud_monthly_price': 8,
        'cloud_annual_price': 80,
        'cloud_annual_price_per_month': 6.67,
    }  # type: Dict[str, Any]
    response = render(request, 'zilencer/upgrade.html', context=context)
    response['error_description'] = error_description
    return response