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

Import helper functions from kofacustom.nigeria.

File:
1 edited

Legend:

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

    r9712 r9750  
    1616## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
    1717##
    18 from datetime import datetime
    1918import httplib
    20 import urllib
    21 from xml.dom.minidom import parseString
    2219import grok
    23 from zope.event import notify
    2420from zope.component import getUtility
     21from kofacustom.nigeria.interswitch.helpers import query_interswitch
    2522from waeup.kofa.browser.layout import KofaPage, UtilityView
    26 from waeup.kofa.accesscodes import create_accesscode
    2723from waeup.kofa.interfaces import RETURNING, CLEARED, IKofaUtils
    2824from waeup.kofa.utils.helpers import to_timezone
    2925from waeup.kofa.students.viewlets import ApprovePaymentActionButton as APABStudent
    30 from waeup.kofa.applicants.viewlets import ApprovePaymentActionButton as APABApplicant
    3126from waeup.futminna.students.interfaces import ICustomStudentOnlinePayment
    32 from waeup.futminna.applicants.interfaces import ICustomApplicantOnlinePayment
    3327from waeup.futminna.interfaces import MessageFactory as _
    3428
     
    5044#URL = '/test_paydirect/services/TransactionQueryWs.asmx'
    5145httplib.HTTPConnection.debuglevel = 0
    52 
    53 
    54 def SOAP_post(soap_action,xml):
    55     """Handles making the SOAP request.
    56 
    57     Further reading:
    58     http://testwebpay.interswitchng.com/test_paydirect/services/TransactionQueryWs.asmx?op=getTransactionData
    59     """
    60     h = httplib.HTTPConnection(HOST)
    61     headers={
    62         'Host':HOST,
    63         'Content-Type':'text/xml; charset=utf-8',
    64         'Content-Length':len(xml),
    65         'SOAPAction':'"%s"' % soap_action,
    66     }
    67     h.request('POST', URL, body=xml,headers=headers)
    68     r = h.getresponse()
    69     d = r.read()
    70     if r.status!=200:
    71         raise ValueError('Error connecting: %s, %s' % (r.status, r.reason))
    72     return d
    73 
    74 def get_SOAP_response(product_id, transref):
    75     xml="""\
    76 <?xml version="1.0" encoding="utf-8"?>
    77 <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/">
    78   <soap:Body>
    79     <getTransactionData xmlns="http://tempuri.org/">
    80       <product_id>%s</product_id>
    81       <trans_ref>%s</trans_ref>
    82     </getTransactionData>
    83   </soap:Body>
    84 </soap:Envelope>""" % (product_id, transref)
    85     result_xml=SOAP_post("http://tempuri.org/getTransactionData",xml)
    86     doc=parseString(result_xml)
    87     response=doc.getElementsByTagName('getTransactionDataResult')[0].firstChild.data
    88     return response
    89 
    90 def query_interswitch(payment):
    91     sr = get_SOAP_response(PRODUCT_ID, payment.p_id)
    92     wlist = sr.split(':')
    93     if len(wlist) != 7:
    94         msg = _('Invalid callback: ${a}', mapping = {'a': sr})
    95         log = 'invalid callback for payment %s: %s' % (payment.p_id, sr)
    96         return False, msg, log
    97     payment.r_code = wlist[0]
    98     payment.r_desc = wlist[1]
    99     payment.r_amount_approved = float(wlist[2]) / 100
    100     payment.r_card_num = wlist[3]
    101     payment.r_pay_reference = wlist[5]
    102     payment.r_company = u'interswitch'
    103     if payment.r_code != '00':
    104         msg = _('Unsuccessful callback: ${a}', mapping = {'a': sr})
    105         log = 'unsuccessful callback for %s payment %s: %s' % (
    106             payment.p_category, payment.p_id, sr)
    107         payment.p_state = 'failed'
    108         notify(grok.ObjectModifiedEvent(payment))
    109         return False, msg, log
    110     if payment.r_amount_approved != payment.amount_auth:
    111         msg = _('Callback amount does not match.')
    112         log = 'wrong callback for %s payment %s: %s' % (
    113             payment.p_category, payment.p_id, sr)
    114         payment.p_state = 'failed'
    115         notify(grok.ObjectModifiedEvent(payment))
    116         return False, msg, log
    117     if wlist[4] != payment.p_id:
    118         msg = _('Callback transaction id does not match.')
    119         log = 'wrong callback for %s payment %s: %s' % (
    120             payment.p_category, payment.p_id, sr)
    121         payment.p_state = 'failed'
    122         notify(grok.ObjectModifiedEvent(payment))
    123         return False, msg, log
    124     payment.p_state = 'paid'
    125     payment.payment_date = datetime.utcnow()
    126     msg = _('Successful callback received')
    127     log = 'valid callback for %s payment %s: %s' % (
    128         payment.p_category, payment.p_id, sr)
    129     notify(grok.ObjectModifiedEvent(payment))
    130     return True, msg, log
    13146
    13247class InterswitchActionButtonStudent(APABStudent):
     
    315230
    316231    def update(self):
    317         ob_class = self.__implemented__.__name__
    318232        if self.context.p_state == 'paid':
    319233            self.flash(_('This ticket has already been paid.'))
    320234            return
    321235        student = self.context.student
    322         success, msg, log = query_interswitch(self.context)
     236        success, msg, log = query_interswitch(
     237            self.context, PRODUCT_ID, HOST, URL)
    323238        student.writeLogMessage(self, log)
    324239        if not success:
Note: See TracChangeset for help on using the changeset viewer.