def test_search_account_failed_payments(session): """Assert that the search account payments is working.""" inv_number_1 = 'REG00001' payment_account = factory_payment_account().save() invoice_1 = factory_invoice(payment_account) invoice_1.save() inv_ref_1 = factory_invoice_reference(invoice_1.id, invoice_number=inv_number_1).save() payment_1 = factory_payment(payment_status_code='FAILED', payment_account_id=payment_account.id, invoice_number=inv_number_1, payment_method_code=PaymentMethod.PAD.value) payment_1.save() auth_account_id = PaymentAccount.find_by_id( payment_account.id).auth_account_id results = Payment_service.search_account_payments( auth_account_id=auth_account_id, status='FAILED', limit=1, page=1) assert results.get('items') assert results.get('total') == 1 # Create one more payment with failed status. inv_number_2 = 'REG00002' invoice_2 = factory_invoice(payment_account) invoice_2.save() inv_ref_2 = factory_invoice_reference(invoice_2.id, invoice_number=inv_number_2).save() payment_2 = factory_payment(payment_status_code='FAILED', payment_account_id=payment_account.id, invoice_number=inv_number_2, payment_method_code=PaymentMethod.PAD.value) payment_2.save() auth_account_id = PaymentAccount.find_by_id( payment_account.id).auth_account_id results = Payment_service.search_account_payments( auth_account_id=auth_account_id, status='FAILED', limit=1, page=1) assert results.get('items') assert results.get('total') == 2 # Now combine both payments into one, by setting status to invoice reference. - NSF payments inv_ref_1.status_code = InvoiceReferenceStatus.CANCELLED.value inv_ref_2.status_code = InvoiceReferenceStatus.CANCELLED.value inv_ref_1.save() inv_ref_2.save() # Now create new invoice reference for consolidated invoice inv_number_3 = 'REG00003' factory_invoice_reference(invoice_1.id, invoice_number=inv_number_3).save() factory_invoice_reference(invoice_2.id, invoice_number=inv_number_3).save() results = Payment_service.search_account_payments( auth_account_id=auth_account_id, status='FAILED', limit=1, page=1) # Now there are no active failed payments, so it should return zero records assert not results.get('items') assert results.get('total') == 0
def test_create_account_payments_for_one_failed_payment(session): """Assert that the create account payments is working.""" inv_number_1 = 'REG00001' payment_account = factory_payment_account().save() invoice_1 = factory_invoice(payment_account) invoice_1.save() factory_invoice_reference(invoice_1.id, invoice_number=inv_number_1).save() payment_1 = factory_payment(payment_status_code='FAILED', payment_account_id=payment_account.id, invoice_number=inv_number_1, payment_method_code=PaymentMethod.PAD.value) payment_1.save() auth_account_id = PaymentAccount.find_by_id( payment_account.id).auth_account_id results = Payment_service.search_account_payments( auth_account_id=auth_account_id, status='FAILED', limit=1, page=1) assert results.get('total') == 1 new_payment = Payment_service.create_account_payment( auth_account_id=auth_account_id, is_retry_payment=True) old_payment = Payment_service.find_by_id(payment_1.id) # Assert new payment invoice number is same as old payment as there is only one failed payment. assert new_payment.invoice_number == old_payment.invoice_number
def test_search_payment_history_with_tz(session): """Assert that the search payment history is working.""" payment_account = factory_payment_account() payment_created_on = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0) payment_created_on = payment_created_on.astimezone(pytz.utc) payment = factory_payment(payment_status_code='CREATED', created_on=payment_created_on) payment_account.save() payment.save() invoice = factory_invoice(payment, payment_account) invoice.save() factory_invoice_reference(invoice.id).save() auth_account_id = PaymentAccount.find_by_id(payment_account.account_id).auth_account_id results = Payment_service.search_purchase_history(auth_account_id=auth_account_id, search_filter={}, limit=1, page=1) assert results is not None assert results.get('items') is not None assert results.get('total') == 1 # Add one more payment payment_created_on = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0) payment_created_on = payment_created_on.astimezone(pytz.utc) payment = factory_payment(payment_status_code='CREATED', created_on=payment_created_on) payment_account.save() payment.save() invoice = factory_invoice(payment, payment_account) invoice.save() factory_invoice_reference(invoice.id).save() results = Payment_service.search_purchase_history(auth_account_id=auth_account_id, search_filter={}, limit=1, page=1) assert results is not None assert results.get('items') is not None assert results.get('total') == 2
def test_create_account_payments_for_multiple_failed_payments(session): """Assert that the create account payments is working.""" inv_number_1 = 'REG00001' payment_account = factory_payment_account().save() invoice_1 = factory_invoice(payment_account, total=100) invoice_1.save() factory_payment_line_item(invoice_id=invoice_1.id, fee_schedule_id=1).save() factory_invoice_reference(invoice_1.id, invoice_number=inv_number_1).save() payment_1 = factory_payment(payment_status_code='FAILED', payment_account_id=payment_account.id, invoice_number=inv_number_1, invoice_amount=100, payment_method_code=PaymentMethod.PAD.value) payment_1.save() # Create one more payment with failed status. inv_number_2 = 'REG00002' invoice_2 = factory_invoice(payment_account, total=100) invoice_2.save() factory_payment_line_item(invoice_id=invoice_2.id, fee_schedule_id=1).save() factory_invoice_reference(invoice_2.id, invoice_number=inv_number_2).save() payment_2 = factory_payment(payment_status_code='FAILED', payment_account_id=payment_account.id, invoice_number=inv_number_2, invoice_amount=100, payment_method_code=PaymentMethod.PAD.value) payment_2.save() auth_account_id = PaymentAccount.find_by_id( payment_account.id).auth_account_id results = Payment_service.search_account_payments( auth_account_id=auth_account_id, status='FAILED', limit=10, page=1) assert results.get('total') == 2 new_payment = Payment_service.create_account_payment( auth_account_id=auth_account_id, is_retry_payment=True) payment_1 = Payment_service.find_by_id(payment_1.id) payment_2 = Payment_service.find_by_id(payment_2.id) # Assert new payment invoice number is different from old payment as there are more than one failed payments. assert new_payment.invoice_number != payment_1.invoice_number assert new_payment.invoice_number != payment_2.invoice_number assert payment_1.cons_inv_number == new_payment.invoice_number assert payment_2.cons_inv_number == new_payment.invoice_number assert new_payment.invoice_amount == payment_1.invoice_amount + payment_2.invoice_amount
def test_create_payment_report_pdf(session, rest_call_mock): """Assert that the create payment report is working.""" payment_account = factory_payment_account() payment_account.save() auth_account_id = PaymentAccount.find_by_id(payment_account.account_id).auth_account_id for i in range(20): payment = factory_payment(payment_status_code='CREATED') payment.save() invoice = factory_invoice(payment, payment_account) invoice.save() factory_invoice_reference(invoice.id).save() Payment_service.create_payment_report(auth_account_id=auth_account_id, search_filter={}, content_type='application/pdf', report_name='test') assert True # If no error, then good
def complete_payment(self, invoice, invoice_reference): """Create payment and related records as if the payment is complete.""" Payment.create(payment_method=self.get_payment_method_code(), payment_system=self.get_payment_system_code(), payment_status=PaymentStatus.COMPLETED.value, invoice_number=invoice_reference.invoice_number, invoice_amount=invoice.total, payment_account_id=invoice.payment_account_id) invoice.invoice_status_code = InvoiceStatus.PAID.value invoice.paid = invoice.total invoice_reference.status_code = InvoiceReferenceStatus.COMPLETED.value # Create receipt. receipt = Receipt() receipt.receipt_number = invoice_reference.invoice_number receipt.receipt_amount = invoice.total receipt.invoice_id = invoice.id receipt.receipt_date = datetime.now() receipt.save()
def test_create_account_payments_after_consolidation(session): """Assert creating account payments after consolidation yields same payment record.""" inv_number_1 = 'REG00001' payment_account = factory_payment_account().save() invoice_1 = factory_invoice(payment_account, total=100) invoice_1.save() factory_payment_line_item(invoice_id=invoice_1.id, fee_schedule_id=1).save() factory_invoice_reference(invoice_1.id, invoice_number=inv_number_1).save() payment_1 = factory_payment(payment_status_code='FAILED', payment_account_id=payment_account.id, invoice_number=inv_number_1, invoice_amount=100, payment_method_code=PaymentMethod.PAD.value) payment_1.save() # Create one more payment with failed status. inv_number_2 = 'REG00002' invoice_2 = factory_invoice(payment_account, total=100) invoice_2.save() factory_payment_line_item(invoice_id=invoice_2.id, fee_schedule_id=1).save() factory_invoice_reference(invoice_2.id, invoice_number=inv_number_2).save() payment_2 = factory_payment(payment_status_code='FAILED', payment_account_id=payment_account.id, invoice_number=inv_number_2, invoice_amount=100, payment_method_code=PaymentMethod.PAD.value) payment_2.save() auth_account_id = PaymentAccount.find_by_id( payment_account.id).auth_account_id results = Payment_service.search_account_payments( auth_account_id=auth_account_id, status='FAILED', limit=10, page=1) assert results.get('total') == 2 new_payment_1 = Payment_service.create_account_payment( auth_account_id=auth_account_id, is_retry_payment=True) # Create account payment again and assert both payments returns same. new_payment_2 = Payment_service.create_account_payment( auth_account_id=auth_account_id, is_retry_payment=True) assert new_payment_1.id == new_payment_2.id
def test_payment_with_no_active_invoice(session): """Assert that the payment is saved to the table.""" payment_account = factory_payment_account() payment = factory_payment() payment_account.save() payment.save() invoice = factory_invoice(payment, payment_account, Status.DELETED.value) invoice.save() factory_invoice_reference(invoice.id).save() p = Payment_service.find_by_id(payment.id, skip_auth_check=True) assert p is not None assert p.id is not None
def test_payment_saved_from_new(session): """Assert that the payment is saved to the table.""" payment_account = factory_payment_account() payment = factory_payment() payment_account.save() payment.save() invoice = factory_invoice(payment_account) invoice.save() factory_invoice_reference(invoice.id).save() p = Payment_service.find_by_id(payment.id) assert p is not None assert p.id is not None assert p.payment_system_code is not None assert p.payment_method_code is not None assert p.payment_status_code is not None
def test_payment_with_no_active_invoice(session): """Assert that the payment is saved to the table.""" payment_account = factory_payment_account() payment = factory_payment() payment_account.save() payment.save() invoice = factory_invoice(payment.id, payment_account.id, Status.CANCELLED.value) invoice.save() p = Payment_service.find_by_id(payment.id) assert p is not None assert p.id is not None json = p.asdict() assert json.get('invoices', None) is None
def test_payment_saved_from_new(session): """Assert that the payment is saved to the table.""" payment_account = factory_payment_account() payment = factory_payment() payment_account.save() payment.save() p = Payment_service.find_by_id(payment.id) assert p is not None assert p.id is not None assert p.payment_system_code is not None assert p.payment_method_code is not None assert p.payment_status_code is not None assert p.created_by is not None assert p.created_on is not None assert p.updated_on is None assert p.updated_by is None
def test_search_payment_history_for_all(session): """Assert that the search payment history is working.""" payment_account = factory_payment_account() payment_account.save() auth_account_id = PaymentAccount.find_by_id(payment_account.account_id).auth_account_id for i in range(20): payment = factory_payment(payment_status_code='CREATED') payment.save() invoice = factory_invoice(payment, payment_account) invoice.save() factory_invoice_reference(invoice.id).save() results = Payment_service.search_all_purchase_history(auth_account_id=auth_account_id, search_filter={}) assert results is not None assert results.get('items') is not None # Returns only the default number if payload is empty assert results.get('total') == 10
def test_search_account_payments(session): """Assert that the search account payments is working.""" inv_number = 'REG00001' payment_account = factory_payment_account().save() invoice_1 = factory_invoice(payment_account) invoice_1.save() factory_invoice_reference(invoice_1.id, invoice_number=inv_number).save() payment_1 = factory_payment(payment_status_code='CREATED', payment_account_id=payment_account.id, invoice_number=inv_number, payment_method_code=PaymentMethod.PAD.value) payment_1.save() auth_account_id = PaymentAccount.find_by_id( payment_account.id).auth_account_id results = Payment_service.search_account_payments( auth_account_id=auth_account_id, status=None, limit=1, page=1) assert results is not None assert results.get('items') is not None assert results.get('total') == 1
def _create_rs_invoices(cls): # pylint: disable=too-many-locals """Create RS invoices in to CFS system.""" # Find all pending routing slips. # find all routing slip invoices [cash or cheque] # create invoices in csf # do the receipt apply invoices: List[InvoiceModel] = db.session.query(InvoiceModel) \ .join(RoutingSlipModel, RoutingSlipModel.number == InvoiceModel.routing_slip) \ .join(CfsAccountModel, CfsAccountModel.account_id == RoutingSlipModel.payment_account_id) \ .filter(InvoiceModel.payment_method_code == PaymentMethod.INTERNAL.value) \ .filter(InvoiceModel.invoice_status_code == InvoiceStatus.APPROVED.value) \ .filter(CfsAccountModel.status.in_([CfsAccountStatus.ACTIVE.value, CfsAccountStatus.FREEZE.value])) \ .filter(InvoiceModel.routing_slip is not None) \ .order_by(InvoiceModel.created_on.asc()).all() current_app.logger.info(f'Found {len(invoices)} to be created in CFS.') for invoice in invoices: # Create a CFS invoice current_app.logger.debug( f'Creating cfs invoice for invoice {invoice.id}') routing_slip = RoutingSlipModel.find_by_number( invoice.routing_slip) # If routing slip is not found in Pay-DB, assume legacy RS and move on to next one. if not routing_slip: continue routing_slip_payment_account: PaymentAccountModel = PaymentAccountModel.find_by_id( routing_slip.payment_account_id) # apply invoice to the active CFS_ACCOUNT which will be the parent routing slip active_cfs_account = CfsAccountModel.find_effective_by_account_id( routing_slip_payment_account.id) invoice_response = CFSService.create_account_invoice( transaction_number=invoice.id, line_items=invoice.payment_line_items, cfs_account=active_cfs_account) invoice_number = invoice_response.json().get( 'invoice_number', None) current_app.logger.info( f'invoice_number {invoice_number} created in CFS.') has_error_in_apply_receipt = RoutingSlipTask.apply_routing_slips_to_invoice( routing_slip_payment_account, active_cfs_account, routing_slip, invoice, invoice_number) if has_error_in_apply_receipt: # move on to next invoice continue invoice_reference: InvoiceReference = InvoiceReference.create( invoice.id, invoice_number, invoice_response.json().get('pbc_ref_number', None)) current_app.logger.debug('>create_invoice') # leave the status as PAID invoice_reference.status_code = InvoiceReferenceStatus.COMPLETED.value invoice.invoice_status_code = InvoiceStatus.PAID.value invoice.paid = invoice.total Payment.create(payment_method=PaymentMethod.INTERNAL.value, payment_system=PaymentSystem.INTERNAL.value, payment_status=PaymentStatus.COMPLETED.value, invoice_number=invoice_reference.invoice_number, invoice_amount=invoice.total, payment_account_id=invoice.payment_account_id) invoice.save()
def test_search_payment_history(session): """Assert that the search payment history is working.""" payment_account = factory_payment_account() payment = factory_payment(payment_status_code='CREATED') payment_account.save() payment.save() invoice = factory_invoice(payment, payment_account) invoice.save() factory_invoice_reference(invoice.id).save() auth_account_id = PaymentAccount.find_by_id( payment_account.account_id).auth_account_id results = Payment_service.search_purchase_history( auth_account_id=auth_account_id, search_filter={}, limit=1, page=1) assert results is not None assert results.get('items') is not None assert results.get('total') == 1 # Add one more payment payment = factory_payment(payment_status_code='CREATED') payment_account.save() payment.save() invoice = factory_invoice(payment, payment_account) invoice.save() factory_invoice_reference(invoice.id).save() results = Payment_service.search_purchase_history( auth_account_id=auth_account_id, search_filter={}, limit=1, page=1) assert results is not None assert results.get('items') is not None assert results.get('total') == 2 # Search by different filter search_filter = {'status': 'CREATED'} results = Payment_service.search_purchase_history( auth_account_id=auth_account_id, search_filter=search_filter, limit=1, page=1) assert results is not None assert results.get('items') is not None assert results.get('total') == 2 # Search by different filter search_filter = {'status': 'COMPLETED'} results = Payment_service.search_purchase_history( auth_account_id=auth_account_id, search_filter=search_filter, limit=1, page=1) assert results is not None assert results.get('items') is not None assert results.get('total') == 0 # Search by different filter search_filter = {'folioNumber': '1234567890'} results = Payment_service.search_purchase_history( auth_account_id=auth_account_id, search_filter=search_filter, limit=1, page=1) assert results is not None assert results.get('items') is not None assert results.get('total') == 2 # Search by different filter search_filter = {'businessIdentifier': invoice.business_identifier} results = Payment_service.search_purchase_history( auth_account_id=auth_account_id, search_filter=search_filter, limit=1, page=1) assert results is not None assert results.get('items') is not None assert results.get('total') == 2 # Search by different filter search_filter = { 'dateFilter': { 'createdFrom': datetime.now().strftime('%m/%d/%Y'), 'createdTo': datetime.now().strftime('%m/%d/%Y') } } results = Payment_service.search_purchase_history( auth_account_id=auth_account_id, search_filter=search_filter, limit=1, page=1) assert results is not None assert results.get('items') is not None assert results.get('total') == 2 # Search by different filter search_filter = {'weekFilter': {'index': 2}} results = Payment_service.search_purchase_history( auth_account_id=auth_account_id, search_filter=search_filter, limit=1, page=1) assert results is not None assert results.get('items') is not None assert results.get('total') == 0 # Search by different filter search_filter = { 'monthFilter': { 'month': datetime.now().month, 'year': datetime.now().year } } results = Payment_service.search_purchase_history( auth_account_id=auth_account_id, search_filter=search_filter, limit=1, page=1) assert results is not None assert results.get('items') is not None assert results.get('total') == 2 # Search by different filter search_filter = {'createdBy': payment.created_name} results = Payment_service.search_purchase_history( auth_account_id=auth_account_id, search_filter=search_filter, limit=1, page=1) assert results is not None assert results.get('items') is not None assert results.get('total') == 2
def test_payment_invalid_lookup(session): """Test invalid lookup.""" p = Payment_service.find_by_id(999) assert p is not None assert p.id is None
def test_failed_payment_after_consolidation(session): """Assert creating account payments after consolidation works.""" # Create 2 failed payments, consolidate it, and then again create another failed payment. # Consolidate it and make sure amount matches. inv_number_1 = 'REG00001' payment_account = factory_payment_account().save() invoice_1 = factory_invoice(payment_account, total=100) invoice_1.save() factory_payment_line_item(invoice_id=invoice_1.id, fee_schedule_id=1).save() factory_invoice_reference(invoice_1.id, invoice_number=inv_number_1).save() payment_1 = factory_payment(payment_status_code='FAILED', payment_account_id=payment_account.id, invoice_number=inv_number_1, invoice_amount=100, payment_method_code=PaymentMethod.PAD.value) payment_1.save() # Create one more payment with failed status. inv_number_2 = 'REG00002' invoice_2 = factory_invoice(payment_account, total=100) invoice_2.save() factory_payment_line_item(invoice_id=invoice_2.id, fee_schedule_id=1).save() factory_invoice_reference(invoice_2.id, invoice_number=inv_number_2).save() payment_2 = factory_payment(payment_status_code='FAILED', payment_account_id=payment_account.id, invoice_number=inv_number_2, invoice_amount=100, payment_method_code=PaymentMethod.PAD.value) payment_2.save() auth_account_id = PaymentAccount.find_by_id( payment_account.id).auth_account_id results = Payment_service.search_account_payments( auth_account_id=auth_account_id, status='FAILED', limit=10, page=1) assert results.get('total') == 2 new_payment_1 = Payment_service.create_account_payment( auth_account_id=auth_account_id, is_retry_payment=True) # Create another failed payment. inv_number_3 = 'REG00003' invoice_3 = factory_invoice(payment_account, total=100) invoice_3.save() factory_payment_line_item(invoice_id=invoice_3.id, fee_schedule_id=1).save() factory_invoice_reference(invoice_3.id, invoice_number=inv_number_3).save() payment_3 = factory_payment(payment_status_code='FAILED', payment_account_id=payment_account.id, invoice_number=inv_number_3, invoice_amount=100, payment_method_code=PaymentMethod.PAD.value) payment_3.save() new_payment_2 = Payment_service.create_account_payment( auth_account_id=auth_account_id, is_retry_payment=True) assert new_payment_1.id != new_payment_2.id assert new_payment_2.invoice_amount == payment_1.invoice_amount + payment_2.invoice_amount + \ payment_3.invoice_amount