def verify_notification(headers, body, key, secret): body = json.loads(body, parse_float=lambda x: decimal.Decimal(x).quantize( decimal.Decimal('0.01'), rounding=decimal.ROUND_UP)) if 'Fecha' not in headers or 'Autorizacion' not in headers: raise InvalidHeaderException fecha = strptime(headers['Fecha'], "%a, %d %b %Y %H:%M:%S GMT") try: authorization_string = util.authorization_string(action='notification', trx_id=body['trx_id'], monto=body['monto'], fecha=fecha, token=body['token']) except KeyError: raise InvalidBodyException expected_headers = util.create_headers(authorization_string, fecha, key, secret) for key, value in expected_headers.items(): try: if headers[key] != value: raise InvalidNotificationException except KeyError: raise InvalidHeaderException return body
def test_create_headers(self): authorization = 'PP 0PN5J17HBGZHT7ZZ3X82:AVrD3e9idIqAxRSH+15Yqz7qQkc=' expected = {'Fecha': 'Mon, 15 Jun 2009 20:45:30 GMT', 'Autorizacion': authorization, 'Content-Type': 'application/json'} headers = util.create_headers(authorization_string=self.authorization_string, time=self.time, key=self.key, secret=self.secret) self.assertEqual(headers, expected)
def test_create_headers(self): authorization = 'PP 0PN5J17HBGZHT7ZZ3X82:AVrD3e9idIqAxRSH+15Yqz7qQkc=' expected = {'Fecha': 'Mon, 15 Jun 2009 20:45:30 GMT', 'Autorizacion': authorization, 'Content-Type': 'application/json; charset=utf-8'} headers = util.create_headers(authorization_string=self.authorization_string, time=self.time, key=self.key, secret=self.secret) self.assertEqual(headers, expected)
def verify_notification(headers, body, key, secret): body = json.loads(body, parse_float=lambda x: decimal.Decimal(x).quantize(decimal.Decimal('0.01'), rounding=decimal.ROUND_UP)) if 'Fecha' not in headers or 'Autorizacion' not in headers: raise InvalidHeaderException fecha = strptime(headers['Fecha'], "%a, %d %b %Y %H:%M:%S GMT") try: authorization_string = util.authorization_string(action='notification', trx_id=body['trx_id'], monto=body['monto'], fecha=fecha, token=body['token']) except KeyError: raise InvalidBodyException expected_headers = util.create_headers(authorization_string, fecha, key, secret) for key, value in expected_headers.items(): try: if headers[key] != value: raise InvalidNotificationException except KeyError: raise InvalidHeaderException return body
def create(self, trx_id, medio_pago, monto, detalle): authorization_string = util.authorization_string(action='create', trx_id=trx_id, monto=monto, fecha=self.time) headers = util.create_headers( authorization_string=authorization_string, time=self.time, key=self.key, secret=self.secret) fecha = strftime(util.RFC1123_FORMAT, self.time) body = json.dumps({ 'trx_id': trx_id, 'medio_pago': medio_pago, 'monto': float(monto.quantize(Decimal('0.01'), rounding=ROUND_UP)), 'detalle': detalle, 'fecha': fecha }) self.connection.request(method='POST', url='/transaccion/crear', headers=headers, body=body) response = self.connection.getresponse() return self.response_class(response)
def status(self, token, trx_id, monto): authorization_string = util.authorization_string(action='status', trx_id=trx_id, monto=monto, fecha=self.time, token=token) headers = util.create_headers(authorization_string=authorization_string, time=self.time, key=self.key, secret=self.secret) self.connection.request(url='/transaccion/%(token)s' % {'token': token}, headers=headers, method='GET') response = self.connection.getresponse() return self.response_class(response)
def test_get_status_aprobado(self): token = '9XJ08401WN0071839' trx_id = '9787415132' monto = Decimal('10.001') authorization_string = util.authorization_string(action='status', trx_id=trx_id, monto=monto, fecha=self.request.time, token=token) headers = util.create_headers(authorization_string=authorization_string, time=self.request.time, key=self.request.key, secret=self.request.secret) self.request.response_class = self.response_class response = self.request.status(token=token, trx_id=trx_id, monto=monto) self.connection.request.assert_called_once_with(method='GET', url='/transaccion/9XJ08401WN0071839', headers=headers) self.connection.getresponse.assert_called_once_with() self.response_class.assert_called_once_with(self.connection.getresponse())
def create(self, trx_id, medio_pago, monto, detalle): authorization_string = util.authorization_string(action='create', trx_id=trx_id, monto=monto, fecha=self.time) headers = util.create_headers(authorization_string=authorization_string, time=self.time, key=self.key, secret=self.secret) fecha = strftime(util.RFC1123_FORMAT, self.time) body = json.dumps({'trx_id': trx_id, 'medio_pago': medio_pago, 'monto': float(monto.quantize(Decimal('0.01'), rounding=ROUND_UP)), 'detalle': detalle, 'fecha': fecha}) self.connection.request(method='POST', url='/transaccion/crear', headers=headers, body=body) response = self.connection.getresponse() return self.response_class(response)
def status(self, token, trx_id, monto): authorization_string = util.authorization_string(action='status', trx_id=trx_id, monto=monto, fecha=self.time, token=token) headers = util.create_headers( authorization_string=authorization_string, time=self.time, key=self.key, secret=self.secret) self.connection.request(url='/transaccion/%(token)s' % {'token': token}, headers=headers, method='GET') response = self.connection.getresponse() return self.response_class(response)
def test_call_create(self): trx_id = 'TRX_ID_1' medio_pago = 3 monto = Decimal('100.002') detalle = 'Lorem ipsum dolor sit amet' authorization_string = util.authorization_string(action='create', trx_id=trx_id, monto=monto, fecha=self.request.time) headers = util.create_headers(authorization_string=authorization_string, time=self.request.time, key=self.request.key, secret=self.request.secret) fecha = strftime('%a, %d %b %Y %H:%M:%S GMT', self.request.time) body = json.dumps({'trx_id': trx_id, 'medio_pago': medio_pago, 'monto': float(monto.quantize(Decimal('0.01'), rounding=ROUND_UP)), 'detalle': detalle, 'fecha': fecha}) self.request.response_class = self.response_class response = self.request.create(trx_id=trx_id, medio_pago=medio_pago, monto=monto, detalle=detalle) self.connection.request.assert_called_once_with(method='POST', url='/transaccion/crear', headers=headers, body=body) self.connection.getresponse.assert_called_once_with() self.response_class.assert_called_once_with(self.connection.getresponse())