Skip to content

Commit

Permalink
Implement better stringified output for Charge
Browse files Browse the repository at this point in the history
  • Loading branch information
jleclanche committed May 27, 2018
1 parent edf83bf commit 5d3a878
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 10 deletions.
32 changes: 26 additions & 6 deletions djstripe/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down
31 changes: 27 additions & 4 deletions tests/test_charge.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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("<amount=50, paid=True, stripe_id=charge_xxxxxxxxxxxxxx>", 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")
Expand Down

0 comments on commit 5d3a878

Please sign in to comment.