Index: main/waeup.uniben/trunk/src/waeup/uniben/etranzact/browser.py
===================================================================
--- main/waeup.uniben/trunk/src/waeup/uniben/etranzact/browser.py	(revision 8129)
+++ main/waeup.uniben/trunk/src/waeup/uniben/etranzact/browser.py	(revision 8247)
@@ -23,19 +23,91 @@
 from waeup.kofa.browser.layout import KofaPage, UtilityView
 from waeup.kofa.students.interfaces import IStudentOnlinePayment
-from waeup.kofa.students.browser import write_log_message
-from waeup.kofa.students.viewlets import RequestCallbackActionButton
+from waeup.kofa.applicants.interfaces import IApplicantOnlinePayment
+from waeup.kofa.applicants.viewlets import RequestCallbackActionButton as RCABApplicant
+from waeup.kofa.students.viewlets import RequestCallbackActionButton as RCABStudent
 from waeup.uniben.interfaces import MessageFactory as _
-from waeup.uniben.utils.utils import actions_after_payment
+from waeup.uniben.students.utils import actions_after_student_payment
+from waeup.uniben.applicants.utils import actions_after_applicant_payment
 
-TERMINAL_ID = '0000000001'
-QUERY_URL =   'http://demo.etranzact.com:8080/WebConnect/queryPayoutletTransaction.jsp'
+TERMINAL_ID = '0500000003'
+QUERY_URL =   'https://www.etranzact.net/Query/queryPayoutletTransaction.jsp'
 
-class EtranzactEnterPinActionButton(RequestCallbackActionButton):
-    grok.order(5)
+
+def query_etranzact(confirmation_number, user, payment, view):
+    ob_class = view.__implemented__.__name__.replace('waeup.kofa.','')
+    postdict = {}
+    postdict['TERMINAL_ID'] = TERMINAL_ID
+    #postdict['RESPONSE_URL'] = 'http://dummy'
+    postdict['CONFIRMATION_NO'] = confirmation_number
+    data = urllib.urlencode(postdict)
+    try:
+        f = urllib.urlopen(url=QUERY_URL, data=data)
+        success = f.read()
+        user.loggerInfo(ob_class, 'callback received: %s' % success)
+        if 'COL1' not in success:
+            view.flash(_('Invalid or unsuccessful callback: ${a}',
+                mapping = {'a': success}))
+            user.loggerInfo(ob_class, 'invalid callback: %s' % payment.p_id)
+            payment.p_state = 'failed'
+            return False
+        success = success.replace('%20',' ').split('&')
+        # We expect at least two parameters
+        if len(success) < 2:
+            view.flash(_('Invalid callback: ${a}',
+                mapping = {'a': success}))
+            user.loggerInfo(ob_class, 'invalid callback: %s' % payment.p_id)
+            payment.p_state = 'failed'
+            return False
+        try:
+            success_dict = dict([tuple(i.split('=')) for i in success])
+        except ValueError:
+            view.flash(_('Invalid callback: ${a}',
+                mapping = {'a': success}))
+            user.loggerInfo(ob_class, 'invalid callback: %s' % payment.p_id)
+            payment.p_state = 'failed'
+            return False
+    except IOError:
+        view.flash(_('eTranzact IOError'))
+        return False
+    payment.r_code = u'ET'
+    payment.r_desc = u'%s' % success_dict.get('TRANS_DESCR')
+    payment.r_amount_approved = float(success_dict.get('TRANS_AMOUNT',0.0))
+    payment.r_card_num = None
+    payment.r_pay_reference = u'%s' % success_dict.get('RECEIPT_NO')
+    if payment.r_amount_approved != payment.amount_auth:
+        view.flash(_('Wrong amount'))
+        user.loggerInfo(ob_class, 'successful callback but wrong amount: %s'
+            % payment.p_id)
+        payment.p_state = 'failed'
+        return False
+    tcode = payment.p_id
+    tcode = tcode[len(tcode)-8:len(tcode)]
+    col1 = success_dict.get('COL1')
+    col1 = col1[len(col1)-8:len(col1)]
+    if tcode != col1:
+        view.flash(_('Wrong transaction code'))
+        write_log_message(
+            view,'successful callback but wrong transaction code: %s'
+            % payment.p_id)
+        user.loggerInfo(ob_class, 'successful callback wrong transaction code: %s'
+            % payment.p_id)
+        payment.p_state = 'failed'
+        return False
+    user.loggerInfo(ob_class, 'successful callback: %s' % payment.p_id)
+    payment.p_state = 'paid'
+    payment.payment_date = datetime.now()
+    return True
+
+class EtranzactEnterPinActionButtonApplicant(RCABApplicant):
     icon = 'actionicon_call.png'
     text = _('Query eTranzact History')
     target = 'enterpin'
 
-class EtranzactEnterPinPage(KofaPage):
+class EtranzactEnterPinActionButtonStudent(RCABStudent):
+    icon = 'actionicon_call.png'
+    text = _('Query eTranzact History')
+    target = 'enterpin'
+
+class EtranzactEnterPinPageStudent(KofaPage):
     """
     """
@@ -49,5 +121,11 @@
     action = 'query_history'
 
-class EtranzactQueryHistoryPage(UtilityView, grok.View):
+class EtranzactEnterPinPageApplicant(EtranzactEnterPinPageStudent):
+    """
+    """
+    grok.require('waeup.payApplicant')
+    grok.context(IApplicantOnlinePayment)
+
+class EtranzactQueryHistoryPageStudent(UtilityView, grok.View):
     """ Query history of eTranzact payments
     """
@@ -61,67 +139,6 @@
             return
         student = self.context.getStudent()
-        postdict = {}
-        postdict['TERMINAL_ID'] = TERMINAL_ID
-        #postdict['RESPONSE_URL'] = 'http://dummy'
-        postdict['CONFIRMATION_NO'] = confirmation_number
-        data = urllib.urlencode(postdict)
-        try:
-            f = urllib.urlopen(url=QUERY_URL, data=data)
-            success = f.read()
-            if '-1' in success:
-                self.flash(_('Unsuccessful callback'))
-                write_log_message(self,'unsuccessful callback: %s' % self.context.p_id)
-                self.context.p_state = 'failed'
-                return
-            success = success.replace('%20',' ').split('&')
-            # We expect at least two parameters
-            if len(success) < 2:
-                self.flash(_('Invalid callback: ${a}',
-                    mapping = {'a': success}))
-                write_log_message(self,'invalid callback: %s' % self.context.p_id)
-                self.context.p_state = 'failed'
-                return
-            try:
-                success_dict = dict([tuple(i.split('=')) for i in success])
-            except ValueError:
-                self.flash(_('Invalid callback: ${a}',
-                    mapping = {'a': success}))
-                write_log_message(self,'invalid callback: %s' % self.context.p_id)
-                self.context.p_state = 'failed'
-                return
-        except IOError:
-            self.flash(_('eTranzact IOError'))
-            return
-        write_log_message(self,'callback received: %s' % success)
-
-        self.context.r_code = u'ET'
-        self.context.r_desc = u'%s' % success_dict.get('TRANS_DESCR')
-        self.context.r_amount_approved = float(success_dict.get('TRANS_AMOUNT',0.0))
-        self.context.r_card_num = None
-        self.context.r_pay_reference = u'%s' % success_dict.get('RECEIPT_NO')
-        
-        if self.context.r_amount_approved != self.context.amount_auth:
-            self.flash(_('Wrong amount'))
-            write_log_message(
-                self,'successful callback but wrong amount: %s'
-                % self.context.p_id)
-            self.context.p_state = 'failed'
-            return
-
-        if success_dict.get('COL1') != self.context.p_id:
-            self.flash(_('Wrong transaction id'))
-            write_log_message(
-                self,'successful callback but wrong transaction id: %s'
-                % self.context.p_id)
-            self.context.p_state = 'failed'
-            return
-
-        write_log_message(self,'successful callback: %s' % self.context.p_id)
-
-        self.context.p_state = 'paid'
-        self.context.payment_date = datetime.now()
-
-        actions_after_payment(student, self.context, self)
-
+        if query_etranzact(confirmation_number, student, self.context, self):
+            actions_after_student_payment(student, self.context, self)
         return
 
@@ -129,2 +146,22 @@
         self.redirect(self.url(self.context, '@@index'))
         return
+
+class EtranzactQueryHistoryPageApplicant(UtilityView, grok.View):
+    """ Query history of eTranzact payments
+    """
+    grok.context(IApplicantOnlinePayment)
+    grok.name('query_history')
+    grok.require('waeup.payApplicant')
+
+    def update(self, confirmation_number=None):
+        if self.context.p_state == 'paid':
+            self.flash(_('This ticket has already been paid.'))
+            return
+        applicant = self.context.__parent__
+        if query_etranzact(confirmation_number, applicant, self.context, self):
+            actions_after_applicant_payment(applicant, self)
+        return
+
+    def render(self):
+        self.redirect(self.url(self.context, '@@index'))
+        return
