1 | ## Script (Python) "interswitch_cb" |
---|
2 | ##bind container=container |
---|
3 | ##bind context=context |
---|
4 | ##bind namespace= |
---|
5 | ##bind script=script |
---|
6 | ##bind subpath=traverse_subpath |
---|
7 | ##parameters= |
---|
8 | ##title= |
---|
9 | ## |
---|
10 | # $Id: interswitch_cb.py 6984 2011-11-01 11:37:59Z henrik $ |
---|
11 | """ |
---|
12 | payment callback |
---|
13 | """ |
---|
14 | try: |
---|
15 | from Products.zdb import set_trace |
---|
16 | except: |
---|
17 | def set_trace(): |
---|
18 | pass |
---|
19 | import logging |
---|
20 | logger = logging.getLogger('Skins.interswitch_cb') |
---|
21 | from AccessControl import Unauthorized |
---|
22 | import DateTime |
---|
23 | |
---|
24 | if context.portal_membership.isAnonymousUser(): |
---|
25 | return None |
---|
26 | |
---|
27 | request = context.REQUEST |
---|
28 | students = context.portal_url.getPortalObject().campus.students |
---|
29 | wftool = context.portal_workflow |
---|
30 | mtool = context.portal_membership |
---|
31 | member = mtool.getAuthenticatedMember() |
---|
32 | member_id = str(member) |
---|
33 | #student_id = context.getStudentId() |
---|
34 | access_info = context.waeup_tool.getAccessInfo(context) |
---|
35 | student_id = access_info['student_id'] |
---|
36 | if not context.isSectionOfficer() and (student_id is None or student_id != member_id): |
---|
37 | logger.info('%s tried to access payment object of %s' % (member_id,student_id)) |
---|
38 | referer = request.get('HTTP_REFERER','none') |
---|
39 | logger.info('%s:%s illegal access, referer = %s' % (member_id,student_id,referer)) |
---|
40 | real_ip = request.get('HTTP_X_REAL_IP',"none") |
---|
41 | logger.info('%s:%s illegal access, real_x_ip = %s' % (member_id,student_id,real_ip)) |
---|
42 | return context.REQUEST.RESPONSE.redirect("%s/srp_anonymous_view" % context.portal_url()) |
---|
43 | |
---|
44 | referer = request.get('HTTP_REFERER','none') |
---|
45 | real_ip = request.get('HTTP_X_REAL_IP',"none") |
---|
46 | logger.info('%s, callback referer = %s, IP = %s' % (student_id,referer,real_ip)) |
---|
47 | |
---|
48 | ontest = False |
---|
49 | |
---|
50 | if not ontest and not 'webpay.interswitchng.com' in referer and not 'waeup.org' in referer: |
---|
51 | logger.info('%s, wrong callback referer %s, callback rejected, IP = %s' % (student_id,referer,real_ip)) |
---|
52 | return request.RESPONSE.redirect("%s/waeup_document_view" % context.absolute_url()) |
---|
53 | |
---|
54 | student = getattr(students,student_id) |
---|
55 | resp_codes = (("desc","resp_desc"), |
---|
56 | ("resp","resp_code"), |
---|
57 | ("txnRef","pay_reference"), |
---|
58 | ("payRef","resp_pay_reference"), |
---|
59 | ("retRef","retRef"), |
---|
60 | ("cardNum","resp_card_num"), |
---|
61 | ("apprAmt","resp_approved_amount"), |
---|
62 | ) |
---|
63 | pd = {} |
---|
64 | for rc,pdk in resp_codes: |
---|
65 | pd[pdk] = request.get(rc,'') |
---|
66 | |
---|
67 | if ontest: |
---|
68 | pd['resp_desc'] = 'Simulated Callback' |
---|
69 | pd['resp_pay_reference'] = 'XXXX' |
---|
70 | pd['resp_code'] = '00' |
---|
71 | pd['resp_card_num'] = '0000' |
---|
72 | pd['resp_approved_amount'] = '4680000' |
---|
73 | |
---|
74 | if pd['resp_code'] == '00' and len(pd['resp_approved_amount']) > 4: |
---|
75 | pd['resp_approved_amount'] = pd['resp_approved_amount'][:-2] |
---|
76 | pd['status'] = 'paid' |
---|
77 | else: |
---|
78 | pd['resp_approved_amount'] = '0' |
---|
79 | pd['status'] = 'failed' |
---|
80 | |
---|
81 | review_state = wftool.getInfoFor(context,'review_state',None) |
---|
82 | if pd['resp_code'] == '': |
---|
83 | logger.info('%s requeried payment %s for %s and got empty response' % (member,context.getId(),student_id)) |
---|
84 | return request.RESPONSE.redirect("%s/waeup_document_view" % context.absolute_url()) |
---|
85 | if access_info['is_student'] and review_state == 'closed': |
---|
86 | wftool.doActionFor(context,'open') |
---|
87 | pay_doc = context.getContent() |
---|
88 | resp = pd['resp_code'] |
---|
89 | |
---|
90 | if resp == '00': |
---|
91 | try: |
---|
92 | amount = int(getattr(pay_doc,'amount',0)) |
---|
93 | try: |
---|
94 | surcharge = int(getattr(pay_doc,'surcharge',0)) |
---|
95 | except: |
---|
96 | surcharge = 0 |
---|
97 | resp_approved_amount = int(pd['resp_approved_amount']) |
---|
98 | #set_trace() |
---|
99 | if not ontest and amount + surcharge != resp_approved_amount: |
---|
100 | logger.info('Warning: %s, approved amount %s and authorized amount %s are different for payment %s of %s' % (member,resp_approved_amount,amount+surcharge,context.getId(),student_id)) |
---|
101 | pd['status'] = 'failed' |
---|
102 | pd['resp_desc'] = 'Warning: approved amount in callback does not match!' |
---|
103 | resp = '' |
---|
104 | else: |
---|
105 | pass |
---|
106 | except: |
---|
107 | logger.info('%s: approved amount could not be verified for payment %s of %s' % (member,context.getId(),student_id)) |
---|
108 | pd['status'] = 'failed' |
---|
109 | pd['resp_desc'] = 'Warning: approved amount could not be verified!' |
---|
110 | resp = '' |
---|
111 | |
---|
112 | pay_doc.edit(mapping = pd) |
---|
113 | s_brain = context.students_catalog(id=student_id)[0] |
---|
114 | session = s_brain.session |
---|
115 | |
---|
116 | next_info = context.getNextInfo(s_brain) |
---|
117 | next_session_id = next_info['next_session_id'] |
---|
118 | next_session_str = next_info['next_session_str'] |
---|
119 | next_level_id = next_info['next_level_id'] |
---|
120 | next_transition = next_info['next_transition'] |
---|
121 | next_verdict = next_info['next_verdict'] |
---|
122 | next_previous_verdict = next_info['next_previous_verdict'] |
---|
123 | if resp == '00': |
---|
124 | if pay_doc.category == 'schoolfee': |
---|
125 | if context.getStudentReviewState() == "school_fee_paid": |
---|
126 | logger.info('%s paid school_fee in state school_fee_paid' % (student_id)) |
---|
127 | else: |
---|
128 | study_course = getattr(student,'study_course') |
---|
129 | try: |
---|
130 | wftool.doActionFor(study_course,'open') |
---|
131 | except: |
---|
132 | pass |
---|
133 | verdict = s_brain.verdict |
---|
134 | if next_previous_verdict == 'N/A': |
---|
135 | next_previous_verdict = '' |
---|
136 | study_course.getContent().edit(mapping= {'current_level': next_level_id, |
---|
137 | 'current_session': next_session_id, |
---|
138 | 'current_verdict': next_verdict, |
---|
139 | 'previous_verdict': next_previous_verdict, |
---|
140 | }) |
---|
141 | if next_transition: |
---|
142 | wftool.doActionFor(student,next_transition) |
---|
143 | |
---|
144 | |
---|
145 | elif pay_doc.category == 'hostel_maintenance': |
---|
146 | |
---|
147 | ## callback function for accommodation_catalog method |
---|
148 | data={} |
---|
149 | data['acco_maint_date'] = pay_doc.date |
---|
150 | data['catkey'] =student_id + '|' + pay_doc.session_id |
---|
151 | data['acco_maint_fee'] = pay_doc.amount |
---|
152 | data['acco_maint_pay_id'] = context.getId() |
---|
153 | data['reservation_status'] = 'maintenance_fee_paid' |
---|
154 | context.accommodation_catalog.modifyRecord(**data) |
---|
155 | |
---|
156 | ## callback function for accommodation object method (deactivated since March 2011) |
---|
157 | #acco_info = context.getAccommodationInfo() |
---|
158 | #d = {} |
---|
159 | #d['acco_maint_date'] = pay_doc.date |
---|
160 | #d['acco_maint_fee'] = pay_doc.amount |
---|
161 | #d['acco_maint_pay_id'] = context.getId() |
---|
162 | #try: |
---|
163 | # acco_info['acco_doc'].edit(mapping=d) |
---|
164 | #except: |
---|
165 | # logger.info('%s requeried payment though maintenance already paid' % student_id) |
---|
166 | #try: |
---|
167 | # wftool.doActionFor(acco_info['acco'],'pay_maintenance_fee',dest_container=acco_info['acco']) |
---|
168 | #except: |
---|
169 | # logger.info('%s no workflow action pay_maintenance_fee' % student_id) |
---|
170 | |
---|
171 | elif pay_doc.category == 'acceptance': |
---|
172 | if context.getStudentReviewState() in ('admitted', 'objection_raised'): |
---|
173 | logger.info('%s paid acceptance fee' % (student_id)) |
---|
174 | current = DateTime.DateTime() |
---|
175 | wf = context.portal_workflow |
---|
176 | info = context.getClearanceInfo() |
---|
177 | wftool.doActionFor(student,'enter_clearance_pin') |
---|
178 | context.waeup_tool.changeWorkflowState(info['clear'], 'opened') |
---|
179 | context.waeup_tool.changeWorkflowState(info['app'], 'closed') |
---|
180 | dc = {} |
---|
181 | app_doc = info['app_doc'] |
---|
182 | #dc['clr_ac_pin'] = pay_doc.order_id |
---|
183 | dc['clr_ac_date'] = current |
---|
184 | dc['entry_date'] = current |
---|
185 | info['clear_doc'].edit(mapping = dc) |
---|
186 | if info['penalty']: |
---|
187 | logger.info('%s started late clearance' % (info['id'])) |
---|
188 | #return redirect("%s/clearance_edit_form" % info['clear'].absolute_url()) |
---|
189 | |
---|
190 | logger.info('%s received valid callback' % student_id) |
---|
191 | |
---|
192 | else: |
---|
193 | logger.info('%s received unsuccessful callback: %s' % (student_id,pd['resp_desc'])) |
---|
194 | |
---|
195 | #review_state = wftool.getInfoFor(context,'review_state',None) |
---|
196 | #if review_state == 'opened': |
---|
197 | # wftool.doActionFor(context,'close') |
---|
198 | context.waeup_tool.changeWorkflowState(context, 'closed') |
---|
199 | |
---|
200 | return request.RESPONSE.redirect("%s/waeup_document_view" % context.absolute_url()) |
---|
201 | |
---|