# transfersmile SDK

Uso del SDK para validar el formato de cada campo

# Validación de Parámetros

Método Arg1 Arg2 Resultado Notas
formatId(value, code) (string) Id a formatear (string) ISO 3166, 3 código digital de país (MEX, CHL, COL, PER, ECU, BRA, PAN, CRI, SLV, GTM, NIC) (string) formatted id IDs pueden ser formateadas: (CPF/CNPJ, RFC, NIT/CC, RUT/RUN, DNI/RUC, RUC, CIP, DUI, DPI)
validateId(value, code) (string) Id a validar (string) ISO 3166, 3 código digital de país (MEX, CHL, COL, PER, ECU, BRA, PAN, CRI, SLV, GTM, NIC) (list) [isIdvalid (boolean), idType (string) ] IDs pueden ser validadas: CPF/CNPJ, RFC, NIT/CC, RUT/RUN, DNI/RUC, RUC, CIP, DUI, DPI
validateName(value) (string) nombre a validar - Sólo funcional en MEX, CHL, COL, PER, ECU, BRA, PAN, CRI, SLV, GTM, NIC
validateEmail(value) (string) email a validar - (boolean) true/false
formatCEP(value) (string) CEP (zip code) a formatear - (string) formatted CEP (zip code)
validateCEP(value) (string) CEP a validar - (boolean) true/false
transfersmileDocNumclearData(value) (string) Datos a borrar para eliminar caracteres innecesarios (-/.). - (string) cleared data Antes de la solicitud API, los parámetros con formato deben borrarse

Paso 1: Importe la siguiente library al agregar un en medio del tag en su código.

<script type="text/javascript" src="https://checkout.transfersmile.com/public/transfersmileSDK/outboundlibs/transfersmileSDK.min.js"></script>

Paso 2: Call los métodos anteriores listados usando transfersmileSDK.

Ejemplo:

transfersmileSDK.formatId("50284414727","BRA") // 502.844.147-27
transfersmileSDK.validateId("502.844.147-27", "BRA") // [true, 'CPF']
transfersmileSDK.clearData("502.844.147-27") // 50284414727

# Demo Website

Click here para accesar el demo website.

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Example</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script type="text/javascript" src="https://checkout.transfersmile.com/public/transfersmileSDK/outboundlibs/transfersmileSDK.min.js"></script>
    <style type="text/css">
      .error {
        border-color: red;
      }
    </style>
  </head>
  <body>
    <label for="">RFC: </label>
    <input type="text" id="input-rfc" /> <button id="submit-mx-id">Submit RFC</button>
    <br />

    <br />
    <label for="">CPF/CNPJ: </label>
    <input type="text" id="input-br-id" /> <button id="submit-br-id">Submit CPF/CNPJ</button>
    <br />

    <br />
    <label for="">CEP: </label>
    <input type="text" id="input-cep" /> <button id="submit-cep">Submit CEP</button>
    <br />

    <script type="text/javascript" charset="utf-8">
      $("#input-rfc").on("input", (e) => {
        const value = e.target.value;
        const idNode = $("#input-rfc");

        const formattedId = transfersmileSDK.formatId(value, "MEX");
        idNode.val(formattedId);

        const [isIdvalid, idType] = transfersmileSDK.validateId(idNode.val(), "MEX");

        if (isIdvalid) {
          idNode.removeClass("error");
        } else {
          idNode.addClass("error");
        }
      });
      $("#submit-mx-id").click(() => {
        const idNode = $("#input-rfc");
        const [isIdvalid, idType] = transfersmileSDK.validateId(idNode.val(), "MEX");
        if (isIdvalid) {
          const requestData = {
            customer: {
              identify: {
                number: transfersmileSDK.clearData(idNode.val()),
                type: idType,
              }, // .....
            },
          };

          window.alert(JSON.stringify(requestData));
        }
      });

      $("#input-br-id").on("input", (e) => {
        const value = e.target.value;
        const idNode = $("#input-br-id");

        const formattedId = transfersmileSDK.formatId(value, "BRA");
        idNode.val(formattedId);

        const [isIdvalid, idType] = transfersmileSDK.validateId(idNode.val(), "BRA");
        if (isIdvalid) {
          idNode.removeClass("error");
        } else {
          idNode.addClass("error");
        }
      });
      $("#submit-br-id").click(() => {
        const idNode = $("#input-br-id");
        const [isIdvalid, idType] = transfersmileSDK.validateId(idNode.val(), "BRA");
        if (isIdvalid) {
          const requestData = {
            customer: {
              identify: {
                number: transfersmileSDK.clearData(idNode.val()),
                type: idType,
              }, // .....
            },
          };

          window.alert(JSON.stringify(requestData));
        }
      });

      $("#input-cep").on("input", (e) => {
        const value = e.target.value;
        const cepNode = $("#input-cep");

        const formattedCep = transfersmileSDK.formatCEP(value);
        cepNode.val(formattedCep);

        const isCepValid = transfersmileSDK.validateCEP(cepNode.val());
        if (isCepValid) {
          cepNode.removeClass("error");
        } else {
          cepNode.addClass("error");
        }
      });
      $("#submit-cep").click(() => {
        const cepNode = $("#input-cep");
        const isCepValid = transfersmileSDK.validateCEP(cepNode.val());
        if (isCepValid) {
          const requestData = {
            customer: {
              address: {
                zip_code: transfersmileSDK.clearData(cepNode.val()),
              },
            },
          };

          window.alert(JSON.stringify(requestData));
        }
      });
    </script>
  </body>
</html>