serviceId = $serviceId; $this->serviceKey = $serviceKey; $this->isDebugMode = $isDebugMode; if($orderArrangement) { $this->orderArrangement = $orderArrangement; } } /** * @param string $status * @param string $code * @param string|int|null $statusBefore * @param string|int|null $statusAfter * * @return string */ public function formatResponse($status, $code = '', $statusBefore = null, $statusAfter = null) { $response = [ 'status' => $status, ]; if($this->isDebugMode && $code) { $response['data'] = [ 'code' => $code, ]; } if($this->orderArrangement) { $response['data']['creatingOrderMode'] = $this->orderArrangement; } if($statusBefore) { $response['data']['statusBefore'] = $statusBefore; } if($statusAfter) { $response['data']['statusAfter'] = $statusAfter; } // region adds additional data for some cases and set proper header switch($status) { case self::NS_OK: header('HTTP/1.1 200 OK'); break; case self::NS_ERROR: header('HTTP/1.1 404 Not Found'); break; default: break; } // endregion header('Content-Type: application/json'); return json_encode($response); } /** * @param array $payloadDecoded * @param int $amount * @param string $currency * * @return bool */ public static function checkRequestAmount($payloadDecoded, $amount, $currency) { return $payloadDecoded['transaction']['amount'] === $amount && $payloadDecoded['transaction']['currency'] === $currency; } /** * Verify notification body and signature * * @return bool|array * @throws Exception */ public function checkRequest() { if(!isset($_SERVER['CONTENT_TYPE'], $_SERVER[self::HEADER_SIGNATURE_NAME]) || strpos($_SERVER['CONTENT_TYPE'], 'application/json') !== 0) { return self::NC_INVALID_SIGNATURE_HEADERS; } $payload = file_get_contents('php://input', true); if(!$payload) { return self::NC_EMPTY_NOTIFICATION; } if(!Util::isJson($payload)) { return self::NC_NOTIFICATION_IS_NOT_JSON; } $header = $_SERVER[self::HEADER_SIGNATURE_NAME]; $header = (explode(';', $header)); $algoFromNotification = explode('=', $header[3]); $algoFromNotification = $algoFromNotification[1]; $headerSignature = explode('=', $header[2]); if($headerSignature[1] !== Util::hashSignature($algoFromNotification, $payload, $this->serviceKey)) { return self::NC_INVALID_SIGNATURE; } try { Validate::notification($payload); } catch(Exception $e) { return self::NC_INVALID_JSON_STRUCTURE; } $payloadDecoded = json_decode($payload, true); if($payloadDecoded['transaction']['serviceId'] !== $this->serviceId) { return self::NC_SERVICE_ID_NOT_MATCH; } return $payloadDecoded; } }