File size: 4,614 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
110
111
112
113
114
115
116
117
118
119
120
121
import { makeSuccessResponse, makeErrorResponse } from '@automattic/composite-checkout';
import debugFactory from 'debug';
import { recordTracksEvent } from 'calypso/state/analytics/actions';
import { logStashEvent, recordTransactionBeginAnalytics } from '../lib/analytics';
import getDomainDetails from './get-domain-details';
import getPostalCode from './get-postal-code';
import {
	doesTransactionResponseRequire3DS,
	handle3DSChallenge,
	handle3DSInFlightError,
} from './stripe-3ds';
import submitWpcomTransaction from './submit-wpcom-transaction';
import {
	createTransactionEndpointRequestPayload,
	createTransactionEndpointCartFromResponseCart,
} from './translate-cart';
import type { PaymentProcessorOptions } from '../types/payment-processors';
import type { StripeConfiguration } from '@automattic/calypso-stripe';
import type { PaymentProcessorResponse } from '@automattic/composite-checkout';
import type { Stripe } from '@stripe/stripe-js';

const debug = debugFactory( 'calypso:composite-checkout:web-pay-processor' );

type WebPayTransactionRequest = {
	stripe: Stripe;
	stripeConfiguration: StripeConfiguration;
	paymentMethodToken: string;
	name: string | undefined;
};

export default async function webPayProcessor(
	webPaymentType: 'google-pay' | 'apple-pay',
	submitData: unknown,
	transactionOptions: PaymentProcessorOptions
): Promise< PaymentProcessorResponse > {
	if ( ! isValidWebPayTransactionData( submitData ) ) {
		throw new Error( 'Required purchase data is missing' );
	}
	transactionOptions.reduxDispatch(
		recordTransactionBeginAnalytics( { paymentMethodId: webPaymentType } )
	);

	const { includeDomainDetails, includeGSuiteDetails, responseCart, siteId, contactDetails } =
		transactionOptions;

	debug( 'formatting web-pay transaction', submitData );
	const formattedTransactionData = createTransactionEndpointRequestPayload( {
		...submitData,
		name: submitData.name || '',
		country: contactDetails?.countryCode?.value ?? '',
		postalCode: getPostalCode( contactDetails ),
		domainDetails: getDomainDetails( contactDetails, {
			includeDomainDetails,
			includeGSuiteDetails,
		} ),
		cart: createTransactionEndpointCartFromResponseCart( {
			siteId,
			contactDetails:
				getDomainDetails( contactDetails, { includeDomainDetails, includeGSuiteDetails } ) ?? null,
			responseCart: responseCart,
		} ),
		paymentMethodType: 'WPCOM_Billing_Stripe_Payment_Method',
		paymentPartnerProcessorId: transactionOptions.stripeConfiguration?.processor_id,
	} );
	debug( 'submitting web-pay transaction', formattedTransactionData );
	let paymentIntentId: string | undefined = undefined;
	return submitWpcomTransaction( formattedTransactionData, transactionOptions )
		.then( async ( stripeResponse ) => {
			if ( doesTransactionResponseRequire3DS( stripeResponse ) ) {
				debug( 'transaction requires authentication' );
				paymentIntentId = stripeResponse.message.payment_intent_id;
				await handle3DSChallenge(
					transactionOptions.reduxDispatch,
					submitData.stripe,
					stripeResponse.message.payment_intent_client_secret,
					paymentIntentId
				);
				// We must return the original authentication response in order
				// to have access to the order_id so that we can display a
				// pending page while we wait for Stripe to send a webhook to
				// complete the purchase so we do not return the result of
				// confirming the payment intent and instead fall through.
			}
			return stripeResponse;
		} )
		.then( makeSuccessResponse )
		.catch( ( error: Error ) => {
			debug( 'transaction failed' );
			transactionOptions.reduxDispatch(
				recordTracksEvent( 'calypso_checkout_web_pay_transaction_failed', {
					payment_intent_id: paymentIntentId ?? '',
					error: error.message,
				} )
			);
			logStashEvent( 'calypso_checkout_web_pay_transaction_failed', {
				payment_intent_id: paymentIntentId ?? '',
				tags: [ `payment_intent_id:${ paymentIntentId }` ],
				error: error.message,
			} );

			handle3DSInFlightError( error, paymentIntentId );

			// Errors here are "expected" errors, meaning that they (hopefully) come
			// from the endpoint and not from some bug in the frontend code.
			return makeErrorResponse( error.message );
		} );
}

function isValidWebPayTransactionData(
	submitData: unknown
): submitData is WebPayTransactionRequest {
	const data = submitData as WebPayTransactionRequest;
	if ( ! data?.stripe ) {
		throw new Error( 'Transaction requires stripe and none was provided' );
	}
	if ( ! data?.stripeConfiguration ) {
		throw new Error( 'Transaction requires stripeConfiguration and none was provided' );
	}
	return true;
}