From e4bfdec0b64eb32263682e80f1657ab1b471ac8b Mon Sep 17 00:00:00 2001
From: PCoder <purple.coder@yahoo.co.uk>
Date: Wed, 3 Oct 2018 09:38:49 +0200
Subject: [PATCH] Update ProductPaymentForm's validation

---
 hosting/forms.py | 26 +++++++++++++++++++++++---
 1 file changed, 23 insertions(+), 3 deletions(-)

diff --git a/hosting/forms.py b/hosting/forms.py
index 91454681..4092f4a1 100644
--- a/hosting/forms.py
+++ b/hosting/forms.py
@@ -99,11 +99,31 @@ class GenericPaymentForm(forms.Form):
 
 class ProductPaymentForm(GenericPaymentForm):
     def __init__(self, *args, **kwargs):
-        super(GenericPaymentForm, self).__init__(*args, **kwargs)
-        self.fields['product_name'].widget = forms.TextInput(
-            attrs={'placeholder': _('Product name'), 'readonly': 'readonly'}
+        product_id = kwargs.pop('product_id', None)
+        if product_id is not None:
+            self.product = GenericProduct.objects.get(id=product_id)
+        super(ProductPaymentForm, self).__init__(*args, **kwargs)
+        self.fields['product_name'] = forms.CharField(
+            widget=forms.TextInput(
+                attrs={'placeholder': _('Product name'),
+                       'readonly': 'readonly' }
+            )
         )
 
+    def clean_amount(self):
+        amount = self.cleaned_data.get('amount')
+        if (self.product is None or
+                float(self.product.get_actual_price()) != amount):
+            raise forms.ValidationError(_("Amount field does not match"))
+        return amount
+
+    def clean_recurring(self):
+        recurring = self.cleaned_data.get('recurring')
+        if (self.product.product_is_subscription !=
+                (True if recurring else False)):
+            raise forms.ValidationError(_("Recurring field does not match"))
+        return recurring
+
 
 class HostingUserSignupForm(forms.ModelForm):
     confirm_password = forms.CharField(label=_("Confirm Password"),