From 71a0661180d99322040323a7730451f3007f6fc1 Mon Sep 17 00:00:00 2001 From: Pablo Castellano Date: Mon, 4 Dec 2023 19:01:26 +0100 Subject: [PATCH] feat: introduce PaymentMethod.last4 property and add last4 to __str__ --- djstripe/models/payment_methods.py | 9 +++++++-- tests/test_payment_method.py | 8 ++++---- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/djstripe/models/payment_methods.py b/djstripe/models/payment_methods.py index 9019bf01d1..7884d81e59 100644 --- a/djstripe/models/payment_methods.py +++ b/djstripe/models/payment_methods.py @@ -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" ) diff --git a/tests/test_payment_method.py b/tests/test_payment_method.py index c4c4e46d62..e769f8fbe3 100644 --- a/tests/test_payment_method.py +++ b/tests/test_payment_method.py @@ -44,8 +44,8 @@ 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'])} + ending in {fake_payment_method_data['card']['last4']} is not associated with any customer" == str(pm) ) else: @@ -54,8 +54,8 @@ 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'])} + ending in {fake_payment_method_data['card']['last4']} for {customer}" == str(pm) ) @pytest.mark.parametrize("customer_exists", [True, False])