Coverage for functions \ flipdare \ service \ payments \ _base_payment_handler.py: 58%

26 statements  

« prev     ^ index     » next       coverage.py v7.13.0, created at 2026-05-08 12:22 +1000

1#!/usr/bin/env python 

2# Copyright (c) 2026 Flipdare Pty Ltd. All rights reserved. 

3# 

4# This file is part of Flipdare's proprietary software and contains 

5# confidential and copyrighted material. Unauthorised copying, 

6# modification, distribution, or use of this file is strictly 

7# prohibited without prior written permission from Flipdare Pty Ltd. 

8# 

9# This software includes third-party components licensed under MIT, 

10# BSD, and Apache 2.0 licences. See THIRD_PARTY_NOTICES for details. 

11# 

12 

13from __future__ import annotations 

14 

15from typing import TYPE_CHECKING, NoReturn 

16from flipdare.app_log import LOG 

17from flipdare.error import ( 

18 AppError, 

19 StripeErrorContext, 

20) 

21 

22from flipdare.message.error_message import ErrorMessage 

23from flipdare.error.app_error_protocol import ( 

24 AppErrorProtocol, 

25 ErrorProtocolGuard, 

26) 

27from flipdare.error.error_context import ErrorContext 

28from flipdare.service._error_mixin import ErrorMixin 

29from flipdare.service._service_provider import ServiceProvider 

30from flipdare.service._user_mixin import UserMixin 

31 

32if TYPE_CHECKING: 

33 from flipdare.manager.backend_manager import BackendManager 

34 from flipdare.manager.db_manager import DbManager 

35 from flipdare.manager.service_manager import ServiceManager 

36 

37__all__ = ["BasePaymentHandler"] 

38 

39 

40class BasePaymentHandler(ErrorMixin, UserMixin, ServiceProvider): 

41 def __init__( 

42 self, 

43 db_manager: DbManager | None = None, 

44 backend_manager: BackendManager | None = None, 

45 service_manager: ServiceManager | None = None, 

46 ) -> None: 

47 super().__init__( 

48 db_manager=db_manager, 

49 backend_manager=backend_manager, 

50 service_manager=service_manager, 

51 ) 

52 

53 def raise_payment_error( 

54 self, 

55 endpoint: str, 

56 error_code: AppErrorProtocol, 

57 message: ErrorMessage | str, 

58 error: Exception | None = None, 

59 ) -> NoReturn: 

60 LOG().error(f"Payment error in request {endpoint}: {message}") 

61 

62 if ErrorProtocolGuard.is_app_code(error_code): 

63 if error is not None: 

64 ctx = ErrorContext.from_exception( 

65 endpoint=endpoint, 

66 error_code=error_code, 

67 message=message, 

68 error=error, 

69 ) 

70 else: 

71 ctx = ErrorContext( 

72 endpoint=endpoint, 

73 error_code=error_code, 

74 message=message, 

75 ) 

76 raise AppError.from_context(ctx) 

77 

78 if ErrorProtocolGuard.is_payment_code(error_code): 

79 stripe_ctx = StripeErrorContext.from_code( 

80 endpoint=endpoint, 

81 error_code=error_code, 

82 cause=message, 

83 error=error, 

84 ) 

85 

86 raise AppError.from_context(stripe_ctx) 

87 

88 # impossible to get here, but python has fucken hopeless linters. 

89 msg = f"Invalid error code ({type(error_code)}) passed to raise_payment_error" 

90 raise Exception(msg) # noqa: TRY002