Skip to content

Commit

Permalink
Merge branch 'master' into plan-per-account-upstream
Browse files Browse the repository at this point in the history
  • Loading branch information
paltman authored Nov 23, 2017
2 parents 2e3f173 + 8e03b4c commit 9830d54
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 16 deletions.
5 changes: 4 additions & 1 deletion pinax/stripe/actions/charges.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ def capture(charge, amount=None, idempotency_key=None):
amount if amount else charge.amount,
charge.currency
)
stripe_charge = stripe.Charge(charge.stripe_id).capture(
stripe_charge = stripe.Charge(
charge.stripe_id,
stripe_account=charge.stripe_account_stripe_id,
).capture(
amount=amount,
idempotency_key=idempotency_key,
expand=["balance_transaction"],
Expand Down
34 changes: 27 additions & 7 deletions pinax/stripe/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ def __repr__(self):
str(self.stripe_id),
)

@property
def stripe_plan(self):
return stripe.Plan.retrieve(
self.stripe_id,
stripe_account=self.stripe_account_stripe_id,
)


@python_2_unicode_compatible
class Coupon(StripeObject):
Expand Down Expand Up @@ -157,11 +164,12 @@ def __str__(self):
return "{} - {}".format(self.kind, self.stripe_id)

def __repr__(self):
return "Event(pk={!r}, kind={!r}, customer={!r}, valid={!r}, stripe_id={!r})".format(
return "Event(pk={!r}, kind={!r}, customer={!r}, valid={!r}, created_at={!s}, stripe_id={!r})".format(
self.pk,
str(self.kind),
self.customer,
self.valid,
self.created_at.replace(microsecond=0).isoformat(),
str(self.stripe_id),
)

Expand Down Expand Up @@ -268,14 +276,26 @@ def stripe_customer(self):
)

def __str__(self):
return str(self.user)
if self.user:
return str(self.user)
elif self.id:
return ", ".join(str(user) for user in self.users.all())
return "No User(s)"

def __repr__(self):
return "Customer(pk={!r}, user={!r}, stripe_id={!r})".format(
self.pk,
self.user,
str(self.stripe_id),
)
if self.user:
return "Customer(pk={!r}, user={!r}, stripe_id={!r})".format(
self.pk,
self.user,
str(self.stripe_id),
)
elif self.id:
return "Customer(pk={!r}, users={}, stripe_id={!r})".format(
self.pk,
", ".join(repr(user) for user in self.users.all()),
str(self.stripe_id),
)
return "Customer(pk={!r}, stripe_id={!r})".format(self.pk, str(self.stripe_id))


class Card(StripeObject):
Expand Down
9 changes: 9 additions & 0 deletions pinax/stripe/tests/test_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,15 @@ def test_capture_with_amount(self, CaptureMock, SyncMock):
self.assertEquals(kwargs["idempotency_key"], "IDEM")
self.assertTrue(SyncMock.called)

@patch("pinax.stripe.actions.charges.sync_charge_from_stripe_data")
@patch("stripe.Charge.capture")
def test_capture_with_connect(self, CaptureMock, SyncMock):
account = Account(stripe_id="acc_001")
customer = Customer(stripe_id="cus_001", stripe_account=account)
charges.capture(Charge(stripe_id="ch_A", amount=decimal.Decimal("100"), currency="usd", customer=customer))
self.assertTrue(CaptureMock.called)
self.assertTrue(SyncMock.called)

@patch("pinax.stripe.actions.charges.sync_charge")
def test_update_availability(self, SyncMock):
Charge.objects.create(customer=self.customer, amount=decimal.Decimal("100"), currency="usd", paid=True, captured=True, available=False, refunded=False)
Expand Down
48 changes: 40 additions & 8 deletions pinax/stripe/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from django.test import TestCase
from django.utils import timezone

from mock import patch
from mock import call, patch

from ..models import (
Account,
Expand Down Expand Up @@ -53,24 +53,56 @@ def test_plan_per_account(self):
Plan.objects.create(stripe_id="plan", stripe_account=account, amount=decimal.Decimal("100"), interval="monthly", interval_count=1)
self.assertEquals(Plan.objects.count(), 2)

@patch("stripe.Plan.retrieve")
def test_plan_stripe_plan(self, RetrieveMock):
c = Plan(stripe_id="plan")
self.assertEqual(c.stripe_plan, RetrieveMock.return_value)
self.assertTrue(RetrieveMock.call_args_list, [
call("plan", stripe_account=None)])

@patch("stripe.Plan.retrieve")
def test_plan_stripe_plan_with_account(self, RetrieveMock):
c = Plan(stripe_id="plan", stripe_account=Account(stripe_id="acct_A"))
self.assertEqual(c.stripe_plan, RetrieveMock.return_value)
self.assertTrue(RetrieveMock.call_args_list, [
call("plan", stripe_account="acct_A")])

def test_event_processing_exception_str(self):
e = EventProcessingException(data="hello", message="hi there", traceback="fake")
self.assertTrue("Event=" in str(e))

def test_event_str_and_repr(self):
e = Event(kind="customer.deleted", webhook_message={})
created_at = datetime.datetime.utcnow()
created_at_iso = created_at.replace(microsecond=0).isoformat()
e = Event(kind="customer.deleted", webhook_message={}, created_at=created_at)
self.assertTrue("customer.deleted" in str(e))
self.assertEquals(repr(e), "Event(pk=None, kind='customer.deleted', customer=None, valid=None, stripe_id='')")
self.assertEquals(repr(e), "Event(pk=None, kind='customer.deleted', customer=None, valid=None, created_at={}, stripe_id='')".format(
created_at_iso))

e.stripe_id = "evt_X"
e.customer = Customer()
self.assertEquals(repr(e), "Event(pk=None, kind='customer.deleted', customer={!r}, valid=None, stripe_id='{}')".format(
e.customer, e.stripe_id))
self.assertEquals(repr(e), "Event(pk=None, kind='customer.deleted', customer={!r}, valid=None, created_at={}, stripe_id='{}')".format(
e.customer, created_at_iso, e.stripe_id))

def test_customer_str_and_repr(self):
c = Customer()
self.assertTrue("None" in str(c))
self.assertEquals(repr(c), "Customer(pk=None, user=None, stripe_id='')")
self.assertTrue("No User(s)" in str(c))
self.assertEquals(repr(c), "Customer(pk=None, stripe_id='')")

def test_customer_with_user_str_and_repr(self):
User = get_user_model()
c = Customer(user=User())
self.assertEqual(str(c), "")
self.assertEqual(repr(c), "Customer(pk=None, user=<User: >, stripe_id='')")

def test_connected_customer_str_and_repr(self):
User = get_user_model()
user = User.objects.create()
account = Account.objects.create(stripe_id="acc_A")
customer = Customer.objects.create(stripe_id="cus_A", stripe_account=account)
UserAccount.objects.create(customer=customer, user=user, account=account)
self.assertEqual(str(customer), "")
self.assertEqual(repr(customer), "Customer(pk={c.pk}, users=<User: >, stripe_id='cus_A')".format(c=customer))

def test_charge_repr(self):
charge = Charge()
Expand Down Expand Up @@ -175,7 +207,7 @@ def test_user_account_repr(self):
self.assertEquals(
repr(ua),
"UserAccount(pk=None, user=<User: >, account=Account(pk=None, display_name='', type=None, stripe_id='', authorized=True)"
", customer=Customer(pk=None, user=None, stripe_id=''))")
", customer=Customer(pk=None, stripe_id=''))")


class StripeObjectTests(TestCase):
Expand Down
6 changes: 6 additions & 0 deletions pinax/stripe/tests/test_webhooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
CustomerSubscriptionCreatedWebhook,
CustomerUpdatedWebhook,
InvoiceCreatedWebhook,
Webhook,
registry
)

Expand Down Expand Up @@ -104,6 +105,11 @@ class WebhookTests(TestCase):
"type": "transfer.created"
}

def test_webhook_init(self):
event = Event(kind=None)
webhook = Webhook(event)
self.assertIsNone(webhook.name)

@patch("stripe.Event.retrieve")
@patch("stripe.Transfer.retrieve")
def test_webhook_with_transfer_event(self, TransferMock, StripeEventMock):
Expand Down
2 changes: 2 additions & 0 deletions pinax/stripe/webhooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ def __new__(cls, clsname, bases, attrs):

class Webhook(with_metaclass(Registerable, object)):

name = None

def __init__(self, event):
if event.kind != self.name:
raise Exception("The Webhook handler ({}) received the wrong type of Event ({})".format(self.name, event.kind))
Expand Down

0 comments on commit 9830d54

Please sign in to comment.