@glomopay/react-native-sdk 2.0.2 → 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (80) hide show
  1. package/CHANGELOG.md +79 -0
  2. package/MIGRATION.md +50 -0
  3. package/README.md +348 -316
  4. package/lib/config/base.d.ts +17 -11
  5. package/lib/config/base.d.ts.map +1 -1
  6. package/lib/config/base.js +19 -7
  7. package/lib/config/segment.js +3 -3
  8. package/lib/glomo-checkout.d.ts +8 -0
  9. package/lib/glomo-checkout.d.ts.map +1 -0
  10. package/lib/glomo-checkout.js +106 -0
  11. package/lib/glomo-lrs-checkout.js +9 -9
  12. package/lib/glomo-standard-checkout.d.ts +25 -0
  13. package/lib/glomo-standard-checkout.d.ts.map +1 -0
  14. package/lib/glomo-standard-checkout.js +229 -0
  15. package/lib/glomo-subscriptions-checkout.d.ts +8 -0
  16. package/lib/glomo-subscriptions-checkout.d.ts.map +1 -0
  17. package/lib/glomo-subscriptions-checkout.js +85 -0
  18. package/lib/index.d.ts +5 -4
  19. package/lib/index.d.ts.map +1 -1
  20. package/lib/index.js +7 -5
  21. package/lib/injections/index.d.ts +1 -0
  22. package/lib/injections/index.d.ts.map +1 -1
  23. package/lib/injections/index.js +2 -0
  24. package/lib/injections/webview-flow.injection.d.ts.map +1 -1
  25. package/lib/injections/webview-flow.injection.js +106 -69
  26. package/lib/injections/webview-main.injection.d.ts.map +1 -1
  27. package/lib/injections/webview-main.injection.js +112 -77
  28. package/lib/injections/webview-standard.injection.d.ts +3 -0
  29. package/lib/injections/webview-standard.injection.d.ts.map +1 -0
  30. package/lib/injections/webview-standard.injection.js +214 -0
  31. package/lib/services/order-type-fetcher.d.ts +28 -0
  32. package/lib/services/order-type-fetcher.d.ts.map +1 -0
  33. package/lib/services/order-type-fetcher.js +99 -0
  34. package/lib/types/checkout.d.ts +65 -0
  35. package/lib/types/checkout.d.ts.map +1 -0
  36. package/lib/types/checkout.js +3 -0
  37. package/lib/types/standard-checkout.d.ts +40 -0
  38. package/lib/types/standard-checkout.d.ts.map +1 -0
  39. package/lib/types/standard-checkout.js +3 -0
  40. package/lib/types/subscriptions-checkout.d.ts +29 -0
  41. package/lib/types/subscriptions-checkout.d.ts.map +1 -0
  42. package/lib/types/subscriptions-checkout.js +3 -0
  43. package/lib/use-glomo-checkout.d.ts +54 -0
  44. package/lib/use-glomo-checkout.d.ts.map +1 -0
  45. package/lib/use-glomo-checkout.js +261 -0
  46. package/lib/use-lrs-checkout.d.ts +9 -4
  47. package/lib/use-lrs-checkout.d.ts.map +1 -1
  48. package/lib/use-lrs-checkout.js +76 -93
  49. package/lib/use-standard-checkout.d.ts +74 -0
  50. package/lib/use-standard-checkout.d.ts.map +1 -0
  51. package/lib/use-standard-checkout.js +849 -0
  52. package/lib/utils/analytics.d.ts +188 -2
  53. package/lib/utils/analytics.d.ts.map +1 -1
  54. package/lib/utils/analytics.js +636 -22
  55. package/lib/utils/device-compliance.d.ts.map +1 -1
  56. package/lib/utils/device-compliance.js +3 -4
  57. package/lib/utils/validation.d.ts.map +1 -1
  58. package/lib/utils/validation.js +7 -6
  59. package/package.json +17 -5
  60. package/src/config/base.ts +36 -17
  61. package/src/config/segment.ts +3 -3
  62. package/src/glomo-checkout.tsx +147 -0
  63. package/src/glomo-lrs-checkout.tsx +9 -9
  64. package/src/glomo-standard-checkout.tsx +353 -0
  65. package/src/glomo-subscriptions-checkout.tsx +83 -0
  66. package/src/index.ts +13 -7
  67. package/src/injections/index.ts +2 -0
  68. package/src/injections/webview-flow.injection.ts +106 -69
  69. package/src/injections/webview-main.injection.ts +112 -77
  70. package/src/injections/webview-standard.injection.ts +211 -0
  71. package/src/services/order-type-fetcher.ts +86 -0
  72. package/src/types/checkout.ts +72 -0
  73. package/src/types/standard-checkout.ts +49 -0
  74. package/src/types/subscriptions-checkout.ts +31 -0
  75. package/src/use-glomo-checkout.tsx +369 -0
  76. package/src/use-lrs-checkout.tsx +91 -111
  77. package/src/use-standard-checkout.tsx +1203 -0
  78. package/src/utils/analytics.ts +908 -34
  79. package/src/utils/device-compliance.ts +3 -4
  80. package/src/utils/validation.ts +7 -8
@@ -0,0 +1,369 @@
1
+ /**
2
+ * Unified wrapper hook - detects order type (LRS vs Standard) via the order API
3
+ * and delegates to the appropriate inner checkout hook once resolved.
4
+ */
5
+
6
+ import { useState, useRef, useCallback, useEffect } from "react";
7
+
8
+ import { type GlomoCheckoutProps, type CheckoutStatus } from "./types/checkout";
9
+ import { type GlomoLrsCheckoutRef } from "./glomo-lrs-checkout";
10
+ import { type GlomoStandardCheckoutRef } from "./types/standard-checkout";
11
+ import { type GlomoSubscriptionsCheckoutRef } from "./types/subscriptions-checkout";
12
+ import { type OrderType, fetchOrderType } from "./services/order-type-fetcher";
13
+ import { isValidPublicKey, isValidOrderId, safeCallback } from "./utils/validation";
14
+ import {
15
+ type SdkError,
16
+ trackOrderTypeDetectionStarted,
17
+ trackOrderTypeDetectionResolved,
18
+ trackOrderTypeDetectionFailed,
19
+ trackUseOfUnsupportedFunctionality,
20
+ } from "./utils/analytics";
21
+
22
+ /** Extended order type including subscriptions */
23
+ type ExtendedOrderType = OrderType | "subscriptions";
24
+
25
+ /**
26
+ * Public return type for useGlomoCheckout.
27
+ * Exposes only start() and getStatus() - no internal refs or state.
28
+ */
29
+ export interface UseGlomoCheckoutReturn {
30
+ start: () => Promise<boolean>;
31
+ getStatus: () => CheckoutStatus;
32
+ }
33
+
34
+ /**
35
+ * Internal return type - used by GlomoCheckout component.
36
+ * Not exported from the public API.
37
+ */
38
+ export interface UseGlomoCheckoutInternalReturn {
39
+ orderType: ExtendedOrderType | null;
40
+ detecting: boolean;
41
+ start: () => Promise<boolean>;
42
+ getStatus: () => CheckoutStatus;
43
+ innerLrsRef: React.RefObject<GlomoLrsCheckoutRef>;
44
+ innerStandardRef: React.RefObject<GlomoStandardCheckoutRef>;
45
+ innerSubscriptionsRef: React.RefObject<GlomoSubscriptionsCheckoutRef>;
46
+ props: GlomoCheckoutProps;
47
+ }
48
+
49
+ /**
50
+ * @deprecated Legacy hook - will be removed in a future major version.
51
+ *
52
+ * This hook does not provide reactive state. `getStatus()` returns a
53
+ * point-in-time snapshot and does not trigger re-renders on status changes.
54
+ * `start()` returns a promise identical to `GlomoCheckoutRef.start()`.
55
+ *
56
+ * Use the `GlomoCheckout` component with a ref instead:
57
+ *
58
+ * ```tsx
59
+ * const ref = useRef<GlomoCheckoutRef>(null);
60
+ * const started = await ref.current?.start();
61
+ * const status = ref.current?.getStatus();
62
+ * ```
63
+ */
64
+ export function useGlomoCheckout(props: GlomoCheckoutProps): UseGlomoCheckoutReturn;
65
+ export function useGlomoCheckout(
66
+ props: GlomoCheckoutProps,
67
+ internalOptions: { _suppressDeprecationWarning: true }
68
+ ): UseGlomoCheckoutInternalReturn;
69
+ export function useGlomoCheckout(
70
+ props: GlomoCheckoutProps,
71
+ internalOptions?: { _suppressDeprecationWarning?: boolean }
72
+ ): UseGlomoCheckoutInternalReturn {
73
+ const { server, publicKey, orderId, subscriptionId, onSdkError, devMode = false } = props;
74
+
75
+ // Order type resolved from the API - null until detection completes
76
+ const [orderType, setOrderType] = useState<ExtendedOrderType | null>(null);
77
+ // Whether order type detection is in progress
78
+ const [detecting, setDetecting] = useState(false);
79
+ // Whether the props have a mutual exclusion error
80
+ const [propsError, setPropsError] = useState(false);
81
+
82
+ // Stabilizing onSdkError ref to avoid re-running effects on inline callback changes
83
+ const onSdkErrorRef = useRef(onSdkError);
84
+ onSdkErrorRef.current = onSdkError;
85
+
86
+ // One-time deprecation warning for direct merchant usage
87
+ const suppressWarning = internalOptions?._suppressDeprecationWarning ?? false;
88
+ const mockMode = publicKey?.toLowerCase().startsWith("test_") ?? false;
89
+ const hasWarnedRef = useRef(false);
90
+ useEffect(() => {
91
+ if (!hasWarnedRef.current && !suppressWarning) {
92
+ hasWarnedRef.current = true;
93
+ console.warn(
94
+ "[Glomo-RN-SDK] useGlomoCheckout is deprecated and will be removed in a future major version. " +
95
+ "Use the GlomoCheckout component with a ref instead. " +
96
+ "This hook does not provide reactive state - getStatus() is a point-in-time snapshot, not a reactive value."
97
+ );
98
+ trackUseOfUnsupportedFunctionality(
99
+ "useGlomoCheckout",
100
+ orderId ?? subscriptionId,
101
+ publicKey,
102
+ devMode,
103
+ mockMode,
104
+ ""
105
+ );
106
+ }
107
+ }, [suppressWarning, orderId, subscriptionId, publicKey, devMode, mockMode]);
108
+
109
+ // Refs for the inner LRS, Standard, and Subscriptions checkout components
110
+ const innerLrsRef = useRef<GlomoLrsCheckoutRef>(null);
111
+ const innerStandardRef = useRef<GlomoStandardCheckoutRef>(null);
112
+ const innerSubscriptionsRef = useRef<GlomoSubscriptionsCheckoutRef>(null);
113
+
114
+ /** Mutual exclusion guard - firing onSdkError if both or neither id is provided */
115
+ useEffect(() => {
116
+ const hasBoth = Boolean(orderId) && Boolean(subscriptionId);
117
+ const hasNeither = !orderId && !subscriptionId;
118
+
119
+ if (hasBoth) {
120
+ setPropsError(true);
121
+ safeCallback(
122
+ onSdkErrorRef.current,
123
+ [[{ type: "validation_error", message: "Provide either orderId or subscriptionId, not both" }]],
124
+ "onSdkError",
125
+ devMode
126
+ );
127
+ } else if (hasNeither) {
128
+ setPropsError(true);
129
+ safeCallback(
130
+ onSdkErrorRef.current,
131
+ [[{ type: "validation_error", message: "Provide orderId or subscriptionId" }]],
132
+ "onSdkError",
133
+ devMode
134
+ );
135
+ } else {
136
+ setPropsError(false);
137
+ }
138
+ }, [orderId, subscriptionId, devMode]);
139
+
140
+ /**
141
+ * Tracking whether start() has been called and the inner component needs to be auto-started.
142
+ * This bridges the gap between order type resolution (async) and inner component mount (next render).
143
+ */
144
+ const pendingStartRef = useRef(false);
145
+
146
+ /**
147
+ * Generation counter for stale fetch detection.
148
+ * Incremented on every start() call and on orderId/publicKey changes. After fetchOrderType
149
+ * resolves, the result is discarded if the generation has moved on (props changed mid-flight).
150
+ */
151
+ const fetchGenerationRef = useRef(0);
152
+
153
+ /**
154
+ * Holds the resolve function for the pending start() promise.
155
+ * This allows the useEffect that auto-starts the inner component to resolve the merchant's
156
+ * awaited promise with the actual result of the inner start().
157
+ */
158
+ const pendingStartResolveRef = useRef<((value: boolean) => void) | null>(null);
159
+
160
+ // Deriving the unified checkout status from the inner component's status
161
+ const getStatus = useCallback((): CheckoutStatus => {
162
+ if (detecting) {
163
+ return "detecting_order_type";
164
+ }
165
+ // Delegating to the inner component's getStatus() once order type is resolved
166
+ if (orderType === "lrs" && innerLrsRef.current) {
167
+ return innerLrsRef.current.getStatus();
168
+ }
169
+ if (orderType === "standard" && innerStandardRef.current) {
170
+ return innerStandardRef.current.getStatus();
171
+ }
172
+ if (orderType === "subscriptions" && innerSubscriptionsRef.current) {
173
+ return innerSubscriptionsRef.current.getStatus();
174
+ }
175
+ return "ready";
176
+ }, [detecting, orderType]);
177
+
178
+ /**
179
+ * Initiator for the unified checkout flow.
180
+ * Validates inputs, calls the order type detection API, and marks the inner component for auto-start.
181
+ */
182
+ const start = useCallback(async (): Promise<boolean> => {
183
+ // If a previous start() call is still pending, resolve it as false before proceeding
184
+ if (pendingStartResolveRef.current) {
185
+ pendingStartResolveRef.current(false);
186
+ pendingStartResolveRef.current = null;
187
+ }
188
+
189
+ if (devMode) {
190
+ console.log("[Glomo-RN-SDK] start() called");
191
+ }
192
+
193
+ // Aborting if mutual exclusion guard detected a props error
194
+ if (propsError) {
195
+ if (devMode) {
196
+ console.error("[Glomo-RN-SDK] start() aborted: invalid orderId/subscriptionId combination");
197
+ }
198
+ return false;
199
+ }
200
+
201
+ // Validating inputs before making the API call
202
+ const errors: SdkError[] = [];
203
+
204
+ if (!isValidPublicKey(publicKey)) {
205
+ errors.push({
206
+ type: "validation_error",
207
+ message: "Invalid publicKey: must start with 'live_', 'mock_', or 'test_' and be a valid string",
208
+ field: "publicKey",
209
+ });
210
+ }
211
+
212
+ /**
213
+ * For subscriptions flow, skipping orderId validation entirely - GlomoSubscriptionsCheckout
214
+ * owns subscriptionId validation (trim, empty, prefix). For order flow, validating orderId.
215
+ */
216
+ if (!subscriptionId && !isValidOrderId(orderId!)) {
217
+ errors.push({
218
+ type: "validation_error",
219
+ message: "Invalid orderId: must start with 'order_' and be a valid string",
220
+ field: "orderId",
221
+ });
222
+ }
223
+
224
+ // Reporting validation errors and aborting if any
225
+ if (errors.length > 0) {
226
+ if (devMode) {
227
+ console.error("[Glomo-RN-SDK] Validation failed.");
228
+ }
229
+ safeCallback(onSdkError, [errors], "onSdkError", devMode);
230
+ return false;
231
+ }
232
+
233
+ /**
234
+ * For subscriptions, orderType is already "subscriptions" and the inner component is mounted.
235
+ * Calling start() directly - no promise/effect bridge needed since no async detection occurs.
236
+ */
237
+ if (subscriptionId) {
238
+ if (innerSubscriptionsRef.current) {
239
+ const started = innerSubscriptionsRef.current.start();
240
+ if (devMode) {
241
+ console.log(`[Glomo-RN-SDK] Subscriptions inner start() returned: ${started}`);
242
+ }
243
+ return started;
244
+ }
245
+ if (devMode) {
246
+ console.error("[Glomo-RN-SDK] Subscriptions component not mounted");
247
+ }
248
+ return false;
249
+ }
250
+
251
+ // Detecting order type via the API
252
+ setDetecting(true);
253
+ const thisGeneration = ++fetchGenerationRef.current;
254
+
255
+ if (devMode) {
256
+ console.log("[Glomo-RN-SDK] Detecting order type...");
257
+ }
258
+
259
+ trackOrderTypeDetectionStarted(orderId!, publicKey, devMode);
260
+
261
+ const result = await fetchOrderType({ orderId: orderId!, publicKey, server, devMode });
262
+
263
+ // Discarding stale result if props changed while the fetch was in flight
264
+ if (thisGeneration !== fetchGenerationRef.current) {
265
+ if (devMode) {
266
+ console.log("[Glomo-RN-SDK] Discarding stale order type result (props changed mid-flight)");
267
+ }
268
+ return false;
269
+ }
270
+
271
+ setDetecting(false);
272
+
273
+ // Handling detection failure
274
+ if (!result.success) {
275
+ if (devMode) {
276
+ console.error(`[Glomo-RN-SDK] Order type detection failed: ${result.error}`);
277
+ }
278
+ trackOrderTypeDetectionFailed(orderId!, publicKey, result.error, devMode);
279
+ safeCallback(onSdkError, [[{ type: "validation_error", message: result.error }]], "onSdkError", devMode);
280
+ return false;
281
+ }
282
+
283
+ if (devMode) {
284
+ console.log(`[Glomo-RN-SDK] Order type resolved: ${result.orderType}`);
285
+ }
286
+
287
+ trackOrderTypeDetectionResolved(orderId!, publicKey, result.orderType, devMode);
288
+
289
+ // Setting the resolved order type - this triggers a re-render that mounts the inner component
290
+ setOrderType(result.orderType);
291
+
292
+ // Returning a promise that resolves only when the inner component's start() completes
293
+ return new Promise<boolean>((resolve) => {
294
+ pendingStartResolveRef.current = resolve;
295
+ pendingStartRef.current = true;
296
+ });
297
+ }, [devMode, publicKey, orderId, subscriptionId, server, onSdkError, propsError]);
298
+
299
+ /**
300
+ * Auto-starting the inner component after order type is resolved and the component has rendered.
301
+ * The pendingStartRef bridges the async gap: start() sets it, and this effect fires after
302
+ * the inner component mounts in the next render cycle.
303
+ */
304
+ useEffect(() => {
305
+ if (!pendingStartRef.current || !orderType) {
306
+ return;
307
+ }
308
+
309
+ // Forwarding start() to the appropriate inner component
310
+ let started = false;
311
+ if (orderType === "lrs" && innerLrsRef.current) {
312
+ pendingStartRef.current = false;
313
+ started = innerLrsRef.current.start();
314
+ if (devMode) {
315
+ console.log(`[Glomo-RN-SDK] LRS inner start() returned: ${started}`);
316
+ }
317
+ } else if (orderType === "standard" && innerStandardRef.current) {
318
+ pendingStartRef.current = false;
319
+ started = innerStandardRef.current.start();
320
+ if (devMode) {
321
+ console.log(`[Glomo-RN-SDK] Standard inner start() returned: ${started}`);
322
+ }
323
+ } else if (orderType === "subscriptions") {
324
+ // No-op: subscription start() calls the inner component directly (not via this effect bridge).
325
+ // If we reach here, pendingStartRef was set unexpectedly - resolve false defensively.
326
+ pendingStartRef.current = false;
327
+ }
328
+
329
+ // Resolving the merchant's awaited start() promise with the inner component's result
330
+ if (pendingStartResolveRef.current) {
331
+ pendingStartResolveRef.current(started);
332
+ pendingStartResolveRef.current = null;
333
+ }
334
+ }, [orderType, devMode]);
335
+
336
+ /**
337
+ * Resetting order type state when orderId, subscriptionId, or publicKey changes.
338
+ * For subscriptions, setting orderType to "subscriptions" directly (no API call needed).
339
+ * For orders, resetting to null to force re-detection on next start().
340
+ * Single effect owns all orderType transitions to avoid race conditions.
341
+ */
342
+ useEffect(() => {
343
+ // Invalidating any in-flight fetchOrderType call so its stale result is discarded
344
+ fetchGenerationRef.current++;
345
+ if (pendingStartResolveRef.current) {
346
+ pendingStartResolveRef.current(false);
347
+ pendingStartResolveRef.current = null;
348
+ }
349
+ setDetecting(false);
350
+ pendingStartRef.current = false;
351
+
352
+ if (subscriptionId && !orderId) {
353
+ setOrderType("subscriptions");
354
+ } else {
355
+ setOrderType(null);
356
+ }
357
+ }, [orderId, subscriptionId, publicKey]);
358
+
359
+ return {
360
+ orderType,
361
+ detecting,
362
+ start,
363
+ getStatus,
364
+ innerLrsRef,
365
+ innerStandardRef,
366
+ innerSubscriptionsRef,
367
+ props,
368
+ };
369
+ }