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 3964 2009-02-15 17:46:44Z 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 | if not 'webpay.interswitchng.com' in referer and not 'waeup.org' in referer: |
---|
49 | logger.info('%s, wrong callback referer %s, callback rejected, IP = %s' % (student_id,referer,real_ip)) |
---|
50 | return request.RESPONSE.redirect("%s/waeup_document_view" % context.absolute_url()) |
---|
51 | |
---|
52 | student = getattr(students,student_id) |
---|
53 | resp_codes = (("desc","resp_desc"), |
---|
54 | ("resp","resp_code"), |
---|
55 | ("txnRef","pay_reference"), |
---|
56 | ("payRef","resp_pay_reference"), |
---|
57 | ("retRef","retRef"), |
---|
58 | ("cardNum","resp_card_num"), |
---|
59 | ("apprAmt","resp_approved_amount"), |
---|
60 | ) |
---|
61 | pd = {} |
---|
62 | for rc,pdk in resp_codes: |
---|
63 | pd[pdk] = request.get(rc,'') |
---|
64 | |
---|
65 | ## for testing purposes |
---|
66 | #pd['resp_desc'] = 'Simulated Callback' |
---|
67 | #pd['resp_pay_reference'] = 'XXXX' |
---|
68 | #pd['resp_code'] = '00' |
---|
69 | #pd['resp_card_num'] = '0000' |
---|
70 | #pd['resp_approved_amount'] = '4680000' |
---|
71 | |
---|
72 | if pd['resp_code'] == '00' and len(pd['resp_approved_amount']) > 4: |
---|
73 | pd['resp_approved_amount'] = pd['resp_approved_amount'][:-2] |
---|
74 | pd['status'] = 'paid' |
---|
75 | else: |
---|
76 | pd['resp_approved_amount'] = '0' |
---|
77 | pd['status'] = 'failed' |
---|
78 | |
---|
79 | review_state = wftool.getInfoFor(context,'review_state',None) |
---|
80 | if pd['resp_code'] == '': |
---|
81 | logger.info('%s requeried payment %s for %s and got empty response' % (member,context.getId(),student_id)) |
---|
82 | return request.RESPONSE.redirect("%s/waeup_document_view" % context.absolute_url()) |
---|
83 | if access_info['is_student'] and review_state == 'closed': |
---|
84 | wftool.doActionFor(context,'open') |
---|
85 | pay_doc = context.getContent() |
---|
86 | resp = pd['resp_code'] |
---|
87 | |
---|
88 | if resp == '00': |
---|
89 | try: |
---|
90 | amount = int(getattr(pay_doc,'amount',0)) |
---|
91 | surcharge = int(getattr(pay_doc,'surcharge',0)) |
---|
92 | resp_approved_amount = int(pd['resp_approved_amount']) |
---|
93 | #set_trace() |
---|
94 | if amount + surcharge != resp_approved_amount: |
---|
95 | 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)) |
---|
96 | pd['status'] = 'failed' |
---|
97 | pd['resp_desc'] = 'Warning: approved amount in callback does not match!' |
---|
98 | resp = '' |
---|
99 | else: |
---|
100 | pass |
---|
101 | except: |
---|
102 | logger.info('%s: approved amount could not be verified for payment %s of %s' % (member,context.getId(),student_id)) |
---|
103 | pd['status'] = 'failed' |
---|
104 | pd['resp_desc'] = 'Warning: approved amount could not be verified!' |
---|
105 | resp = '' |
---|
106 | |
---|
107 | pay_doc.edit(mapping = pd) |
---|
108 | s_brain = context.students_catalog(id=student_id)[0] |
---|
109 | session = s_brain.session |
---|
110 | |
---|
111 | next_info = context.getNextInfo(s_brain) |
---|
112 | next_session_id = next_info['next_session_id'] |
---|
113 | next_session_str = next_info['next_session_str'] |
---|
114 | next_level_id = next_info['next_level_id'] |
---|
115 | next_transition = next_info['next_transition'] |
---|
116 | next_verdict = next_info['next_verdict'] |
---|
117 | next_previous_verdict = next_info['next_previous_verdict'] |
---|
118 | |
---|
119 | if resp == '00': |
---|
120 | if pay_doc.category == 'schoolfee': |
---|
121 | if context.getStudentReviewState() == "school_fee_paid": |
---|
122 | logger.info('%s paid school_fee in state school_fee_paid' % (student_id)) |
---|
123 | else: |
---|
124 | study_course = getattr(student,'study_course') |
---|
125 | try: |
---|
126 | wftool.doActionFor(study_course,'open') |
---|
127 | except: |
---|
128 | pass |
---|
129 | verdict = s_brain.verdict |
---|
130 | if next_previous_verdict == 'N/A': |
---|
131 | next_previous_verdict = '' |
---|
132 | study_course.getContent().edit(mapping= {'current_level': next_level_id, |
---|
133 | 'current_session': next_session_id, |
---|
134 | 'current_verdict': next_verdict, |
---|
135 | 'previous_verdict': next_previous_verdict, |
---|
136 | }) |
---|
137 | if next_transition: |
---|
138 | wftool.doActionFor(student,next_transition) |
---|
139 | |
---|
140 | |
---|
141 | elif pay_doc.category == 'hostel_maintenance': |
---|
142 | acco_info = context.getAccommodationInfo() |
---|
143 | d = {} |
---|
144 | d['acco_maint_date'] = pay_doc.date |
---|
145 | d['acco_maint_fee'] = pay_doc.amount |
---|
146 | d['acco_maint_pay_id'] = context.getId() |
---|
147 | try: |
---|
148 | acco_info['acco_doc'].edit(mapping=d) |
---|
149 | except: |
---|
150 | logger.info('%s requeried payment though maintenance already paid' % student_id) |
---|
151 | |
---|
152 | try: |
---|
153 | wftool.doActionFor(acco_info['acco'],'pay_maintenance_fee',dest_container=acco_info['acco']) |
---|
154 | except: |
---|
155 | logger.info('%s no workflow action pay_maintenance_fee' % student_id) |
---|
156 | |
---|
157 | |
---|
158 | |
---|
159 | logger.info('%s received valid callback' % student_id) |
---|
160 | |
---|
161 | else: |
---|
162 | logger.info('%s received unsuccessful callback: %s' % (student_id,pd['resp_desc'])) |
---|
163 | |
---|
164 | review_state = wftool.getInfoFor(context,'review_state',None) |
---|
165 | if review_state == 'opened': |
---|
166 | wftool.doActionFor(context,'close') |
---|
167 | |
---|
168 | return request.RESPONSE.redirect("%s/waeup_document_view" % context.absolute_url()) |
---|
169 | |
---|