File size: 1,670 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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( ',' ) }` : '';

	// If no products are provided, we might have added plan to the cart so we just go to the checkout page directly.
	// If the flag forceRedirection is true, we also go to the checkout page via redirection.
	// The theme upsell link does not work with siteId and requires a siteSlug.
	// See https://github.com/Automattic/wp-calypso/pull/64899
	window.location.href = addQueryArgs(
		`/checkout/${ encodeURIComponent( siteSlug ) }${ productSlugs }`,
		params
	);
};