All MicroEvals
Minden hibat kerees meg! <?php namespace Barion; if (file_...
Create MicroEval
Header image for Minden hibat kerees meg! <?php

namespace Barion;

if (file_...

Minden hibat kerees meg! <?php namespace Barion; if (file_...

Prompt

Minden hibat kerees meg! <?php namespace Barion; if (file_exists(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php'; } else { require_once 'autoload.php'; } use Barion\Enumerations\BarionEnvironment; use Barion\Enumerations\QRCodeSize; use Barion\Exceptions\BarionException; use Barion\Models\BaseResponseModel; use Barion\Models\Error\ApiErrorModel; use CurlHandle; use Barion\Models\Payment\PreparePaymentRequestModel; use Barion\Models\Payment\PreparePaymentResponseModel; use Barion\Models\Payment\FinishReservationRequestModel; use Barion\Models\Payment\FinishReservationResponseModel; use Barion\Models\Payment\GetPaymentStateRequestModel; use Barion\Models\Payment\GetPaymentStateResponseModel; use Barion\Models\Payment\CaptureRequestModel; use Barion\Models\Payment\CaptureResponseModel; use Barion\Models\Payment\CancelAuthorizationRequestModel; use Barion\Models\Payment\CancelAuthorizationResponseModel; use Barion\Models\Payment\Complete3DSPaymentRequestModel; use Barion\Models\Payment\Complete3DSPaymentResponseModel; use Barion\Models\Payment\PaymentStateRequestModel; use Barion\Models\Payment\PaymentStateResponseModel; use Barion\Models\Payment\PaymentQRRequestModel; use Barion\Models\Refund\RefundRequestModel; use Barion\Models\Refund\RefundResponseModel; class BarionClient { private const MINIMUM_PHP_VERSION = "8.2"; public const BARION_API_URL_PROD = "https://api.barion.com"; public const BARION_WEB_URL_PROD = "https://secure.barion.com/Pay"; public const BARION_API_URL_TEST = "https://api.test.barion.com"; public const BARION_WEB_URL_TEST = "https://secure.test.barion.com/Pay"; public const API_ENDPOINT_PREPAREPAYMENT = "/Payment/Start"; public const API_ENDPOINT_GETPAYMENTSTATE = "/Payment/GetPaymentState"; public const API_ENDPOINT_PAYMENTSTATE = "/Payment/{paymentId}/PaymentState"; public const API_ENDPOINT_QRCODE = "/QR/Generate"; public const API_ENDPOINT_REFUND = "/Payment/Refund"; public const API_ENDPOINT_FINISHRESERVATION = "/Payment/FinishReservation"; public const API_ENDPOINT_CAPTURE = "/Payment/Capture"; public const API_ENDPOINT_CANCELAUTHORIZATION = "/Payment/CancelAuthorization"; public const API_ENDPOINT_3DS_COMPLETE = "/Payment/Complete"; private BarionEnvironment $Environment; private int $APIVersion; private string $POSKey; private string $BARION_API_URL = ""; private string $BARION_WEB_URL = ""; private bool $UseBundledRootCertificates; public function __construct(string $poskey, int $version = 2, BarionEnvironment $env = BarionEnvironment::Prod, bool $useBundledRootCerts = false) { if (version_compare(phpversion(), self::MINIMUM_PHP_VERSION, '<')) { throw new BarionException("The Barion PHP library requires at least PHP version " . self::MINIMUM_PHP_VERSION . " to function. Please update your PHP installation."); } if (!extension_loaded('curl')) { throw new BarionException("The Barion PHP library requires the cURL module to function. Please check your system configuration."); } $this->POSKey = $poskey; $this->APIVersion = $version; $this->Environment = $env; switch ($env) { case BarionEnvironment::Test: $this->BARION_API_URL = self::BARION_API_URL_TEST; $this->BARION_WEB_URL = self::BARION_WEB_URL_TEST; break; case BarionEnvironment::Prod: default: $this->BARION_API_URL = self::BARION_API_URL_PROD; $this->BARION_WEB_URL = self::BARION_WEB_URL_PROD; break; } $this->UseBundledRootCertificates = $useBundledRootCerts; } public function SetVersion(int $version): void { $this->APIVersion = $version; } public function PreparePayment(PreparePaymentRequestModel $model): PreparePaymentResponseModel { if ($this->APIVersion !== 2) { throw new BarionException("Incorrect API version for Payment Prepare endpoint! Current: {$this->APIVersion}. Expected: 2."); } $model->POSKey = $this->POSKey; $url = $this->BARION_API_URL . "/v" . $this->APIVersion . self::API_ENDPOINT_PREPAREPAYMENT; $response = $this->PostToBarion($url, $model); $rm = new PreparePaymentResponseModel(); if (!empty($response) && is_string($response)) { $json = json_decode($response, true); if (is_array($json)) { $rm->fromJson($json); } if (!empty($rm->PaymentId)) { $rm->PaymentRedirectUrl = $this->BARION_WEB_URL . "?" . http_build_query(["id" => $rm->PaymentId]); } } return $rm; } public function FinishReservation(FinishReservationRequestModel $model): FinishReservationResponseModel { if ($this->APIVersion !== 2) { throw new BarionException("Incorrect API version for Finish Reservation endpoint! Current: {$this->APIVersion}. Expected: 2."); } $model->POSKey = $this->POSKey; $url = $this->BARION_API_URL . "/v" . $this->APIVersion . self::API_ENDPOINT_FINISHRESERVATION; $response = $this->PostToBarion($url, $model); $rm = new FinishReservationResponseModel(); if (!empty($response) && is_string($response)) { $json = json_decode($response, true); if (is_array($json)) { $rm->fromJson($json); } } return $rm; } public function Capture(CaptureRequestModel $model): CaptureResponseModel { if ($this->APIVersion !== 2) { throw new BarionException("Incorrect API version for Capture endpoint! Current: {$this->APIVersion}. Expected: 2."); } $model->POSKey = $this->POSKey; $url = $this->BARION_API_URL . "/v" . $this->APIVersion . self::API_ENDPOINT_CAPTURE; $response = $this->PostToBarion($url, $model); $captureResponse = new CaptureResponseModel(); if (!empty($response) && is_string($response)) { $json = json_decode($response, true); if (is_array($json)) { $captureResponse->fromJson($json); } } return $captureResponse; } public function CancelAuthorization(CancelAuthorizationRequestModel $model): CancelAuthorizationResponseModel { if ($this->APIVersion !== 2) { throw new BarionException("Incorrect API version for Cancel Authorization endpoint! Current: {$this->APIVersion}. Expected: 2."); } $model->POSKey = $this->POSKey; $url = $this->BARION_API_URL . "/v" . $this->APIVersion . self::API_ENDPOINT_CANCELAUTHORIZATION; $response = $this->PostToBarion($url, $model); $cancelAuthResponse = new CancelAuthorizationResponseModel(); if (!empty($response) && is_string($response)) { $json = json_decode($response, true); if (is_array($json)) { $cancelAuthResponse->fromJson($json); } } return $cancelAuthResponse; } public function Complete3DSPayment(Complete3DSPaymentRequestModel $model): Complete3DSPaymentResponseModel { if ($this->APIVersion !== 2) { throw new BarionException("Incorrect API version for 3DS Complete endpoint! Current: {$this->APIVersion}. Expected: 2."); } $model->POSKey = $this->POSKey; $url = $this->BARION_API_URL . "/v" . $this->APIVersion . self::API_ENDPOINT_3DS_COMPLETE; $response = $this->PostToBarion($url, $model); $rm = new Complete3DSPaymentResponseModel(); if (!empty($response) && is_string($response)) { $json = json_decode($response, true); if (is_array($json)) { $rm->fromJson($json); } } return $rm; } public function RefundPayment(RefundRequestModel $model): RefundResponseModel { if ($this->APIVersion !== 2) { throw new BarionException("Incorrect API version for Refund endpoint! Current: {$this->APIVersion}. Expected: 2."); } $model->POSKey = $this->POSKey; $url = $this->BARION_API_URL . "/v" . $this->APIVersion . self::API_ENDPOINT_REFUND; $response = $this->PostToBarion($url, $model); $rm = new RefundResponseModel(); if (!empty($response) && is_string($response)) { $json = json_decode($response, true); if (is_array($json)) { $rm->fromJson($json); } } return $rm; } public function GetPaymentState(string $paymentId): GetPaymentStateResponseModel { if ($this->APIVersion !== 2) { throw new BarionException("Incorrect API version for GetPaymentState endpoint! Current: {$this->APIVersion}. Expected: 2."); } $model = new GetPaymentStateRequestModel($paymentId); $model->POSKey = $this->POSKey; $url = $this->BARION_API_URL . "/v" . $this->APIVersion . self::API_ENDPOINT_GETPAYMENTSTATE; $response = $this->GetFromBarion($url, $model); $ps = new GetPaymentStateResponseModel(); if (!empty($response) && is_string($response)) { $json = json_decode($response, true); if (is_array($json)) { $ps->fromJson($json); } } return $ps; } public function PaymentState(string $paymentId): PaymentStateResponseModel { if ($this->APIVersion !== 4) { throw new BarionException("Incorrect API version for PaymentState endpoint! Current: {$this->APIVersion}. Expected: 4."); } $model = new PaymentStateRequestModel($paymentId); $model->POSKey = $this->POSKey; $url = $this->BARION_API_URL . "/v" . $this->APIVersion . str_ireplace("{paymentId}", rawurlencode($paymentId), self::API_ENDPOINT_PAYMENTSTATE); $response = $this->GetFromBarion($url, $model); $ps = new PaymentStateResponseModel(); if (!empty($response) && is_string($response)) { $json = json_decode($response, true); if (is_array($json)) { $ps->fromJson($json); } } return $ps; } public function GetPaymentQRImage(string $username, string $password, string $paymentId, QRCodeSize $qrCodeSize = QRCodeSize::Large): string|bool { if ($this->APIVersion !== 1) { throw new BarionException("Incorrect API version for QR Code endpoint! Current: {$this->APIVersion}. Expected: 1."); } $model = new PaymentQRRequestModel($username, $password, $paymentId); $model->POSKey = $this->POSKey; $model->Size = $qrCodeSize; $url = $this->BARION_API_URL . self::API_ENDPOINT_QRCODE; return $this->GetFromBarion($url, $model); } private function ResolveUserAgent(): string { $userAgent = $_SERVER['HTTP_USER_AGENT'] ?? ''; if ($userAgent === '') { $cver = curl_version(); $userAgent = "curl/" . ($cver["version"] ?? "") . " " . ($cver["ssl_version"] ?? ""); } return trim($userAgent); } private function PostToBarion(string $url, object $data): string|bool { $ch = curl_init(); if ($ch === false) { $error = new ApiErrorModel(); $error->ErrorCode = "CURL_INIT_FAILED"; $error->Title = "CURL Init Failed"; $error->Description = "Failed to initialize cURL handle."; $response = new BaseResponseModel(); $response->Errors = [$error]; return json_encode($response); } $posKey = $this->POSKey; $userAgent = $this->ResolveUserAgent(); $postData = json_encode($data); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $postData); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, [ "Content-Type: application/json", "User-Agent: {$userAgent}", "x-pos-key: {$posKey}" ]); return $this->PostWithCurl($ch); } private function GetFromBarion(string $url, object $data): string|bool { $ch = curl_init(); if ($ch === false) { $error = new ApiErrorModel(); $error->ErrorCode = "CURL_INIT_FAILED"; $error->Title = "CURL Init Failed"; $error->Description = "Failed to initialize cURL handle."; $response = new BaseResponseModel(); $response->Errors = [$error]; return json_encode($response); } $posKey = $this->POSKey; $getData = http_build_query($data); $fullUrl = $getData !== '' ? ($url . '?' . $getData) : $url; $userAgent = $this->ResolveUserAgent(); curl_setopt($ch, CURLOPT_URL, $fullUrl); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, [ "User-Agent: {$userAgent}", "x-pos-key: {$posKey}" ]); return $this->PostWithCurl($ch); } private function PostWithCurl(CurlHandle $ch): string|bool { if ($this->UseBundledRootCertificates) { $caInfo = implode(DIRECTORY_SEPARATOR, [__DIR__, 'SSL', 'cacert.pem']); if (file_exists($caInfo)) { curl_setopt($ch, CURLOPT_CAINFO, $caInfo); } if ($this->Environment === BarionEnvironment::Test) { $caPath = implode(DIRECTORY_SEPARATOR, [__DIR__, 'SSL', 'gd_bundle-g2.crt']); if (file_exists($caPath)) { curl_setopt($ch, CURLOPT_CAPATH, $caPath); } } } $output = curl_exec($ch); $err_nr = curl_errno($ch); if ($err_nr !== 0) { $error = new ApiErrorModel(); $error->ErrorCode = "CURL_ERROR"; $error->Title = "CURL Error #" . $err_nr; $error->Description = curl_error($ch); $response = new BaseResponseModel(); $response->Errors = [$error]; $output = json_encode($response); } curl_close($ch); return $output; } }

Response not available

Drag to resize
Drag to resize
Drag to resize