def test_get_statement_report_for_empty_invoices(session): """Assert that the get statement report works for statement with no invoices.""" bcol_account = factory_premium_payment_account() bcol_account.save() payment = factory_payment() payment.save() i = factory_invoice(payment_account=bcol_account) i.save() factory_invoice_reference(i.id).save() factory_payment_line_item(invoice_id=i.id, fee_schedule_id=1).save() settings_model = factory_statement_settings( payment_account_id=bcol_account.id, frequency=StatementFrequency.DAILY.value) statement_model = factory_statement( payment_account_id=bcol_account.id, frequency=StatementFrequency.DAILY.value, statement_settings_id=settings_model.id) payment_account = PaymentAccount.find_by_id(bcol_account.id) statements = StatementService.find_by_account_id( payment_account.auth_account_id, page=1, limit=10) assert statements is not None report_response, report_name = StatementService.get_statement_report( statement_id=statement_model.id, content_type='application/pdf', auth=get_auth_premium_user()) assert report_response is not None
def test_line_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.id, payment_account.id) invoice.save() factory_invoice_reference(invoice.id).save() fee_schedule = FeeSchedule.find_by_filing_type_and_corp_type('CP', 'OTANN') line = factory_payment_line_item(invoice.id, fee_schedule_id=fee_schedule.fee_schedule_id) line.save() line = factory_payment_line_item(invoice.id, fee_schedule_id=fee_schedule.fee_schedule_id, status='DELETED') line.save() p = PaymentLineService.find_by_id(line.id) assert p is not None assert p.id is not None assert p.invoice_id is not None assert p.filing_fees is not None assert p.fee_schedule_id is not None assert p.gst is None assert p.pst is None assert p.line_item_status_code is not None assert p.priority_fees is None assert p.future_effective_fees is None invoice = Invoice.find_by_id(invoice.id) schema = InvoiceSchema() d = schema.dump(invoice) assert d.get('id') == invoice.id assert len(d.get('line_items')) == 1
def test_create_transaction_for_completed_nsf_payment(session): """Assert that the payment is saved to the table.""" # Create a FAILED payment (NSF), then clone the payment to create another one for CC payment # Create a transaction and assert it's success. 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 payment for NSF payment. payment_2 = factory_payment(payment_status_code='COMPLETED', payment_account_id=payment_account.id, invoice_number=inv_number_1, invoice_amount=100, payment_method_code=PaymentMethod.CC.value) payment_2.save() with pytest.raises(BusinessException) as excinfo: PaymentTransactionService.create_transaction_for_payment( payment_2.id, get_paybc_transaction_request()) assert excinfo.value.code == Error.INVALID_PAYMENT_ID.name
def test_create_account_payments(session, client, jwt, app): """Assert that the endpoint returns 200.""" token = jwt.create_jwt(get_claims(), token_header) headers = { 'Authorization': f'Bearer {token}', 'content-type': 'application/json' } 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() auth_account_id = PaymentAccount.find_by_id( payment_account.id).auth_account_id rv = client.post( f'/api/v1/accounts/{auth_account_id}/payments?retryFailedPayment=true', headers=headers) assert rv.status_code == 201
def test_patch_transaction_for_nsf_payment(session, monkeypatch): """Assert that the payment is saved to the table.""" # Create a FAILED payment (NSF), then clone the payment to create another one for CC payment # Create a transaction and assert it's success. # Patch transaction and check the status of records inv_number_1 = 'REG00001' payment_account = factory_payment_account( cfs_account_status=CfsAccountStatus.FREEZE.value, payment_method_code='PAD').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 payment for NSF payment. payment_2 = factory_payment(payment_status_code='CREATED', payment_account_id=payment_account.id, invoice_number=inv_number_1, invoice_amount=100, payment_method_code=PaymentMethod.CC.value) payment_2.save() def get_receipt(cls, payment_account, pay_response_url: str, invoice_reference): # pylint: disable=unused-argument; mocks of library methods return '1234567890', datetime.now(), 100.00 monkeypatch.setattr( 'pay_api.services.paybc_service.PaybcService.get_receipt', get_receipt) txn = PaymentTransactionService.create_transaction_for_payment( payment_2.id, get_paybc_transaction_request()) txn = PaymentTransactionService.update_transaction( txn.id, pay_response_url='receipt_number=123451') assert txn.status_code == 'COMPLETED' payment_2 = Payment.find_by_id(payment_2.id) assert payment_2.payment_status_code == 'COMPLETED' invoice_1: Invoice = Invoice.find_by_id(invoice_1.id) assert invoice_1.invoice_status_code == 'PAID' cfs_account = CfsAccount.find_effective_by_account_id(payment_account.id) assert cfs_account.status == 'ACTIVE'
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_update_transaction_for_direct_pay_without_response_url(session): """Assert that the receipt records are created.""" current_app.config['DIRECT_PAY_ENABLED'] = True payment_account = factory_payment_account( payment_method_code=PaymentMethod.DIRECT_PAY.value) payment = factory_payment( payment_method_code=PaymentMethod.DIRECT_PAY.value) payment_account.save() payment.save() invoice = factory_invoice(payment_account) invoice.save() factory_invoice_reference(invoice.id).save() fee_schedule = FeeSchedule.find_by_filing_type_and_corp_type('CP', 'OTANN') line = factory_payment_line_item( invoice.id, fee_schedule_id=fee_schedule.fee_schedule_id) line.save() transaction = PaymentTransactionService.create_transaction_for_invoice( invoice.id, get_paybc_transaction_request()) # Update transaction without response url, which should update the receipt transaction = PaymentTransactionService.update_transaction( transaction.id, None) assert transaction.status_code == TransactionStatus.COMPLETED.value
def test_update_transaction_for_direct_pay_with_response_url(session): """Assert that the receipt records are created.""" current_app.config['DIRECT_PAY_ENABLED'] = True response_url = 'trnApproved=1&messageText=Approved&trnOrderId=1003598&trnAmount=201.00&paymentMethod=CC' \ '&cardType=VI&authCode=TEST&trnDate=2020-08-11&pbcTxnNumber=1' valid_hash = f'&hashValue={HashingService.encode(response_url)}' payment_account = factory_payment_account( payment_method_code=PaymentMethod.DIRECT_PAY.value) payment = factory_payment( payment_method_code=PaymentMethod.DIRECT_PAY.value) payment_account.save() payment.save() invoice = factory_invoice(payment_account) invoice.save() factory_invoice_reference(invoice.id).save() fee_schedule = FeeSchedule.find_by_filing_type_and_corp_type('CP', 'OTANN') line = factory_payment_line_item( invoice.id, fee_schedule_id=fee_schedule.fee_schedule_id) line.save() transaction = PaymentTransactionService.create_transaction_for_invoice( invoice.id, get_paybc_transaction_request()) # Update transaction with invalid hash transaction = PaymentTransactionService.update_transaction( transaction.id, f'{response_url}1234567890') assert transaction.status_code == TransactionStatus.FAILED.value # Update transaction with valid hash transaction = PaymentTransactionService.create_transaction_for_invoice( invoice.id, get_paybc_transaction_request()) transaction = PaymentTransactionService.update_transaction( transaction.id, f'{response_url}{valid_hash}') assert transaction.status_code == TransactionStatus.COMPLETED.value
def test_transaction_update(session, stan_server, public_user_mock): """Assert that the payment is saved to the table.""" payment_account = factory_payment_account() payment_account.save() invoice = factory_invoice(payment_account) invoice.save() invoice_reference = factory_invoice_reference(invoice.id).save() fee_schedule = FeeSchedule.find_by_filing_type_and_corp_type('CP', 'OTANN') line = factory_payment_line_item( invoice.id, fee_schedule_id=fee_schedule.fee_schedule_id) line.save() factory_payment(invoice_number=invoice_reference.invoice_number).save() transaction = PaymentTransactionService.create_transaction_for_invoice( invoice.id, get_paybc_transaction_request()) transaction = PaymentTransactionService.update_transaction( transaction.id, pay_response_url='receipt_number=123451') assert transaction is not None assert transaction.id is not None assert transaction.status_code is not None assert transaction.payment_id is not None assert transaction.client_system_url is not None assert transaction.pay_system_url is not None assert transaction.transaction_start_time is not None assert transaction.transaction_end_time is not None assert transaction.status_code == TransactionStatus.COMPLETED.value
def test_transaction_for_direct_pay_create_from_new(session): """Assert that the payment is saved to the table.""" current_app.config['DIRECT_PAY_ENABLED'] = True payment_account = factory_payment_account( payment_method_code=PaymentMethod.DIRECT_PAY.value) payment_account.save() invoice = factory_invoice(payment_account) invoice.save() invoice_reference = factory_invoice_reference(invoice.id).save() factory_payment(invoice_number=invoice_reference.invoice_number).save() fee_schedule = FeeSchedule.find_by_filing_type_and_corp_type('CP', 'OTANN') line = factory_payment_line_item( invoice.id, fee_schedule_id=fee_schedule.fee_schedule_id) line.save() transaction = PaymentTransactionService.create_transaction_for_invoice( invoice.id, get_paybc_transaction_request()) assert transaction is not None assert transaction.id is not None assert transaction.status_code is not None assert transaction.payment_id is not None assert transaction.client_system_url is not None assert transaction.pay_system_url is not None assert transaction.transaction_start_time is not None assert transaction.asdict() is not None
def test_transaction_update_completed(session, stan_server, public_user_mock): """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) invoice.save() factory_invoice_reference(invoice.id).save() fee_schedule = FeeSchedule.find_by_filing_type_and_corp_type('CP', 'OTANN') line = factory_payment_line_item( invoice.id, fee_schedule_id=fee_schedule.fee_schedule_id) line.save() transaction = PaymentTransactionService.create( payment.id, get_paybc_transaction_request()) transaction = PaymentTransactionService.update_transaction( payment.id, transaction.id, '123451') with pytest.raises(BusinessException) as excinfo: PaymentTransactionService.update_transaction(payment.id, transaction.id, '123451') assert excinfo.value.status == Error.PAY006.status assert excinfo.value.message == Error.PAY006.message assert excinfo.value.code == Error.PAY006.name
def test_invoice_saved_from_new(session): """Assert that the invoice is saved to the table.""" payment_account = factory_payment_account() payment = factory_payment() payment_account.save() payment.save() i = factory_invoice(payment=payment, payment_account=payment_account) i.save() fee_schedule = FeeSchedule.find_by_filing_type_and_corp_type('CP', 'OTANN') line = factory_payment_line_item( i.id, fee_schedule_id=fee_schedule.fee_schedule_id) line.save() invoice = Invoice_service.find_by_id(i.id, skip_auth_check=True) assert invoice is not None assert invoice.id is not None assert invoice.payment_id is not None assert invoice.invoice_status_code is not None assert invoice.refund is None assert invoice.payment_date is None assert invoice.total is not None assert invoice.paid is None assert invoice.payment_line_items is not None assert invoice.folio_number is not None assert invoice.business_identifier is not None
def test_delete_payment(session, auth_mock, public_user_mock): """Assert that the payment records are soft deleted.""" payment_account = factory_payment_account() # payment = factory_payment() payment_account.save() # payment.save() invoice = factory_invoice(payment_account, total=10) invoice.save() invoice_reference = factory_invoice_reference(invoice.id).save() # Create a payment for this reference payment = factory_payment(invoice_number=invoice_reference.invoice_number, invoice_amount=10).save() fee_schedule = FeeSchedule.find_by_filing_type_and_corp_type('CP', 'OTANN') line = factory_payment_line_item( invoice.id, fee_schedule_id=fee_schedule.fee_schedule_id) line.save() transaction = factory_payment_transaction(payment.id) transaction.save() PaymentService.delete_invoice(invoice.id) invoice = Invoice.find_by_id(invoice.id) payment = Payment.find_by_id(payment.id) assert invoice.invoice_status_code == InvoiceStatus.DELETED.value assert payment.payment_status_code == PaymentStatus.DELETED.value
def test_invoice_with_temproary_business_identifier(session): """Assert that the invoice dictionary is not include temproary business identifier.""" payment_account = factory_payment_account() payment = factory_payment() payment_account.save() payment.save() i = factory_invoice(payment=payment, payment_account=payment_account, business_identifier='Tzxcasd') i.save() fee_schedule = FeeSchedule.find_by_filing_type_and_corp_type('CP', 'OTANN') line = factory_payment_line_item( i.id, fee_schedule_id=fee_schedule.fee_schedule_id) line.save() invoice = Invoice_service.find_by_id(i.id, skip_auth_check=True) assert invoice is not None assert invoice.id is not None assert invoice.payment_id is not None assert invoice.invoice_status_code is not None assert invoice.refund is None assert invoice.payment_date is None assert invoice.total is not None assert invoice.paid is None assert invoice.payment_line_items is not None assert invoice.folio_number is not None assert invoice.business_identifier is not None invoice_dict = invoice.asdict() assert invoice_dict.get('business_identifier') is None
def test_create_receipt_with_no_receipt(session, public_user_mock): """Try creating a receipt with invoice number.""" payment_account = factory_payment_account() payment = factory_payment() payment_account.save() payment.save() invoice = factory_invoice(payment.id, payment_account.id) invoice.save() factory_invoice_reference(invoice.id).save() fee_schedule = FeeSchedule.find_by_filing_type_and_corp_type('CP', 'OTANN') line = factory_payment_line_item( invoice.id, fee_schedule_id=fee_schedule.fee_schedule_id) line.save() PaymentService.update_payment(payment.id, get_payment_request()) input_data = { 'corpName': 'Pennsular Coop ', 'filingDateTime': '1999', 'fileName': 'coopser' } with pytest.raises(BusinessException) as excinfo: ReceiptService.create_receipt(payment.id, '', input_data, skip_auth_check=True) assert excinfo.type == BusinessException
def test_create_receipt_without_invoice(session, public_user_mock): """Try creating a receipt without invoice number.""" payment_account = factory_payment_account() payment = factory_payment() payment_account.save() payment.save() invoice = factory_invoice(payment.id, payment_account.id) invoice.save() factory_invoice_reference(invoice.id).save() fee_schedule = FeeSchedule.find_by_filing_type_and_corp_type('CP', 'OTANN') line = factory_payment_line_item( invoice.id, fee_schedule_id=fee_schedule.fee_schedule_id) line.save() transaction = factory_payment_transaction(payment.id) transaction.save() PaymentService.update_payment(payment.id, get_payment_request()) input_data = { 'corpName': 'Pennsular Coop ', 'filingDateTime': '1999', 'fileName': 'coopser' } response = ReceiptService.create_receipt(payment.id, '', input_data, skip_auth_check=True) assert response is not None
def test_transaction_create_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, payment_account) invoice.save() factory_invoice_reference(invoice.id).save() fee_schedule = FeeSchedule.find_by_filing_type_and_corp_type('CP', 'OTANN') line = factory_payment_line_item( invoice.id, fee_schedule_id=fee_schedule.fee_schedule_id) line.save() transaction = PaymentTransactionService.create( payment.id, get_paybc_transaction_request()) assert transaction is not None assert transaction.id is not None assert transaction.status_code is not None assert transaction.payment_id is not None assert transaction.client_system_url is not None assert transaction.pay_system_url is not None assert transaction.transaction_start_time is not None assert transaction.asdict() is not None
def test_create_invoice(session): """Test create_invoice.""" pay_account = factory_payment_account(payment_system_code='BCOL', account_number='BCOL_ACC_1', bcol_user_id='test') pay_account.save() payment = factory_payment() payment.save() i = factory_invoice(payment=payment, payment_account=pay_account) i.save() fee_schedule = FeeSchedule.find_by_filing_type_and_corp_type('CP', 'OTANN') line = factory_payment_line_item( i.id, fee_schedule_id=fee_schedule.fee_schedule_id) line.save() line = PaymentLineItem.find_by_id(line.id) # payment_account: PaymentAccount, line_items: [PaymentLineItem], invoice_id: str, **kwargs inv = bcol_service.create_invoice( payment_account=pay_account, line_items=[line], invoice=i, filing_info={'folioNumber': '1234567890'}, corp_type_code=i.corp_type_code, business_identifier=i.business_identifier) assert inv is not None assert inv.get('invoice_number') == 'TEST'
def test_get_receipt(session, public_user_mock): """Assert that get receipt is working.""" response_url = 'trnApproved=1&messageText=Approved&trnOrderId=1003598&trnAmount=201.00&paymentMethod=CC' \ '&cardType=VI&authCode=TEST&trnDate=2020-08-11&pbcTxnNumber=1' invalid_hash = '&hashValue=0f7953db6f02f222f1285e1544c6a765' payment_account = factory_payment_account() payment = factory_payment() payment_account.save() payment.save() invoice = factory_invoice(payment_account) invoice.save() invoice_ref = factory_invoice_reference(invoice.id).save() fee_schedule = FeeSchedule.find_by_filing_type_and_corp_type('CP', 'OTANN') service_fee = 100 line = factory_payment_line_item(invoice.id, fee_schedule_id=fee_schedule.fee_schedule_id, service_fees=service_fee) line.save() direct_pay_service = DirectPayService() rcpt = direct_pay_service.get_receipt(payment_account, f'{response_url}{invalid_hash}', invoice_ref) assert rcpt is None valid_hash = f'&hashValue={HashingService.encode(response_url)}' rcpt = direct_pay_service.get_receipt(payment_account, f'{response_url}{valid_hash}', invoice_ref) assert rcpt is not None # Test receipt without response_url rcpt = direct_pay_service.get_receipt(payment_account, None, invoice_ref) assert rcpt is not None
def test_transaction_find_active_lookup(session): """Invalid lookup..""" payment_account = factory_payment_account() payment = factory_payment() payment_account.save() payment.save() invoice = factory_invoice(payment, payment_account) invoice.save() factory_invoice_reference(invoice.id).save() fee_schedule = FeeSchedule.find_by_filing_type_and_corp_type('CP', 'OTANN') line = factory_payment_line_item( invoice.id, fee_schedule_id=fee_schedule.fee_schedule_id) line.save() transaction = factory_payment_transaction(payment.id, Status.CREATED.value) transaction.save() transaction = PaymentTransactionService.find_active_by_payment_id( payment.id) assert transaction is not None assert transaction.id is not None assert transaction.status_code is not None assert transaction.payment_id is not None assert transaction.client_system_url is not None assert transaction.pay_system_url is not None assert transaction.transaction_start_time is not None assert transaction.status_code == Status.CREATED.value
def test_transaction_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, payment_account) invoice.save() factory_invoice_reference(invoice.id).save() fee_schedule = FeeSchedule.find_by_filing_type_and_corp_type('CP', 'OTANN') line = factory_payment_line_item( invoice.id, fee_schedule_id=fee_schedule.fee_schedule_id) line.save() payment_transaction = PaymentTransactionService() payment_transaction.status_code = 'DRAFT' payment_transaction.transaction_end_time = datetime.now() payment_transaction.transaction_start_time = datetime.now() payment_transaction.pay_system_url = 'http://google.com' payment_transaction.client_system_url = 'http://google.com' payment_transaction.payment_id = payment.id payment_transaction = payment_transaction.save() transaction = PaymentTransactionService.find_by_id(payment.id, payment_transaction.id) assert transaction is not None assert transaction.id is not None assert transaction.status_code is not None assert transaction.payment_id is not None assert transaction.client_system_url is not None assert transaction.pay_system_url is not None assert transaction.transaction_start_time is not None assert transaction.transaction_end_time is not None
def test_transaction_update_with_no_receipt(session, stan_server): """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) invoice.save() factory_invoice_reference(invoice.id, invoice_number='').save() fee_schedule = FeeSchedule.find_by_filing_type_and_corp_type('CP', 'OTANN') line = factory_payment_line_item( invoice.id, fee_schedule_id=fee_schedule.fee_schedule_id) line.save() transaction = PaymentTransactionService.create( payment.id, get_paybc_transaction_request()) transaction = PaymentTransactionService.update_transaction( payment.id, transaction.id, None) assert transaction is not None assert transaction.id is not None assert transaction.status_code is not None assert transaction.payment_id is not None assert transaction.client_system_url is not None assert transaction.pay_system_url is not None assert transaction.transaction_start_time is not None assert transaction.transaction_end_time is not None assert transaction.status_code == Status.FAILED.value assert transaction.asdict() is not None
def test_get_payment_system_url_service_fees(session, public_user_mock): """Assert that the url returned is correct.""" today = current_local_time().strftime(PAYBC_DATE_FORMAT) payment_account = factory_payment_account() payment = factory_payment() payment_account.save() payment.save() invoice = factory_invoice(payment_account) invoice.save() invoice_ref = factory_invoice_reference(invoice.id).save() fee_schedule = FeeSchedule.find_by_filing_type_and_corp_type('CP', 'OTANN') distribution_code = DistributionCodeModel.find_by_active_for_fee_schedule(fee_schedule.fee_schedule_id) distribution_code_svc = DistributionCode() distribution_code_payload = get_distribution_code_payload() # Set service fee distribution distribution_code_payload.update({'serviceFeeDistributionCodeId': distribution_code.distribution_code_id}) # update the existing gl code with new values distribution_code_svc.save_or_update(distribution_code_payload, distribution_code.distribution_code_id) service_fee = 100 line = factory_payment_line_item(invoice.id, fee_schedule_id=fee_schedule.fee_schedule_id, service_fees=service_fee) line.save() direct_pay_service = DirectPayService() payment_response_url = direct_pay_service.get_payment_system_url_for_invoice(invoice, invoice_ref, 'google.com') url_param_dict = dict(urllib.parse.parse_qsl(urllib.parse.urlsplit(payment_response_url).query)) assert url_param_dict['trnDate'] == today assert url_param_dict['glDate'] == today assert url_param_dict['description'] == 'Direct_Sale' assert url_param_dict['pbcRefNumber'] == current_app.config.get('PAYBC_DIRECT_PAY_REF_NUMBER') assert url_param_dict['trnNumber'] == generate_transaction_number(invoice.id) assert url_param_dict['trnAmount'] == str(invoice.total) assert url_param_dict['paymentMethod'] == 'CC' assert url_param_dict['redirectUri'] == 'google.com' revenue_str = f"1:{distribution_code_payload['client']}." \ f"{distribution_code_payload['responsibilityCentre']}." \ f"{distribution_code_payload['serviceLine']}." \ f"{distribution_code_payload['stob']}." \ f"{distribution_code_payload['projectCode']}." \ f'000000.0000:10.00' revenue_str_service_fee = f"2:{distribution_code_payload['client']}." \ f"{distribution_code_payload['responsibilityCentre']}." \ f"{distribution_code_payload['serviceLine']}." \ f"{distribution_code_payload['stob']}." \ f"{distribution_code_payload['projectCode']}." \ f'000000.0000:{format(service_fee, DECIMAL_PRECISION)}' assert url_param_dict['revenue'] == f'{revenue_str}|{revenue_str_service_fee}' urlstring = f"trnDate={today}&pbcRefNumber={current_app.config.get('PAYBC_DIRECT_PAY_REF_NUMBER')}&" \ f'glDate={today}&description=Direct_Sale&' \ f'trnNumber={generate_transaction_number(invoice.id)}&' \ f'trnAmount={invoice.total}&' \ f'paymentMethod=CC&' \ f'redirectUri=google.com&' \ f'currency=CAD&' \ f'revenue={revenue_str}|' \ f'{revenue_str_service_fee}' expected_hash_str = HashingService.encode(urlstring) assert expected_hash_str == url_param_dict['hashValue']
def test_event_failed_transactions(session, public_user_mock, stan_server, monkeypatch): """Assert that the transaction status is EVENT_FAILED when Q is not available.""" # 1. Create payment records # 2. Create a transaction # 3. Fail the queue publishing which will mark the payment as COMPLETED and transaction as EVENT_FAILED # 4. Update the transansaction with queue up which will mark the transaction as COMPLETED current_app.config['DIRECT_PAY_ENABLED'] = True payment_account = factory_payment_account( payment_method_code=PaymentMethod.DIRECT_PAY.value) payment_account.save() fee_schedule = FeeSchedule.find_by_filing_type_and_corp_type('CP', 'OTANN') invoice = factory_invoice(payment_account, total=30) invoice.save() factory_invoice_reference(invoice.id).save() line = factory_payment_line_item( invoice.id, fee_schedule_id=fee_schedule.fee_schedule_id) line.save() transaction = PaymentTransactionService.create_transaction_for_invoice( invoice.id, get_paybc_transaction_request()) def get_receipt(cls, payment_account, pay_response_url: str, invoice_reference): # pylint: disable=unused-argument; mocks of library methods return '1234567890', datetime.now(), 30.00 monkeypatch.setattr( 'pay_api.services.direct_pay_service.DirectPayService.get_receipt', get_receipt) with patch('pay_api.services.payment_transaction.publish_response', side_effect=ConnectionError('mocked error')): transaction = PaymentTransactionService.update_transaction( transaction.id, pay_response_url='?key=value') assert transaction is not None assert transaction.id is not None assert transaction.status_code is not None assert transaction.payment_id is not None assert transaction.client_system_url is not None assert transaction.pay_system_url is not None assert transaction.transaction_start_time is not None assert transaction.transaction_end_time is not None assert transaction.status_code == TransactionStatus.EVENT_FAILED.value # Now update the transaction and check the status of the transaction transaction = PaymentTransactionService.update_transaction( transaction.id, pay_response_url=None) assert transaction is not None assert transaction.id is not None assert transaction.status_code is not None assert transaction.payment_id is not None assert transaction.client_system_url is not None assert transaction.pay_system_url is not None assert transaction.transaction_start_time is not None assert transaction.transaction_end_time is not None assert transaction.status_code == TransactionStatus.COMPLETED.value
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_transaction_post_for_nsf_payment(session, client, jwt, app): """Assert that the endpoint returns 201.""" 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 payment for NSF payment. payment_2 = factory_payment(payment_status_code='CREATED', payment_account_id=payment_account.id, invoice_number=inv_number_1, invoice_amount=100, payment_method_code=PaymentMethod.CC.value) payment_2.save() token = jwt.create_jwt(get_claims(), token_header) headers = { 'Authorization': f'Bearer {token}', 'content-type': 'application/json' } data = { 'clientSystemUrl': 'http://localhost:8080/coops-web/transactions/transaction_id=abcd', 'payReturnUrl': 'http://localhost:8080/pay-web' } rv = client.post(f'/api/v1/payments/{payment_2.id}/transactions', data=json.dumps(data), headers=headers) assert rv.status_code == 201 assert rv.json.get('paymentId') == payment_2.id assert schema_utils.validate(rv.json, 'transaction')[0]
def test_update_distribution(session, public_user_mock, stan_server, monkeypatch): """Assert that the invoice status is updated when the distribution is updated.""" # 1. Create a distribution code # 2. Attach a fee schedule to the distribution # 3. Create and complete payment # 4. Update the distribution and assert the invoice status is changed. distribution_code_svc = services.DistributionCode() fee_schedule = FeeSchedule.find_by_filing_type_and_corp_type('CP', 'OTANN') # Create a direct pay current_app.config['DIRECT_PAY_ENABLED'] = True payment_account = factory_payment_account( payment_method_code=PaymentMethod.DIRECT_PAY.value) payment_account.save() invoice = factory_invoice(payment_account, total=30) invoice.save() invoice_reference = factory_invoice_reference(invoice.id).save() line = factory_payment_line_item( invoice.id, fee_schedule_id=fee_schedule.fee_schedule_id) line.save() factory_payment(invoice_number=invoice_reference.invoice_number, payment_method_code=PaymentMethod.DIRECT_PAY.value, invoice_amount=30).save() distribution_id = line.fee_distribution_id distribution_code = distribution_code_svc.find_by_id(distribution_id) transaction = PaymentTransactionService.create_transaction_for_invoice( invoice.id, get_paybc_transaction_request()) def get_receipt(cls, payment_account, pay_response_url: str, invoice_reference): # pylint: disable=unused-argument; mocks of library methods return '1234567890', datetime.now(), 30.00 monkeypatch.setattr( 'pay_api.services.direct_pay_service.DirectPayService.get_receipt', get_receipt) # Update transaction without response url, which should update the receipt PaymentTransactionService.update_transaction(transaction.id, pay_response_url=None) invoice = InvoiceModel.find_by_id(invoice.id) assert invoice.invoice_status_code == InvoiceStatus.PAID.value # Update distribution code distribution_code_svc.save_or_update(distribution_code, distribution_id) invoice = InvoiceModel.find_by_id(invoice.id) assert invoice.invoice_status_code == InvoiceStatus.UPDATE_REVENUE_ACCOUNT.value
def test_get_receipt(session): """Test cancel_invoice.""" pay_account = factory_payment_account(payment_system_code='BCOL', account_number='BCOL_ACC_1', user_id='test') pay_account.save() payment = factory_payment() payment.save() i = factory_invoice(payment_id=payment.id, account_id=pay_account.id) i.save() fee_schedule = FeeSchedule.find_by_filing_type_and_corp_type('CP', 'OTANN') line = factory_payment_line_item(i.id, fee_schedule_id=fee_schedule.fee_schedule_id) line.save() inv_ref = factory_invoice_reference(i.id).save() receipt = bcol_service.get_receipt(pay_account, None, inv_ref) assert receipt is not None
def test_create_transaction_for_nsf_payment(session): """Assert that the payment is saved to the table.""" # Create a FAILED payment (NSF), then clone the payment to create another one for CC payment # Create a transaction and assert it's success. 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 payment for NSF payment. payment_2 = factory_payment(payment_account_id=payment_account.id, invoice_number=inv_number_1, invoice_amount=100, payment_method_code=PaymentMethod.CC.value) payment_2.save() transaction = PaymentTransactionService.create_transaction_for_payment( payment_2.id, get_paybc_transaction_request()) assert transaction is not None assert transaction.id is not None assert transaction.status_code is not None assert transaction.payment_id is not None assert transaction.client_system_url is not None assert transaction.pay_system_url is not None assert transaction.transaction_start_time is not None assert transaction.asdict() is not None
def test_no_existing_transaction(session): """Assert that the payment is saved to the table.""" payment_account = factory_payment_account() payment_account.save() invoice = factory_invoice(payment_account) invoice.save() factory_invoice_reference(invoice.id).save() fee_schedule = FeeSchedule.find_by_filing_type_and_corp_type('CP', 'OTANN') line = factory_payment_line_item( invoice.id, fee_schedule_id=fee_schedule.fee_schedule_id) line.save() transaction = PaymentTransactionService.find_active_by_invoice_id( invoice.id) assert transaction is None