-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalitorpaymentpage.py
More file actions
105 lines (85 loc) · 3.87 KB
/
Copy pathvalitorpaymentpage.py
File metadata and controls
105 lines (85 loc) · 3.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# -*- coding: utf-8 -*-
from datetime import date, datetime, timedelta
from .errors import ValitorPayException
import uuid
from enum import Enum
import requests
import hashlib
class ValitorPaymentPageClient(object):
def __init__(self, merchant_id, verification_code, testing=True):
self.MERCHANT_ID = merchant_id
self.TESTING = testing
self.VERIFICATION_CODE = verification_code
self._products = []
self.options = {
'AuthorizationOnly': '0',
'ReferenceNumber': '',
'PaymentSuccessfulURL': '',
'PaymentSuccessfulServerSideURL': '',
'Currency': 'ISK',
}
self.set_option('MerchantID', merchant_id)
if not testing:
self.ENDPOINT = 'https://paymentweb.valitor.is'
else:
self.ENDPOINT = 'https://paymentweb.uat.valitor.is'
def format_url(self, path):
return "{}{}".format(self.ENDPOINT, path)
def set_option(self, key, value):
self.options[key] = str(value)
def add_product(self, product):
assert 'Quantity' in product.keys()
assert 'Price' in product.keys()
assert 'Description' in product.keys()
assert 'Discount' in product.keys()
self._products.append({
'Quantity': str(product['Quantity']),
'Price': str(product['Price']),
'Description': str(product['Description']),
'Discount': str(product['Discount']),
})
@property
def products_flat(self):
result = {}
for i, product in enumerate(self._products):
result["Product_{}_Quantity".format(i+1)] = str(product['Quantity'])
result["Product_{}_Price".format(i+1)] = str(product['Price'])
result["Product_{}_Discount".format(i+1)] = str(product['Discount'])
result["Product_{}_Description".format(i+1)] = str(product['Description'])
return result
def generate_signature(self):
m = hashlib.sha256()
m.update(str(self.VERIFICATION_CODE).encode('utf-8'))
m.update(self.options['AuthorizationOnly'].encode('utf-8'))
for product in self._products:
m.update(product['Quantity'].encode('utf-8'))
m.update(product['Price'].encode('utf-8'))
m.update(product['Discount'].encode('utf-8'))
m.update(self.options['MerchantID'].encode('utf-8'))
m.update(self.options['ReferenceNumber'].encode('utf-8'))
m.update(self.options['PaymentSuccessfulURL'].encode('utf-8'))
m.update(self.options['PaymentSuccessfulServerSideURL'].encode('utf-8'))
m.update(self.options['Currency'].encode('utf-8'))
return m.hexdigest()
def verify_signature(self, reference_number, signature_response):
m = hashlib.sha256()
m.update(self.VERIFICATION_CODE.encode('utf-8'))
m.update(reference_number.encode('utf-8'))
signature = m.hexdigest()
return signature == signature_response
def build_form_html(self, button_text="Pay", button_classes=""):
options = ""
for key, value in self.options.items():
options += " <input type=\"hidden\" id=\"{}\" name=\"{}\" value=\"{}\" />\n".format(key, key, value)
for key, value in self.products_flat.items():
options += " <input type=\"hidden\" id=\"{}\" name=\"{}\" value=\"{}\" />\n".format(key, key, value)
if button_classes:
button_classes = " class=\"{}\"".format(button_classes)
form = """
<form action="{}" method="POST">
<input type="hidden" id="DigitalSignature" name="DigitalSignature" value="{}" />
{}
<button type="submit"{}>{}</button>
</form>
""".format(self.ENDPOINT, self.generate_signature(), options, button_classes, button_text)
return form