Save card id as a parameter and use it to delete a source

This commit is contained in:
PCoder 2017-10-21 13:27:35 +02:00
parent 76b3785adc
commit 70b6bbdf2f
4 changed files with 30 additions and 10 deletions

View file

@ -1,5 +1,5 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# Generated by Django 1.9.4 on 2017-10-15 19:20 # Generated by Django 1.9.4 on 2017-10-21 10:26
from __future__ import unicode_literals from __future__ import unicode_literals
from django.db import migrations, models from django.db import migrations, models
@ -21,6 +21,7 @@ class Migration(migrations.Migration):
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('last4', models.CharField(max_length=4)), ('last4', models.CharField(max_length=4)),
('brand', models.CharField(max_length=10)), ('brand', models.CharField(max_length=10)),
('card_id', models.CharField(blank=True, default='', max_length=100)),
('fingerprint', models.CharField(max_length=100)), ('fingerprint', models.CharField(max_length=100)),
('exp_month', models.IntegerField()), ('exp_month', models.IntegerField()),
('exp_year', models.IntegerField()), ('exp_year', models.IntegerField()),

View file

@ -188,6 +188,7 @@ class UserCardDetail(AssignPermissionsMixin, models.Model):
stripe_customer = models.ForeignKey(StripeCustomer) stripe_customer = models.ForeignKey(StripeCustomer)
last4 = models.CharField(max_length=4) last4 = models.CharField(max_length=4)
brand = models.CharField(max_length=10) brand = models.CharField(max_length=10)
card_id = models.CharField(max_length=100, blank=True, default='')
fingerprint = models.CharField(max_length=100) fingerprint = models.CharField(max_length=100)
exp_month = models.IntegerField(null=False) exp_month = models.IntegerField(null=False)
exp_year = models.IntegerField(null=False) exp_year = models.IntegerField(null=False)
@ -200,10 +201,11 @@ class UserCardDetail(AssignPermissionsMixin, models.Model):
@classmethod @classmethod
def create(cls, stripe_customer=None, last4=None, brand=None, def create(cls, stripe_customer=None, last4=None, brand=None,
fingerprint=None, exp_month=None, exp_year=None): fingerprint=None, exp_month=None, exp_year=None, card_id=None):
instance = cls.objects.create( instance = cls.objects.create(
stripe_customer=stripe_customer, last4=last4, brand=brand, stripe_customer=stripe_customer, last4=last4, brand=brand,
fingerprint=fingerprint, exp_month=exp_month, exp_year=exp_year fingerprint=fingerprint, exp_month=exp_month, exp_year=exp_year,
card_id=card_id
) )
instance.assign_permissions(stripe_customer.user) instance.assign_permissions(stripe_customer.user)
return instance return instance

View file

@ -570,6 +570,12 @@ class SettingsView(LoginRequiredMixin, FormView):
try: try:
card = UserCardDetail.objects.get(pk=self.kwargs.get('pk')) card = UserCardDetail.objects.get(pk=self.kwargs.get('pk'))
if request.user.has_perm(self.permission_required[0], card): if request.user.has_perm(self.permission_required[0], card):
if card.card_id is not None:
stripe_utils = StripeUtils()
stripe_utils.delete_customer_card(
request.user.stripecustomer.stripe_id,
card.card_id
)
card.delete() card.delete()
else: else:
msg = _("You are not permitted to do this operation") msg = _("You are not permitted to do this operation")
@ -614,16 +620,20 @@ class SettingsView(LoginRequiredMixin, FormView):
_('You seem to have already added this card') _('You seem to have already added this card')
) )
except UserCardDetail.DoesNotExist: except UserCardDetail.DoesNotExist:
add_result = stripe_utils.add_card_to_stripe_customer(
stripe_customer.stripe_id, token
)
if add_result.get('error') is not None:
form.add_error("__all__", card_details.get('error'))
return self.render_to_response(self.get_context_data())
UserCardDetail.create( UserCardDetail.create(
stripe_customer=stripe_customer, stripe_customer=stripe_customer,
last4=card_details_response['last4'], last4=card_details_response['last4'],
brand=card_details_response['brand'], brand=card_details_response['brand'],
fingerprint=card_details_response['fingerprint'], fingerprint=card_details_response['fingerprint'],
exp_month=card_details_response['exp_month'], exp_month=card_details_response['exp_month'],
exp_year=card_details_response['exp_year'] exp_year=card_details_response['exp_year'],
) card_id=card_details_response['card_id']
stripe_utils.add_card_to_stripe_customer(
stripe_customer.stripe_id, token
) )
return self.render_to_response(self.get_context_data()) return self.render_to_response(self.get_context_data())
else: else:

View file

@ -78,9 +78,15 @@ class StripeUtils(object):
customer.source = token customer.source = token
customer.save() customer.save()
@handleStripeError
def add_card_to_stripe_customer(self, stripe_customer_id, token): def add_card_to_stripe_customer(self, stripe_customer_id, token):
customer = stripe.Customer.retrieve(stripe_customer_id) customer = stripe.Customer.retrieve(stripe_customer_id)
self.update_customer_token(customer, token) customer.sources.create(source=token)
@handleStripeError
def delete_customer_card(self, stripe_customer_id, card_id):
customer = stripe.Customer.retrieve(stripe_customer_id)
customer.sources.retrieve(card_id).delete()
@handleStripeError @handleStripeError
def update_customer_card(self, customer_id, token): def update_customer_card(self, customer_id, token):
@ -114,7 +120,8 @@ class StripeUtils(object):
'brand': stripe_token.card.brand, 'brand': stripe_token.card.brand,
'exp_month': stripe_token.card.exp_month, 'exp_month': stripe_token.card.exp_month,
'exp_year': stripe_token.card.exp_year, 'exp_year': stripe_token.card.exp_year,
'fingerprint': stripe_token.card.fingerprint 'fingerprint': stripe_token.card.fingerprint,
'card_id': stripe_token.card.id
} }
return card_details return card_details