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