@@ -11,15 +11,23 @@ import {
1111import { BaseParams , buildRedirectUrl } from '@fxa/payments/ui' ;
1212import { config } from 'apps/payments/next/config' ;
1313import { getIpAddress } from '@fxa/payments/ui/server' ;
14- import { getSP2Params } from '@fxa/payments/legacy' ;
14+ import {
15+ buildSp2RedirectUrl ,
16+ getSP2Params ,
17+ redirectToSp2 ,
18+ } from '@fxa/payments/legacy' ;
19+ import crypto from 'crypto' ;
20+ import * as Sentry from '@sentry/nextjs' ;
1521
1622export const dynamic = 'force-dynamic' ; // defaults to auto
1723
1824function reportError ( message : string , details ?: any ) {
1925 if ( details ) {
2026 console . error ( message , details ) ;
27+ Sentry . captureMessage ( message , details ) ;
2128 } else {
2229 console . error ( message ) ;
30+ Sentry . captureMessage ( message , 'error' ) ;
2331 }
2432}
2533
@@ -45,17 +53,47 @@ export async function GET(
4553 request : NextRequest ,
4654 { params } : { params : BaseParams }
4755) {
48- const currency = await determineCurrencyAction ( getIpAddress ( ) ) ;
49- const { productId, priceId } = getSP2Params (
50- config . sp2map ,
51- reportError ,
52- params . offeringId ,
53- params . interval ,
54- currency
55- ) ;
56- console . log ( { productId, priceId } ) ;
56+ const requestSearchParams = request . nextUrl . searchParams ;
57+
58+ if ( config . sp2redirect . enabled ) {
59+ const queryCurrency = requestSearchParams . get ( 'currency' ) ;
60+ const querySpVersion = requestSearchParams . get ( 'spVersion' ) ;
61+ const isSp2Redirect = redirectToSp2 (
62+ config . sp2redirect ,
63+ params . offeringId ,
64+ crypto . randomInt ( 1 , 100 ) ,
65+ reportError
66+ ) ;
67+
68+ if ( isSp2Redirect || querySpVersion === '2' ) {
69+ const currency = queryCurrency
70+ ? queryCurrency
71+ : await determineCurrencyAction ( getIpAddress ( ) ) ;
72+ const { productId, priceId } = getSP2Params (
73+ config . sp2map ,
74+ reportError ,
75+ params . offeringId ,
76+ params . interval ,
77+ currency
78+ ) ;
79+
80+ const sp2RedirectUrl = buildSp2RedirectUrl (
81+ productId ,
82+ priceId ,
83+ config . contentServerUrl ,
84+ requestSearchParams
85+ ) ;
86+
87+ if ( ! config . sp2redirect . shadowMode ) {
88+ redirect ( sp2RedirectUrl ) ;
89+ } else {
90+ console . log ( 'SP2 Redirect Shadow Mode enabled' , { sp2RedirectUrl } ) ;
91+ }
92+ }
93+ }
94+
95+ const searchParams = Object . fromEntries ( requestSearchParams ) ;
5796
58- const searchParams = Object . fromEntries ( request . nextUrl . searchParams ) ;
5997 const redirectToUrl = new URL (
6098 buildRedirectUrl ( params . offeringId , params . interval , 'new' , 'checkout' , {
6199 locale : params . locale ,
0 commit comments