Ignore:
Timestamp:
13 Dec 2022, 11:39:13 (2 years ago)
Author:
Henrik Bettermann
Message:

Implement Interswitch WebCheckout?. Tests will follow.

Location:
main/kofacustom.nigeria/trunk/src/kofacustom/nigeria
Files:
3 added
2 edited

Legend:

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

    r16962 r17215  
    141141        )
    142142
     143    interswitch_webcheckout_enabled = schema.Bool(
     144        title = _(u'Interswitch WebCheckout integration enabled'),
     145        default = False,
     146        )
     147
    143148    etranzact_webconnect_enabled = schema.Bool(
    144149        title = _(u'Etranzact Webconnect integration enabled'),
  • main/kofacustom.nigeria/trunk/src/kofacustom/nigeria/interswitch/helpers.py

    r17014 r17215  
    379379    notify(grok.ObjectModifiedEvent(payment))
    380380    return True, msg, log
     381
     382# Web checkout helper functions
     383
     384def get_JSON_webcheckout_response(merchant_code, transref, host, url, https, amount):
     385    headers={
     386        'Content-Type':'text/xml; charset=utf-8',
     387    }
     388    if https:
     389        h = httplib.HTTPSConnection(host)
     390    else:
     391        h = httplib.HTTPConnection(host)
     392    amount = int(100 * amount)
     393    args = {'merchantcode': merchant_code,
     394            'transactionreference': transref,
     395            'amount': amount}
     396    url = '%s?' % url + urlencode(args)
     397    try:
     398        h.request("GET", url, headers=headers)
     399    except SSLError:
     400        return {'error': 'SSL handshake error'}
     401    response = h.getresponse()
     402    if response.status!=200:
     403        return {'error': 'Connection error (%s, %s)' % (response.status, response.reason)}
     404    jsonout = response.read()
     405    parsed_json = json.loads(jsonout)
     406    return parsed_json
     407
     408def confirm_transaction(payment, merchant_code, host, url, https=True):
     409    jr = get_JSON_webcheckout_response(merchant_code, payment.p_id, host, url,
     410                           https, payment.amount_auth)
     411    error = jr.get('error')
     412    if error:
     413        msg = log = error
     414        return False, msg, log
     415
     416    # A typical JSON response ?????????????????
     417
     418    #  {u'SplitAccounts': [],
     419    #  u'MerchantReference':u'p5918633006916',
     420    #  u'TransactionDate':u'2020-06-11T09:17:37',
     421    #  u'ResponseDescription':u'Customer Cancellation',
     422    #  u'Amount': 89525000,
     423    #  u'CardNumber': u'',
     424    #  u'ResponseCode': u'Z6',
     425    #  u'BankCode': u''}
     426
     427    if not 'ResponseCode' in jr.keys() \
     428        or not 'ResponseDescription' in jr.keys() \
     429        or not 'Amount' in jr.keys():
     430        msg = _('Invalid callback: ${a}', mapping = {'a': str(jr)})
     431        log = 'invalid callback for payment %s: %s' % (payment.p_id, str(jr))
     432        return False, msg, log
     433    payment.r_code = jr['ResponseCode']
     434    payment.r_desc = jr['ResponseDescription']
     435    payment.r_amount_approved = jr['Amount'] / 100.0
     436    payment.r_card_num = jr.get('CardNumber', u'')
     437    payment.r_pay_reference = jr.get('PaymentReference', u'')
     438    #payment.r_company = u'interswitch'
     439    if payment.r_code != '00':
     440        msg = _('Unsuccessful callback: ${a}', mapping = {'a': payment.r_desc})
     441        log = 'unsuccessful callback for %s payment %s: %s' % (
     442            payment.p_category, payment.p_id, payment.r_desc)
     443        payment.p_state = 'failed'
     444        notify(grok.ObjectModifiedEvent(payment))
     445        return False, msg, log
     446    if round(payment.r_amount_approved, 0) != round(payment.amount_auth, 0):
     447        msg = _('Callback amount does not match.')
     448        log = 'wrong callback for %s payment %s: %s' % (
     449            payment.p_category, payment.p_id, str(jr))
     450        payment.p_state = 'failed'
     451        notify(grok.ObjectModifiedEvent(payment))
     452        return False, msg, log
     453    if jr['MerchantReference'] != payment.p_id:
     454        msg = _('Callback transaction id does not match.')
     455        log = 'wrong callback for %s payment %s: %s' % (
     456            payment.p_category, payment.p_id, str(jr))
     457        payment.p_state = 'failed'
     458        notify(grok.ObjectModifiedEvent(payment))
     459        return False, msg, log
     460    payment.p_state = 'paid'
     461    if not verify:
     462        payment.payment_date = datetime.utcnow()
     463    msg = _('Successful callback received')
     464    log = 'valid callback for %s payment %s: %s' % (
     465        payment.p_category, payment.p_id, str(jr))
     466    notify(grok.ObjectModifiedEvent(payment))
     467    return True, msg, log
Note: See TracChangeset for help on using the changeset viewer.