Skip to content

Commit

Permalink
feat: introduce PaymentMethod.last4 property and add last4 to __str__
Browse files Browse the repository at this point in the history
  • Loading branch information
PabloCastellano authored and jleclanche committed Apr 25, 2024
1 parent d5aa1ac commit fae47b3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
9 changes: 7 additions & 2 deletions djstripe/models/payment_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -1130,11 +1130,16 @@ class PaymentMethod(StripeModel):
help_text="Additional information for payment methods of type `wechat_pay`",
)

@property
def last4(self):
pm_data = getattr(self, self.type, {})
return pm_data.get("last4", "Unknown")

def __str__(self):
if self.customer:
return f"{enums.PaymentMethodType.humanize(self.type)} for {self.customer}"
return f"{enums.PaymentMethodType.humanize(self.type)} ending in {self.last4} for {self.customer}"
return (
f"{enums.PaymentMethodType.humanize(self.type)} is not associated with any"
f"{enums.PaymentMethodType.humanize(self.type)} ending in {self.last4} is not associated with any"
" customer"
)

Expand Down
10 changes: 6 additions & 4 deletions tests/test_payment_method.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ def test___str__(self, monkeypatch, customer_exists):
pm = models.PaymentMethod.sync_from_stripe_data(fake_payment_method_data)
customer = None
assert (
f"{enums.PaymentMethodType.humanize(fake_payment_method_data['type'])} is"
" not associated with any customer" == str(pm)
f"{enums.PaymentMethodType.humanize(fake_payment_method_data['type'])}"
f"ending in {fake_payment_method_data['card']['last4']} is not associated with any customer"
== str(pm)
)

else:
Expand All @@ -54,8 +55,9 @@ def test___str__(self, monkeypatch, customer_exists):
id=fake_payment_method_data["customer"]
)
assert (
f"{enums.PaymentMethodType.humanize(fake_payment_method_data['type'])} for"
f" {customer}" == str(pm)
f"{enums.PaymentMethodType.humanize(fake_payment_method_data['type'])}"
f"ending in {fake_payment_method_data['card']['last4']} for {customer}"
== str(pm)
)

@pytest.mark.parametrize("customer_exists", [True, False])
Expand Down

0 comments on commit fae47b3

Please sign in to comment.