async function startCardUpdate()

in client/components/mma/paymentUpdate/card/stripeCardInputForm.tsx [133:231]


	async function startCardUpdate() {
		setIsValidating(true);

		const cardElement = elements?.getElement(CardNumberElement);

		if (!cardElement) {
			Sentry.captureException('StripeElements returning null');
			setError({
				message:
					'Something went wrong, please check the details and try again.',
			});
			setIsValidating(false);
			return;
		}

		if (!recaptchaToken) {
			setIsValidating(false);
			setError({ message: 'Recaptcha has not been completed' });
			return;
		}

		// new recaptcha token needed with each call to create a setup intent
		let setupIntent;

		if (!stripeSetupIntent) {
			setupIntent = await loadSetupIntent();
			setStripeSetupIntent(setupIntent);
		} else {
			setupIntent = stripeSetupIntent;
		}

		if (stripe && setupIntent) {
			const createPaymentMethodResult = await stripe.createPaymentMethod({
				type: 'card',
				card: cardElement,
				billing_details: {
					name: props.userEmail,
					email: props.userEmail,
				},
			});

			if (
				!(
					createPaymentMethodResult &&
					createPaymentMethodResult.paymentMethod &&
					createPaymentMethodResult.paymentMethod.id &&
					createPaymentMethodResult.paymentMethod.card &&
					createPaymentMethodResult.paymentMethod.card.brand &&
					createPaymentMethodResult.paymentMethod.card.last4
				)
			) {
				Sentry.captureException(
					createPaymentMethodResult.error ||
						'something missing from the createPaymentMethod response',
				);
				setError(
					createPaymentMethodResult.error || {
						message:
							'Something went wrong, please check the details and try again.',
					},
				);
				setIsValidating(false);
				return;
			}

			const intentResult = await stripe.confirmCardSetup(
				setupIntent.client_secret,
				{ payment_method: createPaymentMethodResult.paymentMethod.id },
			);

			if (
				intentResult.setupIntent &&
				intentResult.setupIntent.status &&
				intentResult.setupIntent.status === 'succeeded'
			) {
				setIsValidating(false);

				const newPaymentMethodDetail = new NewCardPaymentMethodDetail(
					createPaymentMethodResult.paymentMethod as StripePaymentMethod,
					props.stripeApiKey,
				);

				props.newPaymentMethodDetailUpdater(newPaymentMethodDetail);
				props.executePaymentUpdate(newPaymentMethodDetail);
			} else {
				Sentry.captureException(
					intentResult.error ||
						'something missing from the SetupIntent response',
				);
				setError(
					intentResult.error || {
						message:
							'Something went wrong, please check the details and try again.',
					},
				);
				setIsValidating(false);
			}
		}
	}