File size: 8,237 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import debugFactory from 'debug';
import { useRef, useEffect, useState, useContext, createContext, PropsWithChildren } from 'react';

const debug = debugFactory( 'calypso-razorpay' );

export interface RazorpayConfiguration {
	js_url: string;
	options: RazorpayOptions;
}

export interface RazorpayOptions {
	key: string;
	order_id?: string; // This is a razorpay order ID; the name is constrained by a 3rd party library.
	customer_id?: string; // This is a razorpay customer ID; the name is constrained by a 3rd party library.
	handler?: ( response: RazorpayModalResponse ) => void;
	prefill?: {
		contact?: string;
		email?: string;
	};
	modal?: {
		ondismiss?: ( response: RazorpayModalResponse ) => void;
	};
	recurring?: string;
}

export interface RazorpayModalResponse {
	razorpay_payment_id: string;
	razorpay_order_id: string;
	razorpay_signature: string;
}

export interface RazorpayConfirmationRequestArgs {
	razorpay_payment_id: string;
	razorpay_signature: string;
	bd_order_id: string;
}

export declare class Razorpay {
	constructor( options: RazorpayConfiguration[ 'options' ] );
	open: () => unknown;
}

declare global {
	interface Window {
		Razorpay?: typeof Razorpay;
	}
}

export type RazorpayLoadingError = Error | null | undefined;

export interface RazorpayData {
	razorpayConfiguration: RazorpayConfiguration | null;
	isRazorpayLoading: boolean;
	razorpayLoadingError: RazorpayLoadingError;
}

const RazorpayContext = createContext< RazorpayData | undefined >( undefined );

export interface UseRazorpayJs {
	isRazorpayLoading: boolean;
	razorpayLoadingError: RazorpayLoadingError;
}

export type GetRazorpayConfigurationArgs = { sandbox: boolean };
export type GetRazorpayConfiguration = (
	requestArgs: GetRazorpayConfigurationArgs
) => Promise< RazorpayConfiguration >;

export class RazorpayConfigurationError extends Error {}

/**
 * React custom Hook for loading razorpayJs
 *
 * This is internal. You probably actually want the useRazorpay hook.
 *
 * Its parameter is the value returned by useRazorpayConfiguration
 * @param {RazorpayConfiguration} razorpayConfiguration Object holding Razorpay configuration options
 * @param {Error|undefined} [razorpayConfigurationError] Any error that occured trying to load the configuration
 * @returns {UseRazorpayJs} The Razorpay data
 */
function useRazorpayJs(
	razorpayConfiguration: RazorpayConfiguration | null,
	razorpayConfigurationError: Error | undefined
): UseRazorpayJs {
	const [ state, setState ] = useState< UseRazorpayJs >( {
		isRazorpayLoading: true,
		razorpayLoadingError: undefined,
	} );
	useEffect( () => {
		let isSubscribed = true;
		async function loadAndInitRazorpay() {
			if ( razorpayConfigurationError ) {
				throw razorpayConfigurationError;
			}
			if ( ! razorpayConfiguration ) {
				debug( 'Skip loading Razorpay; configuration not available' );
				return;
			}
			if ( ! razorpayConfiguration.js_url ) {
				throw new RazorpayConfigurationError( 'Razorpay library URL not specified.' );
			}

			const script = document.createElement( 'script' );
			script.src = razorpayConfiguration.js_url;
			script.async = true;
			script.onload = () => {
				debug( 'Razorpay JS library loaded!' );

				if ( typeof window === 'undefined' || ! window.Razorpay ) {
					throw new RazorpayConfigurationError(
						'Razorpay loading error: Razorpay object not defined'
					);
				}

				if ( isSubscribed ) {
					setState( {
						isRazorpayLoading: false,
						razorpayLoadingError: undefined,
					} );
				}
			};
			document.body.appendChild( script );
		}

		loadAndInitRazorpay().catch( ( error ) => {
			debug( 'Error while loading Razorpay!' );
			if ( isSubscribed ) {
				setState( {
					isRazorpayLoading: false,
					razorpayLoadingError: error,
				} );
			}
		} );

		return () => {
			isSubscribed = false;
		};
	}, [ razorpayConfigurationError, razorpayConfiguration ] );
	return state;
}

/**
 * React custom Hook for loading the Razorpay Configuration
 *
 * This is internal. You probably actually want the useRazorpay hook.
 */
function useRazorpayConfiguration(
	fetchRazorpayConfiguration: GetRazorpayConfiguration,
	requestArgs?: GetRazorpayConfigurationArgs | null | undefined
): {
	razorpayConfiguration: RazorpayConfiguration | null;
	razorpayConfigurationError: Error | undefined;
} {
	const [ razorpayConfigurationError, setRazorpayConfigurationError ] = useState<
		Error | undefined
	>();
	const [ razorpayConfiguration, setRazorpayConfiguration ] =
		useState< RazorpayConfiguration | null >( null );
	const memoizedRequestArgs = useMemoCompare( requestArgs, areRequestArgsEqual );

	useEffect( () => {
		debug( 'Loading razorpay configuration' );
		let isSubscribed = true;
		fetchRazorpayConfiguration( memoizedRequestArgs || { sandbox: true } )
			.then( ( configuration ) => {
				if ( ! isSubscribed ) {
					return;
				}
				if ( ! configuration.js_url ) {
					debug( 'Invalid razorpay configuration; js_url missing', configuration );
					throw new RazorpayConfigurationError(
						'Error loading payment method configuration. Received invalid data from the server.'
					);
				}
				debug( 'Razorpay configuration received', configuration );
				setRazorpayConfiguration( configuration ?? null );
			} )
			.catch( ( error ) => {
				setRazorpayConfigurationError( error );
			} );
		return () => {
			isSubscribed = false;
		};
	}, [ memoizedRequestArgs, fetchRazorpayConfiguration ] );
	return { razorpayConfiguration, razorpayConfigurationError };
}

function areRequestArgsEqual(
	previous: GetRazorpayConfigurationArgs | null | undefined,
	next: GetRazorpayConfigurationArgs | null | undefined
): boolean {
	if ( ! previous && ! next ) {
		// Catch if both are either null or undefined; don't need to distinguish these
		return true;
	}
	if ( ! previous || ! next ) {
		// Catch if one is an object and the other is not
		return false;
	}
	return previous.sandbox === next.sandbox;
}

export function RazorpayHookProvider( {
	children,
	fetchRazorpayConfiguration,
}: PropsWithChildren< {
	fetchRazorpayConfiguration: GetRazorpayConfiguration;
} > ) {
	const configurationArgs = {
		sandbox: true,
	};

	const { razorpayConfiguration, razorpayConfigurationError } = useRazorpayConfiguration(
		fetchRazorpayConfiguration,
		configurationArgs
	);

	const { isRazorpayLoading, razorpayLoadingError } = useRazorpayJs(
		razorpayConfiguration,
		razorpayConfigurationError
	);

	const razorpayData = {
		razorpayConfiguration,
		isRazorpayLoading,
		razorpayLoadingError,
	};

	return <RazorpayContext.Provider value={ razorpayData }>{ children }</RazorpayContext.Provider>;
}

/**
 * Custom hook to access Razorpay.js
 *
 * First you must wrap a parent component in `RazorpayHookProvider`. Then you can
 * call this hook in any sub-component to get access to the razorpay variables
 * and functions.
 *
 * This returns an object with the following properties:
 *
 * - razorpayConfiguration: the object containing the data returned by the wpcom razorpay configuration endpoint
 * - isRazorpayLoading: a boolean that is true if razorpay is currently being loaded
 * - razorpayLoadingError: an optional object that will be set if there is an error loading razorpay
 * @returns {RazorpayData} See above
 */
export function useRazorpay(): RazorpayData {
	const razorpayData = useContext( RazorpayContext );
	if ( ! razorpayData ) {
		throw new Error( 'useRazorpay can only be used inside a RazorpayHookProvider' );
	}
	return razorpayData;
}

function useMemoCompare< A, B >(
	next: B,
	compare: ( previous: A | B | undefined, next: B ) => boolean
): A | B | undefined {
	// Ref for storing previous value
	const previousRef = useRef< undefined | A | B >();
	const previous = previousRef.current;

	// Pass previous and next value to compare function
	// to determine whether to consider them equal.
	const isEqual = compare( previous, next );

	// If not equal update previousRef to next value.
	// We only update if not equal so that this hook continues to return
	// the same old value if compare keeps returning true.
	useEffect( () => {
		if ( ! isEqual ) {
			previousRef.current = next;
		}
	} );

	// Finally, if equal then return the previous value
	return isEqual ? previous : next;
}