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

Last change on this file since 17421 was 16724, checked in by Henrik Bettermann, 3 years ago

Adjust Interswitch integration.

  • Property svn:keywords set to Id
File size: 9.5 KB
Line 
1## $Id: browser.py 16724 2021-11-25 14:24:37Z 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    module_activated,
23    InterswitchPaymentRequestWebservicePageApplicant,
24    InterswitchPaymentRequestWebservicePageStudent,
25    InterswitchPaymentVerifyWebservicePageApplicant,
26    InterswitchPaymentVerifyWebservicePageStudent,
27    InterswitchPageStudent, InterswitchPageApplicant,
28    )
29from kofacustom.skeleton.students.interfaces import ICustomStudentOnlinePayment
30from kofacustom.skeleton.applicants.interfaces import ICustomApplicantOnlinePayment
31from kofacustom.skeleton.interfaces import MessageFactory as _
32
33PRODUCT_ID = '' # must be provided by Interswitch
34SITE_NAME = 'skeleton.waeup.org'
35PROVIDER_ACCT = '00000000'
36PROVIDER_BANK_ID = '00'
37PROVIDER_ITEM_NAME = 'WAeAC'
38INSTITUTION_NAME = 'SKELETON'
39CURRENCY = '566'
40GATEWAY_AMT = 150.0
41MAC = '' # must be provided by Interswitch
42
43#POST_ACTION = 'https://webpay.interswitchng.com/collections/w/pay'
44POST_ACTION = 'https://sandbox.interswitchng.com/webpay/pay'
45#HOST = 'webpay.interswitchng.com'
46HOST = 'sandbox.interswitchng.com'
47#URL = '/paydirect/api/v1/gettransaction.json'
48URL = '/webpay/api/v1/gettransaction.json'
49
50httplib.HTTPSConnection.debuglevel = 0
51HTTPS = True
52
53class CustomInterswitchPageStudent(InterswitchPageStudent):
54    """ View which sends a POST request to the Interswitch
55    CollegePAY payment gateway.
56    """
57    grok.context(ICustomStudentOnlinePayment)
58    action = POST_ACTION
59    site_name = SITE_NAME
60    currency = CURRENCY
61    product_id = PRODUCT_ID
62    mac = MAC
63    gateway_amt = GATEWAY_AMT
64
65    def update(self):
66        if not module_activated(
67            self.context.student.current_session, self.context):
68            self.flash(_('Forbidden'), type='danger')
69            self.redirect(self.url(self.context, '@@index'))
70            return
71        error = self.init_update()
72        if error:
73            self.flash(error, type='danger')
74            self.redirect(self.url(self.context, '@@index'))
75            return
76        # Already now it becomes an Interswitch payment. We set the net amount
77        # and add the gateway amount.
78        if not self.context.r_company:
79            self.context.net_amt = self.context.amount_auth
80            self.context.amount_auth += self.gateway_amt
81            self.context.gateway_amt = self.gateway_amt
82            self.context.r_company = u'interswitch'
83        student = self.student
84        xmldict = self.xmldict
85        # Provider data
86        xmldict['detail_ref'] = self.context.p_id
87        xmldict['provider_acct'] = PROVIDER_ACCT
88        xmldict['provider_bank_id'] = PROVIDER_BANK_ID
89        xmldict['provider_item_name'] = PROVIDER_ITEM_NAME
90        # Institution data
91        xmldict['institution_acct'] = '00000000'
92        xmldict['institution_bank_id'] = '00'
93        provider_amt = 0.0
94        self.pay_item_id = '0000' # must be provided by Interswitch
95        xmldict['provider_amt'] = 100 * provider_amt
96        xmldict['institution_item_name'] = self.context.category
97        xmldict['institution_name'] = INSTITUTION_NAME
98        xmldict['institution_amt'] = 100 * self.context.net_amt
99        if not self.context.provider_amt:
100            self.context.provider_amt = provider_amt
101            self.context.amount_auth += provider_amt
102        if provider_amt == 0:
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_details>
107</payment_item_detail>""" % xmldict
108        else:
109            xmltext = """<payment_item_detail>
110<item_details detail_ref="%(detail_ref)s" college="%(institution_name)s" department="%(department)s" faculty="%(faculty)s">
111<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" />
112<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" />
113</item_details>
114</payment_item_detail>""" % xmldict
115        self.xml_data = """<input type="hidden" name="xml_data" value='%s'  />""" % xmltext
116        self.context.provider_amt = provider_amt
117        self.amount_auth = int(100 * self.context.amount_auth)
118        hashargs = (
119            self.context.p_id +
120            PRODUCT_ID +
121            self.pay_item_id +
122            str(int(self.amount_auth)) +
123            self.site_redirect_url +
124            self.mac)
125        self.hashvalue = hashlib.sha512(hashargs).hexdigest()
126        return
127
128class CustomInterswitchPageApplicant(InterswitchPageApplicant):
129    """ View which sends a POST request to the Interswitch
130    CollegePAY payment gateway.
131    """
132    grok.context(ICustomApplicantOnlinePayment)
133    action = POST_ACTION
134    site_name = SITE_NAME
135    currency = CURRENCY
136    pay_item_id = '0000' # must be provided by Interswitch
137    product_id = PRODUCT_ID
138    mac = MAC
139    gateway_amt = GATEWAY_AMT
140
141    def update(self):
142        if not module_activated(
143            self.context.__parent__.__parent__.year, self.context):
144            self.flash(_('Forbidden'), type='danger')
145            self.redirect(self.url(self.context, '@@index'))
146            return
147        error = self.init_update()
148        if error:
149            self.flash(error, type='danger')
150            self.redirect(self.url(self.context, '@@index'))
151            return
152        # Already now it becomes an Interswitch payment. We set the net amount
153        # and add the gateway amount.
154        if not self.context.r_company:
155            self.context.net_amt = self.context.amount_auth
156            self.context.amount_auth += self.gateway_amt
157            self.context.gateway_amt = self.gateway_amt
158            self.context.r_company = u'interswitch'
159        xmldict = {}
160        provider_amt = 400.0
161        xmldict['institution_acct'] = '00000000000'
162        xmldict['institution_bank_id'] = '00'
163        xmldict['detail_ref'] = self.context.p_id
164        xmldict['provider_amt'] = 100 * provider_amt
165        xmldict['provider_acct'] = PROVIDER_ACCT
166        xmldict['provider_bank_id'] = PROVIDER_BANK_ID
167        xmldict['provider_item_name'] = PROVIDER_ITEM_NAME
168        xmldict['institution_item_name'] = self.context.category
169        xmldict['institution_name'] = INSTITUTION_NAME
170        xmldict['institution_amt'] = 100 * self.context.net_amt
171        if not self.context.provider_amt:
172            self.context.provider_amt = provider_amt
173            self.context.amount_auth += provider_amt
174        xmltext = """<payment_item_detail>
175<item_details detail_ref="%(detail_ref)s" college="%(institution_name)s">
176<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" />
177<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" />
178</item_details>
179</payment_item_detail>""" % xmldict
180        self.xml_data = """<input type="hidden" name="xml_data" value='%s'  />""" % xmltext
181        self.amount_auth = int(100 * self.context.amount_auth)
182        hashargs = (
183            self.context.p_id +
184            PRODUCT_ID +
185            self.pay_item_id +
186            str(int(self.amount_auth)) +
187            self.site_redirect_url +
188            self.mac)
189        self.hashvalue = hashlib.sha512(hashargs).hexdigest()
190        return
191
192class CustomInterswitchPaymentRequestWebservicePageStudent(
193    InterswitchPaymentRequestWebservicePageStudent):
194    """Request webservice view for the CollegePAY gateway
195    """
196    grok.context(ICustomStudentOnlinePayment)
197    product_id = PRODUCT_ID
198    gateway_host = HOST
199    gateway_url = URL
200    mac = MAC
201
202class CustomInterswitchPaymentVerifyWebservicePageStudent(
203    InterswitchPaymentVerifyWebservicePageStudent):
204    """Payment verify view for the CollegePAY gateway
205    """
206    grok.context(ICustomStudentOnlinePayment)
207    product_id = PRODUCT_ID
208    gateway_host = HOST
209    gateway_url = URL
210    mac = MAC
211
212class CustomInterswitchPaymentRequestWebservicePageApplicant(
213    InterswitchPaymentRequestWebservicePageApplicant):
214    """Request webservice view for the CollegePAY gateway
215    """
216    grok.context(ICustomApplicantOnlinePayment)
217    product_id = PRODUCT_ID
218    gateway_host = HOST
219    gateway_url = URL
220    mac = MAC
221
222class CustomInterswitchPaymentVerifyWebservicePageApplicant(
223    InterswitchPaymentVerifyWebservicePageApplicant):
224    """Payment verify view for the CollegePAY gateway
225    """
226    grok.context(ICustomApplicantOnlinePayment)
227    product_id = PRODUCT_ID
228    gateway_host = HOST
229    gateway_url = URL
230    mac = MAC
Note: See TracBrowser for help on using the repository browser.