File size: 5,454 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import { useI18n } from '@wordpress/react-i18n';
import debugFactory from 'debug';
import { useEffect, useRef, useCallback } from 'react';
import { useTransactionStatus } from '../lib/transaction-status';
import { usePaymentMethodId } from '../public-api';
import { TransactionStatus } from '../types';
import type {
	PaymentEventCallback,
	PaymentErrorCallback,
	PaymentProcessorResponseData,
} from '../types';

const debug = debugFactory( 'composite-checkout:transaction-status-handler' );

/**
 * Helper component to take an action when the form or transaction status changes.
 *
 * For example, if the transaction status changes to "pending", this will set
 * the form status to "submitting"; if the transaction status changes to
 * "redirecting", this will perform the redirect. If there is an error, the
 * `onPaymentError` callback will be called.
 */
export default function FormAndTransactionEventHandler( {
	onPaymentComplete,
	onPaymentRedirect,
	onPaymentError,
	redirectToUrl,
}: {
	onPaymentComplete?: PaymentEventCallback;
	onPaymentRedirect?: PaymentEventCallback;
	onPaymentError?: PaymentErrorCallback;
	redirectToUrl?: ( url: string ) => void;
} ): null {
	useTransactionStatusHandler( {
		onPaymentComplete,
		onPaymentRedirect,
		onPaymentError,
		redirectToUrl,
	} );
	return null;
}

export function useTransactionStatusHandler( {
	onPaymentComplete,
	onPaymentRedirect,
	onPaymentError,
	redirectToUrl,
}: {
	onPaymentComplete?: PaymentEventCallback;
	onPaymentRedirect?: PaymentEventCallback;
	onPaymentError?: PaymentErrorCallback;
	redirectToUrl?: ( url: string ) => void;
} ): void {
	// @ts-expect-error window.location can accept a string, but the types don't like it.
	const defaultRedirect = useCallback( ( url: string ) => ( window.location = url ), [] );
	const performRedirect = redirectToUrl ?? defaultRedirect;

	const { __ } = useI18n();
	const [ paymentMethodId ] = usePaymentMethodId();
	const {
		previousTransactionStatus,
		transactionLastResponse,
		transactionStatus,
		transactionRedirectUrl,
		transactionError,
		resetTransaction,
		setTransactionError,
	} = useTransactionStatus();

	// When form status or transaction status changes, call the appropriate callbacks.
	useCallStatusChangeCallbacks( {
		onPaymentComplete,
		onPaymentRedirect,
		onPaymentError,
		transactionError,
		transactionStatus,
		paymentMethodId,
		transactionLastResponse,
	} );

	const redirectErrormessage = __(
		'An error occurred while redirecting to the payment partner. Please try again or contact support.'
	);
	useEffect( () => {
		if ( transactionStatus === previousTransactionStatus ) {
			return;
		}

		if ( transactionStatus === TransactionStatus.ERROR ) {
			debug( 'an error occurred', transactionError );
			// Reset the transaction after the error has been registered (and
			// other listeners have had a chance to respond to it.)
			resetTransaction();
		}
		if ( transactionStatus === TransactionStatus.REDIRECTING ) {
			if ( ! transactionRedirectUrl ) {
				debug( 'tried to redirect but there was no redirect url' );
				setTransactionError( redirectErrormessage );
				return;
			}
			debug( 'redirecting to', transactionRedirectUrl );
			performRedirect( transactionRedirectUrl );
		}
	}, [ transactionStatus ] ); // eslint-disable-line react-hooks/exhaustive-deps
}

// When form status or transaction status changes, call the appropriate callbacks.
function useCallStatusChangeCallbacks( {
	onPaymentComplete,
	onPaymentRedirect,
	onPaymentError,
	transactionError,
	transactionStatus,
	paymentMethodId,
	transactionLastResponse,
}: {
	onPaymentComplete?: PaymentEventCallback;
	onPaymentRedirect?: PaymentEventCallback;
	onPaymentError?: PaymentErrorCallback;
	transactionError: string | null;
	transactionStatus: TransactionStatus;
	paymentMethodId: string | null | undefined;
	transactionLastResponse: PaymentProcessorResponseData;
} ): void {
	// Store the callbacks as refs so we do not call them more than once if they
	// are anonymous functions. This way they are only called when the
	// transactionStatus changes, which is what we really want.
	const paymentCompleteRef = useRef( onPaymentComplete );
	paymentCompleteRef.current = onPaymentComplete;
	const paymentRedirectRef = useRef( onPaymentRedirect );
	paymentRedirectRef.current = onPaymentRedirect;
	const paymentErrorRef = useRef( onPaymentError );
	paymentErrorRef.current = onPaymentError;

	const prevTransactionStatus = useRef< TransactionStatus >();

	useEffect( () => {
		if ( transactionStatus === prevTransactionStatus.current ) {
			return;
		}
		if ( paymentCompleteRef.current && transactionStatus === TransactionStatus.COMPLETE ) {
			debug( "transactionStatus status changed to complete so I'm calling onPaymentComplete" );
			paymentCompleteRef.current( { transactionLastResponse } );
		}
		if ( paymentRedirectRef.current && transactionStatus === TransactionStatus.REDIRECTING ) {
			debug( "transaction status changed to redirecting so I'm calling onPaymentRedirect" );
			paymentRedirectRef.current( { transactionLastResponse } );
		}
		if ( paymentErrorRef.current && transactionStatus === TransactionStatus.ERROR ) {
			debug( "transaction status changed to error so I'm calling onPaymentError" );
			paymentErrorRef.current( { paymentMethodId, transactionError } );
		}
		prevTransactionStatus.current = transactionStatus;
	}, [ transactionStatus, paymentMethodId, transactionLastResponse, transactionError ] );
}