Fix payment webhook

This commit is contained in:
Jet Li 2025-01-28 07:48:53 +00:00
parent f05e663a10
commit f96ec74176
4 changed files with 10 additions and 23 deletions

View File

@ -19,6 +19,7 @@ export DOCKER_BACKEND_HOME=$DOCKER_APP_HOME/$APP_NAME
export DOCKER_BACKEND_LOG_HOME=$DOCKER_BACKEND_HOME/log export DOCKER_BACKEND_LOG_HOME=$DOCKER_BACKEND_HOME/log
export MONGODB_URI=mongodb://localhost:27017/ export MONGODB_URI=mongodb://localhost:27017/
export FREELEAPS_ENV=local export FREELEAPS_ENV=local
export SITE_URL_ROOT=http://localhost/ export SITE_URL_ROOT=http://localhost:5173/
export LOG_BASE_PATH=${CODEBASE_ROOT}/log export LOG_BASE_PATH=${CODEBASE_ROOT}/log
export STRIPE_API_KEY=sk_test_51Ogsw5B0IyqaSJBrwczlr820jnmvA1qQQGoLZ2XxOsIzikpmXo4pRLjw4XVMTEBR8DdVTYySiAv1XX53Zv5xqynF00GfMqttFd export STRIPE_API_KEY=sk_test_51Ogsw5B0IyqaSJBrwczlr820jnmvA1qQQGoLZ2XxOsIzikpmXo4pRLjw4XVMTEBR8DdVTYySiAv1XX53Zv5xqynF00GfMqttFd
export STRIPE_WEBHOOK_SECRET=whsec_59191b39e20582f0a7eafa7370613ffdaf848fb94d6617493a806d9d2a00ed63

View File

@ -79,10 +79,10 @@ class PaymentHub:
return await self.stripe_manager.fetch_checkout_session_url(transaction_id) return await self.stripe_manager.fetch_checkout_session_url(transaction_id)
async def invoke_checkout_session_webhook( async def invoke_checkout_session_webhook(
self, payload: str, stripe_signature: str self, event: dict
) -> Tuple[bool, Optional[str], Optional[str]]: ) -> Tuple[bool, Optional[str], Optional[str]]:
return await self.stripe_manager.invoke_checkout_session_webhook( return await self.stripe_manager.invoke_checkout_session_webhook(
payload, stripe_signature event
) )
async def handle_account_update( async def handle_account_update(

View File

@ -322,30 +322,16 @@ class StripeManager:
return None return None
async def invoke_checkout_session_webhook( async def invoke_checkout_session_webhook(
self, payload: str, stripe_signature: str self, event: dict
) -> Tuple[bool, Optional[str], Optional[str]]: ) -> Tuple[bool, Optional[str], Optional[str]]:
try:
event = stripe.Webhook.construct_event(
payload, stripe_signature, app_settings.STRIPE_WEBHOOK_SECRET
)
except ValueError as e:
await self.module_logger.log_exception(exception=e, text="Invalid payload")
return False, None, None
except SignatureVerificationError as e:
await self.module_logger.log_exception(
exception=e, text="Invalid signature"
)
return False, None, None
# Handle the checkout.session.completed event # Handle the checkout.session.completed event
if event["type"] == "checkout.session.completed": if event["type"] == "checkout.session.completed":
session = event["data"]["object"] session = event["data"]["object"]
transaction = await self.__fetch_transaction_by_session_id(session.id) transaction = await self.__fetch_transaction_by_session_id(session["id"])
if not transaction: if not transaction:
await self.module_logger.log_error( await self.module_logger.log_error(
error="Transaction not found for session_id: {}".format(session.id), error="Transaction not found for session_id: {}".format(session["id"]),
properties={"session_id": session.id}, properties={"session_id": session["id"]},
) )
return False return False

View File

@ -184,8 +184,8 @@ async def fetch_checkout_session_url(transaction_id: str) -> Optional[str]:
summary="Invoke checkout session webhook", summary="Invoke checkout session webhook",
description="Invoke checkout session webhook", description="Invoke checkout session webhook",
) )
async def invoke_checkout_session_webhook(payload: str, stripe_signature: str): async def invoke_checkout_session_webhook(event: dict):
return await payment_hub.invoke_checkout_session_webhook(payload, stripe_signature) return await payment_hub.invoke_checkout_session_webhook(event)
@router.post( @router.post(