simulator.py 2.01 KB
from pprint import pprint
from datetime import datetime
from hashlib import md5
from wsgiref.simple_server import make_server
from pyramid.config import Configurator
from pyramid.view import view_config
from resp_test import (
    INQ_RESP,
    PAY_RESP,
    REV_RESP,
    )


BASE_PATH = '/api/v2/intg'
INQUIRY_PATH = '/'.join([
    BASE_PATH,
    'ext_bjb_inq/{username}/{password}/{invoice_id}'])
PAYMENT_PATH = '/'.join([
    BASE_PATH,
    'ext_bjb_pay/{username}/{password}/{invoice_id}/{ntb}/{nominal}'])
REVERSAL_PATH = '/'.join([
    BASE_PATH,
    'ext_bjb_rev/{username}/{password}/{invoice_id}/{ntb}'])


def send(r):
    print('Response:')
    pprint(r)
    return r


def create_ntp():
    ntp = datetime.now().strftime('%Y%m%d%H%M%S')
    return md5(ntp.encode('utf-8')).hexdigest()


@view_config(renderer='json', route_name='inquiry')
def view_inquiry(request):
    resp = dict(INQ_RESP)
    resp['data'][0]['nomor_resi'] = request.matchdict['invoice_id']
    return send(resp)


@view_config(renderer='json', route_name='payment')
def view_payment(request):
    ntp = create_ntp()
    waktu = datetime.now()
    resp = dict(PAY_RESP)
    resp['data'][0]['tgl'] = waktu.strftime('%d-%m-%Y')
    resp['data'][0]['jam'] = waktu.strftime('%H:%M:%S')
    resp['data'][0]['nomor_resi'] = request.matchdict['invoice_id']
    resp['data'][0]['ntb'] = request.matchdict['ntb']
    resp['data'][0]['ntp'] = ntp
    return send(resp)


@view_config(renderer='json', route_name='reversal')
def view_reversal(request):
    waktu = datetime.now()
    resp = dict(REV_RESP)
    resp['data'][0]['tgl'] = waktu.strftime('%d-%m-%Y')
    resp['data'][0]['jam'] = waktu.strftime('%H:%M:%S')
    resp['data'][0]['ntb'] = request.matchdict['ntb']
    return send(resp)


with Configurator() as config:
    config.add_route('inquiry', INQUIRY_PATH)
    config.add_route('payment', PAYMENT_PATH)
    config.add_route('reversal', REVERSAL_PATH)
    config.scan()
    app = config.make_wsgi_app()

server = make_server('0.0.0.0', 8080, app)
server.serve_forever()