feat(MED-97): update cart flow for using benefits

This commit is contained in:
2025-09-26 13:24:09 +03:00
parent 56f84a003c
commit db38e602aa
15 changed files with 419 additions and 81 deletions

View File

@@ -2,7 +2,7 @@
import { loadCurrentUserAccount } from '@/app/home/(user)/_lib/server/load-user-account';
import { MontonioOrderHandlerService } from '@/packages/billing/montonio/src';
import { addToCart, deleteLineItem, retrieveCart } from '@lib/data/cart';
import { addToCart, deleteLineItem } from '@lib/data/cart';
import { getCartId } from '@lib/data/cookies';
import { StoreCartLineItem, StoreProductVariant } from '@medusajs/types';
import { z } from 'zod';
@@ -87,9 +87,15 @@ export async function handleDeleteCartItem({ lineId }: { lineId: string }) {
export async function handleNavigateToPayment({
language,
paymentSessionId,
amount,
currencyCode,
cartId,
}: {
language: string;
paymentSessionId: string;
amount: number;
currencyCode: string;
cartId: string;
}) {
const supabase = getSupabaseServerClient();
const { account, user } = await loadCurrentUserAccount();
@@ -97,26 +103,21 @@ export async function handleNavigateToPayment({
throw new Error('Account not found');
}
const cart = await retrieveCart();
if (!cart) {
throw new Error('No cart found');
}
const paymentLink =
await new MontonioOrderHandlerService().getMontonioPaymentLink({
notificationUrl: `${env().medusaBackendPublicUrl}/hooks/payment/montonio_montonio`,
returnUrl: `${env().siteUrl}/home/cart/montonio-callback`,
amount: cart.total,
currency: cart.currency_code.toUpperCase(),
amount,
currency: currencyCode.toUpperCase(),
description: `Order from Medreport`,
locale: language,
merchantReference: `${account.id}:${paymentSessionId}:${cart.id}`,
merchantReference: `${account.id}:${paymentSessionId}:${cartId}`,
});
const { error } = await supabase.schema('audit').from('cart_entries').insert({
operation: 'NAVIGATE_TO_PAYMENT',
account_id: account.id,
cart_id: cart.id,
cart_id: cartId,
changed_by: user.id,
});
if (error) {