source: main/kofacustom.nigeria/trunk/src/kofacustom/nigeria/remita/helpers.py @ 14736

Last change on this file since 14736 was 14736, checked in by Henrik Bettermann, 7 years ago

Catch ValueError?.

  • Property svn:keywords set to Id
File size: 2.9 KB
Line 
1## $Id: helpers.py 14736 2017-08-01 04:28:35Z henrik $
2##
3## Copyright (C) 2017 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 remita module in custom packages.
19"""
20from datetime import datetime
21import httplib
22import hashlib
23import json
24
25from zope.event import notify
26from kofacustom.nigeria.interfaces import MessageFactory as _
27
28def get_JSON_POST_response(
29        merchantId, serviceTypeId, api_key, orderId,
30        amount, responseurl, host, url, https, fullname, email, lineitems):
31    hashargs =  merchantId + serviceTypeId + orderId + amount + responseurl + api_key
32    hashvalue = hashlib.sha512(hashargs).hexdigest()
33    headers={
34        'Content-Type':'application/json; charset=utf-8',
35    }
36    if https:
37        h = httplib.HTTPSConnection(host)
38    else:
39        h = httplib.HTTPConnection(host)
40    data = {
41            "merchantId":merchantId,
42            "serviceTypeId":serviceTypeId,
43            "totalAmount":amount,
44            "hash":hashvalue,
45            "payerName":fullname,
46            "payerEmail":email,
47            "orderId":orderId,
48            "responseurl":responseurl,
49            "lineItems":lineitems
50            }
51    h.request("POST", url, body=json.dumps(data), headers=headers)
52    resp = h.getresponse()
53    jsonout = resp.read()
54    try:
55        parsed_json = json.loads(jsonout[6:-1])
56    except ValueError:
57        return None
58    return parsed_json
59
60
61def get_payment_status_via_rrr(merchantId, api_key, RRR, host, https):
62    hashargs =  RRR + api_key + merchantId
63    hashvalue = hashlib.sha512(hashargs).hexdigest()
64    headers={
65        'Content-Type':'application/json; charset=utf-8',
66    }
67    url = '/remita/ecomm/%s/%s/%s/status.reg' % (merchantId, RRR, hashvalue)
68    if https:
69        h = httplib.HTTPSConnection(host)
70    else:
71        h = httplib.HTTPConnection(host)
72    h.request("GET", url, headers=headers)
73    resp = h.getresponse()
74    jsonout = resp.read()
75    try:
76        parsed_json = json.loads(jsonout)
77    except ValueError:
78        return None
79    return parsed_json
80
81
82def write_payments_log(id, payment):
83    payment.logger.info(
84        '%s,%s,%s,%s,%s,%s,%s,%s,,,' % (
85        id, payment.p_id, payment.p_category,
86        payment.amount_auth, payment.r_code,
87        payment.provider_amt, payment.gateway_amt,
88        payment.thirdparty_amt))
Note: See TracBrowser for help on using the repository browser.