Coverage for functions \ flipdare \ payments \ data \ app_payment_context.py: 68%

38 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 dataclasses import dataclass 

16from typing import Self, final 

17 

18from flipdare.generated.model.payment.account_info_model import AccountInfoModel 

19from flipdare.generated.model.payment.audit_info_model import AuditInfoModel 

20from flipdare.generated.model.payment.customer_info_model import CustomerInfoModel 

21from flipdare.generated.model.payment.risk_assessment_model import RiskAssessmentModel 

22from flipdare.generated.shared.model.user.app_fee_type import AppFeeType 

23from flipdare.generated.shared.stripe.stripe_account_type import StripeAccountType 

24from flipdare.generated.shared.stripe.stripe_currency_code import StripeCurrencyCode 

25from flipdare.payments.core.stripe_guard import StripeGuard 

26from flipdare.wrapper.user_wrapper import UserWrapper 

27 

28 

29@final 

30@dataclass(frozen=True, slots=True) 

31class AppPaymentContext: # type: ignore[misc] 

32 dare_id: str 

33 amount: int 

34 audit_info: AuditInfoModel 

35 risk_assessment: RiskAssessmentModel 

36 customer: CustomerInfoModel 

37 account: AccountInfoModel 

38 

39 @classmethod 

40 def create_from_user( 

41 cls, 

42 dare_id: str, 

43 amount: int, 

44 audit_info: AuditInfoModel, 

45 risk_assessment: RiskAssessmentModel, 

46 customer: UserWrapper, 

47 account: UserWrapper, 

48 ) -> AppPaymentContext: 

49 

50 customer_settings = customer.model.stripe_settings 

51 if not StripeGuard.is_customer(customer_settings): 

52 msg = ( 

53 f"Customer user {customer_settings} does not have valid Stripe customer settings." 

54 ) 

55 raise ValueError(msg) 

56 

57 account_settings = account.model.stripe_settings 

58 if not StripeGuard.is_account(account_settings): 

59 msg = f"Account: {account_settings} does not have valid Stripe settings." 

60 raise ValueError(msg) 

61 

62 return cls.create_from_params( 

63 audit_info=audit_info, 

64 risk_assessment=risk_assessment, 

65 dare_id=dare_id, 

66 amount=amount, 

67 # from - StripeCustomerModel 

68 from_uid=customer.doc_id, 

69 from_customer_id=customer_settings.customer_id, 

70 from_currency_code=customer_settings.currency_code, 

71 # to - StripeAccountModel 

72 to_uid=account.doc_id, 

73 to_account_id=account_settings.account_id, 

74 to_currency_code=account_settings.currency_code, 

75 to_account_type=account_settings.account_type, 

76 to_fee_type=account.fee_type, 

77 ) 

78 

79 @classmethod 

80 def create_from_params( 

81 cls, 

82 audit_info: AuditInfoModel, 

83 risk_assessment: RiskAssessmentModel, 

84 dare_id: str, 

85 amount: int, 

86 from_uid: str, 

87 from_customer_id: str, 

88 from_currency_code: StripeCurrencyCode, 

89 to_uid: str, 

90 to_account_id: str, 

91 to_currency_code: StripeCurrencyCode, 

92 to_account_type: StripeAccountType, 

93 to_fee_type: AppFeeType, 

94 ) -> Self: 

95 

96 return cls( 

97 dare_id=dare_id, 

98 amount=amount, 

99 audit_info=audit_info, 

100 risk_assessment=risk_assessment, 

101 customer=CustomerInfoModel( 

102 uid=from_uid, 

103 customer_id=from_customer_id, 

104 currency_code=from_currency_code, 

105 ), 

106 account=AccountInfoModel( 

107 uid=to_uid, 

108 account_id=to_account_id, 

109 account_type=to_account_type, 

110 currency_code=to_currency_code, 

111 fee_type=to_fee_type, 

112 ), 

113 ) 

114 

115 def debug_str(self) -> str: 

116 msg = f""" 

117 AppPaymentContext: 

118 dare_id: {self.dare_id} 

119 audit_info: ip ={self.audit_info.ip_address} 

120 endpoint ={self.audit_info.endpoint} 

121 customer: uid ={self.customer.uid} 

122 customer_id ={self.customer.customer_id} 

123 currency ={self.customer.currency_code} 

124 account: uid ={self.account.uid} 

125 account_id ={self.account.account_id} 

126 account_type ={self.account.account_type} 

127 currency ={self.account.currency_code} 

128 fee_type ={self.account.fee_type} 

129 risk: overall_score ={self.risk_assessment.overall_score} 

130 risk_score ={self.risk_assessment.risk_score} 

131 risk_factors ={[f.value for f in self.risk_assessment.risk_factors]} 

132 payment: amount ={self.amount}, 

133 currency ={self.account.currency_code} 

134 """ 

135 return msg.strip()