diff --git a/djstripe/models.py b/djstripe/models.py index ca88326073..d96af0a3fa 100644 --- a/djstripe/models.py +++ b/djstripe/models.py @@ -649,10 +649,36 @@ class Charge(StripeObject): objects = ChargeManager() + def __str__(self): + amount = self.human_readable_amount + status = self.human_readable_status + if not status: + return amount + return "{amount} ({status})".format(amount=amount, status=status) + @property def disputed(self): return self.dispute is not None + @property + def human_readable_amount(self): + return get_friendly_currency_amount(self.amount, self.currency) + + @property + def human_readable_status(self): + if not self.captured: + return "Uncaptured" + elif self.disputed: + return "Disputed" + elif self.refunded: + return "Refunded" + elif self.amount_refunded: + return "Partially refunded" + elif self.status == enums.ChargeStatus.failed: + return "Failed" + + return "" + def _attach_objects_hook(self, cls, data): customer = cls._stripe_object_to_customer(target_cls=Customer, data=data) if customer: @@ -671,12 +697,6 @@ def _attach_objects_hook(self, cls, data): self.source, _ = PaymentMethod._get_or_create_source(data["source"], self.source_type) - def str_parts(self): - return [ - "amount={amount}".format(amount=self.amount), - "paid={paid}".format(paid=smart_text(self.paid)), - ] + super(Charge, self).str_parts() - def _calculate_refund_amount(self, amount=None): """ :rtype: int diff --git a/tests/test_charge.py b/tests/test_charge.py index 8b46812be9..9854f3c4d5 100644 --- a/tests/test_charge.py +++ b/tests/test_charge.py @@ -14,8 +14,8 @@ from django.test.testcases import TestCase from mock import patch -from djstripe.enums import LegacySourceType -from djstripe.models import Account, Charge, PaymentMethod +from djstripe.enums import ChargeStatus, LegacySourceType +from djstripe.models import Account, Charge, Dispute, PaymentMethod from . import FAKE_ACCOUNT, FAKE_CHARGE, FAKE_CUSTOMER, FAKE_TRANSFER @@ -28,8 +28,31 @@ def setUp(self): self.account = Account.objects.create() def test_str(self): - charge = Charge(amount=50, paid=True, stripe_id='charge_xxxxxxxxxxxxxx') - self.assertEqual("", str(charge)) + charge = Charge( + amount=50, currency="usd", stripe_id="ch_test", + status=ChargeStatus.failed, + captured=False, + paid=False, + ) + self.assertEqual(str(charge), "$50.00 USD (Uncaptured)") + + charge.captured = True + self.assertEqual(str(charge), "$50.00 USD (Failed)") + charge.status = ChargeStatus.succeeded + + charge.dispute = Dispute() + self.assertEqual(str(charge), "$50.00 USD (Disputed)") + + charge.dispute = None + charge.refunded = True + charge.amount_refunded = 50 + self.assertEqual(str(charge), "$50.00 USD (Refunded)") + + charge.refunded = False + self.assertEqual(str(charge), "$50.00 USD (Partially refunded)") + + charge.amount_refunded = 0 + self.assertEqual(str(charge), "$50.00 USD") @patch("djstripe.models.Account.get_default_account") @patch("stripe.Charge.retrieve")