Changeset 13387 for main/kofacustom.nigeria
- Timestamp:
- 4 Nov 2015, 09:16:13 (9 years ago)
- Location:
- main/kofacustom.nigeria/trunk
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
main/kofacustom.nigeria/trunk/CHANGES.txt
r13260 r13387 5 5 ===================== 6 6 7 - no changes yet 7 - Implement Interswitch JSON transaction query. This webservice is 8 automatically used if a mac key is set in the request webservice 9 view. 8 10 9 11 1.2 (2015-09-14) -
main/kofacustom.nigeria/trunk/src/kofacustom/nigeria/interswitch/browser.py
r13197 r13387 83 83 gateway_url = None 84 84 https = True 85 mac = None 85 86 86 87 def update(self): … … 92 93 self.context, self.product_id, 93 94 self.gateway_host, self.gateway_url, 94 self.https )95 self.https, self.mac) 95 96 student.writeLogMessage(self, log) 96 97 if not success: … … 119 120 gateway_url = None 120 121 https = True 122 mac = None 121 123 122 124 def update(self): … … 128 130 self.context, self.product_id, 129 131 self.gateway_host, self.gateway_url, 130 self.https )132 self.https, self.mac) 131 133 applicant.writeLogMessage(self, log) 132 134 if not success: -
main/kofacustom.nigeria/trunk/src/kofacustom/nigeria/interswitch/helpers.py
r11914 r13387 20 20 from datetime import datetime 21 21 import httplib 22 import hashlib 23 import json 24 from urllib import urlencode 22 25 import grok 23 26 from xml.dom.minidom import parseString … … 44 47 response = h.getresponse() 45 48 return response 49 46 50 47 51 def get_SOAP_response(product_id, transref, host, url, https): … … 64 68 return response 65 69 66 def query_interswitch(payment, product_id, host, url, https=False): 70 71 def get_JSON_response(product_id, transref, host, url, https, mac, amount): 72 hashargs = product_id + transref + mac 73 hashvalue = hashlib.sha512(hashargs).hexdigest() 74 headers={ 75 'Content-Type':'text/xml; charset=utf-8', 76 'Hash':hashvalue, 77 } 78 if https: 79 h = httplib.HTTPSConnection(host) 80 else: 81 h = httplib.HTTPConnection(host) 82 amount = int(100 * amount) 83 args = {'productid': product_id, 84 'transactionreference': transref, 85 'amount': amount} 86 url = '%s?' % url + urlencode(args) 87 h.request("GET", url, headers=headers) 88 response = h.getresponse() 89 if response.status!=200: 90 return {'error': 'Connection error (%s, %s)' % (response.status, response.reason)} 91 jsonout = response.read() 92 parsed_json = json.loads(jsonout) 93 return parsed_json 94 95 def query_interswitch_SOAP(payment, product_id, host, url, https): 67 96 sr = get_SOAP_response(product_id, payment.p_id, host, url, https) 68 97 if sr.startswith('Connection error'): … … 110 139 return True, msg, log 111 140 141 def query_interswitch(payment, product_id, host, url, https, mac): 142 # If no mac mac key is given, fall back to deprecated SOAP method 143 # (Uniben, AAUA, FCEOkene). 144 if mac == None: 145 return query_interswitch_SOAP(payment, product_id, host, url, https) 146 jr = get_JSON_response(product_id, payment.p_id, host, url, 147 https, mac, payment.amount_auth) 148 error = jr.get('error') 149 if error: 150 msg = log = error 151 return False, msg, log 152 153 # A typical JSON response 154 # {u'SplitAccounts': [], 155 # u'MerchantReference':u'p4210665523377', 156 # u'PaymentReference':u'GTB|WEB|KPOLY|12-01-2015|013138', 157 # u'TransactionDate':u'2015-01-12T13:43:39.27', 158 # u'RetrievalReferenceNumber':u'000170548791', 159 # u'ResponseDescription': u'Approved Successful', 160 # u'Amount': 2940000, 161 # u'CardNumber': u'2507', 162 # u'ResponseCode': u'00', 163 # u'LeadBankCbnCode': None, 164 # u'LeadBankName': None} 165 166 if len(jr) != 11: 167 msg = _('Invalid callback: ${a}', mapping = {'a': str(jr)}) 168 log = 'invalid callback for payment %s: %s' % (payment.p_id, str(jr)) 169 return False, msg, log 170 payment.r_code = jr['ResponseCode'] 171 payment.r_desc = jr['ResponseDescription'] 172 payment.r_amount_approved = jr['Amount'] / 100.0 173 payment.r_card_num = jr['CardNumber'] 174 payment.r_pay_reference = jr['PaymentReference'] 175 payment.r_company = u'interswitch' 176 if payment.r_code != '00': 177 msg = _('Unsuccessful callback: ${a}', mapping = {'a': payment.r_desc}) 178 log = 'unsuccessful callback for %s payment %s: %s' % ( 179 payment.p_category, payment.p_id, payment.r_desc) 180 payment.p_state = 'failed' 181 notify(grok.ObjectModifiedEvent(payment)) 182 return False, msg, log 183 if round(payment.r_amount_approved, 2) != round(payment.amount_auth, 2): 184 msg = _('Callback amount does not match.') 185 log = 'wrong callback for %s payment %s: %s' % ( 186 payment.p_category, payment.p_id, str(jr)) 187 payment.p_state = 'failed' 188 notify(grok.ObjectModifiedEvent(payment)) 189 return False, msg, log 190 if jr['MerchantReference'] != payment.p_id: 191 msg = _('Callback transaction id does not match.') 192 log = 'wrong callback for %s payment %s: %s' % ( 193 payment.p_category, payment.p_id, str(jr)) 194 payment.p_state = 'failed' 195 notify(grok.ObjectModifiedEvent(payment)) 196 return False, msg, log 197 payment.p_state = 'paid' 198 payment.payment_date = datetime.utcnow() 199 msg = _('Successful callback received') 200 log = 'valid callback for %s payment %s: %s' % ( 201 payment.p_category, payment.p_id, str(jr)) 202 notify(grok.ObjectModifiedEvent(payment)) 203 return True, msg, log 204 112 205 def write_payments_log(id, payment): 113 206 payment.logger.info( -
main/kofacustom.nigeria/trunk/src/kofacustom/nigeria/interswitch/tests.py
r13386 r13387 31 31 # If you enable this, please make sure the external services 32 32 # do exist really and are not bothered by being spammed by a test programme. 33 EXTERNAL_TESTS = True33 EXTERNAL_TESTS = False 34 34 35 35 def external_test(func): … … 136 136 137 137 @external_test 138 def test_query_interswitch (self):138 def test_query_interswitch_SOAP(self): 139 139 host = 'webpay.interswitchng.com' 140 140 url = '/paydirect/services/TransactionQueryWs.asmx' 141 141 https = True 142 mac = None 142 143 product_id = '5845' # AAUE regular 143 144 payment = StudentOnlinePayment() … … 145 146 payment.amount_auth = 60250.0 146 147 success, msg, log = query_interswitch( 147 payment, product_id, host, url, https )148 payment, product_id, host, url, https, mac) 148 149 self.assertEqual('Successful callback received', msg) 149 150 self.assertTrue(success) … … 151 152 '00:Approved Successful:6025000:3154:p4465649308559:' 152 153 'ZIB|WEB|ABAL|3-11-2015|021336:000457580882' in log) 154 155 @external_test 156 def test_query_interswitch_JSON(self): 157 host = 'webpay.interswitchng.com' 158 url = '/paydirect/api/v1/gettransaction.json' 159 mac = '9718FA00B0F5070B388A9896ADCED9B2FB02D30F71E12E68BDADC63F6852A3496FF97D8A0F9DA9F753B911A49BB09BB87B55FD02046BD325C74C46C0123CF023' 160 https = True 161 product_id = '5845' # AAUE regular 162 payment = StudentOnlinePayment() 163 payment.p_id ='p4465649308559' 164 payment.amount_auth = 1.0 165 success, msg, log = query_interswitch( 166 payment, product_id, host, url, https, mac) 167 self.assertFalse(success) 168 self.assertTrue('Unsuccessful callback:' in msg) 169 self.assertTrue('Amount Inconsistency' in log) 170 payment.amount_auth = 60250.0 171 success, msg, log = query_interswitch( 172 payment, product_id, host, url, https, mac) 173 self.assertEqual('Successful callback received', msg) 174 self.assertTrue(success) 175 self.assertTrue( 176 "{u'SplitAccounts': [], " 177 "u'MerchantReference': u'p4465649308559', " 178 "u'PaymentReference': u'ZIB|WEB|ABAL|3-11-2015|021336', " 179 "u'TransactionDate': u'2015-11-03T16:40:54.487', " 180 "u'RetrievalReferenceNumber': u'000457580882', " 181 "u'ResponseDescription': u'Approved Successful', " 182 "u'Amount': 6025000, " 183 "u'CardNumber': u'3154', " 184 "u'ResponseCode': u'00', " 185 "u'LeadBankCbnCode': None, " 186 "u'LeadBankName': None}" in log) 153 187 154 188 class InterswitchTestsApplicants(ApplicantsFullSetup):
Note: See TracChangeset for help on using the changeset viewer.