File size: 880 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
import { recordTracksEvent } from 'calypso/lib/analytics/tracks';
import { wpcomJetpackLicensing as wpcomJpl } from 'calypso/lib/wp';

interface Params {
	token: string;
	useAsPrimaryPaymentMethod: boolean;
	stripeSetupIntentId: string;
}

export async function saveCreditCard( {
	token,
	useAsPrimaryPaymentMethod,
	stripeSetupIntentId,
}: Params ): Promise< unknown > {
	const response = await wpcomJpl.req.post(
		{
			apiNamespace: 'wpcom/v2',
			path: '/jetpack-licensing/stripe/payment-method',
		},
		{
			payment_method_id: token,
			use_as_primary_payment_method: useAsPrimaryPaymentMethod,
			stripe_setup_intent_id: stripeSetupIntentId,
		}
	);

	if ( response.error ) {
		recordTracksEvent( 'calypso_partner_portal_add_new_credit_card_error' );
		throw new Error( response );
	}

	recordTracksEvent( 'calypso_partner_portal_add_new_credit_card' );
	return response;
}