Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove unexisting name from plan data for Synchronizes a plan action #625

Open
wants to merge 11 commits into
base: original
Choose a base branch
from
21 changes: 11 additions & 10 deletions pinax/stripe/actions/plans.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,17 @@ def sync_plan(plan, event=None):
event: the event associated with the plan
"""

defaults = {
"amount": utils.convert_amount_for_db(plan["amount"], plan["currency"]),
"currency": plan["currency"] or "",
"interval": plan["interval"],
"interval_count": plan["interval_count"],
"name": plan["name"],
"statement_descriptor": plan["statement_descriptor"] or "",
"trial_period_days": plan["trial_period_days"],
"metadata": plan["metadata"]
}
defaults = {x: plan[x] for x in [
"currency",
"interval",
"interval_count",
"statement_descriptor",
"trial_period_days",
"metadata",
"active"
] if x in plan}

defaults["amount"] = utils.convert_amount_for_db(plan["amount"], plan["currency"])

obj, created = models.Plan.objects.get_or_create(
stripe_id=plan["id"],
Expand Down
20 changes: 20 additions & 0 deletions pinax/stripe/migrations/0015_plan_active.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.18 on 2019-01-29 15:04
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('pinax_stripe', '0014_auto_20180413_1959'),
]

operations = [
migrations.AddField(
model_name='plan',
name='active',
field=models.BooleanField(default=True),
),
]
20 changes: 20 additions & 0 deletions pinax/stripe/migrations/0016_auto_20190528_1140.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.16 on 2019-05-28 15:40
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('pinax_stripe', '0015_plan_active'),
]

operations = [
migrations.AlterField(
model_name='coupon',
name='percent_off',
field=models.DecimalField(blank=True, decimal_places=2, max_digits=9, null=True),
),
]
15 changes: 10 additions & 5 deletions pinax/stripe/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from django.core.exceptions import ValidationError
from django.db import models
from django.utils import timezone
from django.utils.encoding import python_2_unicode_compatible
from django.utils.six import python_2_unicode_compatible, text_type
from django.utils.functional import cached_property
from django.utils.translation import ugettext_lazy as _

Expand Down Expand Up @@ -84,9 +84,10 @@ class Plan(UniquePerAccountStripeObject):
statement_descriptor = models.TextField(blank=True)
trial_period_days = models.IntegerField(null=True, blank=True)
metadata = JSONField(null=True, blank=True)
active = models.BooleanField(default=True)

def __str__(self):
return "{} ({}{})".format(self.name, CURRENCY_SYMBOLS.get(self.currency, ""), self.amount)
return "{} {} ({}{})".format(self.stripe_id, self.name, CURRENCY_SYMBOLS.get(self.currency, ""), self.amount)

def __repr__(self):
return "Plan(pk={!r}, name={!r}, amount={!r}, currency={!r}, interval={!r}, interval_count={!r}, trial_period_days={!r}, stripe_id={!r})".format(
Expand Down Expand Up @@ -118,7 +119,7 @@ class Coupon(StripeObject):
livemode = models.BooleanField(default=False)
max_redemptions = models.PositiveIntegerField(null=True, blank=True)
metadata = JSONField(null=True, blank=True)
percent_off = models.PositiveIntegerField(null=True, blank=True)
percent_off = models.DecimalField(decimal_places=2, max_digits=9, null=True, blank=True)
redeem_by = models.DateTimeField(null=True, blank=True)
times_redeemed = models.PositiveIntegerField(null=True, blank=True)
valid = models.BooleanField(default=False)
Expand Down Expand Up @@ -280,11 +281,11 @@ def stripe_customer(self):

def __str__(self):
if self.user:
return str(self.user)
return text_type('{}').format(self.user)
elif self.id:
users = self.users.all()
if users:
return ", ".join(str(user) for user in users)
return text_type(", ".join(str(user) for user in users))
if self.stripe_id:
return "No User(s) ({})".format(self.stripe_id)
return "No User(s)"
Expand Down Expand Up @@ -355,6 +356,7 @@ class BitcoinReceiver(StripeObject):
used_for_payment = models.BooleanField(default=False)


@python_2_unicode_compatible
class Subscription(StripeAccountFromCustomerMixin, StripeObject):

STATUS_CURRENT = ["trialing", "active"]
Expand Down Expand Up @@ -407,6 +409,9 @@ def __repr__(self):
self.stripe_id,
)

def __str__(self):
return text_type('Subscription {} for customer {}').format(self.stripe_id, self.customer)


class Invoice(StripeAccountFromCustomerMixin, StripeObject):

Expand Down
1 change: 0 additions & 1 deletion pinax/stripe/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@
"object": "plan",
"currency": "usd",
"created": 1498573686,
"name": "Pro Plan",
"metadata": {}
}
},
Expand Down
2 changes: 0 additions & 2 deletions pinax/stripe/tests/test_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1293,13 +1293,11 @@ def test_sync_plan(self):
"interval_count": 1,
"livemode": False,
"metadata": {},
"name": "Gold Plan",
"statement_descriptor": "ALTMAN",
"trial_period_days": 3
}
plans.sync_plan(plan)
self.assertTrue(Plan.objects.all().count(), 1)
self.assertEqual(Plan.objects.get(stripe_id="pro2").name, plan["name"])

def test_sync_payment_source_from_stripe_data_card(self):
source = {
Expand Down
2 changes: 1 addition & 1 deletion pinax/stripe/tests/test_webhooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ def test_plan_created(self, EventMock):
)
registry.get(event.kind)(event).process()
plan = Plan.objects.get(stripe_id="gold1")
self.assertEqual(plan.name, PLAN_CREATED_TEST_DATA["data"]["object"]["name"])
self.assertEqual(plan.stripe_id, PLAN_CREATED_TEST_DATA["data"]["object"]["id"])


class CustomerSubscriptionCreatedWebhookTest(TestCase):
Expand Down