config = $config; } public function send(Webhook $webhook, array $dataList) : int { $payload = json_encode($dataList); $signature = null; $secretKey = $webhook->get('secretKey'); if ($secretKey) { $signature = $this->buildSignature($webhook, $payload, $secretKey); } $connectTimeout = $this->config->get('webhookConnectTimeout', self::CONNECT_TIMEOUT); $timeout = $this->config->get('webhookTimeout', self::TIMEOUT); $headerList = []; $headerList[] = 'Content-Type: application/json'; $headerList[] = 'Content-Length: ' . strlen($payload); if ($signature) { $headerList[] = 'X-Signature: ' . $signature; } $handler = curl_init($webhook->get('url')); curl_setopt($handler, \CURLOPT_RETURNTRANSFER, true); curl_setopt($handler, \CURLOPT_FOLLOWLOCATION, true); curl_setopt($handler, \CURLOPT_SSL_VERIFYPEER, false); curl_setopt($handler, \CURLOPT_HEADER, true); curl_setopt($handler, \CURLOPT_CUSTOMREQUEST, 'POST'); curl_setopt($handler, \CURLOPT_CONNECTTIMEOUT, $connectTimeout); curl_setopt($handler, \CURLOPT_TIMEOUT, $timeout); curl_setopt($handler, \CURLOPT_HTTPHEADER, $headerList); curl_setopt($handler, \CURLOPT_POSTFIELDS, $payload); curl_exec($handler); $code = curl_getinfo($handler, \CURLINFO_HTTP_CODE); if (!is_numeric($code)) $code = 0; if (!is_int($code)) $code = intval($code); if ($errorNumber = curl_errno($handler)) { if (in_array($errorNumber, [\CURLE_OPERATION_TIMEDOUT, \CURLE_OPERATION_TIMEOUTED])) { $code = 408; } } curl_close($handler); return $code; } protected function buildSignature(Webhook $webhook, string $payload, string $secretKey) { return base64_encode($webhook->id . ':' . hash_hmac('sha256', $payload, $secretKey, true)); } }