From a823efd8e28376f9651c10746929616af46601c4 Mon Sep 17 00:00:00 2001 From: PCoder Date: Thu, 31 Dec 2020 20:01:52 +0530 Subject: [PATCH] Add get_available_payment_methods --- datacenterlight/views.py | 6 ++++-- utils/stripe_utils.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/datacenterlight/views.py b/datacenterlight/views.py index 44e871a5..3b05bd73 100644 --- a/datacenterlight/views.py +++ b/datacenterlight/views.py @@ -263,9 +263,11 @@ class PaymentOrderView(FormView): stripe_customer = user.stripecustomer else: stripe_customer = None - cards_list = UserCardDetail.get_all_cards_list( - stripe_customer=stripe_customer + stripe_utils = StripeUtils() + cards_list_request = stripe_utils.get_available_payment_methods( + stripe_customer ) + cards_list = cards_list_request.get('response_object') context.update({'cards_list': cards_list}) else: billing_address_form = BillingAddressFormSignup( diff --git a/utils/stripe_utils.py b/utils/stripe_utils.py index a4cc2c6a..e7e8d9af 100644 --- a/utils/stripe_utils.py +++ b/utils/stripe_utils.py @@ -528,6 +528,34 @@ class StripeUtils(object): ) return payment_intent_obj + @handleStripeError + def get_available_payment_methods(self, customer): + """ Retrieves all payment methods of the given customer + :param customer: StripeCustomer object + :return: a list of available payment methods + """ + return_list = [] + if customer is None: + return return_list + cu = stripe.Customer.retrieve(customer.stripe_id) + pms = stripe.PaymentMethod.list( + customer=customer.stripe_id, + type="card", + ) + default_source = None + if cu.default_source: + default_source = cu.default_source + else: + default_source = cu.invoice_settings.default_payment_method + for pm in pms.data: + return_list.append({ + 'last4': pm.card.last4, 'brand': pm.card.brand, 'id': pm.id, + 'exp_year': pm.card.exp_year, + 'exp_month': '{:02d}'.format(pm.card.exp_month), + 'preferred': pm.id == default_source + }) + return return_list + def compare_vat_numbers(self, vat1, vat2): _vat1 = vat1.replace(" ", "").replace(".", "").replace("-","") _vat2 = vat2.replace(" ", "").replace(".", "").replace("-","")