@reevit/react 0.5.5 → 0.5.7

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.mts CHANGED
@@ -697,6 +697,7 @@ declare class ReevitAPIClient {
697
697
  constructor(config: ReevitAPIClientConfig);
698
698
  /**
699
699
  * Makes an authenticated API request
700
+ * @param idempotencyKey Optional deterministic idempotency key for the request
700
701
  */
701
702
  private request;
702
703
  /**
package/dist/index.d.ts CHANGED
@@ -697,6 +697,7 @@ declare class ReevitAPIClient {
697
697
  constructor(config: ReevitAPIClientConfig);
698
698
  /**
699
699
  * Makes an authenticated API request
700
+ * @param idempotencyKey Optional deterministic idempotency key for the request
700
701
  */
701
702
  private request;
702
703
  /**
package/dist/index.js CHANGED
@@ -152,6 +152,18 @@ function createPaymentError(response, errorData) {
152
152
  }
153
153
  };
154
154
  }
155
+ function generateIdempotencyKey(params) {
156
+ const sortedKeys = Object.keys(params).sort();
157
+ const stableString = sortedKeys.map((key) => `${key}:${JSON.stringify(params[key])}`).join("|");
158
+ let hash = 5381;
159
+ for (let i = 0; i < stableString.length; i++) {
160
+ hash = (hash << 5) + hash + stableString.charCodeAt(i);
161
+ hash = hash & hash;
162
+ }
163
+ const hashHex = (hash >>> 0).toString(16);
164
+ const timeBucket = Math.floor(Date.now() / (5 * 60 * 1e3));
165
+ return `reevit_${timeBucket}_${hashHex}`;
166
+ }
155
167
  var ReevitAPIClient = class {
156
168
  constructor(config) {
157
169
  this.publicKey = config.publicKey || "";
@@ -160,8 +172,9 @@ var ReevitAPIClient = class {
160
172
  }
161
173
  /**
162
174
  * Makes an authenticated API request
175
+ * @param idempotencyKey Optional deterministic idempotency key for the request
163
176
  */
164
- async request(method, path, body) {
177
+ async request(method, path, body, idempotencyKey) {
165
178
  const controller = new AbortController();
166
179
  const timeoutId = setTimeout(() => controller.abort(), this.timeout);
167
180
  const headers = {
@@ -173,7 +186,7 @@ var ReevitAPIClient = class {
173
186
  headers["X-Reevit-Key"] = this.publicKey;
174
187
  }
175
188
  if (method === "POST" || method === "PATCH" || method === "PUT") {
176
- headers["Idempotency-Key"] = `${Date.now()}-${Math.random().toString(36).substring(2, 15)}`;
189
+ headers["Idempotency-Key"] = idempotencyKey || (body ? generateIdempotencyKey(body) : `${Date.now()}-${Math.random().toString(36).substring(2, 15)}`);
177
190
  }
178
191
  try {
179
192
  const response = await fetch(`${this.baseUrl}${path}`, {
@@ -246,7 +259,16 @@ var ReevitAPIClient = class {
246
259
  allowed_providers: options?.allowedProviders
247
260
  };
248
261
  }
249
- return this.request("POST", "/v1/payments/intents", request);
262
+ const idempotencyKey = generateIdempotencyKey({
263
+ amount: config.amount,
264
+ currency: config.currency,
265
+ customer: config.email || config.metadata?.customerId || "",
266
+ reference: config.reference || "",
267
+ method: method || "",
268
+ provider: options?.preferredProviders?.[0] || options?.allowedProviders?.[0] || "",
269
+ publicKey: this.publicKey
270
+ });
271
+ return this.request("POST", "/v1/payments/intents", request, idempotencyKey);
250
272
  }
251
273
  /**
252
274
  * Retrieves a payment intent by ID
@@ -488,13 +510,21 @@ function useReevit(options) {
488
510
  let data;
489
511
  let error;
490
512
  if (config.paymentLinkCode) {
513
+ const idempotencyKey = generateIdempotencyKey({
514
+ paymentLinkCode: config.paymentLinkCode,
515
+ amount: config.amount,
516
+ email: config.email || "",
517
+ phone: config.phone || "",
518
+ method: paymentMethod || "",
519
+ provider: options2?.preferredProvider || options2?.allowedProviders?.[0] || ""
520
+ });
491
521
  const response = await fetch(
492
522
  `${apiBaseUrl || DEFAULT_PUBLIC_API_BASE_URL}/v1/pay/${config.paymentLinkCode}/pay`,
493
523
  {
494
524
  method: "POST",
495
525
  headers: {
496
526
  "Content-Type": "application/json",
497
- "Idempotency-Key": `${Date.now()}-${Math.random().toString(36).substring(2, 15)}`
527
+ "Idempotency-Key": idempotencyKey
498
528
  },
499
529
  body: JSON.stringify({
500
530
  amount: config.amount,
@@ -2270,7 +2300,6 @@ function ReevitCheckout({
2270
2300
  async (provider) => {
2271
2301
  if (provider === selectedProvider) {
2272
2302
  setSelectedProvider(null);
2273
- await reset();
2274
2303
  setShowPSPBridge(false);
2275
2304
  setMomoData(null);
2276
2305
  return;
@@ -2278,12 +2307,13 @@ function ReevitCheckout({
2278
2307
  const providerMethods = providerOptions.find((option) => option.provider === provider)?.methods || paymentMethods;
2279
2308
  const methodForInit = selectedMethod && providerMethods.includes(selectedMethod) ? selectedMethod : providerMethods[0] || paymentMethods[0];
2280
2309
  setSelectedProvider(provider);
2281
- await reset();
2282
2310
  setShowPSPBridge(false);
2283
2311
  setMomoData(null);
2284
- initialize(methodForInit, { preferredProvider: provider, allowedProviders: [provider] });
2312
+ if (methodForInit) {
2313
+ selectMethod(methodForInit);
2314
+ }
2285
2315
  },
2286
- [initialize, paymentMethods, providerOptions, reset, selectedMethod, selectedProvider]
2316
+ [paymentMethods, providerOptions, selectMethod, selectedMethod, selectedProvider]
2287
2317
  );
2288
2318
  const handleContinue = react.useCallback(() => {
2289
2319
  if (!selectedMethod) return;