Created
December 18, 2024 16:05
-
-
Save gazmend-sahiti/885621fdb779485ace6d44c4f51a3dcc to your computer and use it in GitHub Desktop.
iPaymer Guzzle Client example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Services; | |
use GuzzleHttp\Client; | |
use GuzzleHttp\Exception\RequestException; | |
class iPaymer | |
{ | |
private $endpoint = 'http://127.0.0.1:8080/api/v{version}/'; | |
private $secret; | |
private $client; | |
private function __construct($secret, $v) | |
{ | |
$this->secret = $secret; | |
$this->endpoint = str_replace('{version}', $v, $this->endpoint); | |
$this->client = new Client(); // Guzzle HTTP client instance | |
} | |
public static function make($secret, $v = '1') | |
{ | |
return new iPaymer($secret, $v); | |
} | |
private function headers() | |
{ | |
return [ | |
'X-iPaymer-Secret' => $this->secret, | |
'Accept' => 'application/json', | |
]; | |
} | |
public function sendGetRequest($endpoint, $parameters = []) | |
{ | |
$url = $this->endpoint . ltrim($endpoint, '/'); | |
try { | |
$response = $this->client->get($url, [ | |
'headers' => $this->headers(), | |
'query' => $parameters, | |
]); | |
return $this->constructResponse($url, $response); | |
} catch (RequestException $e) { | |
return $this->handleException($e, $url); | |
} | |
} | |
public function sendPutRequest($endpoint, $parameters) | |
{ | |
return $this->sendPostRequest($endpoint, array_merge($parameters, [ | |
'_method' => 'PUT', | |
])); | |
} | |
public function sendDeleteRequest($endpoint, $parameters) | |
{ | |
return $this->sendPostRequest($endpoint, array_merge($parameters, [ | |
'_method' => 'DELETE', | |
])); | |
} | |
public function sendPostRequest($endpoint, $parameters = []) | |
{ | |
$url = $this->endpoint . ltrim($endpoint, '/'); | |
try { | |
$response = $this->client->post($url, [ | |
'headers' => $this->headers(), | |
'form_params' => $parameters, | |
]); | |
return $this->constructResponse($url, $response); | |
} catch (RequestException $e) { | |
return $this->handleException($e, $url); | |
} | |
} | |
private function constructResponse($url, $response) | |
{ | |
$body = json_decode($response->getBody(), true); | |
return [ | |
'endpoint' => $url, | |
'response' => $response->getBody(), | |
'body' => $body ?: new \stdClass(), | |
'code' => $response->getStatusCode(), | |
'error' => null, | |
]; | |
} | |
private function handleException(RequestException $e, $url) | |
{ | |
$errorMessage = $e->getMessage(); | |
$response = $e->getResponse(); | |
return [ | |
'endpoint' => $url, | |
'response' => $errorMessage, | |
'body' => new \stdClass(), | |
'code' => $response ? $response->getStatusCode() : 500, | |
'error' => $errorMessage, | |
]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment