source: main/kofacustom.skeleton/trunk/src/kofacustom/skeleton/interswitch/browser.py @ 16326

Last change on this file since 16326 was 15756, checked in by Henrik Bettermann, 5 years ago

Adjust to changes in nigeria package.

File size: 9.0 KB
Line 
1## $Id: browser.py 15269 2018-12-17 16:55:38Z henrik $
2##
3## Copyright (C) 2012 Uli Fouquet & Henrik Bettermann
4## This program is free software; you can redistribute it and/or modify
5## it under the terms of the GNU General Public License as published by
6## the Free Software Foundation; either version 2 of the License, or
7## (at your option) any later version.
8##
9## This program is distributed in the hope that it will be useful,
10## but WITHOUT ANY WARRANTY; without even the implied warranty of
11## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12## GNU General Public License for more details.
13##
14## You should have received a copy of the GNU General Public License
15## along with this program; if not, write to the Free Software
16## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17##
18import httplib
19import hashlib
20import grok
21from kofacustom.nigeria.interswitch.browser import (
22    InterswitchPaymentRequestWebservicePageApplicant,
23    InterswitchPaymentRequestWebservicePageStudent,
24    InterswitchPaymentVerifyWebservicePageApplicant,
25    InterswitchPaymentVerifyWebservicePageStudent,
26    InterswitchPageStudent, InterswitchPageApplicant,
27    )
28from kofacustom.skeleton.students.interfaces import ICustomStudentOnlinePayment
29from kofacustom.skeleton.applicants.interfaces import ICustomApplicantOnlinePayment
30from kofacustom.skeleton.interfaces import MessageFactory as _
31
32PRODUCT_ID = '' # must be provided by Interswitch
33SITE_NAME = 'skeleton-kofa.waeup.org'
34PROVIDER_ACCT = '00000000'
35PROVIDER_BANK_ID = '00'
36PROVIDER_ITEM_NAME = 'BT Education'
37INSTITUTION_NAME = 'Skeleton'
38CURRENCY = '566'
39GATEWAY_AMT = 150.0
40MAC = '' # must be provided by Interswitch
41
42#POST_ACTION = 'https://webpay.interswitchng.com/paydirect/pay'
43POST_ACTION = 'https://sandbox.interswitchng.com/webpay/pay'
44#HOST = 'webpay.interswitchng.com'
45HOST = 'sandbox.interswitchng.com'
46#URL = '/paydirect/api/v1/gettransaction.json'
47URL = '/webpay/api/v1/gettransaction.json'
48
49httplib.HTTPSConnection.debuglevel = 0
50HTTPS = True
51
52class CustomInterswitchPageStudent(InterswitchPageStudent):
53    """ View which sends a POST request to the Interswitch
54    CollegePAY payment gateway.
55    """
56    grok.context(ICustomStudentOnlinePayment)
57    action = POST_ACTION
58    site_name = SITE_NAME
59    currency = CURRENCY
60    product_id = PRODUCT_ID
61    mac = MAC
62    gateway_amt = GATEWAY_AMT
63
64    def update(self):
65        error = self.init_update()
66        if error:
67            self.flash(error, type='danger')
68            self.redirect(self.url(self.context, '@@index'))
69            return
70        # Already now it becomes an Interswitch payment. We set the net amount
71        # and add the gateway amount.
72        if not self.context.r_company:
73            self.context.net_amt = self.context.amount_auth
74            self.context.amount_auth += self.gateway_amt
75            self.context.gateway_amt = self.gateway_amt
76            self.context.r_company = u'interswitch'
77        student = self.student
78        xmldict = self.xmldict
79        # Provider data
80        xmldict['detail_ref'] = self.context.p_id
81        xmldict['provider_acct'] = PROVIDER_ACCT
82        xmldict['provider_bank_id'] = PROVIDER_BANK_ID
83        xmldict['provider_item_name'] = PROVIDER_ITEM_NAME
84        # Institution data
85        xmldict['institution_acct'] = '00000000'
86        xmldict['institution_bank_id'] = '00'
87        provider_amt = 0.0
88        self.pay_item_id = '0000' # must be provided by Interswitch
89        xmldict['provider_amt'] = 100 * provider_amt
90        xmldict['institution_item_name'] = self.context.category
91        xmldict['institution_name'] = INSTITUTION_NAME
92        xmldict['institution_amt'] = 100 * self.context.net_amt
93        if not self.context.provider_amt:
94            self.context.provider_amt = provider_amt
95            self.context.amount_auth += provider_amt
96        if provider_amt == 0:
97            xmltext = """<payment_item_detail>
98<item_details detail_ref="%(detail_ref)s" college="%(institution_name)s" department="%(department)s" faculty="%(faculty)s">
99<item_detail item_id="1" item_name="%(institution_item_name)s" item_amt="%(institution_amt)d" bank_id="%(institution_bank_id)s" acct_num="%(institution_acct)s" />
100</item_details>
101</payment_item_detail>""" % xmldict
102        else:
103            xmltext = """<payment_item_detail>
104<item_details detail_ref="%(detail_ref)s" college="%(institution_name)s" department="%(department)s" faculty="%(faculty)s">
105<item_detail item_id="1" item_name="%(institution_item_name)s" item_amt="%(institution_amt)d" bank_id="%(institution_bank_id)s" acct_num="%(institution_acct)s" />
106<item_detail item_id="2" item_name="%(provider_item_name)s" item_amt="%(provider_amt)d" bank_id="%(provider_bank_id)s" acct_num="%(provider_acct)s" />
107</item_details>
108</payment_item_detail>""" % xmldict
109        self.xml_data = """<input type="hidden" name="xml_data" value='%s'  />""" % xmltext
110        self.context.provider_amt = provider_amt
111        self.amount_auth = int(100 * self.context.amount_auth)
112        hashargs = (
113            self.context.p_id +
114            PRODUCT_ID +
115            self.pay_item_id +
116            str(int(self.amount_auth)) +
117            self.site_redirect_url +
118            self.mac)
119        self.hashvalue = hashlib.sha512(hashargs).hexdigest()
120        return
121
122class CustomInterswitchPageApplicant(InterswitchPageApplicant):
123    """ View which sends a POST request to the Interswitch
124    CollegePAY payment gateway.
125    """
126    grok.context(ICustomApplicantOnlinePayment)
127    action = POST_ACTION
128    site_name = SITE_NAME
129    currency = CURRENCY
130    pay_item_id = '0000' # must be provided by Interswitch
131    product_id = PRODUCT_ID
132    mac = MAC
133    gateway_amt = GATEWAY_AMT
134
135    def update(self):
136        error = self.init_update()
137        if error:
138            self.flash(error, type='danger')
139            self.redirect(self.url(self.context, '@@index'))
140            return
141        # Already now it becomes an Interswitch payment. We set the net amount
142        # and add the gateway amount.
143        if not self.context.r_company:
144            self.context.net_amt = self.context.amount_auth
145            self.context.amount_auth += self.gateway_amt
146            self.context.gateway_amt = self.gateway_amt
147            self.context.r_company = u'interswitch'
148        xmldict = {}
149        provider_amt = 400.0
150        xmldict['institution_acct'] = '00000000000'
151        xmldict['institution_bank_id'] = '00'
152        xmldict['detail_ref'] = self.context.p_id
153        xmldict['provider_amt'] = 100 * provider_amt
154        xmldict['provider_acct'] = PROVIDER_ACCT
155        xmldict['provider_bank_id'] = PROVIDER_BANK_ID
156        xmldict['provider_item_name'] = PROVIDER_ITEM_NAME
157        xmldict['institution_item_name'] = self.context.category
158        xmldict['institution_name'] = INSTITUTION_NAME
159        xmldict['institution_amt'] = 100 * self.context.net_amt
160        if not self.context.provider_amt:
161            self.context.provider_amt = provider_amt
162            self.context.amount_auth += provider_amt
163        xmltext = """<payment_item_detail>
164<item_details detail_ref="%(detail_ref)s" college="%(institution_name)s">
165<item_detail item_id="1" item_name="%(institution_item_name)s" item_amt="%(institution_amt)d" bank_id="%(institution_bank_id)s" acct_num="%(institution_acct)s" />
166<item_detail item_id="2" item_name="%(provider_item_name)s" item_amt="%(provider_amt)d" bank_id="%(provider_bank_id)s" acct_num="%(provider_acct)s" />
167</item_details>
168</payment_item_detail>""" % xmldict
169        self.xml_data = """<input type="hidden" name="xml_data" value='%s'  />""" % xmltext
170        self.amount_auth = int(100 * self.context.amount_auth)
171        hashargs = (
172            self.context.p_id +
173            PRODUCT_ID +
174            self.pay_item_id +
175            str(int(self.amount_auth)) +
176            self.site_redirect_url +
177            self.mac)
178        self.hashvalue = hashlib.sha512(hashargs).hexdigest()
179        return
180
181class CustomInterswitchPaymentRequestWebservicePageStudent(
182    InterswitchPaymentRequestWebservicePageStudent):
183    """Request webservice view for the CollegePAY gateway
184    """
185    grok.context(ICustomStudentOnlinePayment)
186    product_id = PRODUCT_ID
187    gateway_host = HOST
188    gateway_url = URL
189    mac = MAC
190
191class CustomInterswitchPaymentVerifyWebservicePageStudent(
192    InterswitchPaymentVerifyWebservicePageStudent):
193    """Payment verify view for the CollegePAY gateway
194    """
195    grok.context(ICustomStudentOnlinePayment)
196    product_id = PRODUCT_ID
197    gateway_host = HOST
198    gateway_url = URL
199    mac = MAC
200
201class CustomInterswitchPaymentRequestWebservicePageApplicant(
202    InterswitchPaymentRequestWebservicePageApplicant):
203    """Request webservice view for the CollegePAY gateway
204    """
205    grok.context(ICustomApplicantOnlinePayment)
206    product_id = PRODUCT_ID
207    gateway_host = HOST
208    gateway_url = URL
209    mac = MAC
210
211class CustomInterswitchPaymentVerifyWebservicePageApplicant(
212    InterswitchPaymentVerifyWebservicePageApplicant):
213    """Payment verify view for the CollegePAY gateway
214    """
215    grok.context(ICustomApplicantOnlinePayment)
216    product_id = PRODUCT_ID
217    gateway_host = HOST
218    gateway_url = URL
219    mac = MAC
Note: See TracBrowser for help on using the repository browser.