| 1 |
<?php |
| 2 |
|
| 3 |
namespace Hyperpay\Gateways\Helpers; |
| 4 |
|
| 5 |
if (! defined('ABSPATH')) exit; |
| 6 |
|
| 7 |
use WP_Error; |
| 8 |
use Hyperpay\Gateways\Helpers\Log; |
| 9 |
|
| 10 |
final class Http |
| 11 |
{ |
| 12 |
|
| 13 |
public static function post($url, $data = []) |
| 14 |
{ |
| 15 |
$response = wp_remote_post($url, \array_merge($data, ['timeout' => 10000])); // 10 seconds timeout |
| 16 |
return self::handelResponse($response); |
| 17 |
} |
| 18 |
|
| 19 |
public static function get($url, $options = []) |
| 20 |
{ |
| 21 |
$response = wp_remote_get($url, \array_merge($options, ['timeout' => 10000])); // 10 seconds timeout |
| 22 |
return self::handelResponse($response); |
| 23 |
} |
| 24 |
|
| 25 |
public static function delete($url, $data = []) |
| 26 |
{ |
| 27 |
$options = \array_merge($data, [ |
| 28 |
'method' => 'DELETE', |
| 29 |
'timeout' => 10000 |
| 30 |
]); |
| 31 |
|
| 32 |
$response = wp_remote_request($url, $options); |
| 33 |
return self::handelResponse($response); |
| 34 |
} |
| 35 |
|
| 36 |
public static function handelResponse($response) |
| 37 |
{ |
| 38 |
$result = json_decode(wp_remote_retrieve_body($response), true); |
| 39 |
|
| 40 |
if (is_wp_error($response)) { |
| 41 |
$error = $response->get_error_message(); |
| 42 |
Log::write(["error" => $error, "response" => $result]); |
| 43 |
|
| 44 |
return [ |
| 45 |
"result" => [ |
| 46 |
"code" => wp_remote_retrieve_response_code($response), |
| 47 |
"description" => $error |
| 48 |
] |
| 49 |
]; |
| 50 |
} |
| 51 |
|
| 52 |
return $result; |
| 53 |
} |
| 54 |
} |
| 55 |
|