Fix payment micro-service issues

This commit is contained in:
jetli 2025-01-16 02:54:04 +00:00
parent 96e12c1755
commit d0c0e0ca28
4 changed files with 61 additions and 76 deletions

View File

@ -7,9 +7,9 @@ export BACKEND_LOG_FILE_NAME=$APP_NAME
export APPLICATION_ACTIVITY_LOG=$APP_NAME-activity export APPLICATION_ACTIVITY_LOG=$APP_NAME-activity
export MONGODB_NAME=freeleaps2 export MONGODB_NAME=freeleaps2
export MONGODB_PORT=27017 export MONGODB_PORT=27017
GIT_REPO_ROOT=/Users/dongli/workspace/freeleaps-service-hub GIT_REPO_ROOT=/mnt/freeleaps/freeleaps-service-hub
CODEBASE_ROOT=/Users/dongli/workspace/freeleaps-service-hub/apps/payment CODEBASE_ROOT=/mnt/freeleaps/freeleaps-service-hub/apps/payment
SITE_DEPLOY_FOLDER=/Users/dongli/workspace/freeleaps-service-hub/sites/payment/deploy SITE_DEPLOY_FOLDER=/mnt/freeleaps/freeleaps-service-hub/sites/payment/deploy
#!/bin/bash #!/bin/bash
export VENV_DIR=venv_t export VENV_DIR=venv_t
export VENV_ACTIVATE=venv_t/bin/activate export VENV_ACTIVATE=venv_t/bin/activate

View File

@ -66,10 +66,10 @@ class StripeManager:
if len(transactions) > 1: if len(transactions) > 1:
await self.module_logger.log_error( await self.module_logger.log_error(
error="More than one transaction found for session_id: {}".format(session_id), error="More than one transaction found for session_id: {}".format(
properties={ session_id
"session_id": session_id ),
} properties={"session_id": session_id},
) )
elif len(transactions) == 0: elif len(transactions) == 0:
return None return None
@ -83,12 +83,12 @@ class StripeManager:
StripeTransactionDoc.stripe_checkout_session_id == session_id StripeTransactionDoc.stripe_checkout_session_id == session_id
).to_list() ).to_list()
if len(transaction) > 0: if len(transaction) > 1:
await self.module_logger.log_error( await self.module_logger.log_error(
error="More than one transaction found for session_id: {}".format(session_id), error="More than one transaction found for session_id: {}".format(
properties={ session_id
"session_id": session_id ),
} properties={"session_id": session_id},
) )
elif len(transaction) == 0: elif len(transaction) == 0:
return None return None
@ -99,17 +99,18 @@ class StripeManager:
self, project_id: str, milestone_index: int self, project_id: str, milestone_index: int
) -> Optional[Dict[str, any]]: ) -> Optional[Dict[str, any]]:
transaction = await StripeTransactionDoc.find( transaction = await StripeTransactionDoc.find(
project_id=project_id, milestone_index=milestone_index StripeTransactionDoc.project_id == project_id,
StripeTransactionDoc.milestone_index == milestone_index,
).to_list() ).to_list()
if len(transaction) > 1:
if len(transaction) > 0:
await self.module_logger.log_error( await self.module_logger.log_error(
error="More than one transaction found for project_id: {} and milestone_index: {}".format( error="More than one transaction found for project_id: {} and milestone_index: {}".format(
project_id, milestone_index), project_id, milestone_index
),
properties={ properties={
"project_id": project_id, "project_id": project_id,
"milestone_index": milestone_index "milestone_index": milestone_index,
} },
) )
elif len(transaction) == 0: elif len(transaction) == 0:
@ -154,10 +155,11 @@ class StripeManager:
), ),
properties={ properties={
"project_id": project_id, "project_id": project_id,
"milestone_index": milestone_index "milestone_index": milestone_index,
} },
) )
return transactions[0].id res = transactions[0].id
return res
async def create_payment_link(self, transaction_id: str) -> Optional[str]: async def create_payment_link(self, transaction_id: str) -> Optional[str]:
transaction = await StripeTransactionDoc.get(transaction_id) transaction = await StripeTransactionDoc.get(transaction_id)
@ -259,7 +261,9 @@ class StripeManager:
}, },
}, },
mode="payment", mode="payment",
success_url="{}/work-space".format(self.site_url_root), success_url="{}/work-space".format(
self.site_url_root
), # needs to be set, local: http://localhost/
cancel_url="{}/work-space".format(self.site_url_root), cancel_url="{}/work-space".format(self.site_url_root),
) )
@ -302,15 +306,13 @@ class StripeManager:
payload, stripe_signature, settings.STRIPE_WEBHOOK_SECRET payload, stripe_signature, settings.STRIPE_WEBHOOK_SECRET
) )
except ValueError as e: except ValueError as e:
await self.module_logger.log_exception( await self.module_logger.log_exception(exception=e, text="Invalid payload")
exception=e,
text="Invalid payload")
return False, None, None return False, None, None
except stripe.error.SignatureVerificationError as e: except stripe.error.SignatureVerificationError as e:
await self.module_logger.log_exception( await self.module_logger.log_exception(
exception=e, exception=e, text="Invalid signature"
text="Invalid signature") )
return False, None, None return False, None, None
# Handle the checkout.session.completed event # Handle the checkout.session.completed event
@ -320,9 +322,7 @@ class StripeManager:
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={ properties={"session_id": session.id},
"session_id": session.id
}
) )
return False return False

View File

@ -3,13 +3,11 @@
# from .user_profile import profile_models # from .user_profile import profile_models
# #
# TODO: Add all models to backend_models # TODO: Add all models to backend_models
from backend.services.payment.models import IncomeProfileDoc from backend.services.payment.models import IncomeProfileDoc, PaymentProfileDoc
from backend.services.project.models import ProjectDoc from backend.services.project.models import ProjectDoc
from backend.infra.payment.models import StripeTransactionDoc
backend_models = [ backend_models = [IncomeProfileDoc, PaymentProfileDoc, ProjectDoc, StripeTransactionDoc]
IncomeProfileDoc,
ProjectDoc
]
# backend_models.extend(code_models) # backend_models.extend(code_models)
# backend_models.extend(user_models) # backend_models.extend(user_models)
# backend_models.extend(profile_models) # backend_models.extend(profile_models)

View File

@ -2,12 +2,13 @@ from fastapi import APIRouter
from backend.application.payment_hub import PaymentHub from backend.application.payment_hub import PaymentHub
from typing import Dict, Optional, Tuple from typing import Dict, Optional, Tuple
from decimal import Decimal from decimal import Decimal
from fastapi.responses import JSONResponse
from backend.services.project.models import ProjectDoc from fastapi.encoders import jsonable_encoder
router = APIRouter() router = APIRouter()
payment_hub = PaymentHub() payment_hub = PaymentHub()
# Web API # Web API
# Create stripe account # Create stripe account
@router.get( @router.get(
@ -19,6 +20,7 @@ payment_hub = PaymentHub()
async def create_stripe_account(): async def create_stripe_account():
return await payment_hub.create_stripe_account() return await payment_hub.create_stripe_account()
# Web API # Web API
# Create account link # Create account link
@router.get( @router.get(
@ -27,11 +29,10 @@ async def create_stripe_account():
summary="Create account link", summary="Create account link",
description="Create account link", description="Create account link",
) )
async def create_account_link( async def create_account_link(account_id: str):
account_id: str
):
return await payment_hub.create_account_link(account_id) return await payment_hub.create_account_link(account_id)
# Web API # Web API
# Can account receive payments # Can account receive payments
@router.get( @router.get(
@ -40,9 +41,7 @@ async def create_account_link(
summary="Can account receive payments", summary="Can account receive payments",
description="Can account receive payments", description="Can account receive payments",
) )
async def can_account_receive_payments( async def can_account_receive_payments(account_id: str):
account_id: str
):
return await payment_hub.can_account_receive_payments(account_id) return await payment_hub.can_account_receive_payments(account_id)
@ -54,11 +53,10 @@ async def can_account_receive_payments(
summary="Fetch transaction by id", summary="Fetch transaction by id",
description="Fetch transaction by id", description="Fetch transaction by id",
) )
async def fetch_transaction_by_id( async def fetch_transaction_by_id(transaction_id: str):
transaction_id: str
):
return await payment_hub.fetch_transaction_by_id(transaction_id) return await payment_hub.fetch_transaction_by_id(transaction_id)
# Web API # Web API
# Fetch transaction by session id # Fetch transaction by session id
@router.get( @router.get(
@ -67,11 +65,10 @@ async def fetch_transaction_by_id(
summary="Fetch transaction by session id", summary="Fetch transaction by session id",
description="Fetch transaction by session id", description="Fetch transaction by session id",
) )
async def fetch_transaction_by_session_id( async def fetch_transaction_by_session_id(session_id: str):
session_id: str
):
return await payment_hub.fetch_transaction_by_session_id(session_id) return await payment_hub.fetch_transaction_by_session_id(session_id)
# Web API # Web API
# Fetch stripe transaction for milestone # Fetch stripe transaction for milestone
@router.get( @router.get(
@ -80,14 +77,12 @@ async def fetch_transaction_by_session_id(
summary="Fetch stripe transaction for milestone", summary="Fetch stripe transaction for milestone",
description="Fetch stripe transaction for milestone", description="Fetch stripe transaction for milestone",
) )
async def fetch_stripe_transaction_for_milestone( async def fetch_stripe_transaction_for_milestone(project_id: str, milestone_index: int):
project_id: str,
milestone_index: int
):
return await payment_hub.fetch_stripe_transaction_for_milestone( return await payment_hub.fetch_stripe_transaction_for_milestone(
project_id, milestone_index project_id, milestone_index
) )
# Web API # Web API
# Create stripe transaction for milestone # Create stripe transaction for milestone
@router.post( @router.post(
@ -105,7 +100,7 @@ async def create_stripe_transaction_for_milestone(
to_user: str, to_user: str,
to_stripe_account_id: str, to_stripe_account_id: str,
): ):
return await payment_hub.create_stripe_transaction_for_milestone( res = await payment_hub.create_stripe_transaction_for_milestone(
project_id, project_id,
milestone_index, milestone_index,
currency, currency,
@ -114,6 +109,8 @@ async def create_stripe_transaction_for_milestone(
to_user, to_user,
to_stripe_account_id, to_stripe_account_id,
) )
return JSONResponse(content=jsonable_encoder(str(res)))
# Web API # Web API
# Create payment link # Create payment link
@ -123,24 +120,24 @@ async def create_stripe_transaction_for_milestone(
summary="Create payment link", summary="Create payment link",
description="Create payment link", description="Create payment link",
) )
async def create_payment_link( async def create_payment_link(transaction_id: str) -> Optional[str]:
transaction_id: str
) -> Optional[str]:
return await payment_hub.create_payment_link(transaction_id) return await payment_hub.create_payment_link(transaction_id)
# Web API # Web API
# Create checkout session # Create checkout session
@router.get( @router.post(
"/create_checkout_session/{transaction_id}", "/create_checkout_session/{transaction_id}",
operation_id="create_checkout_session", operation_id="create_checkout_session",
summary="Create checkout session", summary="Create checkout session",
description="Create checkout session", description="Create checkout session",
) )
async def create_checkout_session( async def create_checkout_session(
transaction_id: str transaction_id: str,
) -> Tuple[Optional[str], Optional[str]]: ) -> Tuple[Optional[str], Optional[str]]:
return await payment_hub.create_checkout_session(transaction_id) return await payment_hub.create_checkout_session(transaction_id)
# Web API # Web API
# Fetch payment link # Fetch payment link
@router.get( @router.get(
@ -149,9 +146,7 @@ async def create_checkout_session(
summary="Fetch payment link", summary="Fetch payment link",
description="Fetch payment link", description="Fetch payment link",
) )
async def fetch_payment_link( async def fetch_payment_link(transaction_id: str) -> Optional[str]:
transaction_id: str
) -> Optional[str]:
return await payment_hub.fetch_payment_link(transaction_id) return await payment_hub.fetch_payment_link(transaction_id)
@ -163,11 +158,10 @@ async def fetch_payment_link(
summary="Fetch checkout session", summary="Fetch checkout session",
description="Fetch checkout session", description="Fetch checkout session",
) )
async def fetch_checkout_session_id( async def fetch_checkout_session_id(transaction_id: str) -> Optional[str]:
transaction_id: str
) -> Optional[str]:
return await payment_hub.fetch_checkout_session_id(transaction_id) return await payment_hub.fetch_checkout_session_id(transaction_id)
# Web API # Web API
# Fetch checkout session url # Fetch checkout session url
@router.get( @router.get(
@ -176,11 +170,10 @@ async def fetch_checkout_session_id(
summary="Fetch checkout session url", summary="Fetch checkout session url",
description="Fetch checkout session url", description="Fetch checkout session url",
) )
async def fetch_checkout_session_url( async def fetch_checkout_session_url(transaction_id: str) -> Optional[str]:
transaction_id: str
) -> Optional[str]:
return await payment_hub.fetch_checkout_session_url(transaction_id) return await payment_hub.fetch_checkout_session_url(transaction_id)
# Web API # Web API
# Invoke checkout session webhook # Invoke checkout session webhook
@router.post( @router.post(
@ -189,11 +182,5 @@ async def fetch_checkout_session_url(
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( async def invoke_checkout_session_webhook(payload: str, stripe_signature: str):
payload: str, return await payment_hub.invoke_checkout_session_webhook(payload, stripe_signature)
stripe_signature: str
):
return await payment_hub.invoke_checkout_session_webhook(
payload,
stripe_signature
)