|
| 1 | +import unittest |
| 2 | + |
| 3 | +from constants import ( |
| 4 | + API_CONFIG, |
| 5 | + SPACE_ID, |
| 6 | + get_transaction_create, |
| 7 | + FAKE_CARD_DATA, |
| 8 | + TEST_CARD_PAYMENT_METHOD_CONFIGURATION_ID, |
| 9 | + TEST_ISR_INVOICE_PAYMENT_METHOD_CONFIGURATION_ID, |
| 10 | +) |
| 11 | + |
| 12 | +from wallee.api import ( |
| 13 | + TransactionServiceApi, |
| 14 | + CardProcessingServiceApi, |
| 15 | + TransactionInvoiceServiceApi, |
| 16 | + TransactionCompletionServiceApi, |
| 17 | +) |
| 18 | +from wallee.models import ( |
| 19 | + TransactionState, |
| 20 | + TransactionInvoiceState, |
| 21 | + EntityQuery, |
| 22 | + EntityQueryFilter, |
| 23 | + EntityQueryFilterType, |
| 24 | + CriteriaOperator, |
| 25 | + TokenizationMode, |
| 26 | + TransactionCompletionBehavior, |
| 27 | + CustomersPresence, |
| 28 | +) |
| 29 | + |
| 30 | + |
| 31 | +class TransactionInvoiceServiceTest(unittest.TestCase): |
| 32 | + """TransactionInvoiceServiceApi tests""" |
| 33 | + |
| 34 | + def setUp(self): |
| 35 | + self.transaction_service = TransactionServiceApi(API_CONFIG) |
| 36 | + self.transaction_invoice_service = TransactionInvoiceServiceApi(API_CONFIG) |
| 37 | + self.transaction_completion_service = TransactionCompletionServiceApi( |
| 38 | + API_CONFIG) |
| 39 | + self.card_processing_service = CardProcessingServiceApi(API_CONFIG) |
| 40 | + |
| 41 | + def tearDown(self): |
| 42 | + pass |
| 43 | + |
| 44 | + def test_search(self): |
| 45 | + """search() should find transaction invoice by a given criteria""" |
| 46 | + |
| 47 | + transaction_create = get_transaction_create() |
| 48 | + transaction_create.tokenization_mode = TokenizationMode.FORCE_CREATION |
| 49 | + transaction_create.customers_presence = CustomersPresence.NOT_PRESENT |
| 50 | + transaction_create.completion_behavior = TransactionCompletionBehavior.COMPLETE_IMMEDIATELY |
| 51 | + transaction = self.transaction_service.create(space_id=SPACE_ID, transaction=transaction_create) |
| 52 | + |
| 53 | + transaction_processed = self.card_processing_service.process( |
| 54 | + space_id=SPACE_ID, |
| 55 | + transaction_id=transaction.id, |
| 56 | + payment_method_configuration_id=TEST_CARD_PAYMENT_METHOD_CONFIGURATION_ID, |
| 57 | + card_data=FAKE_CARD_DATA, |
| 58 | + ) |
| 59 | + |
| 60 | + self.assertEqual( |
| 61 | + TransactionState.FULFILL, |
| 62 | + transaction_processed.state, |
| 63 | + "State must be FULFILL", |
| 64 | + ) |
| 65 | + |
| 66 | + entity_query_filter = EntityQueryFilter( |
| 67 | + # linkedTransaction does not work here as criteria |
| 68 | + field_name="completion.lineItemVersion.transaction.id", |
| 69 | + value=transaction_processed.id, |
| 70 | + type=EntityQueryFilterType.LEAF, |
| 71 | + operator=CriteriaOperator.EQUALS, |
| 72 | + ) |
| 73 | + entity_query = EntityQuery(filter=entity_query_filter) |
| 74 | + |
| 75 | + invoices_found = self.transaction_invoice_service.search( |
| 76 | + space_id=SPACE_ID, query=entity_query) |
| 77 | + |
| 78 | + self.assertTrue(len(invoices_found) > 0, |
| 79 | + "Should find invoice", ) |
| 80 | + |
| 81 | + for invoice in invoices_found: |
| 82 | + self.assertEqual(TransactionInvoiceState.NOT_APPLICABLE, invoice.state, |
| 83 | + "Invoice paid by card is expected to be of NOT_APPLICABLE state") |
| 84 | + |
| 85 | + |
| 86 | + |
| 87 | + def test_derecognize_transaction_invoice(self): |
| 88 | + """mark_as_derecognized() should derecognize open invoice""" |
| 89 | + |
| 90 | + transaction_create = get_transaction_create() |
| 91 | + transaction_create.tokenization_mode = TokenizationMode.FORCE_CREATION |
| 92 | + transaction_create.customers_presence = CustomersPresence.NOT_PRESENT |
| 93 | + transaction_create.completion_behavior = TransactionCompletionBehavior.COMPLETE_DEFERRED |
| 94 | + |
| 95 | + # we want invoice in OPEN state (OPEN invoices can be derecognized), so we force payment by invoice |
| 96 | + transaction_create.allowed_payment_method_configurations = [TEST_ISR_INVOICE_PAYMENT_METHOD_CONFIGURATION_ID] |
| 97 | + transaction = self.transaction_service.create(space_id=SPACE_ID, transaction=transaction_create) |
| 98 | + |
| 99 | + transaction_processed = self.transaction_service.process_without_user_interaction( |
| 100 | + space_id=SPACE_ID, id=transaction.id) |
| 101 | + |
| 102 | + transaction_completion = self.transaction_completion_service.complete_online( |
| 103 | + space_id=SPACE_ID, id=transaction_processed.id) |
| 104 | + |
| 105 | + entity_query_filter = EntityQueryFilter( |
| 106 | + field_name="completion.lineItemVersion.transaction.id", |
| 107 | + value=transaction_processed.id, |
| 108 | + type=EntityQueryFilterType.LEAF, |
| 109 | + operator=CriteriaOperator.EQUALS, |
| 110 | + ) |
| 111 | + entity_query = EntityQuery(filter=entity_query_filter) |
| 112 | + |
| 113 | + invoices_found = self.transaction_invoice_service.search( |
| 114 | + space_id=SPACE_ID, query=entity_query) |
| 115 | + |
| 116 | + self.assertTrue(len(invoices_found) > 0, |
| 117 | + "Should find invoice", ) |
| 118 | + |
| 119 | + found_invoice = invoices_found[0] |
| 120 | + |
| 121 | + self.assertEqual(TransactionInvoiceState.OPEN, found_invoice.state, |
| 122 | + "Transaction paid by invoice should create invoice in OPEN state" ) |
| 123 | + |
| 124 | + derecognized_invoice = self.transaction_invoice_service.mark_as_derecognized( |
| 125 | + space_id=SPACE_ID, id=found_invoice.id); |
| 126 | + |
| 127 | + self.assertEqual(TransactionInvoiceState.DERECOGNIZED, derecognized_invoice.state, |
| 128 | + "Expected DERECOGNIZED invoice state") |
| 129 | + |
| 130 | + |
| 131 | +if __name__ == "__main__": |
| 132 | + unittest.main(failfast=True) |
0 commit comments