@sunbay/checkout-sdk-react 0.0.0-stage → 0.5.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.
- package/README.md +166 -2
- package/dist/index.cjs +629 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +107 -0
- package/dist/index.d.ts +107 -0
- package/dist/index.mjs +581 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +67 -3
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,629 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
"use strict";
|
|
3
|
+
var __create = Object.create;
|
|
4
|
+
var __defProp = Object.defineProperty;
|
|
5
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
8
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
|
+
var __export = (target, all) => {
|
|
10
|
+
for (var name in all)
|
|
11
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
12
|
+
};
|
|
13
|
+
var __copyProps = (to, from, except, desc) => {
|
|
14
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
15
|
+
for (let key of __getOwnPropNames(from))
|
|
16
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
17
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
18
|
+
}
|
|
19
|
+
return to;
|
|
20
|
+
};
|
|
21
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
22
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
23
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
24
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
25
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
26
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
27
|
+
mod
|
|
28
|
+
));
|
|
29
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
30
|
+
|
|
31
|
+
// src/index.ts
|
|
32
|
+
var index_exports = {};
|
|
33
|
+
__export(index_exports, {
|
|
34
|
+
AddressElement: () => AddressElement,
|
|
35
|
+
CardCvcElement: () => CardCvcElement,
|
|
36
|
+
CardElement: () => CardElement,
|
|
37
|
+
CardExpiryElement: () => CardExpiryElement,
|
|
38
|
+
CardNumberElement: () => CardNumberElement,
|
|
39
|
+
CheckoutProvider: () => CheckoutProvider,
|
|
40
|
+
ErrorCode: () => import_checkout_sdk_js3.ErrorCode,
|
|
41
|
+
ExpressCheckoutElement: () => ExpressCheckoutElement,
|
|
42
|
+
WARNING_CODES: () => import_checkout_sdk_js3.WARNING_CODES,
|
|
43
|
+
useCheckout: () => useCheckout,
|
|
44
|
+
useCheckoutEvent: () => useCheckoutEvent,
|
|
45
|
+
useConfirm: () => useConfirm,
|
|
46
|
+
version: () => version
|
|
47
|
+
});
|
|
48
|
+
module.exports = __toCommonJS(index_exports);
|
|
49
|
+
|
|
50
|
+
// src/CheckoutProvider.tsx
|
|
51
|
+
var React = __toESM(require("react"), 1);
|
|
52
|
+
var import_react2 = require("react");
|
|
53
|
+
var import_checkout_sdk_js = require("@sunbay/checkout-sdk-js");
|
|
54
|
+
|
|
55
|
+
// src/context.ts
|
|
56
|
+
var import_react = require("react");
|
|
57
|
+
var CheckoutContext = (0, import_react.createContext)(null);
|
|
58
|
+
|
|
59
|
+
// src/internal/env.ts
|
|
60
|
+
var isDevelopment = typeof process !== "undefined" && process.env.NODE_ENV !== "production";
|
|
61
|
+
|
|
62
|
+
// src/internal/parity.ts
|
|
63
|
+
var CREATE_OPTION_KINDS = {
|
|
64
|
+
sessionId: "identity",
|
|
65
|
+
checkoutClientSecret: "identity",
|
|
66
|
+
customerAccessSecret: "identity",
|
|
67
|
+
env: "identity",
|
|
68
|
+
hostOrigin: "identity",
|
|
69
|
+
locale: "updatable",
|
|
70
|
+
appearance: "updatable",
|
|
71
|
+
billingAddress: "updatable",
|
|
72
|
+
shippingAddress: "updatable",
|
|
73
|
+
paymentMethods: "creationOnly",
|
|
74
|
+
signal: "internal",
|
|
75
|
+
__transportFactory: "internal"
|
|
76
|
+
};
|
|
77
|
+
function keysOfKind(kind) {
|
|
78
|
+
return Object.keys(CREATE_OPTION_KINDS).filter(
|
|
79
|
+
(k) => CREATE_OPTION_KINDS[k] === kind
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
var IDENTITY_KEYS = keysOfKind("identity");
|
|
83
|
+
var UPDATABLE_KEYS = keysOfKind("updatable");
|
|
84
|
+
|
|
85
|
+
// src/internal/structuralEqual.ts
|
|
86
|
+
function isPlainObject(x) {
|
|
87
|
+
const proto = Object.getPrototypeOf(x);
|
|
88
|
+
return proto === Object.prototype || proto === null;
|
|
89
|
+
}
|
|
90
|
+
function structuralEqual(a, b) {
|
|
91
|
+
if (Object.is(a, b)) {
|
|
92
|
+
return true;
|
|
93
|
+
}
|
|
94
|
+
if (typeof a !== "object" || typeof b !== "object" || a === null || b === null) {
|
|
95
|
+
return false;
|
|
96
|
+
}
|
|
97
|
+
const aIsArray = Array.isArray(a);
|
|
98
|
+
if (aIsArray !== Array.isArray(b)) {
|
|
99
|
+
return false;
|
|
100
|
+
}
|
|
101
|
+
if (!aIsArray && !(isPlainObject(a) && isPlainObject(b))) {
|
|
102
|
+
return false;
|
|
103
|
+
}
|
|
104
|
+
if (aIsArray) {
|
|
105
|
+
const x2 = a;
|
|
106
|
+
const y2 = b;
|
|
107
|
+
return x2.length === y2.length && x2.every((item, i) => structuralEqual(item, y2[i]));
|
|
108
|
+
}
|
|
109
|
+
const x = a;
|
|
110
|
+
const y = b;
|
|
111
|
+
const keys = Object.keys(x);
|
|
112
|
+
if (keys.length !== Object.keys(y).length) {
|
|
113
|
+
return false;
|
|
114
|
+
}
|
|
115
|
+
return keys.every(
|
|
116
|
+
(k) => Object.prototype.hasOwnProperty.call(y, k) && structuralEqual(x[k], y[k])
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// src/CheckoutProvider.tsx
|
|
121
|
+
function identityValues(o) {
|
|
122
|
+
return IDENTITY_KEYS.map((k) => o[k] ?? null);
|
|
123
|
+
}
|
|
124
|
+
function pickUpdatable(o) {
|
|
125
|
+
const picked = {};
|
|
126
|
+
UPDATABLE_KEYS.forEach((k) => {
|
|
127
|
+
picked[k] = o[k];
|
|
128
|
+
});
|
|
129
|
+
return picked;
|
|
130
|
+
}
|
|
131
|
+
function CheckoutProvider(props) {
|
|
132
|
+
const { options, children } = props;
|
|
133
|
+
const identityRef = (0, import_react2.useRef)(null);
|
|
134
|
+
const values = identityValues(options);
|
|
135
|
+
if (identityRef.current === null) {
|
|
136
|
+
identityRef.current = { values, generation: 0 };
|
|
137
|
+
} else if (!structuralEqual(identityRef.current.values, values)) {
|
|
138
|
+
identityRef.current = { values, generation: identityRef.current.generation + 1 };
|
|
139
|
+
}
|
|
140
|
+
const key = identityRef.current.generation;
|
|
141
|
+
const [state, setState] = (0, import_react2.useState)({
|
|
142
|
+
key,
|
|
143
|
+
checkout: null,
|
|
144
|
+
status: "loading",
|
|
145
|
+
error: null
|
|
146
|
+
});
|
|
147
|
+
const optionsRef = (0, import_react2.useRef)(options);
|
|
148
|
+
optionsRef.current = options;
|
|
149
|
+
const onErrorRef = (0, import_react2.useRef)(props.onError);
|
|
150
|
+
onErrorRef.current = props.onError;
|
|
151
|
+
const onWarningRef = (0, import_react2.useRef)(props.onWarning);
|
|
152
|
+
onWarningRef.current = props.onWarning;
|
|
153
|
+
const appliedRef = (0, import_react2.useRef)(
|
|
154
|
+
pickUpdatable(options)
|
|
155
|
+
);
|
|
156
|
+
const destroyedRef = (0, import_react2.useRef)(/* @__PURE__ */ new WeakSet());
|
|
157
|
+
const subscribersRef = (0, import_react2.useRef)(/* @__PURE__ */ new Set());
|
|
158
|
+
const recordRef = (0, import_react2.useRef)({ buffer: [], delivered: /* @__PURE__ */ new WeakMap() });
|
|
159
|
+
const deliver = (0, import_react2.useCallback)((handler) => {
|
|
160
|
+
const record = recordRef.current;
|
|
161
|
+
let index = record.delivered.get(handler) ?? 0;
|
|
162
|
+
while (index < record.buffer.length) {
|
|
163
|
+
const w = record.buffer[index];
|
|
164
|
+
index += 1;
|
|
165
|
+
record.delivered.set(handler, index);
|
|
166
|
+
handler(w);
|
|
167
|
+
}
|
|
168
|
+
}, []);
|
|
169
|
+
const subscribeWarning = (0, import_react2.useCallback)(
|
|
170
|
+
(handler) => {
|
|
171
|
+
subscribersRef.current.add(handler);
|
|
172
|
+
deliver(handler);
|
|
173
|
+
return () => {
|
|
174
|
+
subscribersRef.current.delete(handler);
|
|
175
|
+
};
|
|
176
|
+
},
|
|
177
|
+
[deliver]
|
|
178
|
+
);
|
|
179
|
+
(0, import_react2.useEffect)(() => {
|
|
180
|
+
const forward = (w) => onWarningRef.current?.(w);
|
|
181
|
+
return subscribeWarning(forward);
|
|
182
|
+
}, [subscribeWarning]);
|
|
183
|
+
(0, import_react2.useEffect)(() => {
|
|
184
|
+
const controller = new AbortController();
|
|
185
|
+
let cancelled = false;
|
|
186
|
+
let instance = null;
|
|
187
|
+
const record = { buffer: [], delivered: /* @__PURE__ */ new WeakMap() };
|
|
188
|
+
recordRef.current = record;
|
|
189
|
+
appliedRef.current = pickUpdatable(optionsRef.current);
|
|
190
|
+
const onInstanceWarning = (w) => {
|
|
191
|
+
record.buffer.push(w);
|
|
192
|
+
subscribersRef.current.forEach((h) => deliver(h));
|
|
193
|
+
};
|
|
194
|
+
setState(
|
|
195
|
+
(prev) => prev.key === key && prev.status === "loading" && prev.checkout === null ? prev : { key, checkout: null, status: "loading", error: null }
|
|
196
|
+
);
|
|
197
|
+
(0, import_checkout_sdk_js.createCheckout)({ ...optionsRef.current, signal: controller.signal }).then(
|
|
198
|
+
(created) => {
|
|
199
|
+
if (cancelled) {
|
|
200
|
+
created.destroy();
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
instance = created;
|
|
204
|
+
created.on("warning", onInstanceWarning);
|
|
205
|
+
setState({ key, checkout: created, status: "ready", error: null });
|
|
206
|
+
},
|
|
207
|
+
(err) => {
|
|
208
|
+
if (cancelled) {
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
const error2 = err;
|
|
212
|
+
if (error2 && error2.code === import_checkout_sdk_js.ErrorCode.CANCELLED) {
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
setState({ key, checkout: null, status: "error", error: error2 });
|
|
216
|
+
onErrorRef.current?.(error2);
|
|
217
|
+
}
|
|
218
|
+
);
|
|
219
|
+
return () => {
|
|
220
|
+
cancelled = true;
|
|
221
|
+
controller.abort();
|
|
222
|
+
if (instance) {
|
|
223
|
+
destroyedRef.current.add(instance);
|
|
224
|
+
instance.off("warning", onInstanceWarning);
|
|
225
|
+
instance.destroy();
|
|
226
|
+
}
|
|
227
|
+
};
|
|
228
|
+
}, [key, deliver]);
|
|
229
|
+
const current = state.key === key ? state.checkout : null;
|
|
230
|
+
(0, import_react2.useEffect)(() => {
|
|
231
|
+
if (!current) {
|
|
232
|
+
return;
|
|
233
|
+
}
|
|
234
|
+
const next = pickUpdatable(optionsRef.current);
|
|
235
|
+
const patch = {};
|
|
236
|
+
const sent = [];
|
|
237
|
+
let advanced = false;
|
|
238
|
+
for (const k of UPDATABLE_KEYS) {
|
|
239
|
+
if (!structuralEqual(appliedRef.current[k], next[k])) {
|
|
240
|
+
advanced = true;
|
|
241
|
+
if (next[k] !== void 0) {
|
|
242
|
+
patch[k] = next[k];
|
|
243
|
+
sent.push(k);
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
if (!advanced) {
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
const previous = appliedRef.current;
|
|
251
|
+
appliedRef.current = next;
|
|
252
|
+
if (sent.length === 0) {
|
|
253
|
+
return;
|
|
254
|
+
}
|
|
255
|
+
const onFailure = (failure) => {
|
|
256
|
+
if (destroyedRef.current.has(current)) {
|
|
257
|
+
return;
|
|
258
|
+
}
|
|
259
|
+
const restored = { ...appliedRef.current };
|
|
260
|
+
for (const k of sent) {
|
|
261
|
+
if (structuralEqual(appliedRef.current[k], next[k])) {
|
|
262
|
+
restored[k] = previous[k];
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
appliedRef.current = restored;
|
|
266
|
+
if (isDevelopment) {
|
|
267
|
+
const code = failure?.code;
|
|
268
|
+
console.warn(
|
|
269
|
+
`[checkout-sdk-react] updateCheckout failed (code: ${typeof code === "string" ? code : "UNKNOWN"}). It will be retried on the next render.`
|
|
270
|
+
);
|
|
271
|
+
}
|
|
272
|
+
};
|
|
273
|
+
let pending;
|
|
274
|
+
try {
|
|
275
|
+
pending = Promise.resolve(current.updateCheckout(patch));
|
|
276
|
+
} catch (failure) {
|
|
277
|
+
pending = Promise.reject(failure);
|
|
278
|
+
}
|
|
279
|
+
pending.catch(onFailure);
|
|
280
|
+
});
|
|
281
|
+
const initialPaymentMethods = (0, import_react2.useRef)(options.paymentMethods);
|
|
282
|
+
const warnedRef = (0, import_react2.useRef)(false);
|
|
283
|
+
(0, import_react2.useEffect)(() => {
|
|
284
|
+
if (!warnedRef.current && isDevelopment && !structuralEqual(initialPaymentMethods.current, options.paymentMethods)) {
|
|
285
|
+
warnedRef.current = true;
|
|
286
|
+
console.warn(
|
|
287
|
+
"[checkout-sdk-react] `paymentMethods` is read only when the checkout is created. Changing it later has no effect."
|
|
288
|
+
);
|
|
289
|
+
}
|
|
290
|
+
});
|
|
291
|
+
const stale = state.key !== key;
|
|
292
|
+
const checkout = stale ? null : state.checkout;
|
|
293
|
+
const status = stale ? "loading" : state.status;
|
|
294
|
+
const error = stale ? null : state.error;
|
|
295
|
+
const value = (0, import_react2.useMemo)(
|
|
296
|
+
() => ({ checkout, status, error, subscribeWarning }),
|
|
297
|
+
[checkout, status, error, subscribeWarning]
|
|
298
|
+
);
|
|
299
|
+
return /* @__PURE__ */ React.createElement(CheckoutContext.Provider, { value }, children);
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
// src/hooks/useCheckout.ts
|
|
303
|
+
var import_react3 = require("react");
|
|
304
|
+
function useCheckout() {
|
|
305
|
+
const ctx = (0, import_react3.useContext)(CheckoutContext);
|
|
306
|
+
if (!ctx) {
|
|
307
|
+
throw new Error("useCheckout must be used within <CheckoutProvider>");
|
|
308
|
+
}
|
|
309
|
+
return { checkout: ctx.checkout, status: ctx.status, error: ctx.error };
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
// src/hooks/useCheckoutEvent.ts
|
|
313
|
+
var import_react4 = require("react");
|
|
314
|
+
function useCheckoutEvent(name, handler) {
|
|
315
|
+
const ctx = (0, import_react4.useContext)(CheckoutContext);
|
|
316
|
+
if (!ctx) {
|
|
317
|
+
throw new Error("useCheckoutEvent must be used within <CheckoutProvider>");
|
|
318
|
+
}
|
|
319
|
+
const { checkout, subscribeWarning } = ctx;
|
|
320
|
+
const handlerRef = (0, import_react4.useRef)(handler);
|
|
321
|
+
(0, import_react4.useEffect)(() => {
|
|
322
|
+
handlerRef.current = handler;
|
|
323
|
+
});
|
|
324
|
+
(0, import_react4.useEffect)(() => {
|
|
325
|
+
if (name === "warning") {
|
|
326
|
+
return subscribeWarning((w) => handlerRef.current(w));
|
|
327
|
+
}
|
|
328
|
+
if (!checkout) {
|
|
329
|
+
return void 0;
|
|
330
|
+
}
|
|
331
|
+
const listener = (payload) => handlerRef.current(payload);
|
|
332
|
+
checkout.on(name, listener);
|
|
333
|
+
return () => {
|
|
334
|
+
checkout.off(name, listener);
|
|
335
|
+
};
|
|
336
|
+
}, [checkout, name, subscribeWarning]);
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
// src/elements/createElementComponent.tsx
|
|
340
|
+
var React2 = __toESM(require("react"), 1);
|
|
341
|
+
var import_react5 = require("react");
|
|
342
|
+
var import_checkout_sdk_js2 = require("@sunbay/checkout-sdk-js");
|
|
343
|
+
|
|
344
|
+
// src/elements/updatableOptions.ts
|
|
345
|
+
var UPDATABLE_OPTION_KEYS = {
|
|
346
|
+
card: [],
|
|
347
|
+
cardNumber: [],
|
|
348
|
+
cardExpiry: [],
|
|
349
|
+
cardCvc: [],
|
|
350
|
+
address: [],
|
|
351
|
+
expressCheckout: ["paymentMethods", "layout", "buttonHeight"]
|
|
352
|
+
};
|
|
353
|
+
|
|
354
|
+
// src/elements/createElementComponent.tsx
|
|
355
|
+
function codeOf(e) {
|
|
356
|
+
const code = e?.code;
|
|
357
|
+
return typeof code === "string" ? code : "UNKNOWN";
|
|
358
|
+
}
|
|
359
|
+
function assignRef(ref, value) {
|
|
360
|
+
if (typeof ref === "function") {
|
|
361
|
+
ref(value);
|
|
362
|
+
} else if (ref) {
|
|
363
|
+
ref.current = value;
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
function recreateKeyOf(options, updatable) {
|
|
367
|
+
const entries = Object.keys(options ?? {}).filter((k) => !updatable.includes(k) && options?.[k] !== void 0).sort().map((k) => [k, options?.[k]]);
|
|
368
|
+
return JSON.stringify(entries);
|
|
369
|
+
}
|
|
370
|
+
function createElementComponent(type, displayName) {
|
|
371
|
+
const updatable = UPDATABLE_OPTION_KEYS[type];
|
|
372
|
+
const Component = (0, import_react5.forwardRef)(function ElementComponent(props, ref) {
|
|
373
|
+
const ctx = (0, import_react5.useContext)(CheckoutContext);
|
|
374
|
+
const checkout = ctx ? ctx.checkout : null;
|
|
375
|
+
const divRef = (0, import_react5.useRef)(null);
|
|
376
|
+
const propsRef = (0, import_react5.useRef)(props);
|
|
377
|
+
propsRef.current = props;
|
|
378
|
+
const refRef = (0, import_react5.useRef)(ref);
|
|
379
|
+
refRef.current = ref;
|
|
380
|
+
const appliedRef = (0, import_react5.useRef)({});
|
|
381
|
+
const elementRef = (0, import_react5.useRef)(null);
|
|
382
|
+
const assignedRefRef = (0, import_react5.useRef)(null);
|
|
383
|
+
const [generation, setGeneration] = (0, import_react5.useState)(0);
|
|
384
|
+
const recreateKey = recreateKeyOf(props.options, updatable);
|
|
385
|
+
(0, import_react5.useEffect)(() => {
|
|
386
|
+
const div = divRef.current;
|
|
387
|
+
if (!checkout || !div) {
|
|
388
|
+
return;
|
|
389
|
+
}
|
|
390
|
+
let cancelled = false;
|
|
391
|
+
const reportedIds = /* @__PURE__ */ new Set();
|
|
392
|
+
const reportedErrors = /* @__PURE__ */ new WeakSet();
|
|
393
|
+
let loadErrorReported = false;
|
|
394
|
+
const report = (error, source) => {
|
|
395
|
+
if (cancelled || source === "mount" && loadErrorReported) {
|
|
396
|
+
return;
|
|
397
|
+
}
|
|
398
|
+
const errorId = error?.errorId;
|
|
399
|
+
if (typeof errorId === "string") {
|
|
400
|
+
if (reportedIds.has(errorId)) {
|
|
401
|
+
return;
|
|
402
|
+
}
|
|
403
|
+
reportedIds.add(errorId);
|
|
404
|
+
} else if (error !== null && typeof error === "object") {
|
|
405
|
+
if (reportedErrors.has(error)) {
|
|
406
|
+
return;
|
|
407
|
+
}
|
|
408
|
+
reportedErrors.add(error);
|
|
409
|
+
}
|
|
410
|
+
if (source === "event") {
|
|
411
|
+
loadErrorReported = true;
|
|
412
|
+
}
|
|
413
|
+
if (codeOf(error) === import_checkout_sdk_js2.ErrorCode.ELEMENT_CONFLICT && isDevelopment) {
|
|
414
|
+
console.error(
|
|
415
|
+
`[checkout-sdk-react] <${displayName}> could not be created (code: ELEMENT_CONFLICT). Card Elements are mutually exclusive.`
|
|
416
|
+
);
|
|
417
|
+
}
|
|
418
|
+
propsRef.current.onLoadError?.(error);
|
|
419
|
+
};
|
|
420
|
+
let element;
|
|
421
|
+
try {
|
|
422
|
+
element = checkout.create(type, propsRef.current.options);
|
|
423
|
+
} catch (error) {
|
|
424
|
+
report(error, "create");
|
|
425
|
+
return;
|
|
426
|
+
}
|
|
427
|
+
appliedRef.current = { ...propsRef.current.options ?? {} };
|
|
428
|
+
elementRef.current = element;
|
|
429
|
+
assignedRefRef.current = refRef.current;
|
|
430
|
+
assignRef(refRef.current, element);
|
|
431
|
+
element.on("elementReady", (e) => propsRef.current.onReady?.(e));
|
|
432
|
+
element.on("change", (e) => propsRef.current.onChange?.(e));
|
|
433
|
+
element.on("focus", (e) => propsRef.current.onFocus?.(e));
|
|
434
|
+
element.on("blur", (e) => propsRef.current.onBlur?.(e));
|
|
435
|
+
element.on("loadError", (e) => report(e.error, "event"));
|
|
436
|
+
if (type === "expressCheckout") {
|
|
437
|
+
element.on(
|
|
438
|
+
"expressAvailabilityChange",
|
|
439
|
+
(e) => propsRef.current.onAvailabilityChange?.(e.availableWallets)
|
|
440
|
+
);
|
|
441
|
+
}
|
|
442
|
+
element.mount(div).catch((error) => {
|
|
443
|
+
const code = codeOf(error);
|
|
444
|
+
if (code === import_checkout_sdk_js2.ErrorCode.CANCELLED || code === import_checkout_sdk_js2.ErrorCode.INSTANCE_DESTROYED) {
|
|
445
|
+
return;
|
|
446
|
+
}
|
|
447
|
+
report(error, "mount");
|
|
448
|
+
});
|
|
449
|
+
return () => {
|
|
450
|
+
cancelled = true;
|
|
451
|
+
elementRef.current = null;
|
|
452
|
+
assignRef(assignedRefRef.current, null);
|
|
453
|
+
assignedRefRef.current = null;
|
|
454
|
+
element.destroy();
|
|
455
|
+
};
|
|
456
|
+
}, [checkout, recreateKey, generation]);
|
|
457
|
+
(0, import_react5.useEffect)(() => {
|
|
458
|
+
const assigned = assignedRefRef.current;
|
|
459
|
+
if (!assigned || assigned === ref) {
|
|
460
|
+
return;
|
|
461
|
+
}
|
|
462
|
+
assignRef(assigned, null);
|
|
463
|
+
assignRef(ref, elementRef.current);
|
|
464
|
+
assignedRefRef.current = ref;
|
|
465
|
+
}, [ref]);
|
|
466
|
+
(0, import_react5.useEffect)(() => {
|
|
467
|
+
const element = elementRef.current;
|
|
468
|
+
if (!element) {
|
|
469
|
+
return;
|
|
470
|
+
}
|
|
471
|
+
const next = propsRef.current.options ?? {};
|
|
472
|
+
const applied = appliedRef.current;
|
|
473
|
+
const patch = {};
|
|
474
|
+
let changed = false;
|
|
475
|
+
let removed = false;
|
|
476
|
+
updatable.forEach((k) => {
|
|
477
|
+
if (structuralEqual(applied[k], next[k])) {
|
|
478
|
+
return;
|
|
479
|
+
}
|
|
480
|
+
changed = true;
|
|
481
|
+
if (next[k] === void 0) {
|
|
482
|
+
removed = true;
|
|
483
|
+
} else {
|
|
484
|
+
patch[k] = next[k];
|
|
485
|
+
}
|
|
486
|
+
});
|
|
487
|
+
if (!changed) {
|
|
488
|
+
return;
|
|
489
|
+
}
|
|
490
|
+
if (removed) {
|
|
491
|
+
setGeneration((g) => g + 1);
|
|
492
|
+
return;
|
|
493
|
+
}
|
|
494
|
+
appliedRef.current = { ...next };
|
|
495
|
+
const onFailure = (failure) => {
|
|
496
|
+
if (elementRef.current !== element) {
|
|
497
|
+
return;
|
|
498
|
+
}
|
|
499
|
+
if (isDevelopment) {
|
|
500
|
+
console.warn(
|
|
501
|
+
`[checkout-sdk-react] <${displayName}> update failed (code: ${codeOf(failure)}). The Element is recreated with the current options.`
|
|
502
|
+
);
|
|
503
|
+
}
|
|
504
|
+
setGeneration((g) => g + 1);
|
|
505
|
+
};
|
|
506
|
+
try {
|
|
507
|
+
Promise.resolve(element.update(patch)).catch(onFailure);
|
|
508
|
+
} catch (failure) {
|
|
509
|
+
onFailure(failure);
|
|
510
|
+
}
|
|
511
|
+
});
|
|
512
|
+
return /* @__PURE__ */ React2.createElement("div", { ref: divRef, id: props.id, className: props.className, style: props.style });
|
|
513
|
+
});
|
|
514
|
+
Component.displayName = displayName;
|
|
515
|
+
return Component;
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
// src/elements/index.tsx
|
|
519
|
+
var CardElement = createElementComponent("card", "CardElement");
|
|
520
|
+
var CardNumberElement = createElementComponent("cardNumber", "CardNumberElement");
|
|
521
|
+
var CardExpiryElement = createElementComponent("cardExpiry", "CardExpiryElement");
|
|
522
|
+
var CardCvcElement = createElementComponent("cardCvc", "CardCvcElement");
|
|
523
|
+
var AddressElement = createElementComponent("address", "AddressElement");
|
|
524
|
+
var ExpressCheckoutElement = createElementComponent("expressCheckout", "ExpressCheckoutElement");
|
|
525
|
+
|
|
526
|
+
// src/hooks/useConfirm.ts
|
|
527
|
+
var import_react6 = require("react");
|
|
528
|
+
function notReadyError() {
|
|
529
|
+
const rand = Math.random().toString(36).slice(2, 10) || "0";
|
|
530
|
+
return {
|
|
531
|
+
code: "INVALID_OPTIONS",
|
|
532
|
+
category: "configuration",
|
|
533
|
+
message: "Checkout is not ready",
|
|
534
|
+
errorId: `err_${Date.now().toString(36)}_${rand}`
|
|
535
|
+
};
|
|
536
|
+
}
|
|
537
|
+
function useConfirm() {
|
|
538
|
+
const { checkout } = useCheckout();
|
|
539
|
+
const checkoutRef = (0, import_react6.useRef)(checkout);
|
|
540
|
+
(0, import_react6.useEffect)(() => {
|
|
541
|
+
checkoutRef.current = checkout;
|
|
542
|
+
}, [checkout]);
|
|
543
|
+
const mounted = (0, import_react6.useRef)(true);
|
|
544
|
+
(0, import_react6.useEffect)(() => {
|
|
545
|
+
mounted.current = true;
|
|
546
|
+
return () => {
|
|
547
|
+
mounted.current = false;
|
|
548
|
+
};
|
|
549
|
+
}, []);
|
|
550
|
+
const activeCall = (0, import_react6.useRef)(0);
|
|
551
|
+
const nextCall = (0, import_react6.useRef)(0);
|
|
552
|
+
const [isConfirming, setIsConfirming] = (0, import_react6.useState)(false);
|
|
553
|
+
const [outcome, setOutcome] = (0, import_react6.useState)(null);
|
|
554
|
+
const [error, setError] = (0, import_react6.useState)(null);
|
|
555
|
+
const confirm = (0, import_react6.useCallback)((opts) => {
|
|
556
|
+
const instance = checkoutRef.current;
|
|
557
|
+
const owner = activeCall.current === 0;
|
|
558
|
+
if (owner && mounted.current) {
|
|
559
|
+
setOutcome(null);
|
|
560
|
+
setError(null);
|
|
561
|
+
}
|
|
562
|
+
if (!instance) {
|
|
563
|
+
const err = notReadyError();
|
|
564
|
+
if (owner && mounted.current) {
|
|
565
|
+
setError(err);
|
|
566
|
+
}
|
|
567
|
+
return Promise.reject(err);
|
|
568
|
+
}
|
|
569
|
+
const id = ++nextCall.current;
|
|
570
|
+
let promise;
|
|
571
|
+
try {
|
|
572
|
+
promise = instance.confirm(opts);
|
|
573
|
+
} catch (e) {
|
|
574
|
+
promise = Promise.reject(e);
|
|
575
|
+
}
|
|
576
|
+
if (owner) {
|
|
577
|
+
activeCall.current = id;
|
|
578
|
+
if (mounted.current) {
|
|
579
|
+
setIsConfirming(true);
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
const finish = () => {
|
|
583
|
+
if (owner && activeCall.current === id) {
|
|
584
|
+
activeCall.current = 0;
|
|
585
|
+
if (mounted.current) {
|
|
586
|
+
setIsConfirming(false);
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
};
|
|
590
|
+
return promise.then(
|
|
591
|
+
(result) => {
|
|
592
|
+
finish();
|
|
593
|
+
if (owner && mounted.current) {
|
|
594
|
+
setOutcome(result);
|
|
595
|
+
}
|
|
596
|
+
return result;
|
|
597
|
+
},
|
|
598
|
+
(err) => {
|
|
599
|
+
finish();
|
|
600
|
+
if (owner && mounted.current) {
|
|
601
|
+
setError(err);
|
|
602
|
+
}
|
|
603
|
+
throw err;
|
|
604
|
+
}
|
|
605
|
+
);
|
|
606
|
+
}, []);
|
|
607
|
+
return { confirm, isConfirming, outcome, error };
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
// src/index.ts
|
|
611
|
+
var import_checkout_sdk_js3 = require("@sunbay/checkout-sdk-js");
|
|
612
|
+
var version = true ? "0.5.0" : "0.0.0";
|
|
613
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
614
|
+
0 && (module.exports = {
|
|
615
|
+
AddressElement,
|
|
616
|
+
CardCvcElement,
|
|
617
|
+
CardElement,
|
|
618
|
+
CardExpiryElement,
|
|
619
|
+
CardNumberElement,
|
|
620
|
+
CheckoutProvider,
|
|
621
|
+
ErrorCode,
|
|
622
|
+
ExpressCheckoutElement,
|
|
623
|
+
WARNING_CODES,
|
|
624
|
+
useCheckout,
|
|
625
|
+
useCheckoutEvent,
|
|
626
|
+
useConfirm,
|
|
627
|
+
version
|
|
628
|
+
});
|
|
629
|
+
//# sourceMappingURL=index.cjs.map
|