Fix issues on settings/card save/delete methods

This commit is contained in:
PCoder 2021-01-01 02:37:52 +05:30
parent 31c5336e18
commit 7cd485bc6d
3 changed files with 13 additions and 30 deletions

View file

@ -51,7 +51,7 @@ urlpatterns = [
name='choice_ssh_keys'), name='choice_ssh_keys'),
url(r'delete_ssh_key/(?P<pk>\d+)/?$', SSHKeyDeleteView.as_view(), url(r'delete_ssh_key/(?P<pk>\d+)/?$', SSHKeyDeleteView.as_view(),
name='delete_ssh_key'), name='delete_ssh_key'),
url(r'delete_card/(?P<pk>\d+)/?$', SettingsView.as_view(), url(r'delete_card/(?P<pk>[\w\-]+)/$', SettingsView.as_view(),
name='delete_card'), name='delete_card'),
url(r'create_ssh_key/?$', SSHKeyCreateView.as_view(), url(r'create_ssh_key/?$', SSHKeyCreateView.as_view(),
name='create_ssh_key'), name='create_ssh_key'),

View file

@ -595,33 +595,14 @@ class SettingsView(LoginRequiredMixin, FormView):
messages.add_message(request, messages.SUCCESS, msg) messages.add_message(request, messages.SUCCESS, msg)
return HttpResponseRedirect(reverse_lazy('hosting:settings')) return HttpResponseRedirect(reverse_lazy('hosting:settings'))
if 'delete_card' in request.POST: if 'delete_card' in request.POST:
try: card = self.kwargs.get('pk')
card = UserCardDetail.objects.get(pk=self.kwargs.get('pk')) stripe_utils = StripeUtils()
if (request.user.has_perm(self.permission_required[0], card) stripe_utils.dissociate_customer_card(
and request.user.stripecustomer.stripe_id,
request.user card
.stripecustomer )
.usercarddetail_set msg = _("Card deassociation successful")
.count() > 1): messages.add_message(request, messages.SUCCESS, msg)
if card.card_id is not None:
stripe_utils = StripeUtils()
stripe_utils.dissociate_customer_card(
request.user.stripecustomer.stripe_id,
card.card_id
)
if card.preferred:
UserCardDetail.set_default_card_from_stripe(
request.user.stripecustomer.stripe_id
)
card.delete()
msg = _("Card deassociation successful")
messages.add_message(request, messages.SUCCESS, msg)
else:
msg = _("You are not permitted to do this operation")
messages.add_message(request, messages.ERROR, msg)
except UserCardDetail.DoesNotExist:
msg = _("The selected card does not exist")
messages.add_message(request, messages.ERROR, msg)
return HttpResponseRedirect(reverse_lazy('hosting:settings')) return HttpResponseRedirect(reverse_lazy('hosting:settings'))
form = self.get_form() form = self.get_form()
if form.is_valid(): if form.is_valid():
@ -697,7 +678,7 @@ class SettingsView(LoginRequiredMixin, FormView):
messages.add_message(request, messages.SUCCESS, msg) messages.add_message(request, messages.SUCCESS, msg)
else: else:
# TODO : Test this flow # TODO : Test this flow
id_payment_method = form.cleaned_data.get('id_payment_method') id_payment_method = request.POST.get('id_payment_method', None)
stripe_utils = StripeUtils() stripe_utils = StripeUtils()
card_details = stripe_utils.get_cards_details_from_payment_method( card_details = stripe_utils.get_cards_details_from_payment_method(
id_payment_method id_payment_method

View file

@ -101,7 +101,9 @@ class StripeUtils(object):
if card_id.startswith("pm"): if card_id.startswith("pm"):
logger.debug("PaymentMethod %s detached %s" % (card_id, logger.debug("PaymentMethod %s detached %s" % (card_id,
stripe_customer_id)) stripe_customer_id))
customer.PaymentMethod.detach(card_id) pm = stripe.PaymentMethod.retrieve(card_id)
stripe.PaymentMethod.detach(card_id)
pm.delete()
else: else:
logger.debug("card %s detached %s" % (card_id, stripe_customer_id)) logger.debug("card %s detached %s" % (card_id, stripe_customer_id))
card = customer.sources.retrieve(card_id) card = customer.sources.retrieve(card_id)