Skip to content

Commit

Permalink
Move from Stripe Plans to Prices.
Browse files Browse the repository at this point in the history
No change in functionality.
  • Loading branch information
dracos committed Nov 20, 2024
1 parent 7733131 commit 5040065
Show file tree
Hide file tree
Showing 14 changed files with 146 additions and 106 deletions.
2 changes: 1 addition & 1 deletion bulk_lookup/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def get_context_data(self, form, **kwargs):
context['price'] = settings.BULK_LOOKUP_AMOUNT
if not self.object:
self.object = self.get_object()
if self.object and self.object.plan.id == settings.PRICING[-1]['plan']:
if self.object and self.object.price.id == settings.PRICING[-1]['id']:
context['price'] = 0
return context

Expand Down
12 changes: 6 additions & 6 deletions mapit_mysociety_org/management/commands/add_mapit_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@ class Command(BaseCommand):
help = "Create a new user with associated Stripe subscription"

def add_arguments(self, parser):
plans = stripe.Plan.list()
plan_ids = [plan['id'] for plan in plans.data if plan['id'].startswith('mapit')]
prices = stripe.Price.list(limit=100)
price_ids = [price.id for price in prices.data if price.id.startswith('mapit')]
coupons = stripe.Coupon.list()
self.coupon_ids = [coupon['id'] for coupon in coupons if coupon['id'].startswith('charitable')]
parser.add_argument('--email', required=True)
parser.add_argument('--plan', choices=plan_ids, required=True)
parser.add_argument('--price', choices=price_ids, required=True)
parser.add_argument('--coupon', help='Existing coupons: ' + ', '.join(self.coupon_ids))
parser.add_argument('--trial', type=int)

def handle(self, *args, **options):
email = options['email']
coupon = options['coupon']
plan = options['plan']
price = options['price']

if coupon not in self.coupon_ids:
# coupon ID of the form charitableN(-Nmonths)
Expand All @@ -53,9 +53,9 @@ def handle(self, *args, **options):

customer = stripe.Customer.create(email=email).id
stripe_sub = stripe.Subscription.create(
customer=customer, plan=plan, coupon=coupon, trial_period_days=options['trial']).id
customer=customer, items=[{"price": price}], coupon=coupon, trial_period_days=options['trial']).id

sub = Subscription.objects.create(user=user, stripe_id=stripe_sub)
sub.redis_update_max(plan)
sub.redis_update_max(price)

self.stdout.write("Created user %s with password %s, API key %s\n" % (username, password, api_key.key))
6 changes: 3 additions & 3 deletions mapit_mysociety_org/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ def allow_migrate(self, db, app_label, model_name=None, **hints):

# API subscriptions
PRICING = [
{'plan': 'mapit-10k-v', 'price': 20, 'calls': '10,000'},
{'plan': 'mapit-100k-v', 'price': 100, 'calls': '100,000'},
{'plan': 'mapit-0k-v', 'price': 300, 'calls': '0'},
{'id': 'mapit-10k-v', 'price': 20, 'calls': '10,000'},
{'id': 'mapit-100k-v', 'price': 100, 'calls': '100,000'},
{'id': 'mapit-0k-v', 'price': 300, 'calls': '0'},
]
12 changes: 6 additions & 6 deletions mapit_mysociety_org/templates/pricing.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ <h2 class="pricing__option__cost">£{{ PRICING.0.price }}/mth</h2>
<li><strong>Free</strong> for charitable users</li>
</ul>
{% if request.user.is_authenticated %}
<a class="btn btn--primary" href="{% url 'subscription_update' %}?plan={{ PRICING.0.plan }}">Switch plan</a>
<a class="btn btn--primary" href="{% url 'subscription_update' %}?price={{ PRICING.0.id }}">Switch plan</a>
{% else %}
<a class="btn btn--primary" href="{% url 'account_signup' %}?plan={{ PRICING.0.plan }}">Sign up</a>
<a class="btn btn--primary" href="{% url 'account_signup' %}?price={{ PRICING.0.id }}">Sign up</a>
{% endif %}
</div>

Expand All @@ -51,9 +51,9 @@ <h2 class="pricing__option__cost">£{{ PRICING.1.price }}/mth</h2>
<li><strong>50% off</strong> for charitable users</li>
</ul>
{% if request.user.is_authenticated %}
<a class="btn btn--primary" href="{% url 'subscription_update' %}?plan={{ PRICING.1.plan }}">Switch plan</a>
<a class="btn btn--primary" href="{% url 'subscription_update' %}?price={{ PRICING.1.id }}">Switch plan</a>
{% else %}
<a class="btn btn--primary" href="{% url 'account_signup' %}?plan={{ PRICING.1.plan }}">Sign up</a>
<a class="btn btn--primary" href="{% url 'account_signup' %}?price={{ PRICING.1.id }}">Sign up</a>
{% endif %}
</div>

Expand All @@ -72,9 +72,9 @@ <h2 class="pricing__option__cost">£{{ PRICING.2.price }}/mth</h2>
<li><strong>50% off</strong> for charitable users</li>
</ul>
{% if request.user.is_authenticated %}
<a class="btn btn--primary" href="{% url 'subscription_update' %}?plan={{ PRICING.2.plan }}">Switch plan</a>
<a class="btn btn--primary" href="{% url 'subscription_update' %}?price={{ PRICING.2.id }}">Switch plan</a>
{% else %}
<a class="btn btn--primary" href="{% url 'account_signup' %}?plan={{ PRICING.2.plan }}">Sign up</a>
<a class="btn btn--primary" href="{% url 'account_signup' %}?price={{ PRICING.2.id }}">Sign up</a>
{% endif %}
</div>

Expand Down
20 changes: 14 additions & 6 deletions mapit_mysociety_org/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def test_signup(self, socket):
'password': 'password',
'password_confirm': 'password',
'tandcs_tick': 1,
'plan': 'mapit-10k-v',
'price': 'mapit-10k-v',
'charitable_tick': 1,
'charitable': 'c',
'charity_number': '123',
Expand Down Expand Up @@ -59,7 +59,7 @@ def test_signup_card_error(self):
'password': 'password',
'password_confirm': 'password',
'tandcs_tick': 1,
'plan': 'mapit-10k-v',
'price': 'mapit-10k-v',
'charitable_tick': 1,
'charitable': 'c',
'charity_number': '123',
Expand All @@ -71,18 +71,26 @@ def test_signup_card_error(self):
class ManagementTest(PatchedStripeMixin, PatchedRedisTestCase):
def test_add_mapit_user(self):
with patch('mapit_mysociety_org.management.commands.add_mapit_user.stripe', self.MockStripe):
self.MockStripe.Plan.list.return_value = convert_to_stripe_object({
'data': [{'id': 'mapit-0k-v'}, {'id': 'mapit-10k-v'}, {'id': 'mapit-100k-v'}]
self.MockStripe.Price.list.return_value = convert_to_stripe_object({
'data': [
{'id': 'mapit-0k-v',
'product': {'id': 'prod_GHI', 'name': 'MapIt, unlimited calls'}},
{'id': 'mapit-10k-v',
'product': {'id': 'prod_ABC', 'name': 'MapIt, 10,000 calls'}},
{'id': 'mapit-100k-v',
'product': {'id': 'prod_DEF', 'name': 'MapIt, 100,000 calls'}},
]
}, None, None)
call_command(
'add_mapit_user', '--email=[email protected]', '--plan=mapit-100k-v',
'add_mapit_user', '--email', '[email protected]', '--price', "mapit-100k-v",
coupon='charitable25-6months', trial='10',
stdout=StringIO(), stderr=StringIO())

self.MockStripe.Coupon.create.assert_called_once_with(
id='charitable25-6months', duration='repeating', duration_in_months='6', percent_off='25')
self.MockStripe.Subscription.create.assert_called_once_with(
customer='CUSTOMER-ID', plan='mapit-100k-v', coupon='charitable25-6months', trial_period_days='10')
customer='CUSTOMER-ID', items=[{"price": 'mapit-100k-v'}],
coupon='charitable25-6months', trial_period_days='10')

user = User.objects.get(email='[email protected]')
sub = Subscription.objects.get(user=user)
Expand Down
2 changes: 1 addition & 1 deletion mapit_mysociety_org/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def dispatch(self, *args, **kwargs):

def get_initial(self):
initial = super(SignupView, self).get_initial()
initial['plan'] = self.request.GET.get('plan')
initial['price'] = self.request.GET.get('price')
return initial

def form_valid(self, form):
Expand Down
20 changes: 10 additions & 10 deletions static/js/payment.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
(function() {

function plan_cost() {
var plan = document.querySelector('input[name=plan]:checked'),
function price_cost() {
var price = document.querySelector('input[name=price]:checked'),
ctick = document.getElementById('id_charitable_tick'),
c = document.querySelector('input[name=charitable]:checked');
plan = plan ? plan.value : '';
price = price ? price.value : '';
ctick = ctick ? ctick.checked : '';
c = c ? c.value : '';
var num = 20;
Expand All @@ -26,7 +26,7 @@ function plan_cost() {
}

function need_stripe() {
var num = plan_cost();
var num = price_cost();
if (num === 0 || document.getElementById('js-payment').getAttribute('data-has-payment-data')) {
return false;
}
Expand All @@ -43,10 +43,10 @@ function toggle_stripe() {
}
}

if (document.getElementById('id_plan_0')) {
document.getElementById('id_plan_0').addEventListener('change', toggle_stripe);
document.getElementById('id_plan_1').addEventListener('change', toggle_stripe);
document.getElementById('id_plan_2').addEventListener('change', toggle_stripe);
if (document.getElementById('id_price_0')) {
document.getElementById('id_price_0').addEventListener('change', toggle_stripe);
document.getElementById('id_price_1').addEventListener('change', toggle_stripe);
document.getElementById('id_price_2').addEventListener('change', toggle_stripe);
var opt = document.getElementById('charitable-desc').querySelector('.account-form__help_text');
document.getElementById('id_charitable_tick').addEventListener('click', function(e) {
if (this.checked) {
Expand Down Expand Up @@ -187,8 +187,8 @@ form && form.addEventListener('submit', function(e) {
errors += err('id_email');
errors += err('id_password');
errors += err('id_password_confirm');
var plan = document.querySelector('input[name=plan]:checked');
errors += err_highlight(document.querySelector('label[for=id_plan_0]'), !plan);
var price = document.querySelector('input[name=price]:checked');
errors += err_highlight(document.querySelector('label[for=id_price_0]'), !price);
var ctick = document.getElementById('id_charitable_tick').checked;
var c = document.querySelector('input[name=charitable]:checked');
errors += err_highlight(document.querySelector('label[for=id_charitable_0]'), ctick && !c);
Expand Down
12 changes: 6 additions & 6 deletions subscriptions/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@
from django.utils.safestring import mark_safe


def describe_plan(i):
def describe_price(i):
s = settings.PRICING[i]
if s['calls'] == '0':
desc = '£%d/mth - Unlimited calls' % s['price']
else:
desc = '£%d/mth - %s calls per month' % (s['price'], s['calls'])
return (s['plan'], desc)
return (s['id'], desc)


class SubscriptionMixin(forms.Form):
plan = forms.ChoiceField(choices=(
describe_plan(i) for i in range(3)
price = forms.ChoiceField(choices=(
describe_price(i) for i in range(3)
), label='Please choose a plan', widget=forms.RadioSelect)
charitable_tick = forms.BooleanField(
label='I qualify for a charitable discounted price',
Expand Down Expand Up @@ -61,8 +61,8 @@ def clean(self):

payment_data = cleaned_data.get('stripeToken') or cleaned_data.get('payment_method')
if not self.has_payment_data and not payment_data and not (
cleaned_data.get('plan') == settings.PRICING[0]['plan'] and typ in ('c', 'i')):
self.add_error('plan', 'You need to submit payment')
cleaned_data.get('price') == settings.PRICING[0]['id'] and typ in ('c', 'i')):
self.add_error('price', 'You need to submit payment')

if not self.stripe and not cleaned_data.get('tandcs_tick'):
self.add_error('tandcs_tick', 'Please agree to the terms and conditions')
Expand Down
4 changes: 2 additions & 2 deletions subscriptions/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ def redis_key_blocked(self):
def redis_key_history(self):
return "{0}:history".format(self.redis_key)

def redis_update_max(self, plan):
m = re.match(r'mapit-(\d+)k', plan)
def redis_update_max(self, price):
m = re.match(r'mapit-(\d+)k', price)
max = int(m.group(1)) * 1000
r = redis_connection()
r.set(self.redis_key_max, max)
Expand Down
6 changes: 3 additions & 3 deletions subscriptions/templates/subscriptions/check.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ <h2>{% trans "Subscription" %}</h2>
Sorry, your card has been declined. Perhaps you can try another?
</div>

{% if stripe.plan %}
<p>You are subscribing to <strong>{{ stripe.plan.nickname }}</strong>,
{% if stripe.price %}
<p>You are subscribing to <strong>{{ stripe.price.nickname }}</strong>,
costing £{{ actual_paid|floatformat:-2 }}/mth.
{% if stripe.discount %}
(£{{ stripe.plan.amount|floatformat:-2 }}/mth with
(£{{ stripe.price.unit_amount|floatformat:-2 }}/mth with
{{ stripe.discount.coupon.percent_off|floatformat }}% discount applied.)
{% endif %}
</p>
Expand Down
2 changes: 1 addition & 1 deletion subscriptions/templates/subscriptions/invoices.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<h2>{% trans "Invoices" %}</h2>
{% include 'account/_messages.html' %}

{% if stripe.plan %}
{% if stripe.price %}

<table class="striped">
<tr>
Expand Down
10 changes: 5 additions & 5 deletions subscriptions/templates/subscriptions/subscription_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,19 @@ <h2>{% trans "Subscription" %}</h2>
{% endif %}
{% endif %}

{% if stripe.plan %}
{% if stripe.price %}

<p>Your current plan is <strong>{{ stripe.plan.nickname }}</strong>.</p>
<p>Your current plan is <strong>{{ stripe.price.nickname }}</strong>.</p>

<p>It costs you £{{ actual_paid|floatformat:-2 }}/mth.
{% if stripe.discount %}
(£{{ stripe.plan.amount|floatformat:-2 }}/mth with
(£{{ stripe.price.unit_amount|floatformat:-2 }}/mth with
{{ stripe.discount.coupon.percent_off|floatformat }}% discount applied.)
{% endif %}
</p>

{% if stripe.schedule.phases.1 and stripe.schedule.phases.1.items.0.plan.nickname != stripe.plan.nickname %}
<p>You are switching to <strong>{{ stripe.schedule.phases.1.items.0.plan.nickname }}</strong> at the end of your current period.</p>
{% if stripe.schedule.phases.1 and stripe.schedule.phases.1.items.0.price.nickname != stripe.price.nickname %}
<p>You are switching to <strong>{{ stripe.schedule.phases.1.items.0.price.nickname }}</strong> at the end of your current period.</p>
{% endif %}

{% if stripe.discount.end %}
Expand Down
Loading

0 comments on commit 5040065

Please sign in to comment.