wl-online-payments-direct / sdk-nodejs

Other
3 stars 0 forks source link

TypeError: sdkContext.getLogger is not a function - Context used as instance though require('/context') retrieves a function #4

Closed TheoGautier closed 2 years ago

TheoGautier commented 2 years ago

Hi Worldline,

Version: 4.0.0

Context: Failing validation of create payment body causes TypeError: sdkContext.getLogger is not a function in validation file. Parameters (I only explicit failing parameter):

const paymentDetails = paymentDetails: {
  order: {
    customer: {
      device: {
        browserData: {
          screenHeight: 1080
        }
      }
    }
  }
}

sdk.payments.createPayment(pspid, paymentDetails, null, (error, sdkResponse) => {...}).catch(error => log.error(error))
TypeError: sdkContext.getLogger is not a function
    at Object.validatePostData (/[...]/node_modules/onlinepayments-sdk-nodejs/src/utils/validator.js:9:33)

Expected:

Error: instance.order.customer.device.browserData.screenHeight: is not of a type(s) string

Cause:

//src/utils/validator.js
const { validate } = require('jsonschema');
const sdkContext = require('./context');

validatePostData(postData, requestSchema) {
  const isValidRequest = validate(postData, requestSchema);
  if (!isValidRequest.valid) {
    const logger = sdkContext.getLogger();
    if (sdkContext.isLoggingEnabled()) {
      logger('error', isValidRequest.errors);
    }
    throw new Error(isValidRequest.errors.map((error) => `${error.property}: ${error.message}`).join('\n'));
  }
},

sdkContext is a function, not an object. A similar code can be found in src/index.js at line 35: const sdkContext = clientContext(); (const clientContext = require('./utils/context'); as line 5 states).

I tried to create a Pull Request but I don't have the rights, here's my fix:

src/utils/obfuscate.js:

-     context = require('./context');
+     const clientContext = require('./context');
+     context = clientContext();

src/utils/validator.js

-const sdkContext = require('./context');
+const clientContext = require('./context');

 module.exports = {
   validatePostData(postData, requestSchema) {
     const isValidRequest = validate(postData, requestSchema);
     if (!isValidRequest.valid) {
+      const sdkContext = clientContext();

Can you please fix this? Thank you. I ran tests with this fix, and it works well.

worldline-direct-support-team commented 2 years ago

Hello Theo,

Thank you for reporting the issue, and providing a fix. We we will look into this, and release a version with the fix as soon as possible.

Kind regards, Worldline Support

worldline-direct-support-team commented 2 years ago

Hello Theo,

The fix was just released in version 4.0.1. There are some changes compared to your fix.

This change will fix the related issue, please let us know if you experience any other issues.

Kind regards, Worldline Support