diff --git a/src/app/[locale]/(core)/payment/route.ts b/src/app/[locale]/(core)/payment/route.ts index f9b9c36..f93b3c0 100644 --- a/src/app/[locale]/(core)/payment/route.ts +++ b/src/app/[locale]/(core)/payment/route.ts @@ -7,6 +7,9 @@ export async function GET(req: NextRequest) { const productId = req.nextUrl.searchParams.get("productId"); const placementId = req.nextUrl.searchParams.get("placementId"); const paywallId = req.nextUrl.searchParams.get("paywallId"); + const fbPixels = req.nextUrl.searchParams.get("fb_pixels"); + const productPrice = req.nextUrl.searchParams.get("price"); + const currency = req.nextUrl.searchParams.get("currency"); const data = await createPaymentCheckout({ productId: productId || "", @@ -14,10 +17,13 @@ export async function GET(req: NextRequest) { paywallId: paywallId || "", }); - let redirectUrl: string | URL = data?.paymentUrl; + let redirectUrl: URL = new URL(data?.paymentUrl || "", req.nextUrl.origin); if (!redirectUrl) { redirectUrl = new URL(`${ROUTES.paymentFailed()}`, origin); } + if (fbPixels) redirectUrl.searchParams.set("fb_pixels", fbPixels); + if (productPrice) redirectUrl.searchParams.set("price", productPrice); + if (currency) redirectUrl.searchParams.set("currency", currency); return NextResponse.redirect(redirectUrl, { status: 307 }); } \ No newline at end of file diff --git a/src/app/[locale]/auth/callback/route.ts b/src/app/[locale]/auth/callback/route.ts index 0e0761f..3facaba 100644 --- a/src/app/[locale]/auth/callback/route.ts +++ b/src/app/[locale]/auth/callback/route.ts @@ -32,25 +32,61 @@ import { NextRequest, NextResponse } from "next/server"; import { ROUTES } from "@/shared/constants/client-routes"; +function extractTrackingCookiesFromUrl(url: URL): Record { + const trackingCookieKeys = [ + '_fbc', '_fbp', '_ym_uid', '_ym_d', '_ym_isad', '_ym_visorc', + 'yandexuid', 'ymex' + ]; + + const cookies: Record = {}; + + for (const [key, value] of url.searchParams.entries()) { + if ( + trackingCookieKeys.includes(key) || + key.startsWith('_ga') || + key.startsWith('_gid') + ) { + cookies[key] = value; + } + } + + return cookies; +} + export async function GET(req: NextRequest) { - const { searchParams } = req.nextUrl; - const token = searchParams.get("jwtToken"); - const productId = searchParams.get("productId"); - const placementId = searchParams.get("placementId"); - const paywallId = searchParams.get("paywallId"); + const { searchParams } = req.nextUrl; + const token = searchParams.get("jwtToken"); + const productId = searchParams.get("productId"); + const placementId = searchParams.get("placementId"); + const paywallId = searchParams.get("paywallId"); + const fbPixels = searchParams.get("fb_pixels"); + const productPrice = searchParams.get("price"); + const currency = searchParams.get("currency"); - const redirectUrl = new URL(`${ROUTES.payment()}`, process.env.NEXT_PUBLIC_APP_URL || ""); - if (productId) redirectUrl.searchParams.set("productId", productId); - if (placementId) redirectUrl.searchParams.set("placementId", placementId); - if (paywallId) redirectUrl.searchParams.set("paywallId", paywallId); + const redirectUrl = new URL(`${ROUTES.payment()}`, process.env.NEXT_PUBLIC_APP_URL || ""); + if (productId) redirectUrl.searchParams.set("productId", productId); + if (placementId) redirectUrl.searchParams.set("placementId", placementId); + if (paywallId) redirectUrl.searchParams.set("paywallId", paywallId); + if (fbPixels) redirectUrl.searchParams.set("fb_pixels", fbPixels); + if (productPrice) redirectUrl.searchParams.set("price", productPrice); + if (currency) redirectUrl.searchParams.set("currency", currency); - // Ставим куку через заголовок Set-Cookie, тк в HTML-ответе NextResponse.cookies не работает - const cookie = [ - `accessToken=${encodeURIComponent(token || "")}`, - `HttpOnly; Secure; Path=/; SameSite=Lax; Max-Age=${60 * 60 * 24 * 365}`, - ].join("; "); + const trackingCookies = extractTrackingCookiesFromUrl(req.nextUrl); - const html = ` + const cookies: string[] = []; + + cookies.push( + `accessToken=${encodeURIComponent(token || "")}; HttpOnly; Secure; Path=/; SameSite=Lax; Max-Age=${60 * 60 * 24 * 365}` + ); + + // Ставим куку через заголовок Set-Cookie, тк в HTML-ответе NextResponse.cookies не работает + Object.entries(trackingCookies).forEach(([key, value]) => { + cookies.push( + `${key}=${encodeURIComponent(value)}; HttpOnly=false; Secure=false; Path=/; SameSite=Lax; Max-Age=${60 * 60 * 24 * 365}` + ); + }); + + const html = ` @@ -65,11 +101,11 @@ export async function GET(req: NextRequest) { `; - return new NextResponse(html, { - status: 200, - headers: { - "content-type": "text/html; charset=utf-8", - "set-cookie": cookie, - }, - }); + return new NextResponse(html, { + status: 200, + headers: { + "content-type": "text/html; charset=utf-8", + "set-cookie": cookies.join(", "), + }, + }); }