Skip to content

Commit

Permalink
12 - Changing Payment Methods
Browse files Browse the repository at this point in the history
  • Loading branch information
codingforentrepreneurs committed Oct 12, 2017
1 parent 78924c3 commit 90830de
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/billing/models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from django.conf import settings
from django.db import models
from django.db.models.signals import post_save, pre_save

from django.core.urlresolvers import reverse
from accounts.models import GuestEmail
User = settings.AUTH_USER_MODEL

Expand Down Expand Up @@ -52,14 +52,17 @@ def charge(self, order_obj, card=None):
def get_cards(self):
return self.card_set.all()

def get_payment_method_url(self):
return reverse('billing-payment-method')

@property
def has_card(self): # instance.has_card
card_qs = self.get_cards()
return card_qs.exists() # True or False

@property
def default_card(self):
default_cards = self.get_cards().filter(default=True)
default_cards = self.get_cards().filter(active=True, default=True)
if default_cards.exists():
return default_cards.first()
return None
Expand Down Expand Up @@ -128,6 +131,15 @@ def __str__(self):
return "{} {}".format(self.brand, self.last4)


def new_card_post_save_receiver(sender, instance, created, *args, **kwargs):
if instance.default:
billing_profile = instance.billing_profile
qs = Card.objects.filter(billing_profile=billing_profile).exclude(pk=instance.pk)
qs.update(default=False)


post_save.connect(new_card_post_save_receiver, sender=Card)



# stripe.Charge.create(
Expand Down
2 changes: 2 additions & 0 deletions src/carts/templates/carts/checkout.html
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,11 @@ <h1>Finalize Checkout</h1>
<p>Cart Items: {% for product in object.cart.products.all %}{{ product }}{% if not forloop.last %}, {% endif %}{% endfor %}</p>
<p>Shipping Address: {{ object.shipping_address.get_address }}</p>
<p>Billing Address: {{ object.shipping_address.get_address }}</p>
<p>Payment Method: {{ billing_profile.default_card }} (<a href="{{ billing_profile.get_payment_method_url }}?next={{ request.build_absolute_uri }}">Change</a>)</p>
<p>Cart Total: {{ object.cart.total }}</p>
<p>Shipping Total: {{ object.shipping_total }}</p>
<p>Order Total: {{ object.total }}</p>

<form class='form' method='POST' action="">{% csrf_token %}
<button type='submit' class='btn btn-success'>Checkout</button>
</form>
Expand Down
Binary file modified src/db.sqlite3
Binary file not shown.

0 comments on commit 90830de

Please sign in to comment.