def get(invoice_id): """Get the invoice records.""" try: response, status = InvoiceService.find_by_id(invoice_id).asdict( include_dynamic_fields=True), HTTPStatus.OK except BusinessException as exception: return exception.response() return jsonify(response), status
def get_receipt(self, payment_account: PaymentAccount, receipt_number: str, invoice_reference: InvoiceReference): """Create a static receipt.""" # Find the invoice using the invoice_number invoice = Invoice.find_by_id(invoice_reference.invoice_id, skip_auth_check=True) return f'RCPT_{invoice_reference.invoice_number}', datetime.now( ), invoice.total
def test_invoice_find_by_id(session): """Assert that the invoice is saved to the table.""" payment_account = factory_payment_account() payment_account.save() i = factory_invoice(payment_account=payment_account) i.save() invoice = Invoice_service.find_by_id(i.id, skip_auth_check=True) assert invoice is not None assert invoice.id is not None assert invoice.invoice_status_code is not None assert invoice.refund is None assert invoice.payment_date is None assert invoice.total is not None assert invoice.paid is None assert not invoice.payment_line_items
def create_transaction_for_invoice( invoice_id: int, request_json: Dict) -> PaymentTransaction: """Create transaction record for invoice, by creating a payment record if doesn't exist.""" current_app.logger.debug('<create transaction') # Lookup invoice record invoice: Invoice = Invoice.find_by_id(invoice_id, skip_auth_check=True) if not invoice.id: raise BusinessException(Error.INVALID_INVOICE_ID) if invoice.payment_method_code == PaymentMethod.PAD.value: # No transaction needed for PAD invoices. raise BusinessException(Error.INVALID_TRANSACTION) pay_system_service: PaymentSystemService = PaymentSystemFactory.create_from_payment_method( payment_method=invoice.payment_method_code) # Check if return url is valid PaymentTransaction._validate_redirect_url_and_throw_error( invoice.payment_method_code, request_json.get('clientSystemUrl')) # Check if there is a payment created. If not, create a payment record with status CREATED payment: Payment = Payment.find_payment_for_invoice(invoice_id) if not payment: # Transaction is against payment, so create a payment if not present. invoice_reference = InvoiceReference.find_active_reference_by_invoice_id( invoice.id) # Create a payment record payment = Payment.create( payment_method=pay_system_service.get_payment_method_code(), payment_system=pay_system_service.get_payment_system_code(), payment_status=pay_system_service.get_default_payment_status(), invoice_number=invoice_reference.invoice_number, invoice_amount=invoice.total, payment_account_id=invoice.payment_account_id) transaction = PaymentTransaction._create_transaction(payment, request_json, invoice=invoice) current_app.logger.debug('>create transaction') return transaction
def test_invoice_saved_from_new(session): """Assert that the invoice is saved to the table.""" payment_account = factory_payment_account() payment = factory_payment() payment_account.save() payment.save() i = factory_invoice(payment_account=payment_account) i.save() fee_schedule = FeeSchedule.find_by_filing_type_and_corp_type('CP', 'OTANN') line = factory_payment_line_item(i.id, fee_schedule_id=fee_schedule.fee_schedule_id) line.save() invoice = Invoice_service.find_by_id(i.id, skip_auth_check=True) assert invoice is not None assert invoice.id is not None assert invoice.invoice_status_code is not None assert invoice.refund is None assert invoice.payment_date is None assert invoice.total is not None assert invoice.paid is None assert invoice.payment_line_items is not None assert invoice.folio_number is not None assert invoice.business_identifier is not None
def test_invoice_with_temproary_business_identifier(session): """Assert that the invoice dictionary is not include temproary business identifier.""" payment_account = factory_payment_account() payment = factory_payment() payment_account.save() payment.save() i = factory_invoice(payment_account=payment_account, business_identifier='Tzxcasd') i.save() fee_schedule = FeeSchedule.find_by_filing_type_and_corp_type('CP', 'OTANN') line = factory_payment_line_item(i.id, fee_schedule_id=fee_schedule.fee_schedule_id) line.save() invoice = Invoice_service.find_by_id(i.id, skip_auth_check=True) assert invoice is not None assert invoice.id is not None assert invoice.invoice_status_code is not None assert invoice.refund is None assert invoice.payment_date is None assert invoice.total is not None assert invoice.paid is None assert invoice.payment_line_items is not None assert invoice.folio_number is not None assert invoice.business_identifier is not None invoice_dict = invoice.asdict() assert invoice_dict.get('business_identifier') is None
def test_invoice_invalid_lookup(session): """Test invalid lookup.""" invoice = Invoice_service.find_by_id(999) assert invoice is not None assert invoice.id is None
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)
def test_invoice_invalid_lookup(session): """Test invalid lookup.""" with pytest.raises(BusinessException) as excinfo: Invoice_service.find_by_id(999, skip_auth_check=True) assert excinfo.type == BusinessException
def get_receipt(self, payment_account: PaymentAccount, receipt_number: str, invoice_reference: InvoiceReference): """Get receipt from bcol for the receipt number or get receipt against invoice number.""" current_app.logger.debug('<get_receipt') invoice = Invoice.find_by_id(invoice_reference.invoice_id, skip_auth_check=True) return f'RCPT_{invoice_reference.invoice_number}', datetime.now(), invoice.total