def parse_data_file(filename, term): """ """ data_tree = ET.parse(filename) data_root = data_tree.getroot() cards = OrderedDict() for item in data_root: if item.tag == 'card': c = parse_card_data(item) if c: cards[c.get_str_card_number()] = c if c.get_description(): cards[c.get_description()] = c if not cards: # No <card> items in transaction file: c = Card() cards[c.get_str_card_number()] = c transactions = [] for item in data_root: if item.tag == 'trxn': trxns = parse_transaction_item(item, term, cards) if trxns: for t in trxns: transactions.append(t) return transactions
def setUp(self): self.pan = '4444555566667777' self.expiry_date = '1120' self.service_code = '101' self.PVV_key_index = 1 self.PVV = '8723' self.CVV = '000' self.discretionary_data = '00720' self.card = Card(pan=self.pan, expiry_date=self.expiry_date, service_code=self.service_code, pvvki=self.PVV_key_index, PVV=self.PVV, CVV=self.CVV, discretionary_data=self.discretionary_data)
def parse_card_data(card): """ """ pan = None expiry_date = None service_code = None PVVKi = None PVV = None CVV = None discr_data = None description = '' try: description = card.attrib['description'] except: pass for attrib in card: if attrib.tag.lower() == 'pan': pan = attrib.text elif attrib.tag.lower() == 'expiry_date': expiry_date = attrib.text elif attrib.tag.lower() == 'service_code': service_code = attrib.text elif attrib.tag.lower() == 'pvvki': PVVKi = attrib.text elif attrib.tag.lower() == 'pvv': PVV = attrib.text elif attrib.tag.lower() == 'cvv': CVV = attrib.text elif attrib.tag.lower() == 'discr_data': discr_data = attrib.text return Card(pan=pan, expiry_date=expiry_date, service_code=service_code, pvvki=PVVKi, PVV=PVV, CVV=CVV, discretionary_data=discr_data, description=description)
class TestCardClass(unittest.TestCase): def setUp(self): self.pan = '4444555566667777' self.expiry_date = '1120' self.service_code = '101' self.PVV_key_index = 1 self.PVV = '8723' self.CVV = '000' self.discretionary_data = '00720' self.card = Card(pan=self.pan, expiry_date=self.expiry_date, service_code=self.service_code, pvvki=self.PVV_key_index, PVV=self.PVV, CVV=self.CVV, discretionary_data=self.discretionary_data) def test_get_track2(self): self.assertEqual(self.card.get_track2(), '4444555566667777=11201011872300000720') def test_get_service_code(self): self.assertEqual(self.card.get_service_code(), '101') def test_get_expiry_date(self): self.assertEqual(self.card.get_expiry_date(), 1120) def test_get_card_number(self): self.assertEqual(self.card.get_card_number(), 4444555566667777) def test_get_transaction_counter_length_1(self): self.card._set_transaction_counter(1) self.assertEqual(self.card.get_transaction_counter(), '0001') def test_get_transaction_counter_length_2(self): self.card._set_transaction_counter(88) self.assertEqual(self.card.get_transaction_counter(), '0088') def test_get_transaction_counter_length_3(self): self.card._set_transaction_counter(777) self.assertEqual(self.card.get_transaction_counter(), '0777') def test_get_transaction_counter_length_4(self): self.card._set_transaction_counter(9999) self.assertEqual(self.card.get_transaction_counter(), '9999')
def setUp(self): self.term = Terminal(show_keys=False) self.card = Card() self.trxn = Transaction('echo', self.card, self.term) self.trxn.set_description('Test echo')
class TestTransactionClass(unittest.TestCase): def setUp(self): self.term = Terminal(show_keys=False) self.card = Card() self.trxn = Transaction('echo', self.card, self.term) self.trxn.set_description('Test echo') """ trxn.set_expected_action() """ def test_set_expected_action_approve(self): self.assertEqual(self.trxn.set_expected_action('approve'), True) self.assertEqual(self.trxn.expected_response_action, 'APPROVE') def test_set_expected_action_decline(self): self.assertEqual(self.trxn.set_expected_action('decline'), True) self.assertEqual(self.trxn.expected_response_action, 'DECLINE') def test_set_expected_action_mixed_case(self): self.assertEqual(self.trxn.set_expected_action('ApPrOVe'), True) self.assertEqual(self.trxn.expected_response_action, 'APPROVE') def test_set_expected_action_empty(self): self.assertEqual(self.trxn.set_expected_action(''), False) self.assertEqual(self.trxn.expected_response_action, None) def test_set_expected_action_unknown(self): self.assertEqual(self.trxn.set_expected_action('no reason to decline'), False) self.assertEqual(self.trxn.expected_response_action, None) """ trxn.set_STAN() """ def test_set_stan_empty(self): with self.assertRaisesRegex(ValueError, "Invalid STAN"): self.trxn.set_STAN('') def test_set_stan_negative(self): with self.assertRaisesRegex(ValueError, "Invalid STAN"): self.trxn.set_STAN('-3') def test_set_stan_overflow(self): with self.assertRaisesRegex(ValueError, "Invalid STAN"): self.trxn.set_STAN('1234567') def test_set_stan_applied_passed_as_integer(self): self.trxn.set_STAN(989898) self.assertEqual(self.trxn.IsoMessage.FieldData(11), 989898) def test_set_stan_applied_passed_as_string(self): self.trxn.set_STAN('744321') self.assertEqual(self.trxn.IsoMessage.FieldData(11), 744321) """ trxn.is_response_expected() """ def test_is_response_expected_resp_codes_resp_action_are_not_set(self): self.assertEqual(self.trxn.is_response_expected('000'), True) def test_is_response_expected_resp_codes_equal(self): self.trxn.set_expected_code('000') self.assertEqual(self.trxn.is_response_expected('000'), True) def test_is_response_expected_resp_codes_not_equal(self): self.trxn.set_expected_code('999') self.assertEqual(self.trxn.is_response_expected('000'), False) def test_is_response_expected_resp_action_APPROVED_resp_code_000(self): self.trxn.set_expected_action('APPROVED') self.assertEqual(self.trxn.is_response_expected('000'), True) def test_is_response_expected_resp_action_APPROVED_resp_code_999(self): self.trxn.set_expected_action('APPROVED') self.assertEqual(self.trxn.is_response_expected('999'), False) def test_is_response_expected_resp_action_DECLINED_resp_code_000(self): self.trxn.set_expected_action('DECLINED') self.assertEqual(self.trxn.is_response_expected('000'), False) def test_is_response_expected_resp_action_DECLINED_resp_code_999(self): self.trxn.set_expected_action('DECLINED') self.assertEqual(self.trxn.is_response_expected('777'), True) """ trxn.get_description() """ def test_trxn_description_card_description_empty(self): self.assertEqual(self.trxn.get_description(), 'Test echo') def test_trxn_description_card_description_not_empty(self): card_description = 'On-us card 445566' self.card.set_description(card_description) self.assertEqual(self.trxn.get_description(), card_description + ' | Test echo') """ trxn.set_currency() """ def test_trxn_set_valid_currency(self): self.trxn.set_currency('USD') self.assertEqual(self.trxn.get_currency(), 840) def test_trxn_set_invalid_currency(self): self.trxn.set_currency('IDDQD') self.assertIsNone(self.trxn.get_currency()) """ trxn.set_processing_code() """ def test_trxn_set_processing_code_balance(self): self.trxn.type = 'balance' self.trxn.set_processing_code() self.assertEqual(self.trxn.processing_code, 310000) def test_trxn_set_processing_code_logon(self): self.trxn.type = 'logon' self.trxn.set_processing_code() self.assertEqual(self.trxn.processing_code, 990000) def test_trxn_set_processing_code_echo(self): self.trxn.type = 'echo' self.trxn.set_processing_code() self.assertEqual(self.trxn.processing_code, 990000) def test_trxn_set_processing_code_key_change(self): self.trxn.type = 'key change' self.trxn.set_processing_code() self.assertEqual(self.trxn.processing_code, 990000) def test_trxn_set_processing_code_purchase(self): self.trxn.type = 'purchase' self.trxn.set_processing_code() self.assertEqual(self.trxn.processing_code, 0) def test_trxn_set_processing_code_purchase_w_account_from(self): self.trxn.type = 'purchase' self.trxn.account_from = '10' self.trxn.set_processing_code() self.assertEqual(self.trxn.processing_code, 1000) def test_trxn_set_processing_code_purchase_w_account_to(self): self.trxn.type = 'purchase' self.trxn.account_to = '20' self.trxn.set_processing_code() self.assertEqual(self.trxn.processing_code, 20) def test_trxn_set_processing_code_virtual_purchase(self): self.trxn.type = 'virtual purchase' self.trxn.set_processing_code() self.assertEqual(self.trxn.processing_code, 150000) def test_trxn_set_processing_code_refund(self): self.trxn.type = 'refund' self.trxn.set_processing_code() self.assertEqual(self.trxn.processing_code, 200000) def test_trxn_set_processing_code_pin_change(self): self.trxn.type = 'pin change' self.trxn.set_processing_code() self.assertEqual(self.trxn.processing_code, 760000) def test_trxn_set_processing_code_pin_change_reversal(self): self.trxn.type = 'pin change reversal' self.trxn.set_processing_code() self.assertEqual(self.trxn.processing_code, 760000) def test_trxn_set_processing_code_cash(self): self.trxn.type = 'cash' self.trxn.set_processing_code() self.assertEqual(self.trxn.processing_code, 120000)
elif opt in ('-m', '--merchant'): merchant_id = arg elif opt in ('-k', '--terminal-key'): terminal_key = arg elif opt in ('-K', '--master-key'): master_key = arg elif opt in ('-f', '--file'): data_file = arg except getopt.GetoptError: show_help(sys.argv[0]) sys.exit() term = Terminal(host=ip, port=port, id=terminal_id, merchant=merchant_id, terminal_key=terminal_key, master_key=master_key, show_keys=True) if data_file: transactions = parse_data_file(data_file, term) pos = isoClient(term, Card(), transactions) pos.set_verbosity_level(verbosity) pos.run()