File size: 3,141 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import { confirmStripeSetupIntentAndAttachCard } from '@automattic/calypso-stripe';
import { makeSuccessResponse, makeErrorResponse } from '@automattic/composite-checkout';
import { useTranslate } from 'i18n-calypso';
import { saveCreditCard } from 'calypso/jetpack-cloud/sections/partner-portal/payment-methods/stored-payment-method-api';
import { recordTracksEvent } from 'calypso/state/analytics/actions';
import { createStripeSetupIntentForJetpackManage } from '../lib/create-stripe-setup-intent-for-jetpack-manage';
import type { StripeConfiguration, StripeSetupIntent } from '@automattic/calypso-stripe';
import type { PaymentProcessorResponse } from '@automattic/composite-checkout';
import type { Stripe, StripeCardElement } from '@stripe/stripe-js';
import type { CalypsoDispatch } from 'calypso/state/types';

interface Props {
	useAsPrimaryPaymentMethod?: boolean;
	translate: ReturnType< typeof useTranslate >;
	stripe: Stripe | null;
	stripeConfiguration: StripeConfiguration | null;
	cardElement: StripeCardElement | undefined;
	reduxDispatch: CalypsoDispatch;
}

export async function assignNewCardProcessor(
	{
		useAsPrimaryPaymentMethod,
		translate,
		stripe,
		stripeConfiguration,
		cardElement,
		reduxDispatch,
	}: Props,
	submitData: unknown
): Promise< PaymentProcessorResponse > {
	reduxDispatch( recordFormSubmitEvent() );

	try {
		if ( ! isNewCardDataValid( submitData ) ) {
			throw new Error( 'Credit Card data is missing your full name.' );
		}
		if ( ! stripe || ! stripeConfiguration ) {
			throw new Error( 'Cannot assign payment method if Stripe is not loaded' );
		}
		if ( ! cardElement ) {
			throw new Error( 'Cannot assign payment method if there is no card element' );
		}

		const stripeSetupIntentId = await createStripeSetupIntentForJetpackManage();

		const { name } = submitData;

		const formFieldValues = {
			name,
		};
		const tokenResponse = await prepareAndConfirmStripeSetupIntent(
			formFieldValues,
			stripe,
			cardElement,
			stripeSetupIntentId
		);
		const token = tokenResponse.payment_method;

		if ( ! token ) {
			throw new Error( String( translate( 'Failed to add card.' ) ) );
		}

		const result = await saveCreditCard( {
			token: String( token ),
			useAsPrimaryPaymentMethod: Boolean( useAsPrimaryPaymentMethod ),
			stripeSetupIntentId,
		} );

		return makeSuccessResponse( result );
	} catch ( error ) {
		return makeErrorResponse( ( error as Error ).message );
	}
}

async function prepareAndConfirmStripeSetupIntent(
	{
		name,
	}: {
		name: string;
	},
	stripe: Stripe,
	cardElement: StripeCardElement,
	stripeSetupIntentId: string
): Promise< StripeSetupIntent > {
	const paymentDetailsForStripe = {
		name,
	};
	return confirmStripeSetupIntentAndAttachCard(
		stripe,
		cardElement,
		stripeSetupIntentId,
		paymentDetailsForStripe
	);
}

function isNewCardDataValid( data: unknown ): data is NewCardSubmitData {
	const newCardData = data as NewCardSubmitData;
	return !! newCardData.name;
}

interface NewCardSubmitData {
	name: string;
}

function recordFormSubmitEvent() {
	return recordTracksEvent( 'calypso_partner_portal_add_credit_card_form_submit' );
}