Show cards directly from Stripe and dissociate using PaymentMethod

This commit is contained in:
PCoder 2021-01-01 01:59:41 +05:30
parent 36505db5a2
commit 31c5336e18
2 changed files with 12 additions and 4 deletions

View file

@ -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

View file

@ -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):