source: WAeUP_SRP/trunk/skins/waeup_epayment/interswitch_cb.py @ 5939

Last change on this file since 5939 was 5939, checked in by Henrik Bettermann, 14 years ago

Catch traceback (though it will no more occur).

  • Property svn:keywords set to Id
File size: 7.0 KB
RevLine 
[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 5939 2011-04-13 13:12:52Z henrik $
11"""
12payment callback
13"""
[1937]14try:
15    from Products.zdb import set_trace
16except:
17    def set_trace():
18        pass
[1885]19import logging
[1948]20logger = logging.getLogger('Skins.interswitch_cb')
[1885]21from AccessControl import Unauthorized
22import DateTime
[3236]23
[1885]24if context.portal_membership.isAnonymousUser():
25    return None
26
27request = context.REQUEST
28students = context.portal_url.getPortalObject().campus.students
29wftool = context.portal_workflow
30mtool = context.portal_membership
31member = mtool.getAuthenticatedMember()
32member_id = str(member)
[3236]33#student_id = context.getStudentId()
34access_info = context.waeup_tool.getAccessInfo(context)
35student_id = access_info['student_id']
[3816]36if 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]44referer = request.get('HTTP_REFERER','none')
45real_ip = request.get('HTTP_X_REAL_IP',"none") 
46logger.info('%s, callback referer = %s, IP = %s' % (student_id,referer,real_ip))
47
[5781]48ontest = False
49
50if not ontest and not 'webpay.interswitchng.com' in referer and not 'waeup.org' in referer:
[3886]51    logger.info('%s, wrong callback referer %s, callback rejected, IP = %s' % (student_id,referer,real_ip))
[3874]52    return request.RESPONSE.redirect("%s/waeup_document_view" % context.absolute_url())
53
[1885]54student = getattr(students,student_id)
55resp_codes = (("desc","resp_desc"),
56              ("resp","resp_code"),
[1944]57              ("txnRef","pay_reference"),
58              ("payRef","resp_pay_reference"),
[1885]59              ("retRef","retRef"),
[1944]60              ("cardNum","resp_card_num"),
[1885]61              ("apprAmt","resp_approved_amount"),
62              )
63pd = {}
64for rc,pdk in resp_codes:
[1937]65    pd[pdk] = request.get(rc,'')
[1992]66
[5781]67if 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'
[1992]73
[1948]74if pd['resp_code'] == '00' and len(pd['resp_approved_amount']) > 4:
[1937]75    pd['resp_approved_amount'] = pd['resp_approved_amount'][:-2]
[2859]76    pd['status'] = 'paid'
[1937]77else:
78    pd['resp_approved_amount'] = '0'
[2859]79    pd['status'] = 'failed'
[1948]80
[1944]81review_state = wftool.getInfoFor(context,'review_state',None)
[2940]82if pd['resp_code'] == '':
83    logger.info('%s requeried payment %s for %s and got empty response' % (member,context.getId(),student_id))
[1944]84    return request.RESPONSE.redirect("%s/waeup_document_view" % context.absolute_url())
[3236]85if access_info['is_student'] and review_state == 'closed':
86    wftool.doActionFor(context,'open')
[3239]87pay_doc = context.getContent()
[3875]88resp = pd['resp_code'] 
[1992]89
[3964]90if  resp == '00':
91    try:
92        amount = int(getattr(pay_doc,'amount',0))
[5939]93        try:
94            surcharge = int(getattr(pay_doc,'surcharge',0))
95        except:
96            surcharge = 0
[3964]97        resp_approved_amount = int(pd['resp_approved_amount'])
98        #set_trace()
[5781]99        if not ontest and amount + surcharge != resp_approved_amount:
[3964]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))
[3963]108        pd['status'] = 'failed'
[3964]109        pd['resp_desc'] = 'Warning: approved amount could not be verified!'
110        resp = ''   
[3963]111
112pay_doc.edit(mapping = pd)
[1992]113s_brain = context.students_catalog(id=student_id)[0]
114session = s_brain.session
115
[2454]116next_info = context.getNextInfo(s_brain)
117next_session_id = next_info['next_session_id']
118next_session_str = next_info['next_session_str']
119next_level_id = next_info['next_level_id']
120next_transition = next_info['next_transition']
[2475]121next_verdict = next_info['next_verdict']
[2679]122next_previous_verdict = next_info['next_previous_verdict']
[2454]123
[1885]124if  resp == '00':
[3240]125    if pay_doc.category == 'schoolfee': 
126        if context.getStudentReviewState() == "school_fee_paid":
127            logger.info('%s paid school_fee in state school_fee_paid' % (student_id))
128        else:
129            study_course = getattr(student,'study_course')
130            try:
131                wftool.doActionFor(study_course,'open')
132            except:
133                pass
134            verdict = s_brain.verdict
135            if next_previous_verdict == 'N/A':
136                next_previous_verdict = ''
137            study_course.getContent().edit(mapping= {'current_level': next_level_id,
138                                                     'current_session': next_session_id,
139                                                     'current_verdict': next_verdict,
140                                                     'previous_verdict': next_previous_verdict,
141                                                     })
142            if next_transition:
143                wftool.doActionFor(student,next_transition)
144
145
146    elif pay_doc.category == 'hostel_maintenance':
[5781]147       
148        ## callback function for accommodation_catalog method
149        data={}
150        data['acco_maint_date'] = pay_doc.date
[5782]151        data['catkey'] =student_id + '|' + pay_doc.session_id
[5781]152        data['acco_maint_fee'] = pay_doc.amount
153        data['acco_maint_pay_id'] = context.getId()
154        data['reservation_status'] = 'maintenance_fee_paid'
155        context.accommodation_catalog.modifyRecord(**data)       
156       
157        ## callback function for accommodation object method (deactivated since March 2011)
158        #acco_info = context.getAccommodationInfo()
159        #d = {}
160        #d['acco_maint_date'] = pay_doc.date
161        #d['acco_maint_fee'] = pay_doc.amount
162        #d['acco_maint_pay_id'] = context.getId()
163        #try:
164        #    acco_info['acco_doc'].edit(mapping=d)
165        #except:
166        #    logger.info('%s requeried payment though maintenance already paid' % student_id)
167        #try:
168        #    wftool.doActionFor(acco_info['acco'],'pay_maintenance_fee',dest_container=acco_info['acco'])
169        #except:
170        #    logger.info('%s no workflow action pay_maintenance_fee' % student_id)
[3240]171           
172
173
[1885]174    logger.info('%s received valid callback' % student_id)
175
176else:
[3870]177    logger.info('%s received unsuccessful callback: %s' % (student_id,pd['resp_desc']))
[1885]178
[3240]179review_state = wftool.getInfoFor(context,'review_state',None)
180if review_state == 'opened':
181    wftool.doActionFor(context,'close')
[1948]182
[1885]183return request.RESPONSE.redirect("%s/waeup_document_view" % context.absolute_url())
[2940]184
Note: See TracBrowser for help on using the repository browser.