[1948] | 1 | ## Script (Python) "interswitch_cb" |
---|
[1885] | 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 3886 2009-01-27 09:16:45Z henrik $ |
---|
| 11 | """ |
---|
| 12 | payment callback |
---|
| 13 | """ |
---|
[1937] | 14 | try: |
---|
| 15 | from Products.zdb import set_trace |
---|
| 16 | except: |
---|
| 17 | def set_trace(): |
---|
| 18 | pass |
---|
[1885] | 19 | import logging |
---|
[1948] | 20 | logger = logging.getLogger('Skins.interswitch_cb') |
---|
[1885] | 21 | from AccessControl import Unauthorized |
---|
| 22 | import DateTime |
---|
[3236] | 23 | |
---|
[1885] | 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) |
---|
[3236] | 33 | #student_id = context.getStudentId() |
---|
| 34 | access_info = context.waeup_tool.getAccessInfo(context) |
---|
| 35 | student_id = access_info['student_id'] |
---|
[3816] | 36 | if not context.isSectionOfficer() and (student_id is None or student_id != member_id): |
---|
[1885] | 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 | |
---|
[3874] | 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 | |
---|
[3875] | 48 | if not 'webpay.interswitchng.com' in referer and not 'waeup.org' in referer: |
---|
[3886] | 49 | logger.info('%s, wrong callback referer %s, callback rejected, IP = %s' % (student_id,referer,real_ip)) |
---|
[3874] | 50 | return request.RESPONSE.redirect("%s/waeup_document_view" % context.absolute_url()) |
---|
| 51 | |
---|
[1885] | 52 | student = getattr(students,student_id) |
---|
| 53 | resp_codes = (("desc","resp_desc"), |
---|
| 54 | ("resp","resp_code"), |
---|
[1944] | 55 | ("txnRef","pay_reference"), |
---|
| 56 | ("payRef","resp_pay_reference"), |
---|
[1885] | 57 | ("retRef","retRef"), |
---|
[1944] | 58 | ("cardNum","resp_card_num"), |
---|
[1885] | 59 | ("apprAmt","resp_approved_amount"), |
---|
| 60 | ) |
---|
| 61 | pd = {} |
---|
| 62 | for rc,pdk in resp_codes: |
---|
[1937] | 63 | pd[pdk] = request.get(rc,'') |
---|
[1992] | 64 | |
---|
[2454] | 65 | ## for testing purposes |
---|
[1992] | 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'] = '10000' |
---|
| 71 | |
---|
[1948] | 72 | if pd['resp_code'] == '00' and len(pd['resp_approved_amount']) > 4: |
---|
[1937] | 73 | pd['resp_approved_amount'] = pd['resp_approved_amount'][:-2] |
---|
[2859] | 74 | pd['status'] = 'paid' |
---|
[1937] | 75 | else: |
---|
| 76 | pd['resp_approved_amount'] = '0' |
---|
[2859] | 77 | pd['status'] = 'failed' |
---|
[1948] | 78 | |
---|
[1944] | 79 | review_state = wftool.getInfoFor(context,'review_state',None) |
---|
[2940] | 80 | if pd['resp_code'] == '': |
---|
| 81 | logger.info('%s requeried payment %s for %s and got empty response' % (member,context.getId(),student_id)) |
---|
[1944] | 82 | return request.RESPONSE.redirect("%s/waeup_document_view" % context.absolute_url()) |
---|
[3236] | 83 | if access_info['is_student'] and review_state == 'closed': |
---|
| 84 | wftool.doActionFor(context,'open') |
---|
[3239] | 85 | pay_doc = context.getContent() |
---|
| 86 | pay_doc.edit(mapping = pd) |
---|
[3875] | 87 | resp = pd['resp_code'] |
---|
[1992] | 88 | |
---|
| 89 | s_brain = context.students_catalog(id=student_id)[0] |
---|
| 90 | session = s_brain.session |
---|
| 91 | |
---|
[2454] | 92 | next_info = context.getNextInfo(s_brain) |
---|
| 93 | next_session_id = next_info['next_session_id'] |
---|
| 94 | next_session_str = next_info['next_session_str'] |
---|
| 95 | next_level_id = next_info['next_level_id'] |
---|
| 96 | next_transition = next_info['next_transition'] |
---|
[2475] | 97 | next_verdict = next_info['next_verdict'] |
---|
[2679] | 98 | next_previous_verdict = next_info['next_previous_verdict'] |
---|
[2454] | 99 | |
---|
[1885] | 100 | if resp == '00': |
---|
[3240] | 101 | if pay_doc.category == 'schoolfee': |
---|
| 102 | if context.getStudentReviewState() == "school_fee_paid": |
---|
| 103 | logger.info('%s paid school_fee in state school_fee_paid' % (student_id)) |
---|
| 104 | else: |
---|
| 105 | study_course = getattr(student,'study_course') |
---|
| 106 | try: |
---|
| 107 | wftool.doActionFor(study_course,'open') |
---|
| 108 | except: |
---|
| 109 | pass |
---|
| 110 | verdict = s_brain.verdict |
---|
| 111 | if next_previous_verdict == 'N/A': |
---|
| 112 | next_previous_verdict = '' |
---|
| 113 | study_course.getContent().edit(mapping= {'current_level': next_level_id, |
---|
| 114 | 'current_session': next_session_id, |
---|
| 115 | 'current_verdict': next_verdict, |
---|
| 116 | 'previous_verdict': next_previous_verdict, |
---|
| 117 | }) |
---|
| 118 | if next_transition: |
---|
| 119 | wftool.doActionFor(student,next_transition) |
---|
| 120 | |
---|
| 121 | |
---|
| 122 | elif pay_doc.category == 'hostel_maintenance': |
---|
| 123 | acco_info = context.getAccommodationInfo() |
---|
| 124 | d = {} |
---|
| 125 | d['acco_maint_date'] = pay_doc.date |
---|
| 126 | d['acco_maint_fee'] = pay_doc.amount |
---|
| 127 | d['acco_maint_pay_id'] = context.getId() |
---|
[2454] | 128 | try: |
---|
[3240] | 129 | acco_info['acco_doc'].edit(mapping=d) |
---|
[2454] | 130 | except: |
---|
[3240] | 131 | logger.info('%s requeried payment though maintenance already paid' % student_id) |
---|
[2454] | 132 | |
---|
[3240] | 133 | try: |
---|
| 134 | wftool.doActionFor(acco_info['acco'],'pay_maintenance_fee',dest_container=acco_info['acco']) |
---|
| 135 | except: |
---|
| 136 | logger.info('%s no workflow action pay_maintenance_fee' % student_id) |
---|
| 137 | |
---|
| 138 | |
---|
| 139 | |
---|
[1885] | 140 | logger.info('%s received valid callback' % student_id) |
---|
| 141 | |
---|
| 142 | else: |
---|
[3870] | 143 | logger.info('%s received unsuccessful callback: %s' % (student_id,pd['resp_desc'])) |
---|
[1885] | 144 | |
---|
[3240] | 145 | review_state = wftool.getInfoFor(context,'review_state',None) |
---|
| 146 | if review_state == 'opened': |
---|
| 147 | wftool.doActionFor(context,'close') |
---|
[1948] | 148 | |
---|
[1885] | 149 | return request.RESPONSE.redirect("%s/waeup_document_view" % context.absolute_url()) |
---|
[2940] | 150 | |
---|