From 9ad825ff8302a5338fdfb0d7c866ddea55b3800d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?P=C4=93teris=20Caune?=
Date: Wed, 20 Jul 2016 10:36:30 +0300
Subject: [PATCH] Users can update payment method
---
hc/payments/models.py | 28 +++++++++++++++++
hc/payments/urls.py | 4 +++
hc/payments/views.py | 33 ++++++++++++++++++++
static/js/pricing.js | 31 ++++++++-----------
templates/payments/pricing.html | 54 +++++++++++++++++++++++++++++----
5 files changed, 125 insertions(+), 25 deletions(-)
diff --git a/hc/payments/models.py b/hc/payments/models.py
index 6391bc67..45b095ca 100644
--- a/hc/payments/models.py
+++ b/hc/payments/models.py
@@ -1,3 +1,5 @@
+import braintree
+
from django.contrib.auth.models import User
from django.db import models
@@ -25,3 +27,29 @@ class Subscription(models.Model):
return 20
return 0
+
+ def _get_braintree_payment_method(self):
+ if not hasattr(self, "_pm"):
+ self._pm = braintree.PaymentMethod.find(self.payment_method_token)
+ return self._pm
+
+ def pm_is_credit_card(self):
+ print(self.payment_method_token, self._get_braintree_payment_method())
+ return isinstance(self._get_braintree_payment_method(),
+ braintree.credit_card.CreditCard)
+
+ def pm_is_paypal(self):
+ return isinstance(self._get_braintree_payment_method(),
+ braintree.paypal_account.PayPalAccount)
+
+ def card_type(self):
+ o = self._get_braintree_payment_method()
+ return o.card_type
+
+ def last_4(self):
+ o = self._get_braintree_payment_method()
+ return o.last_4
+
+ def paypal_email(self):
+ o = self._get_braintree_payment_method()
+ return o.email
diff --git a/hc/payments/urls.py b/hc/payments/urls.py
index 882baa26..e55b42a1 100644
--- a/hc/payments/urls.py
+++ b/hc/payments/urls.py
@@ -19,6 +19,10 @@ urlpatterns = [
views.create_plan,
name="hc-create-plan"),
+ url(r'^pricing/update_payment_method/$',
+ views.update_payment_method,
+ name="hc-update-payment-method"),
+
url(r'^pricing/cancel_plan/$',
views.cancel_plan,
name="hc-cancel-plan"),
diff --git a/hc/payments/views.py b/hc/payments/views.py
index e6a3aff1..a981a004 100644
--- a/hc/payments/views.py
+++ b/hc/payments/views.py
@@ -119,6 +119,39 @@ def create_plan(request):
return redirect("hc-pricing")
+@login_required
+@require_POST
+def update_payment_method(request):
+ sub = Subscription.objects.for_user(request.user)
+
+ if not sub.customer_id or not sub.subscription_id:
+ return HttpResponseBadRequest()
+
+ if "payment_method_nonce" not in request.POST:
+ return HttpResponseBadRequest()
+
+ result = braintree.PaymentMethod.create({
+ "customer_id": sub.customer_id,
+ "payment_method_nonce": request.POST["payment_method_nonce"]
+ })
+
+ if not result.is_success:
+ return log_and_bail(request, result)
+
+ payment_method_token = result.payment_method.token
+ result = braintree.Subscription.update(sub.subscription_id, {
+ "payment_method_token": payment_method_token
+ })
+
+ if not result.is_success:
+ return log_and_bail(request, result)
+
+ sub.payment_method_token = payment_method_token
+ sub.save()
+
+ return redirect("hc-pricing")
+
+
@login_required
@require_POST
def cancel_plan(request):
diff --git a/static/js/pricing.js b/static/js/pricing.js
index 28fc8d8b..24cde955 100644
--- a/static/js/pricing.js
+++ b/static/js/pricing.js
@@ -1,24 +1,7 @@
$(function () {
- var prices = [2, 5, 10, 15, 20, 25, 50, 100];
- var initialPrice = parseInt($("#pricing-value").text());
- var priceIdx = prices.indexOf(initialPrice);
-
- function updateDisplayPrice(price) {
- $("#pricing-value").text(price);
- $("#pww-switch-btn").text("Switch to $" + price + " / mo");
-
- if (price == initialPrice) {
- $("#pww-selected-btn").show();
- $("#pww-switch-btn").hide();
- } else {
- $("#pww-selected-btn").hide();
- $("#pww-switch-btn").show();
- }
- }
$(".btn-create-payment-method").click(function() {
var planId = $(this).data("plan-id");
- console.log(planId);
$("#plan_id").val(planId);
$.getJSON("/pricing/get_client_token/", function(data) {
var $modal = $("#payment-method-modal");
@@ -29,8 +12,18 @@ $(function () {
})
});
- $("#payment-method-cancel").click(function() {
- location.reload();
+ $(".btn-update-payment-method").click(function() {
+ $.getJSON("/pricing/get_client_token/", function(data) {
+ var $modal = $("#update-payment-method-modal");
+ braintree.setup(data.client_token, "dropin", {
+ container: "update-payment-form"
+ });
+ $modal.modal("show");
+ })
});
+ $(".pm-modal").on("hidden.bs.modal", function() {
+ location.reload();
+ })
+
});
\ No newline at end of file
diff --git a/templates/payments/pricing.html b/templates/payments/pricing.html
index 7bd576a3..7ea9ba28 100644
--- a/templates/payments/pricing.html
+++ b/templates/payments/pricing.html
@@ -28,12 +28,29 @@
{% if first_charge %}
Success! You just paid ${{ sub.price }}.
{% else %}
- You are currently paying ${{ sub.price }}/month.
+ You are currently paying ${{ sub.price }}/month
+
+ {% if sub.pm_is_credit_card %}
+ using {{ sub.card_type }} card
+ ending with {{ sub.last_4 }}.
+ {% endif %}
+
+ {% if sub.pm_is_paypal %}
+ using PayPal account
+ {{ sub.paypal_email }}.
+ {% endif %}
+
{% endif %}
- See Billing History.
+ Thank you for supporting healthchecks.io!
+
- Thank you for supporting healthchecks.io!
+ See Billing History
+ {% if not first_charge %}
+
+ {% endif %}
@@ -232,7 +249,7 @@
-
+
-
+
+
{% endblock %}
{% block scripts %}