Coverage for functions \ flipdare \ payments \ stripe_webhook_response.py: 81%
36 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 typing import Final
16import flask
17from flipdare.generated.shared.stripe.stripe_onboard_code import StripeOnboardCode
18from flipdare.message.user_message import StripeWebhookMessage, UserMessage
19from string import Template
21__all__ = ["StripeWebhookResponse"]
24class StripeWebhookResponse:
25 """Logic for building Stripe Flask responses."""
27 # Keeping headers as a class constant is fine
28 HEADERS: Final[dict[str, str]] = {"Content-Type": "text/html"}
30 @staticmethod
31 def refresh_hook(url: str) -> flask.Response:
32 # Reference the Enum for the template string
33 msg = Template(StripeWebhookMessage.REDIRECT_TMPL.value).safe_substitute(URL=url)
34 return flask.Response(
35 msg,
36 status=200,
37 headers=StripeWebhookResponse._build_headers({"Location": url}),
38 )
40 @staticmethod
41 def return_hook(code: StripeOnboardCode | None = None) -> flask.Response:
42 if code is None:
43 code = StripeOnboardCode.OK
45 message = code.message
46 if code.show_support_msg:
47 message += f"\n{UserMessage.SUPPORT}"
49 content = Template(StripeWebhookMessage.RETURN_POST_TEMPLATE.value).safe_substitute(
50 TITLE=code.display_title,
51 MESSAGE=message,
52 CODE=code.value,
53 )
55 # note, we have to use 200, so the flutter WebviewClient processes correctly.
56 return flask.Response(
57 content,
58 headers=StripeWebhookResponse._build_headers(),
59 status=200,
60 )
62 @staticmethod
63 def payment_hook(
64 message: str,
65 payment_intent_id: str | None = None,
66 payment_method_id: str | None = None,
67 ) -> flask.Response:
68 if payment_intent_id is None:
69 payment_intent_id = "error"
70 if payment_method_id is None:
71 payment_method_id = "error"
73 content = Template(StripeWebhookMessage.PAY_POST_TEMPLATE.value).safe_substitute(
74 PAYMENT_INTENT_ID=payment_intent_id,
75 MESSAGE=message,
76 PAYMENT_METHOD_ID=payment_method_id,
77 )
79 # note, we have to use 200, so the flutter WebviewClient processes correctly.
80 return flask.Response(
81 content,
82 headers=StripeWebhookResponse._build_headers(),
83 status=200,
84 )
86 @staticmethod
87 def _build_headers(additional: dict[str, str] | None = None) -> dict[str, str]:
88 headers = StripeWebhookResponse.HEADERS.copy()
89 if additional:
90 headers.update(additional)
91 return headers