## $Id: helpers.py 14736 2017-08-01 04:28:35Z 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 remita module in custom packages. """ from datetime import datetime import httplib import hashlib import json from zope.event import notify from kofacustom.nigeria.interfaces import MessageFactory as _ def get_JSON_POST_response( merchantId, serviceTypeId, api_key, orderId, amount, responseurl, host, url, https, fullname, email, lineitems): hashargs = merchantId + serviceTypeId + orderId + amount + responseurl + api_key hashvalue = hashlib.sha512(hashargs).hexdigest() headers={ 'Content-Type':'application/json; charset=utf-8', } if https: h = httplib.HTTPSConnection(host) else: h = httplib.HTTPConnection(host) data = { "merchantId":merchantId, "serviceTypeId":serviceTypeId, "totalAmount":amount, "hash":hashvalue, "payerName":fullname, "payerEmail":email, "orderId":orderId, "responseurl":responseurl, "lineItems":lineitems } h.request("POST", url, body=json.dumps(data), headers=headers) resp = h.getresponse() jsonout = resp.read() try: parsed_json = json.loads(jsonout[6:-1]) except ValueError: return None return parsed_json def get_payment_status_via_rrr(merchantId, api_key, RRR, host, https): hashargs = RRR + api_key + merchantId hashvalue = hashlib.sha512(hashargs).hexdigest() headers={ 'Content-Type':'application/json; charset=utf-8', } url = '/remita/ecomm/%s/%s/%s/status.reg' % (merchantId, RRR, hashvalue) if https: h = httplib.HTTPSConnection(host) else: h = httplib.HTTPConnection(host) h.request("GET", url, headers=headers) resp = h.getresponse() jsonout = resp.read() try: parsed_json = json.loads(jsonout) except ValueError: return None return parsed_json 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))