1 | ## $Id: helpers.py 14735 2017-07-31 18:33:07Z 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 | """ |
---|
20 | from datetime import datetime |
---|
21 | import httplib |
---|
22 | import hashlib |
---|
23 | import json |
---|
24 | |
---|
25 | from zope.event import notify |
---|
26 | from kofacustom.nigeria.interfaces import MessageFactory as _ |
---|
27 | |
---|
28 | def 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 | parsed_json = json.loads(jsonout[6:-1]) |
---|
55 | return parsed_json |
---|
56 | |
---|
57 | |
---|
58 | def get_payment_status_via_rrr(merchantId, api_key, RRR, host, https): |
---|
59 | hashargs = RRR + api_key + merchantId |
---|
60 | hashvalue = hashlib.sha512(hashargs).hexdigest() |
---|
61 | headers={ |
---|
62 | 'Content-Type':'application/json; charset=utf-8', |
---|
63 | } |
---|
64 | url = '/remita/ecomm/%s/%s/%s/status.reg' % (merchantId, RRR, hashvalue) |
---|
65 | if https: |
---|
66 | h = httplib.HTTPSConnection(host) |
---|
67 | else: |
---|
68 | h = httplib.HTTPConnection(host) |
---|
69 | h.request("GET", url, headers=headers) |
---|
70 | resp = h.getresponse() |
---|
71 | jsonout = resp.read() |
---|
72 | parsed_json = json.loads(jsonout) |
---|
73 | return parsed_json |
---|
74 | |
---|
75 | |
---|
76 | def write_payments_log(id, payment): |
---|
77 | payment.logger.info( |
---|
78 | '%s,%s,%s,%s,%s,%s,%s,%s,,,' % ( |
---|
79 | id, payment.p_id, payment.p_category, |
---|
80 | payment.amount_auth, payment.r_code, |
---|
81 | payment.provider_amt, payment.gateway_amt, |
---|
82 | payment.thirdparty_amt)) |
---|