Ignore:
Timestamp:
1 Aug 2017, 07:12:58 (7 years ago)
Author:
Henrik Bettermann
Message:

Add query_remita helper function with test.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/kofacustom.nigeria/trunk/src/kofacustom/nigeria/remita/helpers.py

    r14736 r14737  
    1818"""General helper functions for the remita module in custom packages.
    1919"""
     20import grok
    2021from datetime import datetime
    2122import httplib
     
    5152    h.request("POST", url, body=json.dumps(data), headers=headers)
    5253    resp = h.getresponse()
     54    if resp.status!=200:
     55        return {'error': 'Connection error (%s, %s)' % (response.status, response.reason)}
    5356    jsonout = resp.read()
    5457    try:
    5558        parsed_json = json.loads(jsonout[6:-1])
    5659    except ValueError:
    57         return None
     60        return {'error': 'No JSON response'}
    5861    return parsed_json
    5962
     
    7275    h.request("GET", url, headers=headers)
    7376    resp = h.getresponse()
     77    if resp.status!=200:
     78        return {'error': 'Connection error (%s, %s)' % (response.status, response.reason)}
    7479    jsonout = resp.read()
    7580    try:
    7681        parsed_json = json.loads(jsonout)
    7782    except ValueError:
    78         return None
     83        return {'error': 'No JSON response'}
    7984    return parsed_json
     85
     86
     87def query_remita(payment, merchantId, api_key, RRR, host, https, verify):
     88
     89    jr = get_payment_status_via_rrr(merchantId, api_key, RRR, host, https)
     90    error = jr.get('error')
     91    if error:
     92        msg = log = error
     93        return False, msg, log
     94
     95    # A typical JSON response
     96    # {
     97    # u'orderId': u'3456346346',
     98    # u'status': u'021',
     99    # u'amount': 1000.0,
     100    # u'transactiontime': u'2017-07-31 11:17:24 AM',
     101    # u'message': u'Transaction Pending',
     102    # u'lineitems': [{u'status': u'021', u'lineItemsId': u'itemid1'},
     103    #                {u'status': u'021', u'lineItemsId': u'itemid2'}
     104    #               ],
     105    # u'RRR': u'280007640804'}
     106
     107    payment.r_code = jr['status']
     108    payment.r_desc = jr['message']
     109    payment.r_amount_approved = jr['amount']
     110    payment.r_pay_reference = jr['RRR']
     111    payment.r_company = u'remita'
     112    if payment.r_code != '00':
     113        msg = _('Unsuccessful callback: ${a}', mapping = {'a': payment.r_desc})
     114        log = 'unsuccessful callback for %s payment %s: %s' % (
     115            payment.p_category, payment.p_id, payment.r_desc)
     116        payment.p_state = 'failed'
     117        notify(grok.ObjectModifiedEvent(payment))
     118        return False, msg, log
     119    if round(payment.r_amount_approved, 0) != round(payment.amount_auth, 0):
     120        msg = _('Callback amount does not match.')
     121        log = 'wrong callback for %s payment %s: %s' % (
     122            payment.p_category, payment.p_id, str(jr))
     123        payment.p_state = 'failed'
     124        notify(grok.ObjectModifiedEvent(payment))
     125        return False, msg, log
     126    if jr['orderId'] != payment.p_id:
     127        msg = _('Callback order id does not match.')
     128        log = 'wrong callback for %s payment %s: %s' % (
     129            payment.p_category, payment.p_id, str(jr))
     130        payment.p_state = 'failed'
     131        notify(grok.ObjectModifiedEvent(payment))
     132        return False, msg, log
     133    payment.p_state = 'paid'
     134    if not verify:
     135        payment.payment_date = datetime.utcnow()
     136    msg = _('Successful callback received')
     137    log = 'valid callback for %s payment %s: %s' % (
     138        payment.p_category, payment.p_id, str(jr))
     139    notify(grok.ObjectModifiedEvent(payment))
     140    return True, msg, log
    80141
    81142
Note: See TracChangeset for help on using the changeset viewer.