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 MONGODB_NAME=freeleaps2
export MONGODB_PORT=27017
GIT_REPO_ROOT=/Users/dongli/workspace/freeleaps-service-hub
CODEBASE_ROOT=/Users/dongli/workspace/freeleaps-service-hub/apps/payment
SITE_DEPLOY_FOLDER=/Users/dongli/workspace/freeleaps-service-hub/sites/payment/deploy
GIT_REPO_ROOT=/mnt/freeleaps/freeleaps-service-hub
CODEBASE_ROOT=/mnt/freeleaps/freeleaps-service-hub/apps/payment
SITE_DEPLOY_FOLDER=/mnt/freeleaps/freeleaps-service-hub/sites/payment/deploy
#!/bin/bash
export VENV_DIR=venv_t
export VENV_ACTIVATE=venv_t/bin/activate

View File

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

View File

@ -3,13 +3,11 @@
# from .user_profile import profile_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.infra.payment.models import StripeTransactionDoc
backend_models = [
IncomeProfileDoc,
ProjectDoc
]
backend_models = [IncomeProfileDoc, PaymentProfileDoc, ProjectDoc, StripeTransactionDoc]
# backend_models.extend(code_models)
# backend_models.extend(user_models)
# backend_models.extend(profile_models)

View File

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