diff --git a/hosting/migrations/0044_usercarddetail.py b/hosting/migrations/0044_usercarddetail.py index c2ff25b2..2900a806 100644 --- a/hosting/migrations/0044_usercarddetail.py +++ b/hosting/migrations/0044_usercarddetail.py @@ -1,5 +1,5 @@ # -*- 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 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')), ('last4', models.CharField(max_length=4)), ('brand', models.CharField(max_length=10)), + ('card_id', models.CharField(blank=True, default='', max_length=100)), ('fingerprint', models.CharField(max_length=100)), ('exp_month', models.IntegerField()), ('exp_year', models.IntegerField()), diff --git a/hosting/models.py b/hosting/models.py index ef8b8a74..f7f2fd2a 100644 --- a/hosting/models.py +++ b/hosting/models.py @@ -188,6 +188,7 @@ class UserCardDetail(AssignPermissionsMixin, models.Model): stripe_customer = models.ForeignKey(StripeCustomer) last4 = models.CharField(max_length=4) brand = models.CharField(max_length=10) + card_id = models.CharField(max_length=100, blank=True, default='') fingerprint = models.CharField(max_length=100) exp_month = models.IntegerField(null=False) exp_year = models.IntegerField(null=False) @@ -200,10 +201,11 @@ class UserCardDetail(AssignPermissionsMixin, models.Model): @classmethod 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( 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) return instance diff --git a/hosting/views.py b/hosting/views.py index 71f6cf61..22c5b35e 100644 --- a/hosting/views.py +++ b/hosting/views.py @@ -570,7 +570,13 @@ class SettingsView(LoginRequiredMixin, FormView): try: card = UserCardDetail.objects.get(pk=self.kwargs.get('pk')) if request.user.has_perm(self.permission_required[0], card): - card.delete() + 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() else: msg = _("You are not permitted to do this operation") messages.add_message(request, messages.ERROR, msg) @@ -614,16 +620,20 @@ class SettingsView(LoginRequiredMixin, FormView): _('You seem to have already added this card') ) 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( stripe_customer=stripe_customer, last4=card_details_response['last4'], brand=card_details_response['brand'], fingerprint=card_details_response['fingerprint'], exp_month=card_details_response['exp_month'], - exp_year=card_details_response['exp_year'] - ) - stripe_utils.add_card_to_stripe_customer( - stripe_customer.stripe_id, token + exp_year=card_details_response['exp_year'], + card_id=card_details_response['card_id'] ) return self.render_to_response(self.get_context_data()) else: diff --git a/utils/stripe_utils.py b/utils/stripe_utils.py index 9dd17b63..a7e98a31 100644 --- a/utils/stripe_utils.py +++ b/utils/stripe_utils.py @@ -78,9 +78,15 @@ class StripeUtils(object): customer.source = token customer.save() + @handleStripeError def add_card_to_stripe_customer(self, stripe_customer_id, token): 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 def update_customer_card(self, customer_id, token): @@ -114,7 +120,8 @@ class StripeUtils(object): 'brand': stripe_token.card.brand, 'exp_month': stripe_token.card.exp_month, '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