# Validar Webhook

Usuario mal intencionados podrían tratar de falsificar un webhook de Wompi para forzar que una transacción se marque como pagada en el sistema cliente. Se ofrecen dos formas para evitar esto.

## Validar con Hash

Todo webhook enviado por Wompi incluirá el header "wompi\_hash", este header es el hash SHA256 del contenido del body calculado con HMAC.&#x20;

Los pasos para validar son:

1. Leer el "body" completo del webhook. **Asegurarse leerlo tal cual es enviando sin agregar ningún espacio o salto de linea**.
2. Sacar el HMAC con algoritmo SHA256 del "body" utilizando el API Secret de su aplicativo de Wompi como llave del HMAC
3. Comparar el resultado del punto anterior con el valor de header "wompi\_hash", si es igual el webhook se puede considerar válido.

### Ejemplos

Nuestro plugin de Woocommerce ya hace esta validación, para ver como se realiza la validación en PHP puede consultar el código [aqui](https://github.com/wompisv/wocommerce-wompi-sv-plugin/blob/master/wc-WOMPI-payment.php#L41).

Si desea calcular un hash SHA256 con HMAC en C# puede usar este código

```
        private static string EncriptarData(string body, string secret)
        {
            UTF8Encoding encoding = new UTF8Encoding();

            var hash = new HMACSHA256(encoding.GetBytes(secret));
            byte[] stream = hash.ComputeHash(encoding.GetBytes(body));
            string textHash = string.Concat(stream.Select(b => b.ToString("x2")));
            hash.Dispose();
            return textHash;
        }
```

Si desea probar en linea el calculo del HMAC del webhook puede usar esta herramienta: <https://www.freeformatter.com/hmac-generator.html> colocando SHA256 en el algoritmo Digest y su Api Secret en el "Secret Key".

## Validar Consultando Transacción

Como se puede ver en nuestro [Swagger](https://api.wompi.sv/index.html), se incluye un método para extraer los datos de una transacción **/TransaccionCompra/{id}**. Puede utilizar la propiedad "IdTransaccion" para consultar los datos de la transacción y así confirmar que exista y este aprobada.&#x20;


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.wompi.sv/webhook/validar-webhook.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
