From f5d3bd6261dbb10d757fd40a003ed61b2804d189 Mon Sep 17 00:00:00 2001
From: frekk <frekk@ucc.asn.au>
Date: Mon, 28 Jan 2019 13:59:50 +0800
Subject: [PATCH] fixed bugs

---
 src/squarepay/models.py   |  7 ++++---
 src/squarepay/payments.py |  1 -
 src/squarepay/views.py    | 11 ++++++-----
 3 files changed, 10 insertions(+), 9 deletions(-)

diff --git a/src/squarepay/models.py b/src/squarepay/models.py
index d972fa4..ab2248e 100644
--- a/src/squarepay/models.py
+++ b/src/squarepay/models.py
@@ -2,6 +2,7 @@ import uuid
 
 from django.db import models
 from django.urls import reverse
+from django.utils import timezone
 
 from memberdb.models import Membership, make_token
 
@@ -15,9 +16,9 @@ class CardPayment(models.Model):
     date_paid       = models.DateTimeField('Date paid (payment captured)', null=True, blank=True)
 
     def set_paid(self):
-        card_payment.is_paid = True
-        card_payment.date_paid = timezone.now()
-        card_payment.save()
+        self.is_paid = True
+        self.date_paid = timezone.now()
+        self.save()
 
 class MembershipPayment(CardPayment):
     """
diff --git a/src/squarepay/payments.py b/src/squarepay/payments.py
index 2b7bfca..74c15cf 100644
--- a/src/squarepay/payments.py
+++ b/src/squarepay/payments.py
@@ -5,7 +5,6 @@ import logging
 
 from django.conf import settings
 from django.core.exceptions import ImproperlyConfigured
-from django.utils import timezone
 
 import squareconnect
 from squareconnect.rest import ApiException
diff --git a/src/squarepay/views.py b/src/squarepay/views.py
index ffcc260..54098a2 100644
--- a/src/squarepay/views.py
+++ b/src/squarepay/views.py
@@ -31,11 +31,12 @@ class PaymentFormMixin:
         return context
 
     def payment_success(self, payment):
-        set_paid(payment)
-        messages.success(request, "Your payment of $%1.2f was successful." % amount_aud)
+        payment.set_paid()
+        messages.success(self.request, "Your payment of $%1.2f was successful." % (payment.amount / 100.0))
 
     def payment_error(self, payment):
-        messages.error(request, "Your payment of $%1.2f was unsuccessful. Please try again later." % amount_aud)
+        messages.error(self.request, "Your payment of $%1.2f was unsuccessful. Please try again later." % (payment.amount / 100.0))
+        payment.delete()
 
     def post(self, request, *args, **kwargs):
         nonce = request.POST.get('nonce', None)
@@ -47,9 +48,9 @@ class PaymentFormMixin:
             return self.get(request)
 
         if try_capture_payment(card_payment, nonce):
-            payment_success(card_payment)
+            self.payment_success(card_payment)
         else:
-            payment_error(card_payment)
+            self.payment_error(card_payment)
 
         # redirect to success URL, or redisplay the form with a success message if none is given
         return HttpResponseRedirect(self.get_completed_url())
-- 
GitLab