## $Id: helpers.py 15598 2019-09-20 13:14:07Z henrik $ ## ## Copyright (C) 2017 Uli Fouquet & Henrik Bettermann ## This program is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. ## ## This program is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with this program; if not, write to the Free Software ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ## """General helper functions for the etranzact module in custom packages. """ import grok import re from datetime import datetime from urllib import urlencode import httplib import hashlib import json from zope.event import notify from waeup.kofa.utils.helpers import extract_formvars from kofacustom.nigeria.interfaces import MessageFactory as _ def write_payments_log(id, payment): payment.logger.info( '%s,%s,%s,%s,%s,%s,%s,%s,,,' % ( id, payment.p_id, payment.p_category, payment.amount_auth, payment.r_code, payment.provider_amt, payment.gateway_amt, payment.thirdparty_amt)) def query_history(host, terminal_id, transaction_id, https): headers={"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"} url = "/webconnect/v3/query.jsp" if https: h = httplib.HTTPSConnection(host) else: h = httplib.HTTPConnection(host) args = {'TERMINAL_ID': terminal_id, 'TRANSACTION_ID': transaction_id, } #args['RESPONSE_URL'] = responseurl h.request('POST', url, urlencode(args), headers) response = h.getresponse() if response.status!=200: return 'Connection error (%s, %s)' % (response.status, response.reason), None raw = response.read() return raw, extract_formvars(raw) # A sample caller response sent to the RESPONSE_URL # http://salsa:8080/app/applicants/cbt2015/449072/p5679522929425/receive_etranzact? # AMOUNT=3333.0& # DESCRIPTION=& # CHECKSUM=8aab3904652f8ba69ebed42d3bae80a2& # EMAIL=aa%40aa.de& # SUCCESS=C& # MESSAGE=Cancel& # LOGO_URL=https%3A%2F%2Fiuokada.waeup.org%2Fstatic_custom%2Fiou_logo.png& # RESPONSE_URL=http%3A%2F%2Fsalsa%3A8080%2Fapp%2Fapplicants%2Fcbt2015%2F449072%2Fp5679522929425%2Freceive_etranzact& # CURRENCY_CODE=NGN& # TERMINAL_ID=0000000001& # TRANSACTION_ID=p5679522929425& # MERCHANT_CODE=0339990001& # RESPONSE_CODE=C& # FINAL_CHECKSUM=E524590DBFAB719EEE428C778FFF1650& # STATUS_REASON=Cancel& # TRANS_NUM=01ESA20190913062149AHGUHQ& # CARD_NO=null& # CARD_TYPE=null # A sample query response # # #
# # # # # # # # # # # # # # # # #
# # A sample query response sent to the RESPONSE_URL after the browser has # automatically executed the Javascript above (we don't use this response) # http://salsa:8080/app/applicants/cbt2015/449072/p5686487280654/receive_etranzact? # LOGO_URL=https%3A%2F%2Fiuokada.waeup.org%2Fstatic_custom%2Fiou_logo.png& # RESPONSE_URL=http%3A%2F%2Fsalsa%3A8080%2Fapp%2Fapplicants%2Fcbt2015%2F449072%2Fp5686487280654%2Freceive_etranzact& # CURRENCY_CODE=NGN& # TERMINAL_ID=0000000001& # TRANSACTION_ID=p5686487280654& # AMOUNT=3333.0& # DESCRIPTION=& # CHECKSUM=3886118fcd91a376cc95c48c94dc499a& # MERCHANT_CODE=0339990001& # EMAIL=aa%40aa.de& # SUCCESS=0& # FINAL_CHECKSUM=EE105B703F84B1D67D0A4234622C03E8& # STATUS_REASON=Approved& # TRANS_NUM=01ESA20190916164636L2UTU7& # CARD_NO=506066XXXXXXXXX6666& # CARD_TYPE=Verve def process_response(payment, form, view, verify): if not form or not form.get('SUCCESS', None): msg = _('No valid response from eTranzact.') log = 'No valid response from eTranzact for payment %s' % payment.p_id payment.p_state = 'failed' notify(grok.ObjectModifiedEvent(payment)) return False, msg, log success = form.get('SUCCESS', None) # Compute final checksum transaction_id = payment.p_id amount = "%.1f" % payment.amount_auth responseurl = view.url(payment, 'receive_etranzact') hashargs = success + amount + view.terminal_id + transaction_id \ + responseurl + view.secret_key final_checksum = hashlib.md5(hashargs).hexdigest().upper() if form.get('FINAL_CHECKSUM', None) != final_checksum: msg = _('Wrong checksum.') log = 'wrong checksum for %s payment %s: %s' % ( payment.p_category, payment.p_id, str(form)) payment.p_state = 'failed' notify(grok.ObjectModifiedEvent(payment)) return False, msg, log payment.r_code = form.get('SUCCESS', None) payment.r_desc = form.get('STATUS_REASON', None) # MESSAGE also available payment.r_amount_approved = float(form.get('AMOUNT', None)) payment.r_pay_reference = form.get('TRANS_NUM', None) payment.r_card_num = "%s %s" % (form.get('CARD_TYPE', None), form.get('CARD_NO', None)) payment.r_company = u'etranzact' if payment.r_code != '0': msg = _('Unsuccessful response: ${a}', mapping = {'a': payment.r_desc}) log = 'unsuccessful response for %s payment %s: %s' % ( payment.p_category, payment.p_id, payment.r_desc) payment.p_state = 'failed' notify(grok.ObjectModifiedEvent(payment)) return False, msg, log if round(payment.r_amount_approved/10.0, 0) != round( payment.amount_auth/10.0, 0): msg = _('Response amount does not match.') log = 'wrong response for %s payment %s: %s' % ( payment.p_category, payment.p_id, str(form)) payment.p_state = 'failed' notify(grok.ObjectModifiedEvent(payment)) return False, msg, log transaction_id = form.get('TRANSACTION_ID', None) if transaction_id != payment.p_id: msg = _('Response transaction id does not match.') log = 'wrong response for %s payment %s: %s' % ( payment.p_category, payment.p_id, str(form)) payment.p_state = 'failed' notify(grok.ObjectModifiedEvent(payment)) return False, msg, log payment.p_state = 'paid' if not verify: payment.payment_date = datetime.utcnow() msg = _('Successful response received') log = 'valid response for %s payment %s: %s' % ( payment.p_category, payment.p_id, str(form)) notify(grok.ObjectModifiedEvent(payment)) return True, msg, log