@delopay/sdk 0.70.0 → 0.71.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.cjs CHANGED
@@ -20,12 +20,15 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // src/index.ts
21
21
  var index_exports = {};
22
22
  __export(index_exports, {
23
+ ALL_CUSTOM_FIELD_TYPES: () => ALL_CUSTOM_FIELD_TYPES,
23
24
  Analytics: () => Analytics,
24
25
  AnalyticsDashboard: () => AnalyticsDashboard,
25
26
  AvailabilityOverrides: () => AvailabilityOverrides,
26
27
  BRANDING_EXPORT_FORMAT: () => BRANDING_EXPORT_FORMAT,
27
28
  BRANDING_EXPORT_VERSION: () => BRANDING_EXPORT_VERSION,
28
29
  CUSTOM_CSS_MAX_LENGTH: () => CUSTOM_CSS_MAX_LENGTH,
30
+ CUSTOM_FIELDS_MAX: () => CUSTOM_FIELDS_MAX,
31
+ CUSTOM_FIELD_KEY_PATTERN: () => CUSTOM_FIELD_KEY_PATTERN,
29
32
  Cards: () => Cards,
30
33
  DEFAULT_BADGES: () => DEFAULT_BADGES,
31
34
  DEFAULT_BADGES_DARK: () => DEFAULT_BADGES_DARK,
@@ -49,11 +52,16 @@ __export(index_exports, {
49
52
  buildBrandingExport: () => buildBrandingExport,
50
53
  buttonPadValue: () => buttonPadValue,
51
54
  cloneBranding: () => cloneBranding,
55
+ cloneCustomField: () => cloneCustomField,
56
+ customFieldOptionLabel: () => customFieldOptionLabel,
57
+ customFieldText: () => customFieldText,
52
58
  decodeBadges: () => decodeBadges,
53
59
  decodeBranding: () => decodeBranding,
60
+ decodeCustomFields: () => decodeCustomFields,
54
61
  defaultBranding: () => defaultBranding,
55
62
  encodeBadges: () => encodeBadges,
56
63
  encodeBranding: () => encodeBranding,
64
+ encodeCustomFields: () => encodeCustomFields,
57
65
  feeProgram: () => feeProgram,
58
66
  fontStack: () => fontStack,
59
67
  fontWeightValue: () => fontWeightValue,
@@ -62,6 +70,7 @@ __export(index_exports, {
62
70
  isHexColor: () => isHexColor,
63
71
  leaf: () => leaf,
64
72
  logoDimensions: () => logoDimensions,
73
+ parseCustomFieldsLoose: () => parseCustomFieldsLoose,
65
74
  parseImportedBranding: () => parseImportedBranding,
66
75
  programToTree: () => programToTree,
67
76
  radiusValue: () => radiusValue,
@@ -3947,7 +3956,8 @@ var DEFAULT_BRANDING_BASE = {
3947
3956
  var DEFAULT_BRANDING = {
3948
3957
  ...DEFAULT_BRANDING_BASE,
3949
3958
  ...LIGHT_PALETTE,
3950
- trustBadges: DEFAULT_BADGES.map((b) => ({ ...b }))
3959
+ trustBadges: DEFAULT_BADGES.map((b) => ({ ...b })),
3960
+ customFields: []
3951
3961
  };
3952
3962
  var DEFAULT_BRANDING_DARK = {
3953
3963
  ...DEFAULT_BRANDING_BASE,
@@ -3962,13 +3972,27 @@ var DEFAULT_BRANDING_DARK = {
3962
3972
  buttonBackground: "#1E4FEB",
3963
3973
  buttonText: "#ffffff",
3964
3974
  surfaceStyle: "flat",
3965
- trustBadges: DEFAULT_BADGES_DARK.map((b) => ({ ...b }))
3975
+ trustBadges: DEFAULT_BADGES_DARK.map((b) => ({ ...b })),
3976
+ customFields: []
3966
3977
  };
3967
3978
  function defaultBranding() {
3968
3979
  return cloneBranding(DEFAULT_BRANDING);
3969
3980
  }
3970
3981
  function cloneBranding(b) {
3971
- return { ...b, trustBadges: b.trustBadges.map((badge) => ({ ...badge })) };
3982
+ return {
3983
+ ...b,
3984
+ trustBadges: b.trustBadges.map((badge) => ({ ...badge })),
3985
+ customFields: b.customFields.map(cloneCustomField)
3986
+ };
3987
+ }
3988
+ function cloneCustomField(f) {
3989
+ return {
3990
+ ...f,
3991
+ labelTranslations: { ...f.labelTranslations },
3992
+ placeholderTranslations: { ...f.placeholderTranslations },
3993
+ helpTextTranslations: { ...f.helpTextTranslations },
3994
+ options: f.options.map((o) => ({ ...o, labelTranslations: { ...o.labelTranslations } }))
3995
+ };
3972
3996
  }
3973
3997
  var CUSTOM_CSS_MAX_LENGTH = 5e4;
3974
3998
  function sanitizeCustomCss(raw) {
@@ -4046,10 +4070,135 @@ function encodeBadges(badges) {
4046
4070
  }))
4047
4071
  );
4048
4072
  }
4073
+ var CUSTOM_FIELDS_MAX = 20;
4074
+ var CUSTOM_FIELD_KEY_PATTERN = /^[A-Za-z][A-Za-z0-9_-]{0,39}$/;
4075
+ var ALL_CUSTOM_FIELD_TYPES = [
4076
+ "text",
4077
+ "textarea",
4078
+ "password",
4079
+ "email",
4080
+ "select"
4081
+ ];
4082
+ function parseTranslations(raw) {
4083
+ if (!raw || typeof raw !== "object" || Array.isArray(raw)) return {};
4084
+ const out = {};
4085
+ for (const [k, v] of Object.entries(raw)) {
4086
+ if (typeof v === "string" && v.length > 0) out[k] = v;
4087
+ }
4088
+ return out;
4089
+ }
4090
+ function parseBoundedInt(raw) {
4091
+ const n = typeof raw === "number" ? raw : typeof raw === "string" ? Number(raw) : NaN;
4092
+ if (!Number.isInteger(n) || n < 0) return null;
4093
+ return Math.min(n, 5e3);
4094
+ }
4095
+ function normalizeCustomField(raw, index) {
4096
+ if (!raw || typeof raw !== "object" || Array.isArray(raw)) return null;
4097
+ const f = raw;
4098
+ const key = typeof f["key"] === "string" ? f["key"].trim() : "";
4099
+ if (!CUSTOM_FIELD_KEY_PATTERN.test(key)) return null;
4100
+ const type = pickEnum(f["type"], ALL_CUSTOM_FIELD_TYPES, "text");
4101
+ const options = type === "select" && Array.isArray(f["options"]) ? f["options"].filter((o) => !!o && typeof o === "object").map((o) => {
4102
+ const value = typeof o["value"] === "string" ? o["value"] : "";
4103
+ return {
4104
+ value,
4105
+ label: typeof o["label"] === "string" && o["label"] ? o["label"] : value,
4106
+ labelTranslations: parseTranslations(o["labelTranslations"])
4107
+ };
4108
+ }).filter((o) => o.value.length > 0) : [];
4109
+ const minLength = parseBoundedInt(f["minLength"]);
4110
+ const maxLength = parseBoundedInt(f["maxLength"]);
4111
+ return {
4112
+ id: typeof f["id"] === "string" && f["id"] ? f["id"] : `field-${index}`,
4113
+ key,
4114
+ type,
4115
+ label: typeof f["label"] === "string" && f["label"] ? f["label"] : key,
4116
+ labelTranslations: parseTranslations(f["labelTranslations"]),
4117
+ placeholder: typeof f["placeholder"] === "string" ? f["placeholder"] : "",
4118
+ placeholderTranslations: parseTranslations(f["placeholderTranslations"]),
4119
+ helpText: typeof f["helpText"] === "string" ? f["helpText"] : "",
4120
+ helpTextTranslations: parseTranslations(f["helpTextTranslations"]),
4121
+ required: parseBool(f["required"], false),
4122
+ enabled: parseBool(f["enabled"], true),
4123
+ minLength,
4124
+ // Guard inverted bounds at decode so consumers never see min > max.
4125
+ maxLength: maxLength !== null && minLength !== null && maxLength < minLength ? null : maxLength,
4126
+ defaultValue: typeof f["defaultValue"] === "string" ? f["defaultValue"] : "",
4127
+ options
4128
+ };
4129
+ }
4130
+ function parseCustomFieldsLoose(raw) {
4131
+ if (!Array.isArray(raw)) return null;
4132
+ const seen = /* @__PURE__ */ new Set();
4133
+ const out = [];
4134
+ for (let i = 0; i < raw.length && out.length < CUSTOM_FIELDS_MAX; i++) {
4135
+ const field = normalizeCustomField(raw[i], i);
4136
+ if (!field || seen.has(field.key)) continue;
4137
+ seen.add(field.key);
4138
+ out.push(field);
4139
+ }
4140
+ return out;
4141
+ }
4142
+ function decodeCustomFields(raw) {
4143
+ if (raw === void 0) return null;
4144
+ try {
4145
+ return parseCustomFieldsLoose(JSON.parse(raw));
4146
+ } catch {
4147
+ return null;
4148
+ }
4149
+ }
4150
+ function encodeCustomFields(fields) {
4151
+ const nonEmpty = (m) => {
4152
+ const entries = Object.entries(m).filter(([, v]) => v.trim().length > 0);
4153
+ return entries.length > 0 ? Object.fromEntries(entries) : void 0;
4154
+ };
4155
+ return JSON.stringify(
4156
+ fields.map((f) => ({
4157
+ id: f.id,
4158
+ key: f.key,
4159
+ type: f.type,
4160
+ label: f.label,
4161
+ ...nonEmpty(f.labelTranslations) ? { labelTranslations: nonEmpty(f.labelTranslations) } : {},
4162
+ ...f.placeholder ? { placeholder: f.placeholder } : {},
4163
+ ...nonEmpty(f.placeholderTranslations) ? { placeholderTranslations: nonEmpty(f.placeholderTranslations) } : {},
4164
+ ...f.helpText ? { helpText: f.helpText } : {},
4165
+ ...nonEmpty(f.helpTextTranslations) ? { helpTextTranslations: nonEmpty(f.helpTextTranslations) } : {},
4166
+ ...f.required ? { required: true } : {},
4167
+ ...f.enabled ? {} : { enabled: false },
4168
+ ...f.minLength !== null ? { minLength: f.minLength } : {},
4169
+ ...f.maxLength !== null ? { maxLength: f.maxLength } : {},
4170
+ ...f.defaultValue ? { defaultValue: f.defaultValue } : {},
4171
+ ...f.type === "select" ? { options: f.options } : {}
4172
+ }))
4173
+ );
4174
+ }
4175
+ function translationFor(map, locale) {
4176
+ if (!locale) return null;
4177
+ const own = (k) => {
4178
+ const v = Object.prototype.hasOwnProperty.call(map, k) ? map[k] : void 0;
4179
+ return typeof v === "string" && v.length > 0 ? v : null;
4180
+ };
4181
+ const exact = own(locale);
4182
+ if (exact) return exact;
4183
+ const base = locale.split("-")[0];
4184
+ return base && base !== locale ? own(base) : null;
4185
+ }
4186
+ function customFieldText(field, part, locale) {
4187
+ const map = part === "label" ? field.labelTranslations : part === "placeholder" ? field.placeholderTranslations : field.helpTextTranslations;
4188
+ const translated = translationFor(map, locale);
4189
+ if (translated) return translated;
4190
+ const fallback = field[part];
4191
+ if (fallback) return fallback;
4192
+ return part === "label" ? field.key : "";
4193
+ }
4194
+ function customFieldOptionLabel(option, locale) {
4195
+ return translationFor(option.labelTranslations, locale) ?? (option.label || option.value);
4196
+ }
4049
4197
  function decodeBranding(source) {
4050
4198
  if (!source) return cloneBranding(DEFAULT_BRANDING);
4051
4199
  const extras = source.sdk_ui_rules?.[BRANDING_GROUP_KEY] ?? {};
4052
4200
  const decodedBadges = decodeBadges(extras["trustBadges"]);
4201
+ const decodedCustomFields = decodeCustomFields(extras["customFields"]);
4053
4202
  const displayName = s(source.merchant_name) || s(source.seller_name);
4054
4203
  const logoUrl = s(source.merchant_logo) || s(source.logo);
4055
4204
  const tagline = s(extras["tagline"]) || s(source.merchant_description);
@@ -4151,6 +4300,7 @@ function decodeBranding(source) {
4151
4300
  showCurrencyCode: parseBool(extras["showCurrencyCode"], DEFAULT_BRANDING.showCurrencyCode),
4152
4301
  showOrderItems: parseBool(extras["showOrderItems"], DEFAULT_BRANDING.showOrderItems),
4153
4302
  trustBadges: decodedBadges ?? DEFAULT_BRANDING.trustBadges.map((b) => ({ ...b })),
4303
+ customFields: decodedCustomFields ?? [],
4154
4304
  headerText: s(source.payment_form_header_text),
4155
4305
  payButtonLabel: s(source.payment_button_text),
4156
4306
  cardTermsMessage: s(source.custom_message_for_card_terms),
@@ -4204,6 +4354,9 @@ function encodeBranding(branding, base) {
4204
4354
  labelStyle: branding.labelStyle,
4205
4355
  trustBadges: encodeBadges(branding.trustBadges)
4206
4356
  };
4357
+ if (branding.customFields.length > 0) {
4358
+ extras["customFields"] = encodeCustomFields(branding.customFields);
4359
+ }
4207
4360
  const tagline = trim(branding.tagline);
4208
4361
  if (tagline) extras["tagline"] = tagline;
4209
4362
  const footer = trim(branding.footerText);
@@ -4254,6 +4407,7 @@ function parseImportedBranding(raw) {
4254
4407
  const sStr = (v, fallback) => typeof v === "string" ? v : fallback;
4255
4408
  const sHex = (v, fallback) => typeof v === "string" && isHexColor(v) ? v : fallback;
4256
4409
  const trustBadges = parseTrustBadgesLoose(root["trustBadges"]) ?? dflt.trustBadges.map((b) => ({ ...b }));
4410
+ const customFields = parseCustomFieldsLoose(root["customFields"]) ?? [];
4257
4411
  const labelStyle = (() => {
4258
4412
  const v = root["labelStyle"];
4259
4413
  if (v === "above" || v === "hidden") return v;
@@ -4324,6 +4478,7 @@ function parseImportedBranding(raw) {
4324
4478
  showCurrencyCode: parseBool(root["showCurrencyCode"], dflt.showCurrencyCode),
4325
4479
  showOrderItems: parseBool(root["showOrderItems"], dflt.showOrderItems),
4326
4480
  trustBadges,
4481
+ customFields,
4327
4482
  headerText: sStr(root["headerText"], dflt.headerText),
4328
4483
  payButtonLabel: sStr(root["payButtonLabel"], dflt.payButtonLabel),
4329
4484
  cardTermsMessage: sStr(root["cardTermsMessage"], dflt.cardTermsMessage),
@@ -4394,12 +4549,15 @@ function shadowFor(style) {
4394
4549
  }
4395
4550
  // Annotate the CommonJS export names for ESM import in node:
4396
4551
  0 && (module.exports = {
4552
+ ALL_CUSTOM_FIELD_TYPES,
4397
4553
  Analytics,
4398
4554
  AnalyticsDashboard,
4399
4555
  AvailabilityOverrides,
4400
4556
  BRANDING_EXPORT_FORMAT,
4401
4557
  BRANDING_EXPORT_VERSION,
4402
4558
  CUSTOM_CSS_MAX_LENGTH,
4559
+ CUSTOM_FIELDS_MAX,
4560
+ CUSTOM_FIELD_KEY_PATTERN,
4403
4561
  Cards,
4404
4562
  DEFAULT_BADGES,
4405
4563
  DEFAULT_BADGES_DARK,
@@ -4423,11 +4581,16 @@ function shadowFor(style) {
4423
4581
  buildBrandingExport,
4424
4582
  buttonPadValue,
4425
4583
  cloneBranding,
4584
+ cloneCustomField,
4585
+ customFieldOptionLabel,
4586
+ customFieldText,
4426
4587
  decodeBadges,
4427
4588
  decodeBranding,
4589
+ decodeCustomFields,
4428
4590
  defaultBranding,
4429
4591
  encodeBadges,
4430
4592
  encodeBranding,
4593
+ encodeCustomFields,
4431
4594
  feeProgram,
4432
4595
  fontStack,
4433
4596
  fontWeightValue,
@@ -4436,6 +4599,7 @@ function shadowFor(style) {
4436
4599
  isHexColor,
4437
4600
  leaf,
4438
4601
  logoDimensions,
4602
+ parseCustomFieldsLoose,
4439
4603
  parseImportedBranding,
4440
4604
  programToTree,
4441
4605
  radiusValue,