Changeset 7926


Ignore:
Timestamp:
20 Mar 2012, 10:14:00 (13 years ago)
Author:
Henrik Bettermann
Message:

Avoid duplicate code.

Location:
main/waeup.custom/trunk/src/waeup/custom
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.custom/trunk/src/waeup/custom/interswitch/browser.py

    r7919 r7926  
    2626from waeup.kofa.students.browser import write_log_message
    2727from waeup.kofa.students.viewlets import RequestCallbackActionButton
     28from waeup.custom.utils.utils import actions_after_payment
    2829from waeup.custom.interfaces import MessageFactory as _
    2930
     
    171172        return
    172173
    173 class OnlinePaymentCallbackPage(UtilityView, grok.View):
     174class InterswitchPaymentCallbackPage(UtilityView, grok.View):
    174175    """ Callback view for the CollegePAY gateway
    175176    """
     
    192193        # Should be logged instead of printed
    193194        write_log_message(self,'callback received: %s' % query)
    194         if query.get('resp', None) != '00':
     195
     196        self.context.r_card_num = query.get('cardNum', None)
     197        self.context.r_code = query.get('resp', None)
     198        self.context.r_pay_reference  = query.get('payRef', None)
     199        self.context.r_amount_approved = int(query.get('apprAmt', '0')) / 100
     200        self.context.r_desc = query.get('desc', None)
     201
     202        if self.context.r_code != '00':
    195203            self.flash(_('Unsuccessful callback: ${a}',
    196204                mapping = {'a': query.get('desc', _('Incomplete query string.'))}))
    197205            write_log_message(self,'unsuccessful callback: %s' % self.context.p_id)
    198             self.context.r_card_num = query.get('cardNum', None)
    199             self.context.r_code = query.get('resp', None)
    200             self.context.p_state = 'failed'
    201             return
    202 
    203         if query.get('apprAmt', None) != str(self.context.amount_auth):
     206            self.context.p_state = 'failed'
     207            return
     208
     209        total_amount_auth = (
     210            self.context.amount_auth
     211            + self.context.surcharge_1
     212            + self.context.surcharge_2)
     213
     214        if self.context.r_amount_approved != total_amount_auth:
    204215            self.flash(_('Wrong amount'))
    205216            write_log_message(self,'successful but wrong amount: %s' % self.context.p_id)
    206             self.context.r_card_num = query.get('cardNum', None)
    207             self.context.r_code = query.get('resp', None)
    208217            self.context.p_state = 'failed'
    209218            return
     
    217226
    218227        write_log_message(self,'valid callback: %s' % self.context.p_id)
    219         self.context.r_amount_approved = self.context.amount_auth
    220         self.context.r_card_num = query.get('cardNum', None)
    221         self.context.r_code = query.get('resp', None)
    222         self.context.r_desc = query.get('desc', None)
    223         self.context.r_pay_reference  = query.get('payRef', None)
    224228        self.context.p_state = 'paid'
    225229        self.context.payment_date = datetime.now()
    226230
    227         if self.context.p_category == 'clearance':
    228             # Create CLR access code
    229             pin, error = create_accesscode('CLR',0,student.student_id)
    230             if error:
    231                 self.flash(_('Valid callback received. ${a}',
    232                     mapping = {'a':error}))
    233                 return
    234             self.context.ac = pin
    235         elif self.context.p_category == 'schoolfee':
    236             # Create SFE access code
    237             pin, error = create_accesscode('SFE',0,student.student_id)
    238             if error:
    239                 self.flash(_('Valid callback received. ${a}',
    240                     mapping = {'a':error}))
    241                 return
    242             self.context.ac = pin
    243         elif self.context.p_category == 'bed_allocation':
    244             # Create HOS access code
    245             pin, error = create_accesscode('HOS',0,student.student_id)
    246             if error:
    247                 self.flash(_('Valid callback received. ${a}',
    248                     mapping = {'a':error}))
    249                 return
    250             self.context.ac = pin
    251         self.flash(_('Valid callback received.'))
     231        actions_after_payment(student, self.context, self)
     232
    252233        return
    253234
     
    257238
    258239# Alternative solution, replaces OnlinePaymentCallbackPage
    259 class OnlinePaymentRequestWebservicePage(UtilityView, grok.View):
     240class InterswitchPaymentRequestWebservicePage(UtilityView, grok.View):
    260241    """ Request webservice view for the CollegePAY gateway
    261242    """
     
    279260        self.context.r_code = wlist[0]
    280261        self.context.r_desc = wlist[1]
    281         self.context.r_amount_approved = int(wlist[2])
     262        self.context.r_amount_approved = int(wlist[2]) / 100
    282263        self.context.r_card_num = wlist[3]
    283264        self.context.r_pay_reference = wlist[5]
    284265       
    285         if wlist[0] != '00':
     266        if self.context.r_code != '00':
    286267            self.flash(_('Unsuccessful callback: ${a}',
    287268                mapping = {'a': wlist[1]}))
     
    290271            return
    291272
    292         if wlist[2] != str(self.context.amount_auth):
     273        total_amount_auth = (
     274            self.context.amount_auth
     275            + self.context.surcharge_1
     276            + self.context.surcharge_2)
     277
     278        if self.context.r_amount_approved != total_amount_auth:
    293279            self.flash(_('Wrong amount'))
    294280            write_log_message(self,'successful callback but wrong amount: %s' % self.context.p_id)
     
    296282            return
    297283
    298         if wlist[5] != self.context.p_id:
     284        if wlist[4] != self.context.p_id:
    299285            self.flash(_('Wrong transaction id'))
    300286            write_log_message(self,'successful callback but wrong transaction id: %s' % self.context.p_id)
     
    307293        self.context.payment_date = datetime.now()
    308294
    309         if self.context.p_category == 'clearance':
    310             # Create CLR access code
    311             pin, error = create_accesscode('CLR',0,student.student_id)
    312             if error:
    313                 self.flash(_('Valid callback received. ${a}',
    314                     mapping = {'a':error}))
    315                 return
    316             self.context.ac = pin
    317         elif self.context.p_category == 'schoolfee':
    318             # Create SFE access code
    319             pin, error = create_accesscode('SFE',0,student.student_id)
    320             if error:
    321                 self.flash(_('Valid callback received. ${a}',
    322                     mapping = {'a':error}))
    323                 return
    324             self.context.ac = pin
    325         elif self.context.p_category == 'bed_allocation':
    326             # Create HOS access code
    327             pin, error = create_accesscode('HOS',0,student.student_id)
    328             if error:
    329                 self.flash(_('Valid callback received. ${a}',
    330                     mapping = {'a':error}))
    331                 return
    332             self.context.ac = pin
    333         self.flash(_('Valid callback received.'))
     295        actions_after_payment(student, self.context, self)
     296
    334297        return
    335298
  • main/waeup.custom/trunk/src/waeup/custom/interswitch/tests.py

    r7897 r7926  
    7373        self.assertMatches('...Failed...',
    7474                           self.browser.contents)
    75         self.browser.open(self.callback_url(payment_url, '00', '300'))
     75        self.browser.open(self.callback_url(payment_url, '00', '300000'))
    7676        self.assertMatches('...Wrong amount...',
    7777                          self.browser.contents)
    78         self.browser.open(self.callback_url(payment_url, '00', '40000'))
     78        self.browser.open(self.callback_url(payment_url, '00', '4000000'))
    7979        self.assertMatches('...Valid callback received...',
    8080                          self.browser.contents)
  • main/waeup.custom/trunk/src/waeup/custom/utils/utils.py

    r7920 r7926  
    2020import os
    2121from waeup.kofa.utils.utils import KofaUtils
     22from waeup.kofa.accesscodes import create_accesscode
     23from waeup.custom.interfaces import MessageFactory as _
     24
     25def actions_after_payment(student, payment, view):
     26    if payment.p_category == 'clearance':
     27        # Create CLR access code
     28        pin, error = create_accesscode('CLR',0,student.student_id)
     29        if error:
     30            view.flash(_('Valid callback received. ${a}',
     31                mapping = {'a':error}))
     32            return
     33        payment.ac = pin
     34    elif payment.p_category == 'schoolfee':
     35        # Create SFE access code
     36        pin, error = create_accesscode('SFE',0,student.student_id)
     37        if error:
     38            view.flash(_('Valid callback received. ${a}',
     39                mapping = {'a':error}))
     40            return
     41        payment.ac = pin
     42    elif payment.p_category == 'bed_allocation':
     43        # Create HOS access code
     44        pin, error = create_accesscode('HOS',0,student.student_id)
     45        if error:
     46            view.flash(_('Valid callback received. ${a}',
     47                mapping = {'a':error}))
     48            return
     49        payment.ac = pin
     50    view.flash(_('Valid callback received.'))
     51    return
    2252
    2353
Note: See TracChangeset for help on using the changeset viewer.