source: main/kofacustom.nigeria/trunk/src/kofacustom/nigeria/interswitch/tests.py @ 13387

Last change on this file since 13387 was 13387, checked in by Henrik Bettermann, 9 years ago

Implement Interswitch JSON transaction query. This webservice is
automatically used if a mac key is set in the request webservice
view.

  • Property svn:keywords set to Id
File size: 10.7 KB
Line 
1## $Id: tests.py 13387 2015-11-04 09:16:13Z henrik $
2##
3## Copyright (C) 2011 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 os
19from datetime import datetime, timedelta, date
20from zope.component import createObject, getUtility
21from zope.catalog.interfaces import ICatalog
22from hurry.workflow.interfaces import IWorkflowState
23from waeup.kofa.students.tests.test_browser import StudentsFullSetup
24from waeup.kofa.applicants.tests.test_browser import ApplicantsFullSetup
25from waeup.kofa.configuration import SessionConfiguration
26from waeup.kofa.students.payments import StudentOnlinePayment
27from kofacustom.nigeria.interswitch.helpers import query_interswitch
28from kofacustom.nigeria.testing import FunctionalLayer
29
30# Also run tests that send requests to external servers?
31#   If you enable this, please make sure the external services
32#   do exist really and are not bothered by being spammed by a test programme.
33EXTERNAL_TESTS = False
34
35def external_test(func):
36    if not EXTERNAL_TESTS:
37        myself = __file__
38        if myself.endswith('.pyc'):
39            myself = myself[:-2]
40        print "WARNING: external tests are skipped!"
41        print "WARNING: edit %s to enable them." % myself
42        return
43    return func
44
45class InterswitchTestsStudents(StudentsFullSetup):
46    """Tests for the Interswitch payment gateway.
47    """
48
49    layer = FunctionalLayer
50
51    def setUp(self):
52        super(InterswitchTestsStudents, self).setUp()
53        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
54        self.browser.open(self.payments_path)
55        IWorkflowState(self.student).setState('cleared')
56        self.student.nationality = u'NG'
57        self.browser.open(self.payments_path + '/addop')
58        self.browser.getControl(name="form.p_category").value = ['schoolfee']
59        self.browser.getControl("Create ticket").click()
60        self.assertMatches('...ticket created...',
61                           self.browser.contents)
62        ctrl = self.browser.getControl(name='val_id')
63        self.value = ctrl.options[0]
64        self.browser.getLink(self.value).click()
65        self.assertMatches('...Amount Authorized...',
66                           self.browser.contents)
67        self.assertTrue('<span>40000.0</span>', self.browser.contents)
68        self.payment_url = self.browser.url
69        self.payment = self.student['payments'][self.value]
70
71    def test_interswitch_form(self):
72        # Manager can access InterswitchForm
73        self.browser.getLink("CollegePAY", index=0).click()
74        # The total amount to be processed by Interswitch
75        # has been reduced by the Interswitch fee of 150 Nairas
76        self.assertMatches('...<input type="hidden" name="pay_item_id" />...',
77                           self.browser.contents)
78        self.assertMatches('...Total Amount Authorized:...',
79                           self.browser.contents)
80        self.assertEqual(self.student.current_mode, 'ug_ft')
81        self.assertMatches(
82            '...<input type="hidden" name="amount" value="4000000" />...',
83            self.browser.contents)
84
85        # Create school fee ticket for returning students. Payment is made
86        # for next session.
87        current_payment_key = self.student['payments'].keys()[0]
88        self.certificate.study_mode = u'ug_pt'
89        IWorkflowState(self.student).setState('returning')
90        configuration = createObject('waeup.SessionConfiguration')
91        configuration.academic_session = 2005
92        self.app['configuration'].addSessionConfiguration(configuration)
93        self.browser.open(self.payments_path + '/addop')
94        self.browser.getControl(name="form.p_category").value = ['schoolfee']
95        self.browser.getControl("Create ticket").click()
96
97        ctrl = self.browser.getControl(name='val_id')
98        value = ctrl.options[1]
99        self.assertEqual(self.student['payments'][value].provider_amt, 0.0)
100        self.assertEqual(self.student['payments'][value].gateway_amt, 0.0)
101        self.browser.getLink(value).click()
102        self.browser.getLink("CollegePAY", index=0).click()
103        # Split amounts have been set.
104        self.assertEqual(self.student['payments'][value].provider_amt, 0.0)
105        self.assertEqual(self.student['payments'][value].gateway_amt, 0.0)
106        self.assertMatches('...<input type="hidden" name="pay_item_id" />...',
107                           self.browser.contents)
108        self.assertMatches(
109            '...<input type="hidden" name="amount" value="2000000" />...',
110            self.browser.contents)
111
112    def test_interswitch_form_ticket_expired(self):
113        # Manager can access InterswitchForm
114        self.browser.getLink("CollegePAY", index=0).click()
115        self.assertMatches('...<input type="hidden" name="pay_item_id" />...',
116                           self.browser.contents)
117        self.assertMatches('...Total Amount Authorized:...',
118                           self.browser.contents)
119        self.assertEqual(self.student.current_mode, 'ug_ft')
120        self.assertMatches(
121            '...<input type="hidden" name="amount" value="4000000" />...',
122            self.browser.contents)
123        delta = timedelta(days=8)
124        self.payment.creation_date -= delta
125        self.browser.open(self.payment_url)
126        self.browser.getLink("CollegePAY", index=0).click()
127        self.assertMatches(
128            '...This payment ticket is too old. Please create a new ticket...',
129            self.browser.contents)
130        delta = timedelta(days=2)
131        self.payment.creation_date += delta
132        self.browser.open(self.payment_url)
133        self.browser.getLink("CollegePAY", index=0).click()
134        self.assertMatches('...Total Amount Authorized:...',
135                           self.browser.contents)
136
137    @external_test
138    def test_query_interswitch_SOAP(self):
139        host = 'webpay.interswitchng.com'
140        url = '/paydirect/services/TransactionQueryWs.asmx'
141        https = True
142        mac = None
143        product_id = '5845' # AAUE regular
144        payment = StudentOnlinePayment()
145        payment.p_id ='p4465649308559'
146        payment.amount_auth = 60250.0
147        success, msg, log = query_interswitch(
148            payment, product_id, host, url, https, mac)
149        self.assertEqual('Successful callback received', msg)
150        self.assertTrue(success)
151        self.assertTrue(
152            '00:Approved Successful:6025000:3154:p4465649308559:'
153            'ZIB|WEB|ABAL|3-11-2015|021336:000457580882' in log)
154
155    @external_test
156    def test_query_interswitch_JSON(self):
157        host = 'webpay.interswitchng.com'
158        url = '/paydirect/api/v1/gettransaction.json'
159        mac = '9718FA00B0F5070B388A9896ADCED9B2FB02D30F71E12E68BDADC63F6852A3496FF97D8A0F9DA9F753B911A49BB09BB87B55FD02046BD325C74C46C0123CF023'
160        https = True
161        product_id = '5845' # AAUE regular
162        payment = StudentOnlinePayment()
163        payment.p_id ='p4465649308559'
164        payment.amount_auth = 1.0
165        success, msg, log = query_interswitch(
166            payment, product_id, host, url, https, mac)
167        self.assertFalse(success)
168        self.assertTrue('Unsuccessful callback:' in msg)
169        self.assertTrue('Amount Inconsistency' in log)
170        payment.amount_auth = 60250.0
171        success, msg, log = query_interswitch(
172            payment, product_id, host, url, https, mac)
173        self.assertEqual('Successful callback received', msg)
174        self.assertTrue(success)
175        self.assertTrue(
176            "{u'SplitAccounts': [], "
177            "u'MerchantReference': u'p4465649308559', "
178            "u'PaymentReference': u'ZIB|WEB|ABAL|3-11-2015|021336', "
179            "u'TransactionDate': u'2015-11-03T16:40:54.487', "
180            "u'RetrievalReferenceNumber': u'000457580882', "
181            "u'ResponseDescription': u'Approved Successful', "
182            "u'Amount': 6025000, "
183            "u'CardNumber': u'3154', "
184            "u'ResponseCode': u'00', "
185            "u'LeadBankCbnCode': None, "
186            "u'LeadBankName': None}" in log)
187
188class InterswitchTestsApplicants(ApplicantsFullSetup):
189    """Tests for the Interswitch payment gateway.
190    """
191
192    layer = FunctionalLayer
193
194    def setUp(self):
195        super(InterswitchTestsApplicants, self).setUp()
196        configuration = SessionConfiguration()
197        configuration.academic_session = datetime.now().year - 2
198        self.app['configuration'].addSessionConfiguration(configuration)
199        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
200        self.browser.open(self.manage_path)
201        #IWorkflowState(self.student).setState('started')
202        super(InterswitchTestsApplicants, self).fill_correct_values()
203        self.applicantscontainer.application_fee = 1000.0
204        self.browser.getControl(name="form.nationality").value = ['NG']
205        self.browser.getControl(name="transition").value = ['start']
206        self.browser.getControl("Save").click()
207        self.browser.getControl("Add online").click()
208        self.assertMatches('...ticket created...',
209                           self.browser.contents)
210        self.payment = self.applicant.values()[0]
211        self.payment_url = self.browser.url
212
213    def test_interswitch_form(self):
214        self.assertMatches('...Amount Authorized...',
215                           self.browser.contents)
216        self.assertMatches(
217            '...<span>1000.0</span>...',
218            self.browser.contents)
219        # Manager can access InterswitchForm
220        self.browser.getLink("CollegePAY", index=0).click()
221        self.assertMatches('...Total Amount Authorized:...',
222                           self.browser.contents)
223        self.assertMatches(
224            '...<input type="hidden" name="amount" value="100000" />...',
225            self.browser.contents)
226        delta = timedelta(days=8)
227        self.payment.creation_date -= delta
228        self.browser.open(self.payment_url)
229        self.browser.getLink("CollegePAY", index=0).click()
230        self.assertMatches(
231            '...This payment ticket is too old. Please create a new ticket...',
232            self.browser.contents)
233        delta = timedelta(days=2)
234        self.payment.creation_date += delta
235        self.browser.getLink("CollegePAY", index=0).click()
236        self.assertMatches('...Total Amount Authorized:...',
237                           self.browser.contents)
238
Note: See TracBrowser for help on using the repository browser.