source: main/kofacustom.nigeria/branches/0.1/src/kofacustom/nigeria/interswitch/helpers.py @ 17944

Last change on this file since 17944 was 10273, checked in by Henrik Bettermann, 11 years ago

Use round method.

  • Property svn:keywords set to Id
File size: 4.6 KB
Line 
1## $Id: helpers.py 10273 2013-06-03 12:30:21Z henrik $
2##
3## Copyright (C) 2012 Uli Fouquet & Henrik Bettermann
4## This program is free software; you can redistribute it and/or modify
5## it under the terms of the GNU General Public License as published by
6## the Free Software Foundation; either version 2 of the License, or
7## (at your option) any later version.
8##
9## This program is distributed in the hope that it will be useful,
10## but WITHOUT ANY WARRANTY; without even the implied warranty of
11## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12## GNU General Public License for more details.
13##
14## You should have received a copy of the GNU General Public License
15## along with this program; if not, write to the Free Software
16## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17##
18"""General helper functions for the interswitch module in custom packages.
19"""
20from datetime import datetime
21import httplib
22import grok
23from xml.dom.minidom import parseString
24from zope.event import notify
25from kofacustom.nigeria.interfaces import MessageFactory as _
26
27def SOAP_post(soap_action, xml, host, url):
28    """Handles making the SOAP request.
29
30    Further reading:
31    http://testwebpay.interswitchng.com/test_paydirect/services/TransactionQueryWs.asmx?op=getTransactionData
32    """
33    h = httplib.HTTPConnection(host)
34    headers={
35        'Host':host,
36        'Content-Type':'text/xml; charset=utf-8',
37        'Content-Length':len(xml),
38        'SOAPAction':'"%s"' % soap_action,
39    }
40    h.request('POST', url, body=xml,headers=headers)
41    response = h.getresponse()
42    return response
43
44def get_SOAP_response(product_id, transref, host, url):
45    xml="""\
46<?xml version="1.0" encoding="utf-8"?>
47<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/">
48  <soap:Body>
49    <getTransactionData xmlns="http://tempuri.org/">
50      <product_id>%s</product_id>
51      <trans_ref>%s</trans_ref>
52    </getTransactionData>
53  </soap:Body>
54</soap:Envelope>""" % (product_id, transref)
55    response=SOAP_post("http://tempuri.org/getTransactionData",xml, host, url)
56    if response.status!=200:
57        return 'Connection error (%s, %s)' % (response.status, response.reason)
58    result_xml = response.read()
59    doc=parseString(result_xml)
60    response=doc.getElementsByTagName('getTransactionDataResult')[0].firstChild.data
61    return response
62
63def query_interswitch(payment, product_id, host, url):
64    sr = get_SOAP_response(product_id, payment.p_id, host, url)
65    if sr.startswith('Connection error'):
66        msg = _('Connection error')
67        log = sr
68        return False, msg, log
69    wlist = sr.split(':')
70    if len(wlist) != 7:
71        msg = _('Invalid callback: ${a}', mapping = {'a': sr})
72        log = 'invalid callback for payment %s: %s' % (payment.p_id, sr)
73        return False, msg, log
74    payment.r_code = wlist[0]
75    payment.r_desc = wlist[1]
76    payment.r_amount_approved = float(wlist[2]) / 100
77    payment.r_card_num = wlist[3]
78    payment.r_pay_reference = wlist[5]
79    payment.r_company = u'interswitch'
80    if payment.r_code != '00':
81        msg = _('Unsuccessful callback: ${a}', mapping = {'a': sr})
82        log = 'unsuccessful callback for %s payment %s: %s' % (
83            payment.p_category, payment.p_id, sr)
84        payment.p_state = 'failed'
85        notify(grok.ObjectModifiedEvent(payment))
86        return False, msg, log
87    if round(payment.r_amount_approved, 2) != round(payment.amount_auth, 2):
88        msg = _('Callback amount does not match.')
89        log = 'wrong callback for %s payment %s: %s' % (
90            payment.p_category, payment.p_id, sr)
91        payment.p_state = 'failed'
92        notify(grok.ObjectModifiedEvent(payment))
93        return False, msg, log
94    if wlist[4] != payment.p_id:
95        msg = _('Callback transaction id does not match.')
96        log = 'wrong callback for %s payment %s: %s' % (
97            payment.p_category, payment.p_id, sr)
98        payment.p_state = 'failed'
99        notify(grok.ObjectModifiedEvent(payment))
100        return False, msg, log
101    payment.p_state = 'paid'
102    payment.payment_date = datetime.utcnow()
103    msg = _('Successful callback received')
104    log = 'valid callback for %s payment %s: %s' % (
105        payment.p_category, payment.p_id, sr)
106    notify(grok.ObjectModifiedEvent(payment))
107    return True, msg, log
108
109def write_payments_log(id, payment):
110    payment.logger.info(
111        '%s,%s,%s,%s,%s,%s,%s,%s,,,' % (
112        id, payment.p_id, payment.p_category,
113        payment.amount_auth, payment.r_code,
114        payment.provider_amt, payment.gateway_amt,
115        payment.thirdparty_amt))
Note: See TracBrowser for help on using the repository browser.