feat(MED-100): update montonio redirect

This commit is contained in:
2025-07-24 08:02:13 +03:00
parent 8633ac4bce
commit e59ad6af00
11 changed files with 261 additions and 166 deletions

View File

@@ -1,13 +1,34 @@
'use server';
import { z } from 'zod';
import { loadCurrentUserAccount } from '@/app/home/(user)/_lib/server/load-user-account';
import { getSupabaseServerClient } from '@kit/supabase/server-client';
import { addToCart, deleteLineItem, retrieveCart } from '@lib/data/cart';
import { StoreCartLineItem, StoreProductVariant } from '@medusajs/types';
import { MontonioOrderHandlerService } from '@/packages/billing/montonio/src';
import { headers } from 'next/headers';
import { requireUserInServerComponent } from '../server/require-user-in-server-component';
const medusaBackendUrl = process.env.MEDUSA_BACKEND_URL!;
const siteUrl = process.env.NEXT_PUBLIC_SITE_URL!;
const env = z
.object({
medusaBackendUrl: z
.string({
required_error: 'MEDUSA_BACKEND_URL is required',
})
.min(1),
siteUrl: z
.string({
required_error: 'NEXT_PUBLIC_SITE_URL is required',
})
.min(1),
})
.parse({
medusaBackendUrl,
siteUrl,
});
export async function handleAddToCart({
selectedVariant,
countryCode,
@@ -46,7 +67,7 @@ export async function handleAddToCart({
return cart;
}
export async function handleNavigateToPayment({ language }: { language: string }) {
export async function handleNavigateToPayment({ language, paymentSessionId }: { language: string, paymentSessionId: string }) {
const supabase = getSupabaseServerClient();
const user = await requireUserInServerComponent();
const account = await loadCurrentUserAccount()
@@ -59,21 +80,14 @@ export async function handleNavigateToPayment({ language }: { language: string }
throw new Error("No cart found");
}
const headersList = await headers();
const host = "webhook.site:3000";
const proto = "http";
// const host = headersList.get('host');
// const proto = headersList.get('x-forwarded-proto') ?? 'http';
const publicUrl = `${proto}://${host}`;
const paymentLink = await new MontonioOrderHandlerService().getMontonioPaymentLink({
notificationUrl: `${publicUrl}/api/billing/webhook`,
returnUrl: `${publicUrl}/home/cart/montonio-callback`,
notificationUrl: `${env.medusaBackendUrl}/api/billing/webhook`,
returnUrl: `${env.siteUrl}/home/cart/montonio-callback`,
amount: cart.total,
currency: cart.currency_code.toUpperCase(),
description: `Order from Medreport`,
locale: language,
merchantReference: `${account.id}:${cart.id}:${Date.now()}`,
merchantReference: `${account.id}:${paymentSessionId}:${cart.id}`,
});
const { error } = await supabase
@@ -104,12 +118,6 @@ export async function handleLineItemTimeout({
throw new Error('Account not found');
}
if (lineItem.updated_at) {
const updatedAt = new Date(lineItem.updated_at);
const now = new Date();
const diff = now.getTime() - updatedAt.getTime();
}
await deleteLineItem(lineItem.id);
const { error } = await supabase