pi-spi-sdk 0.1.2 → 0.1.3

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 (113) hide show
  1. package/CHANGELOG.md +9 -0
  2. package/dist/config.js +2 -1
  3. package/dist/error-handler.js +11 -8
  4. package/dist/errors.js +13 -5
  5. package/dist/examples.js +11 -9
  6. package/dist/generated/core/ApiError.js +5 -1
  7. package/dist/generated/core/ApiRequestOptions.js +2 -1
  8. package/dist/generated/core/ApiResult.js +2 -1
  9. package/dist/generated/core/CancelablePromise.js +7 -2
  10. package/dist/generated/core/OpenAPI.js +4 -1
  11. package/dist/generated/core/request.js +75 -52
  12. package/dist/generated/index.js +82 -39
  13. package/dist/generated/models/AliasCreationReponse.js +2 -1
  14. package/dist/generated/models/AliasCreationRequest.js +2 -1
  15. package/dist/generated/models/AliasReponseListe.js +2 -1
  16. package/dist/generated/models/AnnulationStatut.js +5 -2
  17. package/dist/generated/models/Champs.js +2 -1
  18. package/dist/generated/models/CompteOperation.js +5 -2
  19. package/dist/generated/models/CompteOperationListe.js +2 -1
  20. package/dist/generated/models/CompteSolde.js +5 -2
  21. package/dist/generated/models/CompteTransfertIntraReponse.js +5 -2
  22. package/dist/generated/models/CompteTransfertIntraRequest.js +2 -1
  23. package/dist/generated/models/DemandePaiementConfirmationAnnulationRaison.js +5 -2
  24. package/dist/generated/models/DemandePaiementConfirmationReponse.js +5 -2
  25. package/dist/generated/models/DemandePaiementConfirmationRequest.js +2 -1
  26. package/dist/generated/models/DemandePaiementConfirmationRequestAccepter.js +2 -1
  27. package/dist/generated/models/DemandePaiementConfirmationRequestRejeter.js +2 -1
  28. package/dist/generated/models/DemandePaiementConsultationReponse.js +5 -2
  29. package/dist/generated/models/DemandePaiementEnMasseConfirmationRequest.js +2 -1
  30. package/dist/generated/models/DemandePaiementEnMasseConfirmationRequestAccepter.js +2 -1
  31. package/dist/generated/models/DemandePaiementEnMasseConfirmationRequestRejeter.js +2 -1
  32. package/dist/generated/models/DemandePaiementEnMasseRequest.js +2 -1
  33. package/dist/generated/models/DemandePaiementEnMasseStatutReponse.js +5 -2
  34. package/dist/generated/models/DemandePaiementListe.js +2 -1
  35. package/dist/generated/models/DemandePaiementListeItem.js +5 -2
  36. package/dist/generated/models/DemandePaiementReponse.js +2 -1
  37. package/dist/generated/models/DemandePaiementReponseRequest.js +5 -2
  38. package/dist/generated/models/DemandePaiementRequest.js +5 -2
  39. package/dist/generated/models/DemandePaiementRequestBase.js +2 -1
  40. package/dist/generated/models/DemandePaiementRequestCategorie.js +5 -2
  41. package/dist/generated/models/DemandePaiementStatut.js +5 -2
  42. package/dist/generated/models/DemandePaiementStatutRaison.js +5 -2
  43. package/dist/generated/models/ListeMeta.js +2 -1
  44. package/dist/generated/models/Paiement.js +5 -2
  45. package/dist/generated/models/PaiementAnnulationMotif.js +5 -2
  46. package/dist/generated/models/PaiementAnnulationReponseRequest.js +2 -1
  47. package/dist/generated/models/PaiementAnnulationReponseRequestAccepter.js +2 -1
  48. package/dist/generated/models/PaiementAnnulationReponseRequestRejeter.js +2 -1
  49. package/dist/generated/models/PaiementAnnulationRequest.js +2 -1
  50. package/dist/generated/models/PaiementAnnulationStatutRaison.js +5 -2
  51. package/dist/generated/models/PaiementEnMasseConfirmationRequest.js +2 -1
  52. package/dist/generated/models/PaiementEnMasseConfirmationRequestAccepter.js +2 -1
  53. package/dist/generated/models/PaiementEnMasseConfirmationRequestRejeter.js +2 -1
  54. package/dist/generated/models/PaiementEnMasseReponseStatut.js +5 -2
  55. package/dist/generated/models/PaiementEnMasseRequest.js +2 -1
  56. package/dist/generated/models/PaiementImmediatConfirmationReponse.js +5 -2
  57. package/dist/generated/models/PaiementImmediatConfirmationRequest.js +2 -1
  58. package/dist/generated/models/PaiementImmediatConfirmationRequestAccepter.js +2 -1
  59. package/dist/generated/models/PaiementImmediatConfirmationRequestRejeter.js +2 -1
  60. package/dist/generated/models/PaiementImmediatReponse.js +5 -2
  61. package/dist/generated/models/PaiementImmediatRequest.js +2 -1
  62. package/dist/generated/models/PaiementListe.js +2 -1
  63. package/dist/generated/models/PaiementRequest.js +2 -1
  64. package/dist/generated/models/PaiementStatut.js +5 -2
  65. package/dist/generated/models/PaiementStatutRaison.js +5 -2
  66. package/dist/generated/models/Problem7807.js +2 -1
  67. package/dist/generated/models/RefDocType.js +5 -2
  68. package/dist/generated/models/RetourStatut.js +5 -2
  69. package/dist/generated/models/RetourStatutRaison.js +5 -2
  70. package/dist/generated/models/WebhookCreationRequest.js +2 -1
  71. package/dist/generated/models/WebhookCreationResponse.js +2 -1
  72. package/dist/generated/models/WebhookData.js +2 -1
  73. package/dist/generated/models/WebhookEvent.js +5 -2
  74. package/dist/generated/models/WebhookEventsList.js +2 -1
  75. package/dist/generated/models/WebhookList.js +2 -1
  76. package/dist/generated/models/WebhookModificationRequest.js +2 -1
  77. package/dist/generated/models/WebhooksEvents.js +5 -2
  78. package/dist/generated/services/AliasService.js +10 -6
  79. package/dist/generated/services/ComptesService.js +10 -6
  80. package/dist/generated/services/DemandeAnnulationService.js +9 -5
  81. package/dist/generated/services/DemandesDePaiementEnMasseService.js +10 -6
  82. package/dist/generated/services/DemandesDePaiementService.js +12 -8
  83. package/dist/generated/services/NotificationService.js +12 -8
  84. package/dist/generated/services/PaiementEnMasseService.js +10 -6
  85. package/dist/generated/services/PaiementImmediatService.js +12 -8
  86. package/dist/generated/services/RetoursdeFondsService.js +8 -4
  87. package/dist/index.d.ts +1 -0
  88. package/dist/index.js +57 -8
  89. package/dist/qrcode/index.d.ts +62 -0
  90. package/dist/qrcode/index.js +541 -0
  91. package/dist/qrcode/logo.d.ts +3 -0
  92. package/dist/qrcode/logo.js +142 -0
  93. package/dist/query-builder.js +5 -1
  94. package/dist/sdk.d.ts +16 -0
  95. package/dist/sdk.js +81 -41
  96. package/dist/services/alias.d.ts +7 -3
  97. package/dist/services/alias.js +13 -15
  98. package/dist/services/base.d.ts +12 -1
  99. package/dist/services/base.js +54 -4
  100. package/dist/services/comptes.js +6 -2
  101. package/dist/services/demandes-annulation.js +6 -2
  102. package/dist/services/demandes-paiement-en-masse.d.ts +4 -110
  103. package/dist/services/demandes-paiement-en-masse.js +21 -116
  104. package/dist/services/demandes-paiement.d.ts +5 -1
  105. package/dist/services/demandes-paiement.js +7 -5
  106. package/dist/services/paiements-en-masse.js +6 -2
  107. package/dist/services/paiements.js +6 -2
  108. package/dist/services/retours-fonds.js +6 -2
  109. package/dist/services/webhooks.js +6 -2
  110. package/dist/types/alias.js +12 -7
  111. package/dist/utils/constants.js +12 -9
  112. package/dist/utils/index.js +20 -9
  113. package/package.json +6 -5
@@ -0,0 +1,541 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.QRCode = exports.PISPI_QRCODE_LOGO_DATA_URL = exports.PISPI_AMBER_LOGO_DATA_URL = exports.DEFAULT_PISPI_LOGO_DATA_URL = void 0;
37
+ exports.createQrPayload = createQrPayload;
38
+ exports.computeCrc16 = computeCrc16;
39
+ exports.buildPayloadString = buildPayloadString;
40
+ exports.generateQrCodeSvg = generateQrCodeSvg;
41
+ exports.isValidPispiQrPayload = isValidPispiQrPayload;
42
+ var logo_1 = require("./logo");
43
+ Object.defineProperty(exports, "DEFAULT_PISPI_LOGO_DATA_URL", { enumerable: true, get: function () { return logo_1.DEFAULT_PISPI_LOGO_DATA_URL; } });
44
+ Object.defineProperty(exports, "PISPI_AMBER_LOGO_DATA_URL", { enumerable: true, get: function () { return logo_1.PISPI_AMBER_LOGO_DATA_URL; } });
45
+ Object.defineProperty(exports, "PISPI_QRCODE_LOGO_DATA_URL", { enumerable: true, get: function () { return logo_1.PISPI_QRCODE_LOGO_DATA_URL; } });
46
+ const logo_2 = require("./logo");
47
+ let cachedQrCodeModule = null;
48
+ function resolveQrCodeModule(module) {
49
+ if (module?.create) {
50
+ return module;
51
+ }
52
+ if (module?.default?.create) {
53
+ return module.default;
54
+ }
55
+ throw new Error('Le module "qrcode" n\'expose pas l\'API attendue.');
56
+ }
57
+ async function getQrCodeModule() {
58
+ if (cachedQrCodeModule) {
59
+ return cachedQrCodeModule;
60
+ }
61
+ // Dans le navigateur, utiliser QRCode global si disponible (depuis qrcode/build/qrcode.min.js)
62
+ // @ts-ignore
63
+ if (typeof window !== 'undefined' && window.QRCode) {
64
+ // @ts-ignore
65
+ const resolved = resolveQrCodeModule(window.QRCode);
66
+ cachedQrCodeModule = resolved;
67
+ return resolved;
68
+ }
69
+ // Dans Node.js ou si QRCode global n'est pas disponible, utiliser l'import dynamique
70
+ const module = await Promise.resolve().then(() => __importStar(require('qrcode')));
71
+ const resolved = resolveQrCodeModule(module);
72
+ cachedQrCodeModule = resolved;
73
+ return resolved;
74
+ }
75
+ const DEFAULT_MERCHANT_CATEGORY_CODE = '0000';
76
+ const DEFAULT_CURRENCY = '952'; // XOF
77
+ const DEFAULT_MERCHANT_NAME = 'X';
78
+ const DEFAULT_MERCHANT_CITY = 'X';
79
+ const DEFAULT_REFERENCE_LABEL_TAG = '05';
80
+ const DEFAULT_MERCHANT_CHANNEL_TAG = '11';
81
+ const DEFAULT_LOGO_SIZE_RATIO = 0.18;
82
+ const DEFAULT_LOGO_PADDING_RATIO = 0;
83
+ const DEFAULT_LOGO_BORDER_RADIUS_RATIO = 0.5;
84
+ const DEFAULT_MARGIN = 0;
85
+ const DEFAULT_SVG_SIZE = 400;
86
+ const DEFAULT_DOT_COLOR = '#1A1A1A';
87
+ const DEFAULT_BACKGROUND_COLOR = '#FFFFFF';
88
+ const DOT_RADIUS_RATIO = 0.44;
89
+ const FINDER_CORNER_RADIUS = 0.8;
90
+ /**
91
+ * Génère la payload EMV conforme PI-SPI pour un QR Code.
92
+ */
93
+ function createQrPayload(input, options = {}) {
94
+ const { alias, amount, countryCode, qrType, referenceLabel, } = input;
95
+ if (!alias) {
96
+ throw new Error('Le paramètre "alias" est obligatoire.');
97
+ }
98
+ validateAlias(alias);
99
+ if (!countryCode) {
100
+ throw new Error('Le paramètre "countryCode" est obligatoire.');
101
+ }
102
+ validateCountryCode(countryCode);
103
+ if (!qrType) {
104
+ throw new Error('Le paramètre "qrType" est obligatoire.');
105
+ }
106
+ if (!referenceLabel) {
107
+ throw new Error('Le paramètre "referenceLabel" est obligatoire.');
108
+ }
109
+ validateReferenceLabel(referenceLabel);
110
+ if (amount !== undefined && amount !== null && amount !== '') {
111
+ validateAmount(amount);
112
+ }
113
+ const payloadSegments = [];
114
+ payloadSegments.push(formatDataObject('00', '01'));
115
+ const merchantAccountInformation = [
116
+ formatDataObject('00', 'int.bceao.pi'),
117
+ formatDataObject('01', alias),
118
+ ].join('');
119
+ payloadSegments.push(formatDataObject('36', merchantAccountInformation), formatDataObject('52', DEFAULT_MERCHANT_CATEGORY_CODE), formatDataObject('53', DEFAULT_CURRENCY));
120
+ if (amount !== undefined && amount !== null && amount !== '') {
121
+ payloadSegments.push(formatDataObject('54', sanitizeAmount(amount)));
122
+ }
123
+ payloadSegments.push(formatDataObject('58', countryCode), formatDataObject('59', DEFAULT_MERCHANT_NAME), formatDataObject('60', DEFAULT_MERCHANT_CITY));
124
+ const normalizedQrType = normalizeQrType(qrType);
125
+ const additionalData = buildAdditionalData(normalizedQrType, referenceLabel, options.additionalData);
126
+ if (additionalData) {
127
+ payloadSegments.push(formatDataObject('62', additionalData));
128
+ }
129
+ const payloadWithoutCrc = payloadSegments.join('');
130
+ const crcInput = `${payloadWithoutCrc}6304`;
131
+ const crc = computeCrc16(crcInput);
132
+ return { payload: `${payloadWithoutCrc}6304${crc}` };
133
+ }
134
+ function buildAdditionalData(qrType, referenceLabel, overrides) {
135
+ const segments = [];
136
+ const referenceLabelTag = formatDataObject(DEFAULT_REFERENCE_LABEL_TAG, referenceLabel);
137
+ segments.push(referenceLabelTag);
138
+ const merchantChannelValue = mapMerchantChannelFromType(qrType);
139
+ segments.push(formatDataObject(DEFAULT_MERCHANT_CHANNEL_TAG, merchantChannelValue));
140
+ if (overrides?.purposeOfTransaction) {
141
+ segments.push(formatDataObject('12', overrides.purposeOfTransaction));
142
+ }
143
+ if (overrides?.custom) {
144
+ const entries = Object.entries(overrides.custom).sort(([a], [b]) => a.localeCompare(b));
145
+ for (const [tag, value] of entries) {
146
+ validateSubTag(tag);
147
+ segments.push(formatDataObject(tag, value));
148
+ }
149
+ }
150
+ return segments.join('');
151
+ }
152
+ function mapMerchantChannelFromType(value) {
153
+ return value === 'DYNAMIC' ? '400' : '000';
154
+ }
155
+ function mapQrTypeFromChannel(channel) {
156
+ const normalized = channel?.trim().toUpperCase();
157
+ if (normalized === '400') {
158
+ return 'DYNAMIC';
159
+ }
160
+ return 'STATIC';
161
+ }
162
+ function formatDataObject(id, value) {
163
+ const length = value.length.toString().padStart(2, '0');
164
+ return `${id}${length}${value}`;
165
+ }
166
+ function sanitizeAmount(value) {
167
+ return typeof value === 'number' ? value.toString() : value.trim();
168
+ }
169
+ function validateSubTag(tag) {
170
+ if (!/^[0-9A-Za-z]{2}$/.test(tag)) {
171
+ throw new Error(`Le sous-tag additional data "${tag}" doit contenir exactement 2 caractères alphanumériques.`);
172
+ }
173
+ }
174
+ function validateAlias(alias) {
175
+ const uuidV4Regex = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
176
+ if (!uuidV4Regex.test(alias)) {
177
+ throw new Error('L\'alias doit être un UUID v4 valide.');
178
+ }
179
+ }
180
+ function validateReferenceLabel(reference) {
181
+ if (reference.length > 25) {
182
+ throw new Error('Le referenceLabel ne doit pas dépasser 25 caractères.');
183
+ }
184
+ }
185
+ function normalizeQrType(type) {
186
+ const normalized = type.trim().toUpperCase();
187
+ if (normalized === 'STATIC' || normalized === 'DYNAMIC') {
188
+ return normalized;
189
+ }
190
+ throw new Error('Le paramètre "qrType" doit être "STATIC" ou "DYNAMIC".');
191
+ }
192
+ const UEMOA_COUNTRIES = new Set(['BJ', 'BF', 'CI', 'ML', 'NE', 'SN', 'TG', 'GW']);
193
+ function validateCountryCode(code) {
194
+ if (!UEMOA_COUNTRIES.has(code.toUpperCase())) {
195
+ throw new Error("Le countryCode doit être l'un des codes ISO2 de l'UEMOA (BJ, BF, CI, ML, NE, SN, TG, GW).");
196
+ }
197
+ }
198
+ function validateAmount(amount) {
199
+ const normalized = typeof amount === 'number' ? amount.toString() : amount.trim();
200
+ if (!/^\d+$/.test(normalized)) {
201
+ throw new Error('Le montant doit contenir uniquement des chiffres.');
202
+ }
203
+ if (normalized.length > 13) {
204
+ throw new Error('Le montant ne doit pas dépasser 13 chiffres.');
205
+ }
206
+ }
207
+ function computeCrc16(input) {
208
+ let crc = 0xffff;
209
+ const polynomial = 0x1021;
210
+ for (let i = 0; i < input.length; i += 1) {
211
+ crc ^= (input.codePointAt(i) ?? 0) << 8;
212
+ for (let j = 0; j < 8; j += 1) {
213
+ const hasHighBit = (crc & 0x8000) === 0;
214
+ crc = hasHighBit ? crc << 1 : (crc << 1) ^ polynomial;
215
+ crc &= 0xffff;
216
+ }
217
+ }
218
+ return crc.toString(16).toUpperCase().padStart(4, '0');
219
+ }
220
+ function buildPayloadString(params, options) {
221
+ return createQrPayload(params, options).payload;
222
+ }
223
+ async function generateQrCodeSvg(input, options = {}) {
224
+ const { payload } = createQrPayload(input);
225
+ const size = options.size ?? DEFAULT_SVG_SIZE;
226
+ const margin = options.margin ?? DEFAULT_MARGIN;
227
+ const module = await getQrCodeModule();
228
+ const qr = module.create(payload, {
229
+ errorCorrectionLevel: 'M',
230
+ });
231
+ const dotColor = options.dotColor ?? DEFAULT_DOT_COLOR;
232
+ const backgroundColor = options.backgroundColor ?? DEFAULT_BACKGROUND_COLOR;
233
+ return buildDotPatternSvg(qr, {
234
+ size,
235
+ margin,
236
+ dotColor,
237
+ backgroundColor,
238
+ logo: {
239
+ dataUrl: options.logoDataUrl ?? logo_2.DEFAULT_PISPI_LOGO_DATA_URL,
240
+ sizeRatio: options.logoSizeRatio ?? DEFAULT_LOGO_SIZE_RATIO,
241
+ paddingRatio: options.logoPaddingRatio ?? DEFAULT_LOGO_PADDING_RATIO,
242
+ borderRadiusRatio: options.logoBorderRadiusRatio ?? DEFAULT_LOGO_BORDER_RADIUS_RATIO,
243
+ backgroundColor: options.logoBackgroundColor ?? DEFAULT_BACKGROUND_COLOR,
244
+ },
245
+ });
246
+ }
247
+ function clamp(value, min, max) {
248
+ return Math.min(Math.max(value, min), max);
249
+ }
250
+ function formatSvgNumber(value) {
251
+ const normalized = Number.parseFloat(value.toFixed(3));
252
+ if (Number.isNaN(normalized)) {
253
+ return '0';
254
+ }
255
+ return normalized.toString();
256
+ }
257
+ exports.QRCode = {
258
+ // Original methods
259
+ createQrPayload,
260
+ buildPayloadString,
261
+ computeCrc16,
262
+ generateQrCodeSvg,
263
+ isValidPispiQrPayload,
264
+ // Simplified aliases
265
+ payload: buildPayloadString,
266
+ svg: generateQrCodeSvg,
267
+ validate: isValidPispiQrPayload,
268
+ raw: createQrPayload, // For when you need the object wrapper
269
+ };
270
+ function isValidPispiQrPayload(value) {
271
+ const basicErrors = validatePayloadBasics(value);
272
+ if (basicErrors.length > 0) {
273
+ return { valid: false, errors: basicErrors };
274
+ }
275
+ const { segments, errors: parseErrors } = parseEmvSegments(value);
276
+ if (parseErrors.length > 0) {
277
+ return { valid: false, errors: parseErrors };
278
+ }
279
+ const segmentValidation = validateSegmentContent(segments, value);
280
+ if (segmentValidation.errors.length > 0) {
281
+ return { valid: false, errors: segmentValidation.errors };
282
+ }
283
+ const data = buildValidationData(segments, segmentValidation);
284
+ return {
285
+ valid: true,
286
+ errors: [],
287
+ data,
288
+ };
289
+ }
290
+ function validatePayloadBasics(value) {
291
+ if (!value) {
292
+ return ['La payload doit être une chaîne non vide.'];
293
+ }
294
+ if (value.length < 12) {
295
+ return ['Payload trop courte pour contenir des segments EMV.'];
296
+ }
297
+ return [];
298
+ }
299
+ function parseEmvSegments(value) {
300
+ const segments = {};
301
+ const errors = [];
302
+ let cursor = 0;
303
+ try {
304
+ while (cursor < value.length) {
305
+ const tag = value.slice(cursor, cursor + 2);
306
+ const lengthStr = value.slice(cursor + 2, cursor + 4);
307
+ const length = Number.parseInt(lengthStr, 10);
308
+ if (Number.isNaN(length) || length < 0) {
309
+ errors.push(`Longueur invalide pour le tag ${tag}.`);
310
+ break;
311
+ }
312
+ const valueStart = cursor + 4;
313
+ const valueEnd = valueStart + length;
314
+ if (valueEnd > value.length) {
315
+ errors.push(`Segment ${tag} tronqué.`);
316
+ break;
317
+ }
318
+ segments[tag] = value.slice(valueStart, valueEnd);
319
+ cursor = valueEnd;
320
+ }
321
+ }
322
+ catch (error) {
323
+ errors.push(`Erreur lors de l'analyse de la payload: ${error?.message ?? error}`);
324
+ }
325
+ return { segments, errors };
326
+ }
327
+ function validateSegmentContent(segments, rawValue) {
328
+ const errors = [];
329
+ const formatErrors = validateFormatIndicator(segments['00']);
330
+ const merchantInfoResult = extractMerchantInfo(segments['36']);
331
+ const countryCodeResult = validateCountryCodeSegment(segments['58']);
332
+ const additionalDataResult = extractAdditionalData(segments['62']);
333
+ const crcErrors = validateCrcSegment(segments['63'], rawValue);
334
+ errors.push(...formatErrors, ...merchantInfoResult.errors, ...countryCodeResult.errors, ...additionalDataResult.errors, ...crcErrors);
335
+ return {
336
+ errors,
337
+ merchantInfo: merchantInfoResult.merchantInfo,
338
+ referenceLabel: additionalDataResult.referenceLabel,
339
+ merchantChannel: additionalDataResult.merchantChannel,
340
+ countryCode: countryCodeResult.countryCode,
341
+ };
342
+ }
343
+ function validateFormatIndicator(formatIndicator) {
344
+ if (!formatIndicator) {
345
+ return ['Tag 00 (format indicator) manquant.'];
346
+ }
347
+ if (formatIndicator !== '01') {
348
+ return ['Tag 00 invalide (doit être 01).'];
349
+ }
350
+ return [];
351
+ }
352
+ function extractMerchantInfo(segment) {
353
+ if (!segment) {
354
+ return {
355
+ merchantInfo: null,
356
+ errors: ['Tag 36 (Merchant Account Information) manquant.'],
357
+ };
358
+ }
359
+ try {
360
+ const merchantInfo = parseSubFields(segment);
361
+ const errors = [];
362
+ if (!merchantInfo['01']) {
363
+ errors.push('Alias manquant dans les informations marchand (tag 36).');
364
+ }
365
+ return { merchantInfo, errors };
366
+ }
367
+ catch (error) {
368
+ return {
369
+ merchantInfo: null,
370
+ errors: [`Erreur lors de l'analyse des informations marchand: ${error?.message ?? error}`],
371
+ };
372
+ }
373
+ }
374
+ function validateCountryCodeSegment(countryCode) {
375
+ if (!countryCode) {
376
+ return {
377
+ errors: ['Tag 58 (Country Code) manquant.'],
378
+ };
379
+ }
380
+ return {
381
+ countryCode,
382
+ errors: [],
383
+ };
384
+ }
385
+ function extractAdditionalData(segment) {
386
+ if (!segment) {
387
+ return {
388
+ errors: ['Tag 62 (Additional Data Field) manquant.'],
389
+ };
390
+ }
391
+ try {
392
+ const additionalSegments = parseSubFields(segment);
393
+ const referenceLabel = additionalSegments['05'];
394
+ const merchantChannel = additionalSegments[DEFAULT_MERCHANT_CHANNEL_TAG];
395
+ const errors = [];
396
+ if (!referenceLabel) {
397
+ errors.push('Tag 05 (Reference Label) manquant dans les données additionnelles.');
398
+ }
399
+ if (!merchantChannel) {
400
+ errors.push('Tag 11 (Merchant Channel) manquant dans les données additionnelles.');
401
+ }
402
+ return {
403
+ referenceLabel,
404
+ merchantChannel,
405
+ errors,
406
+ };
407
+ }
408
+ catch (error) {
409
+ return {
410
+ errors: [`Erreur lors de l'analyse des données additionnelles: ${error?.message ?? error}`],
411
+ };
412
+ }
413
+ }
414
+ function validateCrcSegment(crc, rawValue) {
415
+ if (!crc) {
416
+ return ['Tag 63 (CRC) manquant.'];
417
+ }
418
+ const payloadWithoutCrc = rawValue.slice(0, -4);
419
+ const computedCrc = computeCrc16(payloadWithoutCrc);
420
+ if (crc !== computedCrc) {
421
+ return ['CRC invalide.'];
422
+ }
423
+ return [];
424
+ }
425
+ function buildValidationData(segments, context) {
426
+ const amountValue = segments['54'];
427
+ const data = {
428
+ alias: context.merchantInfo?.['01'] ?? '',
429
+ countryCode: context.countryCode ?? '',
430
+ qrType: mapQrTypeFromChannel(context.merchantChannel),
431
+ referenceLabel: context.referenceLabel ?? '',
432
+ };
433
+ if (amountValue !== undefined) {
434
+ data.amount = amountValue;
435
+ }
436
+ return data;
437
+ }
438
+ function parseSubFields(data) {
439
+ const segments = {};
440
+ let cursor = 0;
441
+ while (cursor < data.length) {
442
+ const tag = data.slice(cursor, cursor + 2);
443
+ const lengthStr = data.slice(cursor + 2, cursor + 4);
444
+ const length = Number.parseInt(lengthStr, 10);
445
+ if (Number.isNaN(length) || length < 0) {
446
+ throw new Error(`Longueur invalide pour le sous-tag ${tag}`);
447
+ }
448
+ const valueStart = cursor + 4;
449
+ const valueEnd = valueStart + length;
450
+ if (valueEnd > data.length) {
451
+ throw new Error(`Sous-segment ${tag} tronqué`);
452
+ }
453
+ const segmentValue = data.slice(valueStart, valueEnd);
454
+ segments[tag] = segmentValue;
455
+ cursor = valueEnd;
456
+ }
457
+ return segments;
458
+ }
459
+ function buildDotPatternSvg(qr, options) {
460
+ const modules = qr.modules;
461
+ const moduleCount = typeof modules?.size === 'number' ? modules.size : modules.length;
462
+ if (typeof moduleCount !== 'number' || Number.isNaN(moduleCount)) {
463
+ throw new TypeError('Format du QR Code inattendu: impossible de déterminer la taille de la matrice.');
464
+ }
465
+ const svgSize = options.size;
466
+ const margin = options.margin;
467
+ const drawableSize = svgSize - margin * 2;
468
+ const cellSize = drawableSize / moduleCount;
469
+ const dotRadius = cellSize * DOT_RADIUS_RATIO;
470
+ const finderRadius = cellSize * FINDER_CORNER_RADIUS;
471
+ const paths = [];
472
+ const backgroundRect = `<rect fill="${options.backgroundColor}" width="${svgSize}" height="${svgSize}" rx="${formatSvgNumber(finderRadius)}" />`;
473
+ for (let row = 0; row < moduleCount; row += 1) {
474
+ for (let col = 0; col < moduleCount; col += 1) {
475
+ if (!isDarkModule(modules, moduleCount, row, col)) {
476
+ continue;
477
+ }
478
+ const x = margin + col * cellSize + cellSize / 2;
479
+ const y = margin + row * cellSize + cellSize / 2;
480
+ if (isFinderPattern(moduleCount, row, col)) {
481
+ paths.push(generateFinderPatternPath(x, y, cellSize, finderRadius, options.dotColor));
482
+ }
483
+ else {
484
+ paths.push(`<circle cx="${formatSvgNumber(x)}" cy="${formatSvgNumber(y)}" r="${formatSvgNumber(dotRadius)}" fill="${options.dotColor}" />`);
485
+ }
486
+ }
487
+ }
488
+ const logoSvg = generateLogoOverlay(svgSize, margin, moduleCount, cellSize, options.logo);
489
+ return [
490
+ `<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 ${svgSize} ${svgSize}" shape-rendering="geometricPrecision">`,
491
+ backgroundRect,
492
+ ...paths,
493
+ logoSvg,
494
+ '</svg>',
495
+ ].join('');
496
+ }
497
+ function isFinderPattern(moduleCount, row, col) {
498
+ const patternSize = 7;
499
+ const inTop = row < patternSize;
500
+ const inBottom = row >= moduleCount - patternSize;
501
+ const inLeft = col < patternSize;
502
+ const inRight = col >= moduleCount - patternSize;
503
+ return (inTop && inLeft) || (inTop && inRight) || (inBottom && inLeft);
504
+ }
505
+ function generateFinderPatternPath(x, y, cellSize, radius, color) {
506
+ const centerX = formatSvgNumber(x);
507
+ const centerY = formatSvgNumber(y);
508
+ const radiusSvg = formatSvgNumber(radius);
509
+ return `<circle cx="${centerX}" cy="${centerY}" r="${radiusSvg}" fill="${color}" />`;
510
+ }
511
+ function generateLogoOverlay(svgSize, margin, moduleCount, cellSize, logo) {
512
+ if (!logo.dataUrl) {
513
+ return '';
514
+ }
515
+ const qrDrawableSize = moduleCount * cellSize;
516
+ const logoSize = qrDrawableSize * clamp(logo.sizeRatio, 0.05, 0.5);
517
+ const logoPadding = logoSize * clamp(logo.paddingRatio, 0, 0.25);
518
+ const backgroundSize = logoSize + logoPadding * 2;
519
+ const logoBorderRadius = clamp(logo.borderRadiusRatio, 0, 0.5) * backgroundSize;
520
+ const originX = margin + (qrDrawableSize - backgroundSize) / 2;
521
+ const originY = margin + (qrDrawableSize - backgroundSize) / 2;
522
+ return [
523
+ `<g class="pispi-logo" transform="translate(${formatSvgNumber(originX)}, ${formatSvgNumber(originY)})" pointer-events="none">`,
524
+ `<rect width="${formatSvgNumber(backgroundSize)}" height="${formatSvgNumber(backgroundSize)}" rx="${formatSvgNumber(logoBorderRadius)}" fill="${logo.backgroundColor}" opacity="0.95"/>`,
525
+ `<image x="${formatSvgNumber(logoPadding)}" y="${formatSvgNumber(logoPadding)}" width="${formatSvgNumber(logoSize)}" height="${formatSvgNumber(logoSize)}" href="${logo.dataUrl}" xlink:href="${logo.dataUrl}" preserveAspectRatio="xMidYMid meet"/>`,
526
+ '</g>',
527
+ ].join('');
528
+ }
529
+ function isDarkModule(modules, moduleCount, row, col) {
530
+ if (modules?.data && Array.isArray(modules.data)) {
531
+ const index = row * moduleCount + col;
532
+ return Boolean(modules.data[index]);
533
+ }
534
+ if (typeof modules?.get === 'function') {
535
+ return Boolean(modules.get(row, col));
536
+ }
537
+ if (Array.isArray(modules[row])) {
538
+ return Boolean(modules[row][col]);
539
+ }
540
+ return false;
541
+ }
@@ -0,0 +1,3 @@
1
+ export declare const PISPI_AMBER_LOGO_DATA_URL: string;
2
+ export declare const PISPI_QRCODE_LOGO_DATA_URL: string;
3
+ export declare const DEFAULT_PISPI_LOGO_DATA_URL: string;