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_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_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_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_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_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_transaction_update(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.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 = PaymentTransactionService.create( payment.id, get_paybc_transaction_request()) transaction = PaymentTransactionService.update_transaction( payment.id, transaction.id, '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 == Status.COMPLETED.value
def test_receipt_saved_from_new(session): """Assert that the receipt is saved to the table.""" payment_account = factory_payment_account() payment = factory_payment() payment_account.save() payment.save() i = factory_invoice(payment_id=payment.id, account_id=payment_account.id) i.save() factory_invoice_reference(i.id).save() receipt_service = ReceiptService() receipt_service.receipt_number = '1234567890' receipt_service.invoice_id = i.id receipt_service.receipt_date = datetime.now() receipt_service.receipt_amount = 100 receipt_service = receipt_service.save() receipt_service = ReceiptService.find_by_id(receipt_service.id) assert receipt_service is not None assert receipt_service.id is not None assert receipt_service.receipt_date is not None assert receipt_service.invoice_id is not None receipt_service = ReceiptService.find_by_invoice_id_and_receipt_number( i.id, receipt_service.receipt_number) assert receipt_service is not None assert receipt_service.id 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_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_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_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_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_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_update_statement_daily_to_daily(session): """Assert that going from daily to daily creates a new record.""" 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_statement_settings(payment_account_id=bcol_account.id, frequency=StatementFrequency.DAILY.value) # update to weekly payment_account = PaymentAccount.find_by_id(bcol_account.id) statement_settings = StatementSettingsService.update_statement_settings(payment_account.auth_account_id, StatementFrequency.DAILY.value) assert statement_settings is not None assert statement_settings.get('frequency') == StatementFrequency.DAILY.value assert statement_settings.get('to_date') is None # daily to daily - assert daily should start by tomorow assert statement_settings.get('from_date') == (datetime.today() + timedelta(days=1)).strftime('%Y-%m-%d') # daily to daily - assert current active one is stil daily ending today current_statement_settings = StatementSettingsModel.find_active_settings(payment_account.auth_account_id, datetime.today()) assert current_statement_settings is not None assert current_statement_settings.frequency == StatementFrequency.DAILY.value assert current_statement_settings.to_date == datetime.today().date()
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_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 = '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_1.save() auth_account_id = PaymentAccount.find_by_id( payment_account.id).auth_account_id rv = client.get(f'/api/v1/accounts/{auth_account_id}/payments', headers=headers) assert rv.status_code == 200 assert rv.json.get('total') == 1 rv = client.get( f'/api/v1/accounts/{auth_account_id}/payments?status=FAILED', headers=headers) assert rv.status_code == 200 assert rv.json.get('total') == 0
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_statement_find_by_account(session): """Assert that the statement settings by id works.""" 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() 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) factory_statement_invoices(statement_id=statement_model.id, invoice_id=i.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 assert statements.get('total') == 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_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_refund_for_unpaid_invoice(session): """Assert that the create refund fails for unpaid invoices.""" payment_account = factory_payment_account() payment_account.save() i = factory_invoice(payment_account=payment_account) i.save() factory_invoice_reference(i.id).save() with pytest.raises(Exception) as excinfo: RefundService.create_refund(invoice_id=i.id, request={'reason': 'Test'}) assert excinfo.type == BusinessException
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_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_create_refund_for_paid_invoice(session, monkeypatch, payment_method, invoice_status, pay_status, has_reference, expected_inv_status): """Assert that the create refund succeeds for paid invoices.""" expected = REFUND_SUCCESS_MESSAGES[f'{payment_method}.{invoice_status}'] payment_account = factory_payment_account() payment_account.save() i = factory_invoice(payment_account=payment_account, payment_method_code=payment_method) i.save() if has_reference: inv_ref = factory_invoice_reference(i.id) inv_ref.status_code = InvoiceReferenceStatus.COMPLETED.value inv_ref.save() payment = factory_payment(invoice_number=inv_ref.invoice_number, payment_status_code=pay_status).save() factory_payment_transaction(payment_id=payment.id, status_code=TransactionStatus.COMPLETED.value).save() i.invoice_status_code = invoice_status i.save() factory_receipt(invoice_id=i.id).save() monkeypatch.setattr('pay_api.services.payment_transaction.publish_response', lambda *args, **kwargs: None) message = RefundService.create_refund(invoice_id=i.id, request={'reason': 'Test'}) i = InvoiceModel.find_by_id(i.id) assert i.invoice_status_code == expected_inv_status assert message['message'] == expected
def test_create_duplicate_refund_for_paid_invoice(session, monkeypatch): """Assert that the create duplicate refund fails for paid invoices.""" payment_account = factory_payment_account() payment_account.save() i = factory_invoice(payment_account=payment_account) i.save() inv_ref = factory_invoice_reference(i.id) inv_ref.status_code = InvoiceReferenceStatus.COMPLETED.value inv_ref.save() payment = factory_payment(invoice_number=inv_ref.invoice_number).save() factory_payment_transaction(payment_id=payment.id, status_code=TransactionStatus.COMPLETED.value).save() i.invoice_status_code = InvoiceStatus.PAID.value i.save() factory_receipt(invoice_id=i.id).save() monkeypatch.setattr('pay_api.services.payment_transaction.publish_response', lambda *args, **kwargs: None) RefundService.create_refund(invoice_id=i.id, request={'reason': 'Test'}) i = InvoiceModel.find_by_id(i.id) payment: PaymentModel = PaymentModel.find_by_id(payment.id) assert i.invoice_status_code == InvoiceStatus.REFUND_REQUESTED.value assert payment.payment_status_code == PaymentStatus.REFUNDED.value with pytest.raises(Exception) as excinfo: RefundService.create_refund(invoice_id=i.id, request={'reason': 'Test'}) assert excinfo.type == BusinessException
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_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_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']