コード例 #1
0
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, 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())

    # Update transaction with invalid hash
    transaction = PaymentTransactionService.update_transaction(
        payment.id, transaction.id, f'{response_url}1234567890')
    assert transaction.status_code == TransactionStatus.FAILED.value

    # Update transaction with valid hash
    transaction = PaymentTransactionService.create(
        payment.id, get_paybc_transaction_request())
    transaction = PaymentTransactionService.update_transaction(
        payment.id, transaction.id, f'{response_url}{valid_hash}')
    assert transaction.status_code == TransactionStatus.COMPLETED.value
コード例 #2
0
def test_transaction_create_new_on_completed_payment(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()
    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,
                                                   'http://google.com/',
                                                   skip_auth_check=True)
    PaymentTransactionService.update_transaction(payment.id,
                                                 transaction.id,
                                                 '123451',
                                                 skip_auth_check=True)

    with pytest.raises(BusinessException) as excinfo:
        PaymentTransactionService.create(payment.id,
                                         'http://google.com/',
                                         skip_auth_check=True)
    assert excinfo.value.status == Error.PAY006.status
    assert excinfo.value.message == Error.PAY006.message
    assert excinfo.value.code == Error.PAY006.name
コード例 #3
0
def test_multiple_transactions_for_single_payment(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()

    PaymentTransactionService.create(payment.id,
                                     get_paybc_transaction_request())
    PaymentTransactionService.create(payment.id,
                                     get_paybc_transaction_request())
    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.status_code == TransactionStatus.CREATED.value
コード例 #4
0
def test_multiple_transactions_for_single_payment(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()
    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()

    PaymentTransactionService.create(payment.id,
                                     'http://google.com/',
                                     skip_auth_check=True)
    PaymentTransactionService.create(payment.id,
                                     'http://google.com/',
                                     skip_auth_check=True)
    transaction = PaymentTransactionService.create(payment.id,
                                                   'http://google.com/',
                                                   skip_auth_check=True)

    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
コード例 #5
0
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, 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())

    # Update transaction without response url, which should update the receipt
    transaction = PaymentTransactionService.update_transaction(
        payment.id, transaction.id, None)
    assert transaction.status_code == TransactionStatus.COMPLETED.value
コード例 #6
0
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 = factory_payment(
        payment_method_code=PaymentMethod.DIRECT_PAY.value)
    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
コード例 #7
0
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
コード例 #8
0
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.id, payment_account.id)
    invoice.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,
                                                   'http://google.com/',
                                                   skip_auth_check=True)
    transaction = PaymentTransactionService.update_transaction(
        payment.id, transaction.id, None, skip_auth_check=True)

    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
コード例 #9
0
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
コード例 #10
0
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
コード例 #11
0
def test_transaction_create_from_invalid_payment(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()

    with pytest.raises(BusinessException) as excinfo:
        PaymentTransactionService.create(999, get_paybc_transaction_request())
    assert excinfo.value.code == Error.INVALID_PAYMENT_ID.name
コード例 #12
0
def test_transaction_create_from_invalid_payment(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()
    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()

    with pytest.raises(BusinessException) as excinfo:
        PaymentTransactionService.create(999, 'http://google.com/')
    assert excinfo.value.status == Error.PAY005.status
    assert excinfo.value.message == Error.PAY005.message
    assert excinfo.value.code == Error.PAY005.name
コード例 #13
0
def test_transaction_create_new_on_completed_payment(session):
    """Assert that the payment is saved to the table."""
    payment_account = factory_payment_account()
    payment = factory_payment(payment_status_code=Status.COMPLETED.value)
    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()

    with pytest.raises(BusinessException) as excinfo:
        PaymentTransactionService.create(payment.id,
                                         get_paybc_transaction_request())
    assert excinfo.value.status == Error.PAY006.status
    assert excinfo.value.message == Error.PAY006.message
    assert excinfo.value.code == Error.PAY006.name
コード例 #14
0
def test_transaction_update_on_paybc_connection_error(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).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())

    from unittest.mock import patch
    from requests.exceptions import ConnectTimeout, ConnectionError

    # Mock here that the invoice update fails here to test the rollback scenario
    with patch('pay_api.services.oauth_service.requests.post',
               side_effect=ConnectionError('mocked error')):
        transaction = PaymentTransactionService.update_transaction(
            payment.id,
            transaction.id,
            pay_response_url='receipt_number=123451')
        assert transaction.pay_system_reason_code == 'SERVICE_UNAVAILABLE'
    with patch('pay_api.services.oauth_service.requests.post',
               side_effect=ConnectTimeout('mocked error')):
        transaction = PaymentTransactionService.update_transaction(
            payment.id,
            transaction.id,
            pay_response_url='receipt_number=123451')
        assert transaction.pay_system_reason_code == 'SERVICE_UNAVAILABLE'

    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.FAILED.value
コード例 #15
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 = factory_payment(payment_method_code=PaymentMethod.DIRECT_PAY.value)
    payment_account.save()
    payment.save()

    invoice = factory_invoice(payment, 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()

    distribution_id = line.fee_distribution_id

    distribution_code = distribution_code_svc.find_by_id(distribution_id)

    transaction = PaymentTransactionService.create(payment.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(payment.id, 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
コード例 #16
0
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.id, payment_account.id)
    invoice.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,
                                                   'http://google.com/')

    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
コード例 #17
0
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 = factory_payment(
        payment_method_code=PaymentMethod.DIRECT_PAY.value)
    payment_account.save()
    payment.save()
    fee_schedule = FeeSchedule.find_by_filing_type_and_corp_type('CP', 'OTANN')

    invoice = factory_invoice(payment, 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(
        payment.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(
            payment.id, 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(
        payment.id, 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