Exemplo n.º 1
0
 def __init__(self, *args, **kwargs):
     if 'log_info' in kwargs:
         self.log_info = kwargs['log_info']
         del kwargs['log_info']
     if 'log_error' in kwargs:
         self.log_error = kwargs['log_error']
         del kwargs['log_error']
     if 'conf' in kwargs:
         self.conf = kwargs['conf']
         del kwargs['conf']
     else:
         self.conf = {}
     Data.__init__(self, *args, **kwargs)
     self.raw = ''
     bits = self.get_bit_definition()
     for bit in bits:
         short_name, long_name, type_, size, format_ = bits[bit]
         self.redefineBit(bit, short_name, long_name, type_, size, format_)
     self.transmission = DateTimeVar()
     if self.from_iso:
         self.set_response()
Exemplo n.º 2
0
 def __init__(self, *args, **kwargs):
     if 'log_info' in kwargs:
         self.log_info = kwargs['log_info']
         del kwargs['log_info']
     if 'log_error' in kwargs:
         self.log_error = kwargs['log_error']
         del kwargs['log_error']
     if 'conf' in kwargs:
         self.conf = kwargs['conf']
         del kwargs['conf']
     else:
         self.conf = {}
     Data.__init__(self, *args, **kwargs)
     self.raw = ''
     bits = self.get_bit_definition()
     for bit in bits: 
         short_name, long_name, type_, size, format_ = bits[bit]
         self.redefineBit(bit, short_name, long_name, type_, size, format_)
     self.transmission = DateTimeVar()
     if self.from_iso:
         self.set_response()
Exemplo n.º 3
0
 def __init__(self, *args, **kwargs):
     self.settlement = DateVar()
     self.transaction_datetime = DateTimeVar()
     self.transaction_date = DateVar()
     self.transaction_time = TimeVar()
     Network.__init__(self, *args, **kwargs)
Exemplo n.º 4
0
class BaseTransaction(Network):
    def __init__(self, *args, **kwargs):
        self.settlement = DateVar()
        self.transaction_datetime = DateTimeVar()
        self.transaction_date = DateVar()
        self.transaction_time = TimeVar()
        Network.__init__(self, *args, **kwargs)

    # Override
    def get_bit_definition(self):
        return BASE_TRANSACTION_BITS

    # Override
    def get_func_name(self):
        return self.is_echo_test_request() or self.is_sign_on_request() or \
               self.is_sign_off_request() or self.is_inquiry_request() or \
               self.is_payment_request() or self.is_reversal_request()

    # Override
    def set_response(self):
        if self.from_iso.is_network_request():
            Network.set_response(self)
        elif self.from_iso.is_transaction_request():
            self.set_transaction_response()
        elif self.from_iso.is_reversal_request():
            self.set_reversal_response()

    # Override
    def setIsoContent(self, raw):
        Network.setIsoContent(self, raw)
        self.raw = raw

    def is_transaction_request(self):
        return self.getMTI() == '0200'

    def is_transaction_response(self):
        return self.getMTI() == '0210'

    def is_response(self):
        return Network.is_response(self) or self.is_transaction_response() or \
                self.is_reversal_response()

    def get_transaction_code(self):
        return self.get_value(3)

    def set_transaction_code(self, code):
        self.setBit(3, code)

    def get_inquiry_code(self):
        pass

    def is_inquiry_request(self):
        if not self.is_transaction_request():
            return
        code = self.get_transaction_code()
        return code == self.get_inquiry_code() and 'inquiry_request_handler'

    def is_inquiry_response(self):
        return self.get_transaction_code() == self.get_inquiry_code()

    def get_payment_code(self):
        pass

    def is_payment_request(self):
        if not self.is_transaction_request():
            return
        code = self.get_transaction_code()
        return code == self.get_payment_code() and 'payment_request_handler'

    def is_payment_response(self):
        return self.get_transaction_code() == self.get_payment_code()

    def set_transaction_response(self):
        self.setMTI('0210')
        # Sesuai kolom yang berisi E (Equal) pada PDF
        self.copy(
            [2, 3, 7, 11, 12, 13, 15, 18, 22, 32, 33, 35, 37, 41, 42, 43])

    def set_transaction_request(self):
        self.setMTI('0200')
        self.set_transmission()
        self.set_stan()

    def get_settlement(self):
        raw = self.get_value(15)
        self.settlement.set_raw(raw)
        try:
            return self.settlement.get_value()
        except ValueError:
            self.ack_settlement_date(raw)

    def get_transaction_datetime_raw(self):
        return self.get_transaction_date_raw() + self.get_transaction_time_raw(
        )

    def get_transaction_datetime(self):
        raw = self.get_transaction_datetime_raw()
        self.transaction_datetime.set_raw(raw)
        try:
            return self.transaction_datetime.get_value()
        except ValueError:
            self.ack_transaction_datetime()

    def get_transaction_time_raw(self):
        return self.get_value(12)

    def get_transaction_time(self):
        raw = self.get_transaction_time_raw()
        self.transaction_time.set_raw(raw)
        try:
            return self.transaction_time.get_value()
        except ValueError:
            self.ack_transaction_time()

    def get_transaction_date_raw(self):
        return self.get_value(13)

    def get_transaction_date(self):
        raw = self.get_transaction_date_raw()
        self.transaction_date.set_raw(raw)
        try:
            return self.transaction_date.get_value()
        except ValueError:
            self.ack_transaction_date()

    def get_channel(self):
        return int(self.get_value(18))

    def get_bank_id(self):
        return int(self.get_value(32))

    def get_forwarder(self):
        return self.get_value(33)

    def get_sequence(self):
        return self.get_value(37)

    def get_invoice_id(self):
        return self.get_value(61).strip()

    def get_amount(self):
        return int(self.get_value(4))

    def set_amount(self, v):
        self.setBit(4, v)

    def get_channel(self):
        return int(self.get_value(18))  # Merchant / Channel

    def get_bank_ip(self):
        return self.conf['ip']

    def is_reversal_request(self):
        s = self.getMTI()
        return s[:3] == '040' and 'reversal_request_handler'

    def is_reversal_response(self):
        s = self.getMTI()
        return s[:3] == '041'

    def set_reversal_response(self):
        s = self.from_iso.getMTI()
        mti = s[:2] + '1' + s[3:]  # 0400 -> 0410, 0401 -> 0411
        self.setMTI(mti)
        self.copy(from_iso=self.from_iso)
        self.copy([7, 12])

    def set_reversal_request(self):
        self.setMTI('0400')
        self.set_transmission()
        self.set_stan()

    def ack_settlement_date(self, raw):
        msg = ERR_SETTLEMENT_DATE.format(raw=[raw])
        self.ack_other(msg)

    def ack_transaction_datetime(self):
        raw = self.get_transaction_datetime_raw()
        msg = ERR_TRANSACTION_DATETIME.format(raw=[raw])
        self.ack_other(msg)

    def ack_transaction_date(self):
        raw = self.get_transaction_date_raw()
        msg = ERR_TRANSACTION_DATE.format(raw=[raw])
        self.ack_other(msg)

    def ack_transaction_time(self):
        raw = self.get_transaction_time_raw()
        msg = ERR_TRANSACTION_TIME.format(raw=[raw])
        self.ack_other(msg)

    def ack_invalid_number(self):
        msg = ERR_INVALID_NUMBER.format(
            invoice_id=self.from_iso.get_invoice_id())
        self.ack(RC_INVALID_NUMBER, msg)

    def ack_not_allowed(self):
        msg = ERR_INVALID_BANK.format(id=self.from_iso.get_bank_id())
        self.ack(RC_INVALID_NUMBER, msg)

    def ack_not_available(self):
        msg = ERR_NOT_AVAILABLE.format(
            invoice_id=self.from_iso.get_invoice_id())
        self.ack(RC_NOT_AVAILABLE, msg)

    def ack_already_paid(self):
        msg = ERR_ALREADY_PAID.format(
            invoice_id=self.from_iso.get_invoice_id())
        self.ack(RC_ALREADY_PAID)

    def ack_insufficient_fund(self, nilai_tagihan):
        msg = ERR_INSUFFICIENT_FUND.format(
            invoice_id=self.from_iso.get_invoice_id(),
            bayar=self.from_iso.get_amount(),
            tagihan=nilai_tagihan)
        self.ack(RC_INSUFFICIENT_FUND)

    def ack_iso_payment_not_found(self):
        self.ack(RC_NOT_AVAILABLE,
                 ERR_ISO_PAYMENT.format(invoice_id=self.get_invoice_id()))
Exemplo n.º 5
0
 def __init__(self, *args, **kwargs):
     self.settlement = DateVar()
     self.transaction_datetime = DateTimeVar()
     self.transaction_date = DateVar()
     self.transaction_time = TimeVar()
     Network.__init__(self, *args, **kwargs)
Exemplo n.º 6
0
class BaseTransaction(Network):
    def __init__(self, *args, **kwargs):
        self.settlement = DateVar()
        self.transaction_datetime = DateTimeVar()
        self.transaction_date = DateVar()
        self.transaction_time = TimeVar()
        Network.__init__(self, *args, **kwargs)

    # Override
    def get_bit_definition(self):
        return BASE_TRANSACTION_BITS

    # Override
    def get_func_name(self):
        return self.is_echo_test_request() or self.is_sign_on_request() or \
               self.is_sign_off_request() or self.is_inquiry_request() or \
               self.is_payment_request() or self.is_reversal_request()

    # Override
    def set_response(self):
        if self.from_iso.is_network_request():
            Network.set_response(self)
        elif self.from_iso.is_transaction_request():
            self.set_transaction_response()
        elif self.from_iso.is_reversal_request():
            self.set_reversal_response()

    # Override
    def setIsoContent(self, raw):
        Network.setIsoContent(self, raw)
        self.raw = raw

    def is_transaction_request(self):
        return self.getMTI() == '0200'

    def is_transaction_response(self):
        return self.getMTI() == '0210'

    def is_response(self):
        return Network.is_response(self) or self.is_transaction_response() or \
                self.is_reversal_response()

    def get_transaction_code(self):
        return self.get_value(3)

    def set_transaction_code(self, code):
        self.setBit(3, code)

    def get_inquiry_code(self):
        pass

    def is_inquiry_request(self):
        if not self.is_transaction_request():
            return
        code = self.get_transaction_code()
        return code == self.get_inquiry_code() and 'inquiry_request_handler'

    def is_inquiry_response(self):
        return self.get_transaction_code() == self.get_inquiry_code() 

    def get_payment_code(self):
        pass

    def is_payment_request(self):
        if not self.is_transaction_request():
            return
        code = self.get_transaction_code()
        return code == self.get_payment_code() and 'payment_request_handler'

    def is_payment_response(self):
        return self.get_transaction_code() == self.get_payment_code() 

    def set_transaction_response(self):
        self.setMTI('0210')
        # Sesuai kolom yang berisi E (Equal) pada PDF
        self.copy([2, 3, 7, 11, 12, 13, 15, 18, 22, 32, 33, 35, 37, 41, 42, 43])

    def set_transaction_request(self):
        self.setMTI('0200')
        self.set_transmission()
        self.set_stan()

    def get_settlement(self):
        raw = self.get_value(15)
        self.settlement.set_raw(raw)
        try:
            return self.settlement.get_value()
        except ValueError:
            self.ack_settlement_date(raw)

    def get_transaction_datetime_raw(self):
        return self.get_transaction_date_raw() + self.get_transaction_time_raw()

    def get_transaction_datetime(self):
        raw = self.get_transaction_datetime_raw()
        self.transaction_datetime.set_raw(raw)
        try:
            return self.transaction_datetime.get_value()
        except ValueError:
            self.ack_transaction_datetime()

    def get_transaction_time_raw(self):
        return self.get_value(12)

    def get_transaction_time(self):
        raw = self.get_transaction_time_raw()
        self.transaction_time.set_raw(raw)
        try:
            return self.transaction_time.get_value()
        except ValueError:
            self.ack_transaction_time()

    def get_transaction_date_raw(self):
        return self.get_value(13)

    def get_transaction_date(self):
        raw = self.get_transaction_date_raw()
        self.transaction_date.set_raw(raw)
        try:
            return self.transaction_date.get_value()
        except ValueError:
            self.ack_transaction_date()

    def get_channel(self):
        return int(self.get_value(18))

    def get_bank_id(self):
        return int(self.get_value(32))

    def get_forwarder(self):
        return self.get_value(33)

    def get_sequence(self):
        return self.get_value(37)

    def get_invoice_id(self):
        return self.get_value(61).strip()

    def get_amount(self):
        return int(self.get_value(4))

    def set_amount(self, v):
        self.setBit(4, v)

    def get_channel(self):
        return int(self.get_value(18)) # Merchant / Channel 

    def get_bank_ip(self):
        return self.conf['ip']

    def is_reversal_request(self):
        s = self.getMTI()
        return s[:3] == '040' and 'reversal_request_handler'

    def is_reversal_response(self):
        s = self.getMTI()
        return s[:3] == '041'

    def set_reversal_response(self):
        s = self.from_iso.getMTI()
        mti = s[:2] + '1' + s[3:] # 0400 -> 0410, 0401 -> 0411
        self.setMTI(mti)
        self.copy(from_iso=self.from_iso)
        self.copy([7, 12])

    def set_reversal_request(self):
        self.setMTI('0400')
        self.set_transmission()
        self.set_stan()

    def ack_settlement_date(self, raw):
        msg = ERR_SETTLEMENT_DATE.format(raw=[raw])
        self.ack_other(msg)

    def ack_transaction_datetime(self):
        raw = self.get_transaction_datetime_raw()
        msg = ERR_TRANSACTION_DATETIME.format(raw=[raw])
        self.ack_other(msg)

    def ack_transaction_date(self):
        raw = self.get_transaction_date_raw()
        msg = ERR_TRANSACTION_DATE.format(raw=[raw])
        self.ack_other(msg)

    def ack_transaction_time(self):
        raw = self.get_transaction_time_raw()
        msg = ERR_TRANSACTION_TIME.format(raw=[raw])
        self.ack_other(msg)

    def ack_invalid_number(self):
        msg = ERR_INVALID_NUMBER.format(
                invoice_id=self.from_iso.get_invoice_id())
        self.ack(RC_INVALID_NUMBER, msg)

    def ack_not_allowed(self):
        msg = ERR_INVALID_BANK.format(id=self.from_iso.get_bank_id())
        self.ack(RC_INVALID_NUMBER, msg)

    def ack_not_available(self):
        msg = ERR_NOT_AVAILABLE.format(invoice_id=self.from_iso.get_invoice_id())
        self.ack(RC_NOT_AVAILABLE, msg)

    def ack_already_paid(self):
        msg = ERR_ALREADY_PAID.format(invoice_id=self.from_iso.get_invoice_id())
        self.ack(RC_ALREADY_PAID)

    def ack_insufficient_fund(self, nilai_tagihan):
        msg = ERR_INSUFFICIENT_FUND.format(
                invoice_id=self.from_iso.get_invoice_id(),
                bayar=self.from_iso.get_amount(),
                tagihan=nilai_tagihan)
        self.ack(RC_INSUFFICIENT_FUND)

    def ack_iso_payment_not_found(self):
        self.ack(RC_NOT_AVAILABLE, ERR_ISO_PAYMENT.format(
            invoice_id=self.get_invoice_id()))
Exemplo n.º 7
0
class Network(Data):
    def __init__(self, *args, **kwargs):
        if 'log_info' in kwargs:
            self.log_info = kwargs['log_info']
            del kwargs['log_info']
        if 'log_error' in kwargs:
            self.log_error = kwargs['log_error']
            del kwargs['log_error']
        if 'conf' in kwargs:
            self.conf = kwargs['conf']
            del kwargs['conf']
        else:
            self.conf = {}
        Data.__init__(self, *args, **kwargs)
        self.raw = ''
        bits = self.get_bit_definition()
        for bit in bits:
            short_name, long_name, type_, size, format_ = bits[bit]
            self.redefineBit(bit, short_name, long_name, type_, size, format_)
        self.transmission = DateTimeVar()
        if self.from_iso:
            self.set_response()

    def getRawIso(self):
        return str(Data.getRawIso(self)).upper()

    def get_bit_definition(self):
        return NETWORK_BITS

    def get_func_name(self):
        return self.is_echo_test_request() or self.is_sign_on_request() or \
               self.is_sign_off_request()

    def process(self):
        func_name = self.from_iso.get_func_name()
        if func_name:
            func = getattr(self, func_name)
            func()
        else:
            self.ack_function_not_found()

    def is_response(self):
        return self.is_network_response()

    def set_response(self):
        if self.from_iso.is_network_request():
            self.set_network_response()

    def is_ok_response(self):
        try:
            return self.getBit(39) == RC_OK
        except BitNotSet:
            self.log_error('Bit 39 tidak ada.')

    def set_transmission(self):
        kini = datetime.now()
        self.setBit(7, kini.strftime('%m%d%H%M%S'))

    def get_transmission(self):
        raw = self.get_value(7)
        self.transmission.set_raw(raw)
        # without time zone
        t = self.transmission.get_value()
        # with time zone
        return create_datetime(t.year, t.month, t.day, t.hour, t.minute,
                               t.second)

    def set_stan(self):  # System Trace Audit Number
        kini = datetime.now()
        self.setBit(11, kini.strftime('%H%M%S'))

    def get_stan(self):
        return self.get_value(11)

    ###########
    # Network #
    ###########
    def set_network_request(self):
        self.setMTI('0800')
        self.set_transmission()
        self.set_stan()

    def set_network_response(self):
        self.setMTI('0810')
        self.copy([7, 11, 70])

    def set_func_code(self, code):
        self.set_network_request()
        self.setBit(70, code)

    def get_func_code(self):
        return self.getBit(70)

    def is_network_request(self):
        return self.getMTI() == '0800'

    def is_network_response(self):
        return self.getMTI() == '0810'

    ###################
    # Network Sign On #
    ###################
    def is_sign_on_request(self):
        return self.is_network_request() and \
               self.get_func_code() == '001' and \
               'sign_on_response'

    def is_sign_on_response(self):
        return self.is_network_response() and \
               self.get_func_code() == '001'

    def sign_on_request(self):
        self.set_func_code('001')

    def sign_on_response(self):
        self.set_network_response()
        self.ack()

    #####################
    # Network Echo Test #
    #####################
    def is_echo_test_request(self):
        return self.is_network_request() and \
               self.get_func_code() == '301' and \
               'echo_test_response'

    def is_echo_test_response(self):
        return self.is_network_response() and \
               self.get_func_code() == '301'

    def is_echo_test(self):
        return self.is_echo_test_request() or self.is_echo_test_response()

    def echo_test_request(self):
        self.set_func_code('301')

    def echo_test_response(self):
        self.set_network_response()
        self.ack()

    ####################
    # Network Sign Off #
    ####################
    def is_sign_off_request(self):
        return self.is_network_request() and \
               self.get_func_code() == '002' and \
               'sign_off_response'

    def is_sign_off_response(self):
        return self.is_network_request() and self.get_func_code() == '002'

    def is_sign_off(self):
        return self.is_sign_off_request() or self.is_sign_off_response()

    def sign_off_request(self):
        self.set_func_code('002')

    def sign_off_response(self):
        self.set_network_response()
        self.ack()

    ###################
    # Acknowledgement #
    ###################
    def ack(self, code=RC_OK, log_message=''):
        self.setBit(39, code)
        self.set_transmission()
        if log_message:
            if int(code):
                self.log_error(log_message)
            else:
                self.log_info(log_message)

    def ack_other(self, msg='Ada masalah yang belum dipahami'):
        self.ack(RC_OTHER_ERROR, msg)

    def ack_function_not_found(self):
        self.ack_other('Fungsi tidak ditemukan')

    def ack_unknown(self):
        s = exception_message()
        self.log_error(s)
        self.ack_other('Unknown')
Exemplo n.º 8
0
class Network(Data):
    def __init__(self, *args, **kwargs):
        if 'log_info' in kwargs:
            self.log_info = kwargs['log_info']
            del kwargs['log_info']
        if 'log_error' in kwargs:
            self.log_error = kwargs['log_error']
            del kwargs['log_error']
        if 'conf' in kwargs:
            self.conf = kwargs['conf']
            del kwargs['conf']
        else:
            self.conf = {}
        Data.__init__(self, *args, **kwargs)
        self.raw = ''
        bits = self.get_bit_definition()
        for bit in bits: 
            short_name, long_name, type_, size, format_ = bits[bit]
            self.redefineBit(bit, short_name, long_name, type_, size, format_)
        self.transmission = DateTimeVar()
        if self.from_iso:
            self.set_response()

    def getRawIso(self):
        return str(Data.getRawIso(self)).upper()

    def get_bit_definition(self):
        return NETWORK_BITS

    def get_func_name(self):
        return self.is_echo_test_request() or self.is_sign_on_request() or \
               self.is_sign_off_request()

    def process(self):
        func_name = self.from_iso.get_func_name()
        if func_name:
            func = getattr(self, func_name)
            func()
        else:
            self.ack_function_not_found()

    def is_response(self):
        return self.is_network_response()

    def set_response(self):
        if self.from_iso.is_network_request():
            self.set_network_response()

    def is_ok_response(self):
        try:
            return self.getBit(39) == RC_OK 
        except BitNotSet:
            self.log_error('Bit 39 tidak ada.')

    def set_transmission(self):
        kini = datetime.now()
        self.setBit(7, kini.strftime('%m%d%H%M%S'))

    def get_transmission(self):
        raw = self.get_value(7)
        self.transmission.set_raw(raw)
        # without time zone
        t = self.transmission.get_value()
        # with time zone
        return create_datetime(t.year, t.month, t.day, t.hour, t.minute, t.second)

    def set_stan(self): # System Trace Audit Number
        kini = datetime.now()
        self.setBit(11, kini.strftime('%H%M%S'))

    def get_stan(self):
        return self.get_value(11)

    ###########
    # Network #
    ###########
    def set_network_request(self):
        self.setMTI('0800')
        self.set_transmission()
        self.set_stan()

    def set_network_response(self):
        self.setMTI('0810')
        self.copy([7, 11, 70])

    def set_func_code(self, code):
        self.set_network_request()
        self.setBit(70, code)

    def get_func_code(self):
        return self.getBit(70)

    def is_network_request(self):
        return self.getMTI() == '0800'

    def is_network_response(self):
        return self.getMTI() == '0810' 

    ###################
    # Network Sign On #
    ###################
    def is_sign_on_request(self):
        return self.is_network_request() and \
               self.get_func_code() == '001' and \
               'sign_on_response'

    def is_sign_on_response(self):
        return self.is_network_response() and \
               self.get_func_code() == '001'

    def sign_on_request(self):
        self.set_func_code('001')

    def sign_on_response(self):
        self.set_network_response()
        self.ack()

    #####################
    # Network Echo Test #
    #####################
    def is_echo_test_request(self):
        return self.is_network_request() and \
               self.get_func_code() == '301' and \
               'echo_test_response'

    def is_echo_test_response(self):
        return self.is_network_response() and \
               self.get_func_code() == '301'

    def is_echo_test(self):
        return self.is_echo_test_request() or self.is_echo_test_response()

    def echo_test_request(self):
        self.set_func_code('301')

    def echo_test_response(self):
        self.set_network_response()
        self.ack()

    ####################
    # Network Sign Off #
    ####################
    def is_sign_off_request(self):
        return self.is_network_request() and \
               self.get_func_code() == '002' and \
               'sign_off_response'

    def is_sign_off_response(self):
        return self.is_network_request() and self.get_func_code() == '002'

    def is_sign_off(self):
        return self.is_sign_off_request() or self.is_sign_off_response()

    def sign_off_request(self):
        self.set_func_code('002')

    def sign_off_response(self):
        self.set_network_response()
        self.ack()

    ###################
    # Acknowledgement #
    ###################
    def ack(self, code=RC_OK, log_message=''):
        self.setBit(39, code)
        self.set_transmission()
        if log_message:
            if int(code):
                self.log_error(log_message)
            else:
                self.log_info(log_message)

    def ack_other(self, msg='Ada masalah yang belum dipahami'):
        self.ack(RC_OTHER_ERROR, msg)

    def ack_function_not_found(self):
        self.ack_other('Fungsi tidak ditemukan')

    def ack_unknown(self):
        s = exception_message()
        self.log_error(s)
        self.ack_other('Unknown')