redsys-easy 5.2.4 → 5.2.5

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/lib/client.d.ts CHANGED
@@ -87,6 +87,8 @@ export interface RedsysAPI {
87
87
  restIniciaPeticion: RestIniciaPeticion;
88
88
  restTrataPeticion: RestTrataPeticion;
89
89
  createRedirectForm: CreateRedirectForm;
90
+ processRedirectNotification: ProcessRestNotification;
91
+ processDirectRestNotification: ProcessRestNotification;
90
92
  processRestNotification: ProcessRestNotification;
91
93
  processSoapNotification: ProcessSoapNotification;
92
94
  createSoapNotificationAnswer: CreateSoapNotificationAnswer;
package/lib/index.esm.js CHANGED
@@ -1927,7 +1927,7 @@ const deserializeJSONMerchantParams = (strPayload) => {
1927
1927
  if (typeof strPayload !== 'string') {
1928
1928
  throw new ParseError('Payload must be a base-64 encoded string');
1929
1929
  }
1930
- const payload = JSON.parse(decodeURIComponent(Buffer.from(strPayload, 'base64').toString('utf8')));
1930
+ const payload = JSON.parse(Buffer.from(strPayload, 'base64').toString('utf8'));
1931
1931
  if (typeof payload !== 'object' || payload == null) {
1932
1932
  throw new ParseError('Cannot parse notification payload');
1933
1933
  }
@@ -2126,6 +2126,22 @@ const serializeAndSignJSONRequest = (merchantKey, requestParams) => {
2126
2126
  return sha256SignJSONRequest(merchantKey, serializedParams, requestParams);
2127
2127
  };
2128
2128
 
2129
+ /**
2130
+ * Tries to decode x-www-form-urlencoded values of an object
2131
+ */
2132
+ const decodeUrlEntries = (params) => {
2133
+ return Object.fromEntries(Object.entries(params).map((entry) => {
2134
+ let processedValue = entry[1];
2135
+ if (typeof entry[1] === 'string') {
2136
+ try {
2137
+ processedValue = decodeURIComponent(entry[1].replaceAll('+', ' '));
2138
+ }
2139
+ catch (_) { }
2140
+ }
2141
+ return [entry[0], processedValue];
2142
+ }));
2143
+ };
2144
+
2129
2145
  const serializeSoapNotificationResponse = ({ allow: allowOperation }) => {
2130
2146
  const answer = allowOperation ? 'OK' : 'KO';
2131
2147
  return `<Response Ds_Version="0.0"><Ds_Response_Merchant>${answer}</Ds_Response_Merchant></Response>`;
@@ -2270,14 +2286,44 @@ const createRedsysAPI = (config) => {
2270
2286
  };
2271
2287
  };
2272
2288
  /**
2273
- * Processes a JSON REST notification
2289
+ * Processes a URLOK/URLKO query notification
2274
2290
  */
2275
- const processRestNotification = (
2291
+ const processRedirectNotification = (
2292
+ /** Body of JSON notification, as a POJO (Plain Old Javascript Object) */
2293
+ body) => {
2294
+ // A notification can't contain a gateway error, it didn't initiate the request
2295
+ const params = deserializeAndVerifyJSONResponse(config.secretKey, body);
2296
+ return decodeUrlEntries(params);
2297
+ };
2298
+ /**
2299
+ * Processes a direct JSON REST notification
2300
+ */
2301
+ const processDirectRestNotification = (
2276
2302
  /** Body of JSON notification, as a POJO (Plain Old Javascript Object) */
2277
2303
  body) => {
2278
2304
  // A notification can't contain a gateway error, it didn't initiate the request
2279
2305
  return deserializeAndVerifyJSONResponse(config.secretKey, body);
2280
2306
  };
2307
+ /**
2308
+ * Processes either a URLOK/URLKO query or a JSON REST notification
2309
+ *
2310
+ * @remarks
2311
+ * This utility exists only for backwards compatibility. Users are adviced
2312
+ * not to rely on the autodetection of this tool and use specific functions
2313
+ * @see {@link processRedirectNotification}
2314
+ * @see {@link processDirectRestNotification}
2315
+ *
2316
+ * @deprecated
2317
+ */
2318
+ const processRestNotification = (
2319
+ /** Body of JSON notification, as a POJO (Plain Old Javascript Object) */
2320
+ body) => {
2321
+ // A notification can't contain a gateway error, it didn't initiate the request
2322
+ const params = deserializeAndVerifyJSONResponse(config.secretKey, body);
2323
+ return typeof params.Ds_Date === 'string' && params.Ds_Date.includes('%2F')
2324
+ ? decodeUrlEntries(params)
2325
+ : params;
2326
+ };
2281
2327
  /**
2282
2328
  * Parses and verifies the body of a SOAP notification
2283
2329
  */
@@ -2303,6 +2349,8 @@ const createRedsysAPI = (config) => {
2303
2349
  restIniciaPeticion,
2304
2350
  restTrataPeticion,
2305
2351
  createRedirectForm,
2352
+ processRedirectNotification,
2353
+ processDirectRestNotification,
2306
2354
  processRestNotification,
2307
2355
  processSoapNotification,
2308
2356
  createSoapNotificationAnswer
package/lib/index.js CHANGED
@@ -1929,7 +1929,7 @@ const deserializeJSONMerchantParams = (strPayload) => {
1929
1929
  if (typeof strPayload !== 'string') {
1930
1930
  throw new ParseError('Payload must be a base-64 encoded string');
1931
1931
  }
1932
- const payload = JSON.parse(decodeURIComponent(Buffer.from(strPayload, 'base64').toString('utf8')));
1932
+ const payload = JSON.parse(Buffer.from(strPayload, 'base64').toString('utf8'));
1933
1933
  if (typeof payload !== 'object' || payload == null) {
1934
1934
  throw new ParseError('Cannot parse notification payload');
1935
1935
  }
@@ -2128,6 +2128,22 @@ const serializeAndSignJSONRequest = (merchantKey, requestParams) => {
2128
2128
  return sha256SignJSONRequest(merchantKey, serializedParams, requestParams);
2129
2129
  };
2130
2130
 
2131
+ /**
2132
+ * Tries to decode x-www-form-urlencoded values of an object
2133
+ */
2134
+ const decodeUrlEntries = (params) => {
2135
+ return Object.fromEntries(Object.entries(params).map((entry) => {
2136
+ let processedValue = entry[1];
2137
+ if (typeof entry[1] === 'string') {
2138
+ try {
2139
+ processedValue = decodeURIComponent(entry[1].replaceAll('+', ' '));
2140
+ }
2141
+ catch (_) { }
2142
+ }
2143
+ return [entry[0], processedValue];
2144
+ }));
2145
+ };
2146
+
2131
2147
  const serializeSoapNotificationResponse = ({ allow: allowOperation }) => {
2132
2148
  const answer = allowOperation ? 'OK' : 'KO';
2133
2149
  return `<Response Ds_Version="0.0"><Ds_Response_Merchant>${answer}</Ds_Response_Merchant></Response>`;
@@ -2272,14 +2288,44 @@ const createRedsysAPI = (config) => {
2272
2288
  };
2273
2289
  };
2274
2290
  /**
2275
- * Processes a JSON REST notification
2291
+ * Processes a URLOK/URLKO query notification
2276
2292
  */
2277
- const processRestNotification = (
2293
+ const processRedirectNotification = (
2294
+ /** Body of JSON notification, as a POJO (Plain Old Javascript Object) */
2295
+ body) => {
2296
+ // A notification can't contain a gateway error, it didn't initiate the request
2297
+ const params = deserializeAndVerifyJSONResponse(config.secretKey, body);
2298
+ return decodeUrlEntries(params);
2299
+ };
2300
+ /**
2301
+ * Processes a direct JSON REST notification
2302
+ */
2303
+ const processDirectRestNotification = (
2278
2304
  /** Body of JSON notification, as a POJO (Plain Old Javascript Object) */
2279
2305
  body) => {
2280
2306
  // A notification can't contain a gateway error, it didn't initiate the request
2281
2307
  return deserializeAndVerifyJSONResponse(config.secretKey, body);
2282
2308
  };
2309
+ /**
2310
+ * Processes either a URLOK/URLKO query or a JSON REST notification
2311
+ *
2312
+ * @remarks
2313
+ * This utility exists only for backwards compatibility. Users are adviced
2314
+ * not to rely on the autodetection of this tool and use specific functions
2315
+ * @see {@link processRedirectNotification}
2316
+ * @see {@link processDirectRestNotification}
2317
+ *
2318
+ * @deprecated
2319
+ */
2320
+ const processRestNotification = (
2321
+ /** Body of JSON notification, as a POJO (Plain Old Javascript Object) */
2322
+ body) => {
2323
+ // A notification can't contain a gateway error, it didn't initiate the request
2324
+ const params = deserializeAndVerifyJSONResponse(config.secretKey, body);
2325
+ return typeof params.Ds_Date === 'string' && params.Ds_Date.includes('%2F')
2326
+ ? decodeUrlEntries(params)
2327
+ : params;
2328
+ };
2283
2329
  /**
2284
2330
  * Parses and verifies the body of a SOAP notification
2285
2331
  */
@@ -2305,6 +2351,8 @@ const createRedsysAPI = (config) => {
2305
2351
  restIniciaPeticion,
2306
2352
  restTrataPeticion,
2307
2353
  createRedirectForm,
2354
+ processRedirectNotification,
2355
+ processDirectRestNotification,
2308
2356
  processRestNotification,
2309
2357
  processSoapNotification,
2310
2358
  createSoapNotificationAnswer
@@ -0,0 +1,4 @@
1
+ /**
2
+ * Tries to decode x-www-form-urlencoded values of an object
3
+ */
4
+ export declare const decodeUrlEntries: <T extends object>(params: T) => T;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "redsys-easy",
3
3
  "description": "Node JS interface for Redsys payment gateway",
4
- "version": "5.2.4",
4
+ "version": "5.2.5",
5
5
  "author": "Javier Garcia <javiertury@gmail.com>",
6
6
  "license": "MIT",
7
7
  "keywords": [