Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.orm import selectinload

from app.core.payment import models_payment, schemas_payment
from app.core.checkout import models_checkout, schemas_checkout


async def create_checkout(
db: AsyncSession,
checkout: models_payment.Checkout,
) -> models_payment.Checkout:
checkout: models_checkout.Checkout,
) -> models_checkout.Checkout:
db.add(checkout)

return checkout
Expand All @@ -21,22 +21,22 @@ async def get_checkouts(
module: str,
db: AsyncSession,
last_checked: datetime | None = None,
) -> list[schemas_payment.CheckoutComplete]:
) -> list[schemas_checkout.CheckoutComplete]:
result = await db.execute(
select(models_payment.Checkout)
.options(selectinload(models_payment.Checkout.payments))
select(models_checkout.Checkout)
.options(selectinload(models_checkout.Checkout.payments))
.where(
models_payment.Checkout.module == module,
models_checkout.Checkout.module == module,
),
)
return [
schemas_payment.CheckoutComplete(
schemas_checkout.CheckoutComplete(
id=checkout.id,
module=checkout.module,
name=checkout.name,
amount=checkout.amount,
payments=[
schemas_payment.CheckoutPayment(
schemas_checkout.CheckoutPayment(
id=payment.id,
checkout_id=payment.checkout_id,
paid_amount=payment.paid_amount,
Expand All @@ -51,33 +51,33 @@ async def get_checkouts(
async def get_checkout_by_id(
checkout_id: uuid.UUID,
db: AsyncSession,
) -> models_payment.Checkout | None:
) -> models_checkout.Checkout | None:
result = await db.execute(
select(models_payment.Checkout)
select(models_checkout.Checkout)
.where(
models_payment.Checkout.id == checkout_id,
models_checkout.Checkout.id == checkout_id,
)
.options(selectinload(models_payment.Checkout.payments)),
.options(selectinload(models_checkout.Checkout.payments)),
)
return result.scalars().first()


async def get_checkout_by_hello_asso_checkout_id(
hello_asso_checkout_id: int,
db: AsyncSession,
) -> models_payment.Checkout | None:
) -> models_checkout.Checkout | None:
result = await db.execute(
select(models_payment.Checkout).where(
models_payment.Checkout.hello_asso_checkout_id == hello_asso_checkout_id,
select(models_checkout.Checkout).where(
models_checkout.Checkout.hello_asso_checkout_id == hello_asso_checkout_id,
),
)
return result.scalars().first()


async def create_checkout_payment(
db: AsyncSession,
checkout_payment: models_payment.CheckoutPayment,
) -> models_payment.CheckoutPayment:
checkout_payment: models_checkout.CheckoutPayment,
) -> models_checkout.CheckoutPayment:
db.add(checkout_payment)
await db.flush()
return checkout_payment
Expand All @@ -86,10 +86,10 @@ async def create_checkout_payment(
async def get_checkout_payment_by_hello_asso_payment_id(
hello_asso_payment_id: int,
db: AsyncSession,
) -> models_payment.CheckoutPayment | None:
) -> models_checkout.CheckoutPayment | None:
result = await db.execute(
select(models_payment.CheckoutPayment).where(
models_payment.CheckoutPayment.hello_asso_payment_id
select(models_checkout.CheckoutPayment).where(
models_checkout.CheckoutPayment.hello_asso_payment_id
== hello_asso_payment_id,
),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
from pydantic import TypeAdapter, ValidationError
from sqlalchemy.ext.asyncio import AsyncSession

from app.core.payment import cruds_payment, models_payment, schemas_payment
from app.core.payment.types_payment import (
from app.core.checkout import cruds_checkout, models_checkout, schemas_checkout
from app.core.checkout.types_checkout import (
NotificationResultContent,
)
from app.dependencies import get_db
Expand Down Expand Up @@ -48,7 +48,7 @@ async def webhook(
)
if content.metadata:
checkout_metadata = (
schemas_payment.HelloAssoCheckoutMetadata.model_validate(
schemas_checkout.HelloAssoCheckoutMetadata.model_validate(
content.metadata,
)
)
Expand All @@ -74,7 +74,7 @@ async def webhook(
# We may receive the webhook multiple times, we only want to save a CheckoutPayment
# in the database the first time
existing_checkout_payment_model = (
await cruds_payment.get_checkout_payment_by_hello_asso_payment_id(
await cruds_checkout.get_checkout_payment_by_hello_asso_payment_id(
hello_asso_payment_id=content.data.id,
db=db,
)
Expand All @@ -92,7 +92,7 @@ async def webhook(
)
return

checkout = await cruds_payment.get_checkout_by_id(
checkout = await cruds_checkout.get_checkout_by_id(
checkout_id=uuid.UUID(checkout_metadata.hyperion_checkout_id),
db=db,
)
Expand All @@ -116,14 +116,14 @@ async def webhook(
detail="Secret mismatch",
)

checkout_payment_model = models_payment.CheckoutPayment(
checkout_payment_model = models_checkout.CheckoutPayment(
id=uuid.uuid4(),
checkout_id=checkout.id,
paid_amount=content.data.amount,
tip_amount=content.data.amountTip,
hello_asso_payment_id=content.data.id,
)
await cruds_payment.create_checkout_payment(
await cruds_checkout.create_checkout_payment(
checkout_payment=checkout_payment_model,
db=db,
)
Expand All @@ -136,20 +136,28 @@ async def webhook(
try:
for module in all_modules:
if module.root == checkout.module:
if module.payment_callback is not None:
if module.checkout_callback is None:
hyperion_error_logger.info(
f"Payment: calling module {checkout.module} payment callback",
)
checkout_payment_schema = (
schemas_payment.CheckoutPayment.model_validate(
checkout_payment_model.__dict__,
)
)
await module.payment_callback(checkout_payment_schema, db)
hyperion_error_logger.info(
f"Payment: call to module {checkout.module} payment callback for checkout (hyperion_checkout_id: {checkout_metadata.hyperion_checkout_id}, HelloAsso checkout_id: {checkout.id}) succeeded",
)
return
hyperion_error_logger.info(
f"Payment: calling module {checkout.module} payment callback",
)
checkout_payment_schema = schemas_checkout.CheckoutPayment(
id=checkout_payment_model.id,
paid_amount=checkout_payment_model.paid_amount,
checkout_id=checkout_payment_model.checkout_id,
)
await module.checkout_callback(checkout_payment_schema, db)
hyperion_error_logger.info(
f"Payment: call to module {checkout.module} payment callback for checkout (hyperion_checkout_id: {checkout_metadata.hyperion_checkout_id}, HelloAsso checkout_id: {checkout.id}) succeeded",
)
return

hyperion_error_logger.info(
f"Payment: callback for checkout (hyperion_checkout_id: {checkout_metadata.hyperion_checkout_id}, HelloAsso checkout_id: {checkout.id}) was not called for module {checkout.module}",
)
except Exception:
hyperion_error_logger.exception(
f"Payment: call to module {checkout.module} payment callback for checkout (hyperion_checkout_id: {checkout_metadata.hyperion_checkout_id}, HelloAsso checkout_id: {checkout.id}) failed",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
)
from sqlalchemy.ext.asyncio import AsyncSession

from app.core.payment import cruds_payment, models_payment, schemas_payment
from app.core.payment.types_payment import HelloAssoConfig
from app.core.checkout import cruds_checkout, models_checkout, schemas_checkout
from app.core.checkout.types_checkout import HelloAssoConfig
from app.core.users import schemas_users
from app.core.utils import security
from app.types.exceptions import (
Expand Down Expand Up @@ -132,7 +132,7 @@ async def init_checkout(
db: AsyncSession,
payer_user: schemas_users.CoreUser | None = None,
redirection_uri: str | None = None,
) -> schemas_payment.Checkout:
) -> schemas_checkout.Checkout:
"""
Init an HelloAsso checkout

Expand Down Expand Up @@ -182,7 +182,7 @@ async def init_checkout(
return_url=redirection_uri,
contains_donation=False,
payer=payer,
metadata=schemas_payment.HelloAssoCheckoutMetadata(
metadata=schemas_checkout.HelloAssoCheckoutMetadata(
secret=secret,
hyperion_checkout_id=str(checkout_model_id),
).model_dump(),
Expand All @@ -196,7 +196,7 @@ async def init_checkout(
self._helloasso_slug,
init_checkout_body,
)
except (UnauthorizedException, BadRequestException):
except UnauthorizedException, BadRequestException:
# We know that HelloAsso may refuse some payer infos, like using the firstname "test"
# Even when prefilling the payer infos,the user will be able to edit them on the payment page,
# so we can safely retry without the payer infos
Expand All @@ -223,7 +223,7 @@ async def init_checkout(
)

if response and response.id:
checkout_model = models_payment.Checkout(
checkout_model = models_checkout.Checkout(
id=checkout_model_id,
module=module,
name=checkout_name,
Expand All @@ -232,11 +232,11 @@ async def init_checkout(
secret=secret,
)

await cruds_payment.create_checkout(db=db, checkout=checkout_model)
await cruds_checkout.create_checkout(db=db, checkout=checkout_model)

return schemas_payment.Checkout(
return schemas_checkout.Checkout(
id=checkout_model_id,
payment_url=response.redirect_url,
payment_url=response.redirect_url or "",
)
hyperion_error_logger.error(
f"Payment: failed to init a checkout with HA for module {module} and name {checkout_name}. No checkout id returned",
Expand All @@ -257,8 +257,8 @@ async def get_checkout(
self,
checkout_id: uuid.UUID,
db: AsyncSession,
) -> schemas_payment.CheckoutComplete | None:
checkout_model = await cruds_payment.get_checkout_by_id(
) -> schemas_checkout.CheckoutComplete | None:
checkout_model = await cruds_checkout.get_checkout_by_id(
checkout_id=checkout_id,
db=db,
)
Expand All @@ -267,10 +267,10 @@ async def get_checkout(

checkout_dict = checkout_model.__dict__
checkout_dict["payments"] = [
schemas_payment.CheckoutPayment(**payment.__dict__)
schemas_checkout.CheckoutPayment(**payment.__dict__)
for payment in checkout_dict["payments"]
]
return schemas_payment.CheckoutComplete(**checkout_dict)
return schemas_checkout.CheckoutComplete(**checkout_dict)

async def refund_payment(
self,
Expand Down
Loading
Loading