def test_payment_staff_registration_mock(client, jwt, pay_trans_type, routing_slip, bcol_number, dat_number, waive_fees): """Assert that a pay-api staff registration payment request works as expected with the mock service endpoint.""" # setup token = helper_create_jwt(jwt, [PPR_ROLE]) payment = Payment(jwt=token, account_id=None, details=PAY_DETAILS_REGISTRATION) payment.api_url = MOCK_URL_NO_KEY transaction_info = {'transactionType': pay_trans_type, 'feeQuantity': 1} if waive_fees: transaction_info['waiveFees'] = True if routing_slip: transaction_info['routingSlipNumber'] = routing_slip if bcol_number: transaction_info['bcolAccountNumber'] = bcol_number if dat_number: transaction_info['datNumber'] = dat_number # test pay_data = payment.create_payment_staff_registration( transaction_info, 'UT-PAY-SEARCH-01') # print(pay_data) # check assert pay_data assert pay_data['invoiceId'] assert pay_data['receipt']
def test_payment_staff_search_mock(client, jwt, pay_trans_type, routing_slip, bcol_number, dat_number, waive_fees): """Assert that a pay-api staff search payment request works as expected with the mock service endpoint.""" # setup token = helper_create_jwt(jwt, [PPR_ROLE]) payment = Payment(jwt=token, account_id='PS12345', details=PAY_DETAILS_SEARCH) payment.api_url = MOCK_URL_NO_KEY transaction_info = {'transactionType': pay_trans_type} if pay_trans_type in (TransactionTypes.SEARCH_STAFF_CERTIFIED_NO_FEE.value, TransactionTypes.SEARCH_STAFF_NO_FEE.value): transaction_info['certified'] = True if routing_slip: transaction_info['routingSlipNumber'] = routing_slip if bcol_number: transaction_info['bcolAccountNumber'] = bcol_number if dat_number: transaction_info['datNumber'] = dat_number # test pay_data = payment.create_payment_staff_search(transaction_info, 'UT-PAY-SEARCH-01') # print(pay_data) # check assert pay_data assert pay_data['invoiceId'] assert pay_data['receipt']
def test_payment_exception(client, jwt): """Assert that a pay-api payment request works as expected with a 500 exception.""" # setup token = helper_create_jwt(jwt, [PPR_ROLE]) payment = Payment(jwt=token, account_id='PS12345') payment.api_url = MOCK_URL # test with pytest.raises(SBCPaymentException) as request_err: payment.create_payment(TransactionTypes.SEARCH.value, 1, '200000001', 'UT-PAY-SEARCH-01') assert request_err assert request_err.value.status_code == HTTPStatus.INTERNAL_SERVER_ERROR
def staff_search(req: request, request_json, account_id: str): """Execute a staff search with special payment validation and methods.""" payment_info = build_staff_payment(req, account_id) # bcol help is no fee; reg staff can be no fee. # FAS is routing slip only. # BCOL is dat number (optional) and BCOL account number (mandatory). # All staff roles including SBC can submit no fee searches. if ROUTING_SLIP_PARAM in payment_info and BCOL_NUMBER_PARAM in payment_info: return resource_utils.staff_payment_bcol_fas() if CERTIFIED_PARAM in payment_info: request_json['certified'] = True query: SearchRequest = SearchRequest.create_from_json( request_json, account_id, g.jwt_oidc_token_info.get('username', None)) # Always create a payment transaction. invoice_id = None payment = Payment(jwt=jwt.get_token_auth_header(), account_id=account_id, details=get_payment_details(query, request_json['type'])) # staff payment pay_ref = payment.create_payment_staff_search(payment_info, query.client_reference_id) invoice_id = pay_ref['invoiceId'] query.pay_invoice_id = int(invoice_id) query.pay_path = pay_ref['receipt'] # Execute the search query: treat no results as a success. try: query.search() # Now save the initial detail results in the search_result table with no # search selection criteria (the absence indicates an incomplete search). search_result = SearchResult.create_from_search_query(query) search_result.save() except Exception as db_exception: # noqa: B902; handle all db related errors. current_app.logger.error( SAVE_ERROR_MESSAGE.format(account_id, repr(db_exception))) if invoice_id is not None: current_app.logger.info( PAY_REFUND_MESSAGE.format(account_id, invoice_id)) try: payment.cancel_payment(invoice_id) except Exception as cancel_exception: # noqa: B902; log exception current_app.logger.error( PAY_REFUND_ERROR.format(account_id, invoice_id, repr(cancel_exception))) raise db_exception return query.json, HTTPStatus.CREATED
def test_payment_search_mock(client, jwt): """Assert that a pay-api search payment request works as expected with the mock service endpoint.""" # setup token = helper_create_jwt(jwt, [PPR_ROLE]) payment = Payment(jwt=token, account_id='PS12345') payment.api_url = MOCK_URL_NO_KEY # test pay_data = payment.create_payment(TransactionTypes.SEARCH.value, 1, '200000001', 'UT-PAY-SEARCH-01') print(pay_data) # check assert pay_data assert pay_data['invoiceId'] assert pay_data['receipt']
def test_payment_apikey(client, jwt): """Assert that a gateway pay-api payment request works as expected.""" # setup apikey = os.getenv('PAYMENT_GATEWAY_APIKEY_TEST') if apikey: token = helper_create_jwt(jwt, [PPR_ROLE]) payment = Payment(jwt=token, account_id='PS12345', api_key=apikey) payment.api_url = MOCK_URL # test pay_data = payment.create_payment(TransactionTypes.SEARCH.value, 1, '200000001', 'UT-PAY-SEARCH-01') print(pay_data) # check assert pay_data assert pay_data['invoiceId'] assert pay_data['receipt']
def post(): # pylint: disable=too-many-branches """Execute a new search request using criteria in the request body.""" try: # Quick check: must be staff or provide an account ID. account_id = resource_utils.get_account_id(request) if not account_id: return resource_utils.account_required_response() # Verify request JWT and account ID if not authorized(account_id, jwt): return resource_utils.unauthorized_error_response(account_id) request_json = request.get_json(silent=True) # Validate request against the schema. valid_format, errors = schema_utils.validate( request_json, 'searchQuery', 'ppr') if not valid_format: return resource_utils.validation_error_response( errors, VAL_ERROR) # Perform any extra data validation such as start and end dates here SearchRequest.validate_query(request_json) # Staff has special payment rules and setup. if is_staff_account(account_id) or is_bcol_help(account_id): return staff_search(request, request_json, account_id) query = SearchRequest.create_from_json( request_json, account_id, g.jwt_oidc_token_info.get('username', None)) # Charge a search fee. invoice_id = None payment = Payment(jwt=jwt.get_token_auth_header(), account_id=account_id, details=get_payment_details( query, request_json['type'])) pay_ref = payment.create_payment(TransactionTypes.SEARCH.value, 1, None, query.client_reference_id) invoice_id = pay_ref['invoiceId'] query.pay_invoice_id = int(invoice_id) query.pay_path = pay_ref['receipt'] # Execute the search query: treat no results as a success. try: query.search() # Now save the initial detail results in the search_result table with no # search selection criteria (the absence indicates an incomplete search). search_result = SearchResult.create_from_search_query(query) search_result.save() except Exception as db_exception: # noqa: B902; handle all db related errors. current_app.logger.error( SAVE_ERROR_MESSAGE.format(account_id, repr(db_exception))) if invoice_id is not None: current_app.logger.info( PAY_REFUND_MESSAGE.format(account_id, invoice_id)) try: payment.cancel_payment(invoice_id) except Exception as cancel_exception: # noqa: B902; log exception current_app.logger.error( PAY_REFUND_ERROR.format(account_id, invoice_id, repr(cancel_exception))) raise db_exception return query.json, HTTPStatus.CREATED except SBCPaymentException as pay_exception: return resource_utils.pay_exception_response(pay_exception) except BusinessException as exception: return resource_utils.business_exception_response(exception) except Exception as default_exception: # noqa: B902; return nicer default error return resource_utils.default_exception_response(default_exception)
def post(): """Execute a new search request using criteria in the request body.""" try: # Quick check: must be staff or provide an account ID. account_id = resource_utils.get_account_id(request) if not is_staff(jwt) and account_id is None: return resource_utils.account_required_response() # Verify request JWT and account ID if not authorized(account_id, jwt): return resource_utils.unauthorized_error_response(account_id) request_json = request.get_json(silent=True) # Validate request against the schema. valid_format, errors = schema_utils.validate( request_json, 'searchQuery', 'ppr') if not valid_format: return resource_utils.validation_error_response( errors, VAL_ERROR) # Perform any extra data validation such as start and end dates here SearchClient.validate_query(request_json) query = SearchClient.create_from_json(request_json, account_id) # Charge a search fee. if account_id: payment = Payment(jwt=jwt.get_token_auth_header(), account_id=account_id) pay_ref = payment.create_payment(TransactionTypes.SEARCH.value, 1, None, query.client_reference_id) invoice_id = pay_ref['invoiceId'] query.pay_invoice_id = int(invoice_id) query.pay_path = pay_ref['receipt'] # Execute the search query: treat no results as a success. try: query.search() # if not query.search_response or query.returned_results_size == 0: # return resource_utils.unprocessable_error_response('search query') # Now save the initial detail results in the search_result table with no # search selection criteria (the absence indicates an incomplete search). search_result = SearchResult.create_from_search_query(query) search_result.save() except Exception as db_exception: # noqa: B902; handle all db related errors. current_app.logger.error( f'Search {account_id} db save failed: ' + repr(db_exception)) current_app.logger.error( f'Search {account_id} rolling back payment for invoice {invoice_id}.' ) try: payment.cancel_payment(invoice_id) except Exception as cancel_exception: # noqa: B902; log exception current_app.logger.error( f'Search {account_id} payment refund failed for invoice {invoice_id}: ' + repr(cancel_exception)) raise db_exception return query.json, HTTPStatus.CREATED except SBCPaymentException as pay_exception: return resource_utils.pay_exception_response(pay_exception) except BusinessException as exception: return resource_utils.business_exception_response(exception) except Exception as default_exception: # noqa: B902; return nicer default error return resource_utils.default_exception_response(default_exception)