added shopid
[indico-PayWay] / MaKaC / plugins / EPayment / payWay / epayment.py
1 # -*- coding: utf-8 -*-
2 ##
3 ##
4 ## This file is part of Indico.
5 ## Copyright (C) 2002 - 2014 European Organization for Nuclear Research (CERN).
6 ##
7 ## Indico is free software; you can redistribute it and/or
8 ## modify it under the terms of the GNU General Public License as
9 ## published by the Free Software Foundation; either version 3 of the
10 ## License, or (at your option) any later version.
11 ##
12 ## Indico is distributed in the hope that it will be useful, but
13 ## WITHOUT ANY WARRANTY; without even the implied warranty of
14 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 ## General Public License for more details.
16 ##
17 ## You should have received a copy of the GNU General Public License
18 ## along with Indico;if not, see <http://www.gnu.org/licenses/>.
19
20 from MaKaC.epayment import BaseEPayMod, BaseTransaction
21 import MaKaC.webinterface.urlHandlers as urlHandlers
22 from MaKaC.webinterface.common.tools import strip_ml_tags
23
24
25 from MaKaC.plugins.EPayment.payWay.webinterface import urlHandlers as localUrlHandlers
26 from MaKaC.plugins.EPayment.payWay import MODULE_ID
27
28 import unicodedata
29 import hashlib
30
31 class PayWayMod(BaseEPayMod):
32
33     def __init__(self, data=None):
34         BaseEPayMod.__init__(self)
35         self._title = "payway"
36
37         self._url="https://pgwtest.ht.hr/services/payment/api/authorize-form"
38         self._business="please fill in private key here"
39         self._shopid="please fill in shop id here"
40
41         if data is not None:
42             setValue(data)
43
44     def getId(self):
45         return MODULE_ID
46
47     def clone(self, newSessions):
48         sesf = PayWayMod()
49         sesf.setTitle(self.getTitle())
50         sesf.setUrl(self.getUrl())
51         sesf.setBusiness(self.getBusiness())
52
53         return sesf
54
55     def setValues(self, data):
56         self.setTitle(data.get("title", "epayment"))
57         self.setUrl(data.get("url", ""))
58         self.setBusiness(data["business"])
59         self.setShopID(data["shopid"])
60
61     def getUrl(self):
62         return self._url
63     def setUrl(self,url):
64         self._url=url
65
66     def getBusiness(self):
67         return self._business
68     def setBusiness(self,business):
69         self._business= business
70
71     def getShopID(self):
72         if hasattr(self, '_shopid'):
73             return self._shopid
74         else:
75             self._shopid = "please fill in shop id"
76             return self._shopid
77     def setShopID(self,shopid):
78         self._shopid= shopid
79
80     def strip_accents(self,text):
81         """
82         Strip accents from input String.
83     
84         :param text: The input string.
85         :type text: String.
86     
87         :returns: The processed String.
88         :rtype: String.
89         """
90         try:
91             text = unicode(text, 'utf-8')
92         except NameError: # unicode is a default on python 3 
93             pass
94         text = unicodedata.normalize('NFD', text)
95         text = text.encode('ascii', 'ignore')
96         text = text.decode("utf-8")
97         return str(text)
98
99     def getFormHTML(self,prix,Currency,conf,registrant,lang = "en_GB", secure=False):
100
101         pgw_shop_id=str(self._shopid)
102         pgw_order_id=str(registrant.getId())
103         pgw_amount=str(int(prix * 100))
104
105         pgw_success_url=str(localUrlHandlers.UHPayConfirmPayWay.getURL(registrant))
106         pgw_failure_url=str(localUrlHandlers.UHPayCancelPayWay.getURL(registrant))
107         url_notify=localUrlHandlers.UHPayParamsPayWay.getURL(registrant)
108
109         pgw_first_name=self.strip_accents(registrant.getFirstName())
110         pgw_last_name=self.strip_accents(registrant.getSurName())
111
112         pgw_telephone=registrant.getPhone()
113         pgw_email=registrant.getEmail()
114
115         secret_key=self.getBusiness()
116         pgw_signature= \
117 'authorize-form' + secret_key + \
118 pgw_shop_id + secret_key + \
119 pgw_order_id + secret_key + \
120 pgw_amount + secret_key + \
121 '0' + secret_key + \
122 'en' + secret_key + \
123 pgw_success_url + secret_key + \
124 pgw_failure_url + secret_key + \
125 pgw_first_name + secret_key + \
126 pgw_last_name + secret_key + \
127 pgw_telephone + secret_key + \
128 pgw_email + secret_key
129
130         """
131 gnature = SHA512 (
132 naziv_metode + secret_key +
133 pgw_shop_id + secret_key +
134 pgw_order_id + secret_key +
135 pgw_amount + secret_key +
136 #pgw_installments + secret_key +
137 pgw_authorization_type + secret_key +
138 #pgw_authorization_token + secret_key +
139 pgw_language + secret_key +
140 #pgw_return_method + secret_key +
141 pgw_success_url + secret_key +
142 pgw_failure_url + secret_key +
143 #pgw_card_number + secret_key +
144 #pgw_card_expiration_date + secret_key +
145 #pgw_card_verification_data + secret_key +
146 pgw_first_name + secret_key +
147 pgw_last_name + secret_key +
148 #pgw_street + secret_key +
149 #pgw_city + secret_key +
150 #pgw_post_code + secret_key +
151 #pgw_country + secret_key +
152 pgw_telephone + secret_key +
153 pgw_email + secret_key +
154 #pgw_merchant_data + secret_key
155 """
156
157         pgw_signature=hashlib.sha512(pgw_signature).hexdigest()
158
159         s="""<form name="Button" action="%s" method="post" id="%s">
160              <input type="hidden" name="pgw_shop_id" value="%s" />
161              <input type="hidden" name="pgw_order_id" value="%s" />
162              <input type="hidden" name="pgw_amount" value="%s" />
163
164              <input type="hidden" name="pgw_authorization_type" value="0" />
165              <input type="hidden" name="pgw_language" value="en" />
166
167              <input type="hidden" name="pgw_success_url" value="%s" />
168              <input type="hidden" name="pgw_failure_url" value="%s" />
169
170              <input type="hidden" name="pgw_first_name" value="%s" />
171              <input type="hidden" name="pgw_last_name" value="%s" />
172
173              <input type="hidden" name="pgw_telephone" value="%s" />
174              <input type="hidden" name="pgw_email" value="%s" />
175              
176              <input type="hidden" name="pgw_signature" value="%s" />
177
178              </form>""" % (self.getUrl(), self.getId(), \
179                 pgw_shop_id, pgw_order_id, pgw_amount, \
180                 pgw_success_url, pgw_failure_url, \
181                 pgw_first_name, pgw_last_name, pgw_telephone, pgw_email, \
182                 pgw_signature)
183
184         return s
185
186     def getConfModifEPaymentURL(self, conf):
187         return localUrlHandlers.UHConfModifEPaymentPayWay.getURL(conf)
188
189
190
191 class TransactionPayWay(BaseTransaction):
192
193     def __init__(self,parms):
194         BaseTransaction.__init__(self)
195         self._Data=parms
196
197
198     def getId(self):
199         try:
200             if self._id:
201                 pass
202         except AttributeError, e:
203             self._id="payway"
204         return self._id
205
206     def getTransactionHTML(self):
207         return"""<table>
208                           <tr>
209                             <td align="right"><b>Payment with:</b></td>
210                             <td align="left">PayWay</td>
211                           </tr>
212                           <tr>
213                             <td align="right"><b>Payment Date:</b></td>
214                             <td align="left">%s</td>
215                           </tr>
216                           <tr>
217                             <td align="right"><b>Payment ID:</b></td>
218                             <td align="left">%s</td>
219                           </tr>
220                           <tr>
221                             <td align="right"><b>Order Total:</b></td>
222                             <td align="left">%s %s</td>
223                           </tr>
224                           <tr>
225                             <td align="right"><b>verify sign:</b></td>
226                             <td align="left">%s</td>
227                           </tr>
228                         </table>"""%(self._Data["payment_date"],self._Data["payer_id"], self._Data["mc_gross"], \
229                              self._Data["mc_currency"], self._Data["verify_sign"])
230     def getTransactionTxt(self):
231         return"""
232 \tPayment with:PayWay\n
233 \tPayment Date:%s\n
234 \tPayment ID:%s\n
235 \tOrder Total:%s %s\n
236 \tverify sign:%s
237 """%(self._Data["payment_date"],self._Data["payer_id"], self._Data["mc_gross"], \
238                              self._Data["mc_currency"], self._Data["verify_sign"])
239
240
241
242 def getPayMod():
243     return PayWayMod()
244
245 def getPayModClass():
246     return PayWayMod