def test_account_purchase_history_invalid_request(session, client, jwt, app): """Assert that the endpoint returns 400.""" 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()), headers=headers) payment: Payment = Payment.find_by_id(rv.json.get('id')) credit_account: CreditPaymentAccount = CreditPaymentAccount.find_by_id( payment.invoices[0].credit_account_id) pay_account: PaymentAccount = PaymentAccount.find_by_id( credit_account.account_id) search_filter = {'businessIdentifier': 1111} rv = client.post( f'/api/v1/accounts/{pay_account.auth_account_id}/payments/queries?page=1&limit=5', data=json.dumps(search_filter), headers=headers) assert rv.status_code == 400 assert schema_utils.validate(rv.json, 'problem')[0]
def test_get_monthly_statement_report_as_csv(session, client, jwt, app): """Assert that the monthly statement report is returning response.""" # Create a payment account and statement details, then get all statements for the account token = jwt.create_jwt(get_claims(), token_header) headers = { 'Authorization': f'Bearer {token}', 'content-type': 'application/json', 'Accept': ContentType.CSV.value } rv = client.post('/api/v1/payment-requests', data=json.dumps( get_payment_request(business_identifier='CP0002000')), headers=headers) payment: Payment = Payment.find_by_id(rv.json.get('id')) bcol_account: BcolPaymentAccount = BcolPaymentAccount.find_by_id( payment.invoices[0].bcol_account_id) pay_account: PaymentAccount = PaymentAccount.find_by_id( bcol_account.account_id) settings_model = factory_statement_settings( payment_account_id=pay_account.id, frequency=StatementFrequency.DAILY.value) statement_model = factory_statement( payment_account_id=pay_account.id, frequency=StatementFrequency.DAILY.value, statement_settings_id=settings_model.id) factory_statement_invoices(statement_id=statement_model.id, invoice_id=payment.invoices[0].id) rv = client.get( f'/api/v1/accounts/{pay_account.auth_account_id}/statements/{statement_model.id}', headers=headers) assert rv.status_code == 200
def test_account_purchase_history_pagination(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' } for i in range(10): rv = client.post('/api/v1/payment-requests', data=json.dumps(get_payment_request()), headers=headers) payment: Payment = Payment.find_by_id(rv.json.get('id')) credit_account: CreditPaymentAccount = CreditPaymentAccount.find_by_id( payment.invoices[0].credit_account_id) pay_account: PaymentAccount = PaymentAccount.find_by_id( credit_account.account_id) rv = client.post( f'/api/v1/accounts/{pay_account.auth_account_id}/payments/queries?page=1&limit=5', data=json.dumps({}), headers=headers) assert rv.status_code == 200 assert rv.json.get('total') == 10 assert len(rv.json.get('items')) == 5
def test_account_purchase_history_default_list(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' } # Create 11 payments for i in range(11): rv = client.post('/api/v1/payment-requests', data=json.dumps(get_payment_request()), headers=headers) payment: Payment = Payment.find_by_id(rv.json.get('id')) credit_account: CreditPaymentAccount = CreditPaymentAccount.find_by_id( payment.invoices[0].credit_account_id) pay_account: PaymentAccount = PaymentAccount.find_by_id( credit_account.account_id) rv = client.post( f'/api/v1/accounts/{pay_account.auth_account_id}/payments/queries', data=json.dumps({}), headers=headers) assert rv.status_code == 200 # Assert the total is coming as 10 which is the value of default TRANSACTION_REPORT_DEFAULT_TOTAL assert rv.json.get('total') == 10
def test_account_purchase_history_export_invalid_request( 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' } rv = client.post('/api/v1/payment-requests', data=json.dumps(get_payment_request()), headers=headers) payment: Payment = Payment.find_by_id(rv.json.get('id')) credit_account: CreditPaymentAccount = CreditPaymentAccount.find_by_id( payment.invoices[0].credit_account_id) pay_account: PaymentAccount = PaymentAccount.find_by_id( credit_account.account_id) headers = { 'Authorization': f'Bearer {token}', 'content-type': 'application/json', 'Accept': 'application/pdf' } rv = client.post( f'/api/v1/accounts/{pay_account.auth_account_id}/payments/reports', data=json.dumps({'businessIdentifier': 1111}), headers=headers) assert rv.status_code == 400
def test_get_default_statement_settings_weekly(session, client, jwt, app): """Assert that the default statement setting is weekly.""" 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(business_identifier='CP0002000')), headers=headers) payment: Payment = Payment.find_by_id(rv.json.get('id')) bcol_account: BcolPaymentAccount = BcolPaymentAccount.find_by_id(payment.invoices[0].bcol_account_id) pay_account: PaymentAccount = PaymentAccount.find_by_id(bcol_account.account_id) rv = client.get(f'/api/v1/accounts/{pay_account.auth_account_id}/statements/settings', headers=headers) assert rv.status_code == 200 assert rv.json.get('frequency') == StatementFrequency.WEEKLY.value
def _create_statement_records(cls, current_time, search_filter, statement_settings): statement_from = None statement_to = None if search_filter.get('dateFilter', None): statement_from = parse( search_filter.get('dateFilter').get('startDate')) statement_to = parse( search_filter.get('dateFilter').get('endDate')) elif search_filter.get('weekFilter', None): index = search_filter.get('weekFilter').get('index') statement_from, statement_to = get_week_start_and_end_date(index) elif search_filter.get('monthFilter', None): statement_from, statement_to = get_first_and_last_dates_of_month( search_filter.get('monthFilter').get('month'), search_filter.get('monthFilter').get('year')) for setting, pay_account in statement_settings: statement = StatementModel( frequency=setting.frequency, statement_settings_id=setting.id, payment_account_id=pay_account.id, created_on=current_time, from_date=statement_from, to_date=statement_to, notification_status_code=NotificationStatus.PENDING.value if pay_account.statement_notification_enabled is True else NotificationStatus.SKIP.value) # Add to DB session statement = statement.flush() purchases, total = PaymentModel.search_purchase_history( auth_account_id=pay_account.auth_account_id, return_all=True, search_filter=search_filter, page=None, limit=None) for purchase in purchases: invoice = purchase[1] statement_invoice = StatementInvoicesModel( statement_id=statement.id, invoice_id=invoice.id) db.session.add(statement_invoice)
def test_post_default_statement_settings_daily(session, client, jwt, app): """Assert that the post endpoint works.""" 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(business_identifier='CP0002000')), headers=headers) payment: Payment = Payment.find_by_id(rv.json.get('id')) bcol_account: BcolPaymentAccount = BcolPaymentAccount.find_by_id(payment.invoices[0].bcol_account_id) pay_account: PaymentAccount = PaymentAccount.find_by_id(bcol_account.account_id) rv = client.get(f'/api/v1/accounts/{pay_account.auth_account_id}/statements/settings', data=json.dumps({}), headers=headers) assert rv.status_code == 200 assert rv.json.get('frequency') == StatementFrequency.WEEKLY.value # Set the frequency to Daily and assert daily_frequency = {'frequency': 'DAILY'} rv = client.post(f'/api/v1/accounts/{pay_account.auth_account_id}/statements/settings', data=json.dumps(daily_frequency), headers=headers) assert rv.json.get('frequency') == StatementFrequency.DAILY.value end_date = get_week_start_and_end_date()[1] assert rv.json.get('fromDate') == (end_date + timedelta(days=1)).strftime('%Y-%m-%d') # Set the frequency to Monthly and assert daily_frequency = {'frequency': 'MONTHLY'} rv = client.post(f'/api/v1/accounts/{pay_account.auth_account_id}/statements/settings', data=json.dumps(daily_frequency), headers=headers) end_date = get_first_and_last_dates_of_month(current_local_time().month, current_local_time().year)[1] assert rv.json.get('frequency') == StatementFrequency.MONTHLY.value assert rv.json.get('fromDate') == (end_date + timedelta(days=1)).strftime('%Y-%m-%d') # Get the latest frequency rv = client.get(f'/api/v1/accounts/{pay_account.auth_account_id}/statements/settings', data=json.dumps({}), headers=headers) assert rv.status_code == 200 assert rv.json.get('frequency') == StatementFrequency.MONTHLY.value
def test_get_weekly_statements(session, client, jwt, app): """Assert that the default statement setting is weekly.""" # Create a payment account and statement details, then get all statements for the account 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(business_identifier='CP0002000')), headers=headers) payment: Payment = Payment.find_by_id(rv.json.get('id')) bcol_account: BcolPaymentAccount = BcolPaymentAccount.find_by_id( payment.invoices[0].bcol_account_id) pay_account: PaymentAccount = PaymentAccount.find_by_id( bcol_account.account_id) settings_model = factory_statement_settings( payment_account_id=pay_account.id, frequency=StatementFrequency.DAILY.value) statement_model = factory_statement( payment_account_id=pay_account.id, frequency=StatementFrequency.WEEKLY.value, statement_settings_id=settings_model.id) factory_statement_invoices(statement_id=statement_model.id, invoice_id=payment.invoices[0].id) rv = client.get( f'/api/v1/accounts/{pay_account.auth_account_id}/statements', headers=headers) assert rv.status_code == 200 assert rv.json.get('total') == 1 assert rv.json.get('items')[0].get( 'frequency') == StatementFrequency.WEEKLY.value
def update_failed_distributions(cls): # pylint:disable=too-many-locals """Update failed distributions. Steps: 1. Get all invoices with status UPDATE_REVENUE_ACCOUNT. 2. Find the completed invoice reference for the invoice. 3. Call the paybc GET service and check if there is any revenue not processed. 4. If yes, update the revenue details. 5. Update the invoice status as PAID and save. """ gl_updated_invoices = InvoiceModel.query.filter_by( invoice_status_code=InvoiceStatus.UPDATE_REVENUE_ACCOUNT.value).all() current_app.logger.debug(f'Found {len(gl_updated_invoices)} invoices to update revenue details.') if len(gl_updated_invoices) > 0: # pylint:disable=too-many-nested-blocks access_token: str = cls._get_token().json().get('access_token') paybc_ref_number: str = current_app.config.get('PAYBC_DIRECT_PAY_REF_NUMBER') paybc_svc_base_url = current_app.config.get('PAYBC_DIRECT_PAY_BASE_URL') for gl_updated_invoice in gl_updated_invoices: payment: PaymentModel = PaymentModel.find_payment_for_invoice(gl_updated_invoice.id) # For now handle only GL updates for Direct Pay, more to come in future if payment.payment_method_code != PaymentMethod.DIRECT_PAY.value: cls._update_invoice_status(gl_updated_invoice, InvoiceStatus.PAID.value) else: active_reference = list( filter(lambda reference: (reference.status_code == InvoiceReferenceStatus.COMPLETED.value), gl_updated_invoice.references))[0] payment_url: str = \ f'{paybc_svc_base_url}/paybc/payment/{paybc_ref_number}/{active_reference.invoice_number}' payment_details: dict = cls.get_payment_details(payment_url, access_token) if payment_details and payment_details.get('paymentstatus') == STATUS_PAID: has_gl_completed: bool = True for revenue in payment_details.get('revenue'): if revenue.get('glstatus') in STATUS_NOT_PROCESSED: has_gl_completed = False if not has_gl_completed: post_revenue_payload = { 'revenue': [] } invoice: InvoiceService = InvoiceService.find_by_id(identifier=gl_updated_invoice.id, skip_auth_check=True) payment_line_items = PaymentLineItemModel.find_by_invoice_ids([invoice.id]) index: int = 0 for payment_line_item in payment_line_items: distribution_code = DistributionCodeModel.find_by_id( payment_line_item.fee_distribution_id) index = index + 1 post_revenue_payload['revenue'].append( cls.get_revenue_details(index, distribution_code, payment_line_item.total)) if payment_line_item.service_fees is not None and payment_line_item.service_fees > 0: index = index + 1 post_revenue_payload['revenue'].append( cls.get_revenue_details(index, distribution_code, payment_line_item.service_fees, is_service_fee=True)) OAuthService.post(payment_url, access_token, AuthHeaderType.BEARER, ContentType.JSON, post_revenue_payload) cls._update_invoice_status(gl_updated_invoice, InvoiceStatus.PAID.value)