Coverage for functions \ flipdare \ payments \ app_stripe_config.py: 100%
48 statements
« prev ^ index » next coverage.py v7.13.0, created at 2026-05-08 12:22 +1000
« 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#
13from __future__ import annotations
15from dataclasses import dataclass
16from urllib.parse import urlencode
17from stripe import RequestOptions
18from flipdare.constants import STRIPE_CLIENT_NETWORK_RETRIES, STRIPE_VERSION
19from flipdare.generated.schema.payment.stripe_webhook_request_schema import (
20 StripeWebhookRequestSchema,
21)
22from flipdare.payments.payment_types import StripeLinkInfo
24__all__ = ["AppStripeConfig", "AppStripeWebhookConfig"]
27@dataclass(frozen=True, kw_only=True)
28class AppStripeConfig:
29 webhook_key: str
30 refresh_webhook_url: str
31 return_webhook_url: str
32 pay_webhook_url: str
33 secret_key: str
34 platform_account_id: str
35 stripe_version: str = STRIPE_VERSION
36 max_retries: int = STRIPE_CLIENT_NETWORK_RETRIES
38 @classmethod
39 def create(
40 cls,
41 webhook_key: str,
42 refresh_webhook_url: str | None = None,
43 return_webhook_url: str | None = None,
44 pay_webhook_url: str | None = None,
45 secret_key: str | None = None,
46 platform_account_id: str | None = None,
47 max_retries: int = STRIPE_CLIENT_NETWORK_RETRIES,
48 stripe_version: str = STRIPE_VERSION,
49 ) -> AppStripeConfig:
50 from flipdare.app_config import get_app_config
52 cfg = get_app_config()
54 return cls(
55 webhook_key=webhook_key,
56 secret_key=secret_key or cfg.stripe_secret_key,
57 return_webhook_url=return_webhook_url or cfg.stripe_return_webhook_url,
58 pay_webhook_url=pay_webhook_url or cfg.stripe_pay_webhook_url,
59 platform_account_id=platform_account_id or cfg.stripe_platform_account_id,
60 refresh_webhook_url=refresh_webhook_url or cfg.stripe_refresh_webhook_url,
61 max_retries=max_retries,
62 stripe_version=stripe_version,
63 )
65 @classmethod
66 def default(cls, webhook_key: str) -> AppStripeConfig:
67 """Generates config using only the app defaults."""
68 return cls.create(webhook_key=webhook_key)
70 def request_options(self, account_id: str | None = None) -> RequestOptions:
71 max_retries = self.max_retries
72 stripe_version = self.stripe_version
74 return RequestOptions(
75 max_network_retries=max_retries,
76 stripe_version=stripe_version,
77 stripe_account=account_id,
78 )
80 @property
81 def webhook_config(self) -> AppStripeWebhookConfig:
82 return AppStripeWebhookConfig(
83 webhook_key=self.webhook_key,
84 _refresh_url=self.refresh_webhook_url,
85 _return_url=self.return_webhook_url,
86 )
89@dataclass(frozen=True, kw_only=True)
90class AppStripeWebhookConfig:
91 webhook_key: str
92 _refresh_url: str
93 _return_url: str
95 @staticmethod
96 def _encode_params(
97 base_url: str,
98 params: StripeWebhookRequestSchema,
99 ) -> str:
100 encoded = urlencode(params)
101 return f"{base_url}?{encoded}"
103 def webhook_params(
104 self,
105 info: StripeLinkInfo,
106 ) -> StripeWebhookRequestSchema:
107 return {
108 "uid": info.uid,
109 "stripe_account_id": info.stripe_account_id,
110 "webhook_key": self.webhook_key,
111 "stripe_account_type": info.account_type,
112 }
114 def refresh_url(self, info: StripeLinkInfo) -> str:
115 return self._encode_params(self._refresh_url, self.webhook_params(info))
117 def return_url(self, info: StripeLinkInfo) -> str:
118 return self._encode_params(self._return_url, self.webhook_params(info))