Skip to content

Open source, composable payments platform | PCI compliant | SaaS and Self-host options | Enables connectivity to multiple payment, payout, fraud, vault and tokenization providers | Uplifts authorization with intelligent routing and revenue recovery | Reduce payment processing costs with cost observability | Reduces payment ops with reconciliation

2.8 KB
Raw Blame
import State from "../../../utils/State";

const globalState = new State({
  connectorId: Cypress.env("CONNECTOR"),
  baseUrl: Cypress.env("BASEURL"),
  adminApiKey: Cypress.env("ADMINAPIKEY"),
  connectorAuthFilePath: Cypress.env("CONNECTOR_AUTH_FILE_PATH"),
});

const connectorName = normalise(globalState.get("connectorId"));

function normalise(input) {
  const exceptions = {
    adyenplatform: "Adyenplatform",
    wise: "Wise",
    wellsfargo: "Wellsfargo",
    gotyme_sanlam: "GotymeSanlam",
    // Add more known exceptions here
  };

  if (typeof input !== "string") {
    const specName = Cypress.spec.name;

    if (specName.includes("-")) {
      const parts = specName.split("-");

      if (parts.length > 1 && parts[1].includes(".")) {
        return parts[1].split(".")[0];
      }
    }

    // Fallback
    return `${specName}`;
  }

  if (exceptions[input.toLowerCase()]) {
    return exceptions[input.toLowerCase()];
  } else {
    return input;
  }
}

/*
`getDefaultExchange` contains the default Request and Response to be considered if none provided.
`getCustomExchange` takes in 2 optional fields named as Request and Response.
with `getCustomExchange`, if 501 response is expected, there is no need to pass Response as it considers default values.
*/

// Connectors served exclusively by the Unified Connector Service surface the
// UCS server's message for unsupported payment methods: the UCS server
// prefixes the reason with "This feature is not implemented: " and uses the
// title-cased connector name (the router forwards it verbatim and appends the
// trailing " is not implemented"). Legacy-path connectors keep the
// router-generated message without the prefix, so only UCS-only connectors
// are listed here.
const UCS_SERVER_MESSAGE_CONNECTORS = new Set(["truelayer"]);

const rawConnectorId = String(
  globalState.get("connectorId") || ""
).toLowerCase();

const notImplementedMessage = UCS_SERVER_MESSAGE_CONNECTORS.has(rawConnectorId)
  ? `This feature is not implemented: Selected payment method through ${
      rawConnectorId.charAt(0).toUpperCase() + rawConnectorId.slice(1)
    } is not implemented`
  : `Selected payment method through ${connectorName} is not implemented`;

// Const to get default PaymentExchange object
const getDefaultExchange = () => ({
  Request: {},
  Response: {
    status: 501,
    body: {
      error: {
        type: "invalid_request",
        message: notImplementedMessage,
        code: "IR_00",
      },
    },
  },
});

// Const to get PaymentExchange with overridden properties
export const getCustomExchange = (overrides) => {
  const defaultExchange = getDefaultExchange();

  return {
    ...defaultExchange,
    Request: {
      ...defaultExchange.Request,
      ...(overrides.Request || {}),
    },
    Response: {
      ...defaultExchange.Response,
      ...(overrides.Response || {}),
    },
  };
};