Пример #1
0
def test_transaction_patch_direct_pay(session, client, jwt, app):
    """Assert that the transaction patch updates the payment receipts."""
    token = jwt.create_jwt(get_claims(), token_header)
    headers = {
        'Authorization': f'Bearer {token}',
        'content-type': 'application/json'
    }

    # Create a payment first
    rv = client.post('/api/v1/payment-requests',
                     data=json.dumps(
                         get_payment_request_with_payment_method(
                             payment_method=PaymentMethod.DIRECT_PAY.value)),
                     headers=headers)
    invoice_id = rv.json.get('id')
    data = {
        'clientSystemUrl':
        'http://localhost:8080/coops-web/transactions/transaction_id=abcd',
        'payReturnUrl': 'http://localhost:8080/pay-web'
    }
    rv = client.post(f'/api/v1/payment-requests/{invoice_id}/transactions',
                     data=json.dumps(data),
                     headers={'content-type': 'application/json'})
    txn_id = rv.json.get('id')
    assert rv.status_code == 201
    assert rv.json.get('paymentId')

    client.patch(
        f'/api/v1/payment-requests/{invoice_id}/transactions/{txn_id}',
        data=json.dumps({}),
        headers={'content-type': 'application/json'})

    # Get payment details
    rv = client.get(f'/api/v1/payment-requests/{invoice_id}', headers=headers)
    assert rv.json.get('statusCode') == 'COMPLETED'
Пример #2
0
def test_premium_payment_creation_with_payment_method_ob(
        session, client, jwt, app):
    """Assert that the endpoint returns 201."""
    token = jwt.create_jwt(get_claims(), token_header)
    headers = {
        'Authorization': f'Bearer {token}',
        'content-type': 'application/json'
    }

    rv = client.post('/api/v1/payment-requests',
                     data=json.dumps(
                         get_payment_request_with_payment_method(
                             business_identifier='CP0002000',
                             payment_method='ONLINE_BANKING')),
                     headers=headers)
    assert rv.status_code == 201
    assert rv.json.get('paymentMethod') == 'ONLINE_BANKING'
    invoice_id = rv.json.get('id')

    rv = client.patch(f'/api/v1/payment-requests/{invoice_id}',
                      data=json.dumps(
                          {'paymentInfo': {
                              'methodOfPayment': 'CC'
                          }}),
                      headers=headers)

    assert rv.status_code == 200
    assert rv.json.get('paymentMethod') == 'DIRECT_PAY'
Пример #3
0
def test_transaction_post_direct_pay(session, client, jwt, app):
    """Assert that the endpoint returns 201."""
    token = jwt.create_jwt(get_claims(), token_header)
    headers = {
        'Authorization': f'Bearer {token}',
        'content-type': 'application/json'
    }

    # Create a payment first
    rv = client.post('/api/v1/payment-requests',
                     data=json.dumps(
                         get_payment_request_with_payment_method(
                             payment_method=PaymentMethod.DIRECT_PAY.value)),
                     headers=headers)
    invoice_id = rv.json.get('id')
    data = {
        'clientSystemUrl':
        'http://localhost:8080/coops-web/transactions/transaction_id=abcd',
        'payReturnUrl': 'http://localhost:8080/pay-web'
    }
    rv = client.post(f'/api/v1/payment-requests/{invoice_id}/transactions',
                     data=json.dumps(data),
                     headers={'content-type': 'application/json'})
    assert rv.status_code == 201
    assert rv.json.get('paymentId')
    assert schema_utils.validate(rv.json, 'transaction')[0]
Пример #4
0
def test_create_drawdown_refund(session, client, jwt, app, stan_server):
    """Assert that the endpoint returns 202."""
    token = jwt.create_jwt(get_claims(), token_header)
    headers = {
        'Authorization': f'Bearer {token}',
        'content-type': 'application/json'
    }

    rv = client.post('/api/v1/payment-requests',
                     data=json.dumps(
                         get_payment_request_with_payment_method(
                             business_identifier='CP0002000',
                             payment_method='DRAWDOWN')),
                     headers=headers)
    inv_id = rv.json.get('id')

    token = jwt.create_jwt(get_claims(app_request=app, role=Role.SYSTEM.value),
                           token_header)
    headers = {
        'Authorization': f'Bearer {token}',
        'content-type': 'application/json'
    }
    rv = client.post(f'/api/v1/payment-requests/{inv_id}/refunds',
                     data=json.dumps({'reason': 'Test'}),
                     headers=headers)
    assert rv.status_code == 202
    assert rv.json.get('message') == REFUND_SUCCESS_MESSAGES['DIRECT_PAY.PAID']
Пример #5
0
def test_create_bcol_payment_for_basic_user(session, public_user_mock):
    """Assert that the payment records are created."""
    with pytest.raises(Exception) as excinfo:
        PaymentService.create_payment(
            get_payment_request_with_payment_method(payment_method='DRAWDOWN', business_identifier='CP0002000'),
            get_auth_basic_user())
    assert excinfo.type == BusinessException
Пример #6
0
def test_create_bcol_payment(session, public_user_mock):
    """Assert that the payment records are created."""
    payment_response = PaymentService.create_invoice(
        get_payment_request_with_payment_method(payment_method='DRAWDOWN', business_identifier='CP0002000'),
        get_auth_premium_user())
    assert payment_response is not None
    assert payment_response.get('payment_method') == 'DRAWDOWN'
    assert payment_response.get('status_code') == 'COMPLETED'
Пример #7
0
def test_payment_creation_with_service_account(session, client, jwt, app):
    """Assert that the endpoint returns 201."""
    token = jwt.create_jwt(
        get_claims(roles=[Role.SYSTEM.value, Role.EDITOR.value]), token_header)
    headers = {
        'Authorization': f'Bearer {token}',
        'content-type': 'application/json'
    }

    rv = client.post('/api/v1/payment-requests',
                     data=json.dumps(
                         get_payment_request_with_payment_method()),
                     headers=headers)
    assert rv.status_code == 201
    assert rv.json.get('_links') is not None

    assert schema_utils.validate(rv.json, 'invoice')[0]
Пример #8
0
def test_invoice_pdf(session, client, jwt, app):
    """Test invoice pdf generation."""
    token = jwt.create_jwt(get_claims(), token_header)
    headers = {
        'Authorization': f'Bearer {token}',
        'content-type': 'application/json'
    }

    rv = client.post('/api/v1/payment-requests',
                     data=json.dumps(
                         get_payment_request_with_payment_method(
                             business_identifier='CP0002000',
                             payment_method='ONLINE_BANKING')),
                     headers=headers)
    invoice_id = rv.json.get('id')
    client.post(f'/api/v1/payment-requests/{invoice_id}/reports',
                headers=headers)
    assert True
Пример #9
0
def test_premium_payment_creation_with_payment_method(session, client, jwt,
                                                      app):
    """Assert that the endpoint returns 201."""
    token = jwt.create_jwt(get_claims(), token_header)
    headers = {
        'Authorization': f'Bearer {token}',
        'content-type': 'application/json'
    }

    rv = client.post('/api/v1/payment-requests',
                     data=json.dumps(
                         get_payment_request_with_payment_method(
                             business_identifier='CP0002000',
                             payment_method='DRAWDOWN')),
                     headers=headers)
    assert rv.status_code == 201
    assert rv.json.get('_links') is not None
    assert schema_utils.validate(rv.json, 'invoice')[0]
    assert rv.json.get('paymentMethod') == 'DRAWDOWN'
Пример #10
0
def test_create_payment_record_rollback(session, public_user_mock):
    """Assert that the payment records are created."""
    # Mock here that the invoice update fails here to test the rollback scenario
    with patch('pay_api.services.invoice.Invoice.flush', side_effect=Exception('mocked error')):
        with pytest.raises(Exception) as excinfo:
            PaymentService.create_invoice(get_payment_request(), get_auth_basic_user())
        assert excinfo.type == Exception

    # with patch('pay_api.services.invoice.InvoiceReference.create', side_effect=Exception('mocked error')):
    #     with pytest.raises(Exception) as excinfo:
    #         PaymentService.create_invoice(get_payment_request(), get_auth_basic_user())
    #     assert excinfo.type == Exception
    with patch('pay_api.services.direct_pay_service.DirectPayService.create_invoice',
               side_effect=Exception('mocked error')):
        with pytest.raises(Exception) as excinfo:
            PaymentService.create_invoice(
                get_payment_request_with_payment_method(payment_method=PaymentMethod.DIRECT_PAY.value),
                get_auth_basic_user())
        assert excinfo.type == Exception
Пример #11
0
def test_premium_payment_creation_with_payment_method_ob_cc(
        session, client, jwt, app):
    """Assert that the endpoint returns 201."""
    token = jwt.create_jwt(get_claims(), token_header)
    headers = {
        'Authorization': f'Bearer {token}',
        'content-type': 'application/json'
    }

    rv = client.post('/api/v1/payment-requests',
                     data=json.dumps(
                         get_payment_request_with_payment_method(
                             business_identifier='CP0002000',
                             payment_method='ONLINE_BANKING')),
                     headers=headers)
    assert rv.status_code == 201
    assert rv.json.get('paymentMethod') == 'ONLINE_BANKING'
    assert rv.json.get('isOnlineBankingAllowed')
    invoice_id = rv.json.get('id')

    rv = client.patch(f'/api/v1/payment-requests/{invoice_id}',
                      data=json.dumps(
                          {'paymentInfo': {
                              'methodOfPayment': 'CC'
                          }}),
                      headers=headers)

    assert rv.status_code == 200
    assert rv.json.get('paymentMethod') == 'DIRECT_PAY'

    data = {
        'clientSystemUrl':
        'http://localhost:8080/coops-web/transactions/transaction_id=abcd',
        'payReturnUrl': 'http://localhost:8080/pay-web'
    }
    rv = client.post(f'/api/v1/payment-requests/{invoice_id}/transactions',
                     data=json.dumps(data),
                     headers={'content-type': 'application/json'})
    assert rv.status_code == 201
Пример #12
0
def test_transaction_patch_direct_pay(session, client, jwt, app):
    """Assert that the transaction patch updates the payment receipts."""
    token = jwt.create_jwt(get_claims(), token_header)
    headers = {
        'Authorization': f'Bearer {token}',
        'content-type': 'application/json'
    }

    # Create a payment first
    rv = client.post('/api/v1/payment-requests',
                     data=json.dumps(
                         get_payment_request_with_payment_method(
                             payment_method=PaymentMethod.DIRECT_PAY.value)),
                     headers=headers)
    invoice_id = rv.json.get('id')
    data = {
        'clientSystemUrl':
        'http://localhost:8080/coops-web/transactions/transaction_id=abcd',
        'payReturnUrl': 'http://localhost:8080/pay-web'
    }
    rv = client.post(f'/api/v1/payment-requests/{invoice_id}/transactions',
                     data=json.dumps(data),
                     headers={'content-type': 'application/json'})
    txn_id = rv.json.get('id')
    assert rv.status_code == 201
    assert rv.json.get('paymentId')

    url = 'trnApproved=0&messageText=Duplicate%20Order%20Number%20-%20This%20order%20number%20has%20already%20been' \
          '%20processed&trnOrderId=169124&trnAmount=31.50&paymentMethod=CC&cardType=MC&authCode=null&trnDate=2020-12' \
          '-17&pbcTxnNumber=REG00001593&hashValue=a5a48bf399af4e18c2233078ebabff73'
    param = {'payResponseUrl': url}

    rv = client.patch(
        f'/api/v1/payment-requests/{invoice_id}/transactions/{txn_id}',
        data=json.dumps(param),
        headers={'content-type': 'application/json'})
    assert rv.json.get('paySystemReasonCode') == 'DUPLICATE_ORDER_NUMBER'
Пример #13
0
def test_payment_request_online_banking_with_credit(session, client, jwt, app):
    """Assert that the endpoint returns 201."""
    token = jwt.create_jwt(get_claims(role=Role.SYSTEM.value), token_header)
    headers = {
        'Authorization': f'Bearer {token}',
        'content-type': 'application/json'
    }

    rv = client.post(
        '/api/v1/accounts',
        data=json.dumps(
            get_basic_account_payload(
                payment_method=PaymentMethod.ONLINE_BANKING.value)),
        headers=headers)
    auth_account_id = rv.json.get('authAccountId')

    # Update the payment account as ACTIVE
    payment_account: PaymentAccountModel = PaymentAccountModel.find_by_auth_account_id(
        auth_account_id)
    payment_account.credit = 51
    payment_account.save()

    token = jwt.create_jwt(get_claims(), token_header)
    headers = {
        'Authorization': f'Bearer {token}',
        'content-type': 'application/json'
    }
    rv = client.post('/api/v1/payment-requests',
                     data=json.dumps(
                         get_payment_request_with_payment_method(
                             business_identifier='CP0002000',
                             payment_method='ONLINE_BANKING')),
                     headers=headers)
    invoice_id = rv.json.get('id')

    rv = client.patch(
        f'/api/v1/payment-requests/{invoice_id}?applyCredit=true',
        data=json.dumps({}),
        headers=headers)

    payment_account: PaymentAccountModel = PaymentAccountModel.find_by_auth_account_id(
        auth_account_id)
    assert payment_account.credit == 1

    # Now set the credit less than the total of invoice.
    payment_account.credit = 49
    headers = {
        'Authorization': f'Bearer {token}',
        'content-type': 'application/json'
    }
    rv = client.post('/api/v1/payment-requests',
                     data=json.dumps(
                         get_payment_request_with_payment_method(
                             business_identifier='CP0002000',
                             payment_method='ONLINE_BANKING')),
                     headers=headers)
    invoice_id = rv.json.get('id')

    rv = client.patch(
        f'/api/v1/payment-requests/{invoice_id}?applyCredit=true',
        data=json.dumps({}),
        headers=headers)
    # Credit won't be applied as the invoice total is 50 and the credit should remain as 0.
    payment_account: PaymentAccountModel = PaymentAccountModel.find_by_auth_account_id(
        auth_account_id)
    assert payment_account.credit == 0
Пример #14
0
def test_create_pad_refund(session, client, jwt, app, stan_server):
    """Assert that the endpoint returns 202 and creates a credit on the account."""
    # 1. Create a PAD payment_account and cfs_account.
    # 2. Create a PAD invoice and mark as PAID.
    # 3. Issue a refund and assert credit is reflected on payment_account.
    # 4. Create an invoice again and assert that credit is updated.
    auth_account_id = 1234

    token = jwt.create_jwt(get_claims(role=Role.SYSTEM.value), token_header)
    headers = {
        'Authorization': f'Bearer {token}',
        'content-type': 'application/json'
    }
    client.post(
        '/api/v1/accounts',
        data=json.dumps(
            get_unlinked_pad_account_payload(account_id=auth_account_id)),
        headers=headers)
    pay_account: PaymentAccountModel = PaymentAccountModel.find_by_auth_account_id(
        auth_account_id)
    cfs_account: CfsAccountModel = CfsAccountModel.find_by_account_id(
        pay_account.id)[0]
    cfs_account.cfs_party = '1111'
    cfs_account.cfs_account = '1111'
    cfs_account.cfs_site = '1111'
    cfs_account.status = CfsAccountStatus.ACTIVE.value
    cfs_account.save()

    pay_account.pad_activation_date = datetime.now()
    pay_account.save()

    token = jwt.create_jwt(get_claims(), token_header)
    headers = {
        'Authorization': f'Bearer {token}',
        'content-type': 'application/json',
        'Account-Id': auth_account_id
    }

    rv = client.post('/api/v1/payment-requests',
                     data=json.dumps(
                         get_payment_request_with_payment_method(
                             payment_method=PaymentMethod.PAD.value)),
                     headers=headers)

    inv_id = rv.json.get('id')
    inv_total = rv.json.get('total')

    inv: InvoiceModel = InvoiceModel.find_by_id(inv_id)
    inv.invoice_status_code = InvoiceStatus.PAID.value
    inv.save()

    token = jwt.create_jwt(get_claims(app_request=app, role=Role.SYSTEM.value),
                           token_header)
    headers = {
        'Authorization': f'Bearer {token}',
        'content-type': 'application/json'
    }
    rv = client.post(f'/api/v1/payment-requests/{inv_id}/refunds',
                     data=json.dumps({'reason': 'Test'}),
                     headers=headers)
    assert rv.status_code == 202
    assert rv.json.get('message') == REFUND_SUCCESS_MESSAGES['PAD.PAID']

    # Assert credit is updated.
    pay_account: PaymentAccountModel = PaymentAccountModel.find_by_auth_account_id(
        auth_account_id)
    credit = pay_account.credit
    assert pay_account.credit == inv_total

    # Create an invoice again and assert that credit is updated.
    token = jwt.create_jwt(get_claims(), token_header)
    headers = {
        'Authorization': f'Bearer {token}',
        'content-type': 'application/json',
        'Account-Id': auth_account_id
    }

    rv = client.post('/api/v1/payment-requests',
                     data=json.dumps(
                         get_payment_request_with_service_fees(
                             corp_type='CP', filing_type='OTADD')),
                     headers=headers)
    inv_total = rv.json.get('total')

    pay_account: PaymentAccountModel = PaymentAccountModel.find_by_auth_account_id(
        auth_account_id)
    credit -= inv_total
    assert pay_account.credit == credit

    # Create an invoice again and assert that credit is updated.
    token = jwt.create_jwt(get_claims(), token_header)
    headers = {
        'Authorization': f'Bearer {token}',
        'content-type': 'application/json',
        'Account-Id': auth_account_id
    }

    rv = client.post('/api/v1/payment-requests',
                     data=json.dumps(
                         get_payment_request_with_payment_method(
                             payment_method=PaymentMethod.PAD.value)),
                     headers=headers)
    inv_total = rv.json.get('total')
    # Credit must be zero now as the new invoice amount exceeds remaining credit.
    assert pay_account.credit == 0