Changeset 9751 for main/waeup.kwarapoly


Ignore:
Timestamp:
1 Dec 2012, 06:48:28 (12 years ago)
Author:
Henrik Bettermann
Message:

Import helper functions from kofacustom.nigeria.

File:
1 edited

Legend:

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

    r9737 r9751  
    1616## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
    1717##
    18 from datetime import datetime
    1918import httplib
    20 import urllib
    2119import hashlib
    22 from xml.dom.minidom import parseString
    2320import grok
    24 from zope.event import notify
    2521from zope.interface import Interface
    2622from zope.component import getUtility, queryAdapter
     23from kofacustom.nigeria.interswitch.helpers import query_interswitch
    2724from waeup.kofa.browser.layout import KofaPage, UtilityView
    28 from waeup.kofa.accesscodes import create_accesscode
    29 from waeup.kofa.interfaces import RETURNING, IKofaUtils
     25from waeup.kofa.interfaces import IKofaUtils
    3026from waeup.kofa.utils.helpers import to_timezone
    3127from waeup.kofa.students.viewlets import ApprovePaymentActionButton as APABStudent
     
    5551httplib.HTTPConnection.debuglevel = 0
    5652
    57 
    58 def SOAP_post(soap_action,xml):
    59     """Handles making the SOAP request.
    60 
    61     Further reading:
    62     http://testwebpay.interswitchng.com/test_paydirect/services/TransactionQueryWs.asmx?op=getTransactionData
    63     """
    64     h = httplib.HTTPConnection(HOST)
    65     headers={
    66         'Host':HOST,
    67         'Content-Type':'text/xml; charset=utf-8',
    68         'Content-Length':len(xml),
    69         'SOAPAction':'"%s"' % soap_action,
    70     }
    71     h.request('POST', URL, body=xml,headers=headers)
    72     r = h.getresponse()
    73     d = r.read()
    74     if r.status!=200:
    75         raise ValueError('Error connecting: %s, %s' % (r.status, r.reason))
    76     return d
    77 
    78 def get_SOAP_response(product_id, transref):
    79     xml="""\
    80 <?xml version="1.0" encoding="utf-8"?>
    81 <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    82   <soap:Body>
    83     <getTransactionData xmlns="http://tempuri.org/">
    84       <product_id>%s</product_id>
    85       <trans_ref>%s</trans_ref>
    86     </getTransactionData>
    87   </soap:Body>
    88 </soap:Envelope>""" % (product_id, transref)
    89     result_xml=SOAP_post("http://tempuri.org/getTransactionData",xml)
    90     doc=parseString(result_xml)
    91     response=doc.getElementsByTagName('getTransactionDataResult')[0].firstChild.data
    92     return response
    93 
    94 def query_interswitch(payment):
    95     sr = get_SOAP_response(PRODUCT_ID, payment.p_id)
    96     wlist = sr.split(':')
    97     if len(wlist) != 7:
    98         msg = _('Invalid callback: ${a}', mapping = {'a': sr})
    99         log = 'invalid callback for payment %s: %s' % (payment.p_id, sr)
    100         return False, msg, log
    101     payment.r_code = wlist[0]
    102     payment.r_desc = wlist[1]
    103     payment.r_amount_approved = float(wlist[2]) / 100
    104     payment.r_card_num = wlist[3]
    105     payment.r_pay_reference = wlist[5]
    106     payment.r_company = u'interswitch'
    107     if payment.r_code != '00':
    108         msg = _('Unsuccessful callback: ${a}', mapping = {'a': sr})
    109         log = 'unsuccessful callback for %s payment %s: %s' % (
    110             payment.p_category, payment.p_id, sr)
    111         payment.p_state = 'failed'
    112         notify(grok.ObjectModifiedEvent(payment))
    113         return False, msg, log
    114     if payment.r_amount_approved != payment.amount_auth:
    115         msg = _('Callback amount does not match.')
    116         log = 'wrong callback for %s payment %s: %s' % (
    117             payment.p_category, payment.p_id, sr)
    118         payment.p_state = 'failed'
    119         notify(grok.ObjectModifiedEvent(payment))
    120         return False, msg, log
    121     if wlist[4] != payment.p_id:
    122         msg = _('Callback transaction id does not match.')
    123         log = 'wrong callback for %s payment %s: %s' % (
    124             payment.p_category, payment.p_id, sr)
    125         payment.p_state = 'failed'
    126         notify(grok.ObjectModifiedEvent(payment))
    127         return False, msg, log
    128     payment.p_state = 'paid'
    129     payment.payment_date = datetime.utcnow()
    130     msg = _('Successful callback received')
    131     log = 'valid callback for %s payment %s: %s' % (
    132         payment.p_category, payment.p_id, sr)
    133     notify(grok.ObjectModifiedEvent(payment))
    134     return True, msg, log
    135 
    13653def interswitch_img_url(view):
    13754    static = view.static
     
    13956        'interswitch_verve_mastercard.gif', None) is None:
    14057        static = queryAdapter(
    141             self.request, Interface, name='waeup.kwarapoly.interswitch')
     58            view.request, Interface, name='waeup.kwarapoly.interswitch')
    14259    return static['interswitch_verve_mastercard.gif']()
    14360
     
    368285
    369286    def update(self):
    370         ob_class = self.__implemented__.__name__
    371287        if self.context.p_state == 'paid':
    372288            self.flash(_('This ticket has already been paid.'))
    373289            return
    374290        student = self.context.student
    375         success, msg, log = query_interswitch(self.context)
     291        success, msg, log = query_interswitch(
     292            self.context, PRODUCT_ID, HOST, URL)
    376293        student.writeLogMessage(self, log)
    377294        if not success:
     
    400317            return
    401318        applicant = self.context.__parent__
    402         success, msg, log = query_interswitch(self.context)
     319        success, msg, log = query_interswitch(
     320            self.context, PRODUCT_ID, HOST, URL)
    403321        applicant.writeLogMessage(self, log)
    404322        if not success:
Note: See TracChangeset for help on using the changeset viewer.