|
|
import { addQueryArgs } from '@wordpress/url'; |
|
|
import { |
|
|
setSignupCompleteSlug, |
|
|
persistSignupDestination, |
|
|
setSignupCompleteFlowName, |
|
|
setSignupCompleteStepName, |
|
|
} from 'calypso/signup/storageUtils'; |
|
|
|
|
|
interface GoToCheckoutProps { |
|
|
flowName: string; |
|
|
stepName: string; |
|
|
siteSlug: string; |
|
|
destination: string; |
|
|
from?: string; |
|
|
plan?: string; |
|
|
cancelDestination?: string; |
|
|
extraProducts?: string[]; |
|
|
forceRedirection?: boolean; |
|
|
extraQueryParams?: Record< string, string >; |
|
|
} |
|
|
|
|
|
export const goToCheckout = ( { |
|
|
flowName, |
|
|
stepName, |
|
|
siteSlug, |
|
|
destination, |
|
|
plan, |
|
|
cancelDestination, |
|
|
extraProducts = [], |
|
|
extraQueryParams: extraParams = {}, |
|
|
}: GoToCheckoutProps ) => { |
|
|
const relativeCurrentPath = window.location.href.replace( window.location.origin, '' ); |
|
|
const params = { |
|
|
redirect_to: destination, |
|
|
cancel_to: cancelDestination || relativeCurrentPath, |
|
|
signup: '1', |
|
|
...extraParams, |
|
|
}; |
|
|
|
|
|
persistSignupDestination( destination ); |
|
|
setSignupCompleteSlug( siteSlug ); |
|
|
setSignupCompleteFlowName( flowName ); |
|
|
setSignupCompleteStepName( stepName ); |
|
|
|
|
|
const products = [ ...( plan ? [ plan ] : [] ), ...extraProducts ]; |
|
|
const productSlugs = products.length > 0 ? `/${ products.join( ',' ) }` : ''; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
window.location.href = addQueryArgs( |
|
|
`/checkout/${ encodeURIComponent( siteSlug ) }${ productSlugs }`, |
|
|
params |
|
|
); |
|
|
}; |
|
|
|