@mainmoneyapp/js-checkout 0.1.5 → 0.2.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/dist/index.d.ts CHANGED
@@ -43,6 +43,13 @@ type CreateCheckoutOptions = {
43
43
  * reference and Idempotency-Key suffix.
44
44
  */
45
45
  onRetry?: () => void | Promise<void>;
46
+ /**
47
+ * Also send the reference as an `Idempotency-Key` header on deposits and payouts. Off by default: a custom header
48
+ * makes the browser preflight list it in `Access-Control-Allow-Headers`, and a merchant backend that does not
49
+ * allow it fails every payment with a CORS error. The `reference` in the body already makes the call idempotent;
50
+ * turn this on only when your backend allows the header (or forwards it to the aggregator).
51
+ */
52
+ idempotencyHeader?: boolean;
46
53
  /** Polling interval; defaults to 1000 ms. */
47
54
  pollIntervalMs?: number;
48
55
  /** After this long without a terminal status the widget flags `slow` (default 30000 ms) but keeps polling. */
package/dist/index.js CHANGED
@@ -301,6 +301,8 @@ function createCheckout(session, options) {
301
301
  state.highlightedProviderCode = void 0;
302
302
  state.customerName = null;
303
303
  lastMatchedIdentifier = "";
304
+ lookupSeq += 1;
305
+ lookup = void 0;
304
306
  state.providers = paginated(payload);
305
307
  state.quoteUnavailable = false;
306
308
  if (!state.lockCurrency) {
@@ -362,36 +364,67 @@ function createCheckout(session, options) {
362
364
  translate = createTranslator(locale, catalogOverrides);
363
365
  notify();
364
366
  };
365
- const matchProvider = async () => {
366
- const trimmed = state.identifier.trim();
367
- if (trimmed === "") {
368
- return;
369
- }
370
- if (trimmed === lastMatchedIdentifier) {
371
- return;
372
- }
367
+ let lookup;
368
+ let lookupSeq = 0;
369
+ const runMatch = async (trimmed, key) => {
370
+ const seq = ++lookupSeq;
371
+ let payload;
373
372
  try {
374
- const payload = await http.get(session.paths.matchProvider, {
373
+ payload = await http.get(session.paths.matchProvider, {
375
374
  account_number: e164OrIdentifier(),
376
375
  get_lookup: true,
377
376
  operation_type: routingOperationType()
378
377
  });
379
- const entity = typeof payload.entity === "string" ? payload.entity : void 0;
380
- if (entity !== void 0) {
381
- state.highlightedProviderCode = entity;
382
- const matched = state.providers.find((item) => item.code === entity);
383
- if (matched !== void 0) {
384
- state.selectedProvider = matched;
385
- applyProviderCurrency(matched);
386
- }
378
+ } catch (error) {
379
+ if (seq === lookupSeq) {
380
+ fail(error);
387
381
  }
388
- state.customerName = extractCustomerName(payload);
389
- lastMatchedIdentifier = trimmed;
382
+ return;
383
+ }
384
+ if (seq !== lookupSeq || key !== lookupKey()) {
385
+ return;
386
+ }
387
+ const entity = typeof payload.entity === "string" ? payload.entity : void 0;
388
+ if (entity !== void 0) {
389
+ state.highlightedProviderCode = entity;
390
+ const matched = state.providers.find((item) => item.code === entity);
391
+ if (matched !== void 0) {
392
+ state.selectedProvider = matched;
393
+ applyProviderCurrency(matched);
394
+ }
395
+ }
396
+ state.customerName = extractCustomerName(payload);
397
+ lastMatchedIdentifier = trimmed;
398
+ notify();
399
+ try {
390
400
  await refreshLimitsAndFees();
391
- notify();
392
401
  } catch (error) {
393
- fail(error);
402
+ if (seq === lookupSeq) {
403
+ fail(error);
404
+ return;
405
+ }
406
+ }
407
+ if (seq === lookupSeq) {
408
+ notify();
409
+ }
410
+ };
411
+ const lookupKey = () => `${state.selectedCountry?.code ?? ""}:${e164OrIdentifier()}`;
412
+ const matchProvider = () => {
413
+ const trimmed = state.identifier.trim();
414
+ if (trimmed === "" || trimmed === lastMatchedIdentifier) {
415
+ return Promise.resolve();
394
416
  }
417
+ const key = lookupKey();
418
+ if (lookup !== void 0 && lookup.key === key) {
419
+ return lookup.promise;
420
+ }
421
+ const promise = runMatch(trimmed, key).finally(() => {
422
+ if (lookup?.promise === promise) {
423
+ lookup = void 0;
424
+ }
425
+ });
426
+ lookup = { key, promise };
427
+ return promise;
395
428
  };
396
429
  const setCurrency = async (code) => {
397
430
  if (state.lockCurrency) {
@@ -588,9 +621,7 @@ function createCheckout(session, options) {
588
621
  if (fees !== "") {
589
622
  payload.fees = fees;
590
623
  }
591
- created = await http.post(session.paths.deposits, payload, {
592
- "Idempotency-Key": reference
593
- });
624
+ created = await http.post(session.paths.deposits, payload, idempotencyHeaders(reference));
594
625
  } else {
595
626
  created = await http.post(session.paths.payouts, {
596
627
  provider_code: state.selectedProvider.code,
@@ -598,7 +629,7 @@ function createCheckout(session, options) {
598
629
  amount: state.amount,
599
630
  currency: state.currency,
600
631
  destination_account: e164OrIdentifier()
601
- }, { "Idempotency-Key": reference });
632
+ }, idempotencyHeaders(reference));
602
633
  }
603
634
  state.status = created;
604
635
  if (typeof created.status === "string" && isTerminalStatus2(created.status)) {
@@ -661,6 +692,7 @@ function createCheckout(session, options) {
661
692
  fail(error);
662
693
  }
663
694
  };
695
+ const idempotencyHeaders = (reference) => options.idempotencyHeader === true ? { "Idempotency-Key": reference } : {};
664
696
  const retry = async () => {
665
697
  if (options.onRetry !== void 0) {
666
698
  await options.onRetry();
@@ -991,7 +1023,7 @@ function phoneField(checkout, state) {
991
1023
  });
992
1024
  input.addEventListener("blur", () => {
993
1025
  clearTimeout(timer);
994
- if (input.value.trim() !== "") {
1026
+ if (input.isConnected && input.value.trim() !== "") {
995
1027
  runLookup();
996
1028
  }
997
1029
  });
@@ -1454,7 +1486,9 @@ function identifierField(checkout, state, phone) {
1454
1486
  input.value = state.identifier;
1455
1487
  input.addEventListener("input", () => checkout.setIdentifier(input.value));
1456
1488
  input.addEventListener("blur", () => {
1457
- run(() => checkout.matchProvider());
1489
+ if (input.isConnected) {
1490
+ run(() => checkout.matchProvider());
1491
+ }
1458
1492
  });
1459
1493
  if (phone && state.selectedCountry?.phone_code) {
1460
1494
  const row = document.createElement("div");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mainmoneyapp/js-checkout",
3
- "version": "0.1.5",
3
+ "version": "0.2.0",
4
4
  "description": "Checkout wizard for the MainMoney JS/TS frontend SDK",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -41,7 +41,7 @@
41
41
  "typecheck": "tsc --noEmit -p tsconfig.json"
42
42
  },
43
43
  "dependencies": {
44
- "@mainmoneyapp/js-core": "0.1.5",
45
- "@mainmoneyapp/js-http": "0.1.5"
44
+ "@mainmoneyapp/js-core": "0.2.0",
45
+ "@mainmoneyapp/js-http": "0.2.0"
46
46
  }
47
47
  }