From 31c5336e1829171267bdcf24c9715d7f020526c7 Mon Sep 17 00:00:00 2001 From: PCoder Date: Fri, 1 Jan 2021 01:59:41 +0530 Subject: [PATCH] Show cards directly from Stripe and dissociate using PaymentMethod --- hosting/views.py | 6 ++++-- utils/stripe_utils.py | 10 ++++++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/hosting/views.py b/hosting/views.py index 441e33b5..df497a64 100644 --- a/hosting/views.py +++ b/hosting/views.py @@ -565,9 +565,11 @@ class SettingsView(LoginRequiredMixin, FormView): stripe_customer = None if hasattr(user, 'stripecustomer'): stripe_customer = user.stripecustomer - 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, 'stripe_key': settings.STRIPE_API_PUBLIC_KEY diff --git a/utils/stripe_utils.py b/utils/stripe_utils.py index 7ef306bf..09ffc17b 100644 --- a/utils/stripe_utils.py +++ b/utils/stripe_utils.py @@ -98,8 +98,14 @@ class StripeUtils(object): @handleStripeError def dissociate_customer_card(self, stripe_customer_id, card_id): customer = stripe.Customer.retrieve(stripe_customer_id) - card = customer.sources.retrieve(card_id) - card.delete() + if card_id.startswith("pm"): + logger.debug("PaymentMethod %s detached %s" % (card_id, + stripe_customer_id)) + customer.PaymentMethod.detach(card_id) + else: + logger.debug("card %s detached %s" % (card_id, stripe_customer_id)) + card = customer.sources.retrieve(card_id) + card.delete() @handleStripeError def update_customer_card(self, customer_id, token):