|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Midtrans; |
| 4 | + |
| 5 | +use Exception; |
| 6 | +/** |
| 7 | + * Send request to Midtrans API |
| 8 | + * Better don't use this class directly, please use CoreApi, Snap, and Transaction instead |
| 9 | + */ |
| 10 | + |
| 11 | +class ApiRequestor |
| 12 | +{ |
| 13 | + |
| 14 | + /** |
| 15 | + * Send GET request |
| 16 | + * |
| 17 | + * @param string $url |
| 18 | + * @param string $server_key |
| 19 | + * @param mixed[] $data_hash |
| 20 | + * @return mixed |
| 21 | + * @throws Exception |
| 22 | + */ |
| 23 | + public static function get($url, $server_key, $data_hash) |
| 24 | + { |
| 25 | + return self::remoteCall($url, $server_key, $data_hash, 'GET'); |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * Send POST request |
| 30 | + * |
| 31 | + * @param string $url |
| 32 | + * @param string $server_key |
| 33 | + * @param mixed[] $data_hash |
| 34 | + * @return mixed |
| 35 | + * @throws Exception |
| 36 | + */ |
| 37 | + public static function post($url, $server_key, $data_hash) |
| 38 | + { |
| 39 | + return self::remoteCall($url, $server_key, $data_hash, 'POST'); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Send PATCH request |
| 44 | + * |
| 45 | + * @param string $url |
| 46 | + * @param string $server_key |
| 47 | + * @param mixed[] $data_hash |
| 48 | + * @return mixed |
| 49 | + * @throws Exception |
| 50 | + */ |
| 51 | + public static function patch($url, $server_key, $data_hash) |
| 52 | + { |
| 53 | + return self::remoteCall($url, $server_key, $data_hash, 'PATCH'); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Actually send request to API server |
| 58 | + * |
| 59 | + * @param string $url |
| 60 | + * @param string $server_key |
| 61 | + * @param mixed[] $data_hash |
| 62 | + * @param bool $post |
| 63 | + * @return mixed |
| 64 | + * @throws Exception |
| 65 | + */ |
| 66 | + public static function remoteCall($url, $server_key, $data_hash, $method) |
| 67 | + { |
| 68 | + $ch = curl_init(); |
| 69 | + |
| 70 | + if (!$server_key) { |
| 71 | + throw new Exception( |
| 72 | + 'The ServerKey/ClientKey is null, You need to set the server-key from Config. Please double-check Config and ServerKey key. ' . |
| 73 | + 'You can check from the Midtrans Dashboard. ' . |
| 74 | + 'See https://docs.midtrans.com/en/midtrans-account/overview?id=retrieving-api-access-keys ' . |
| 75 | + 'for the details or contact support at support@midtrans.com if you have any questions.' |
| 76 | + ); |
| 77 | + } else { |
| 78 | + if ($server_key == "") { |
| 79 | + throw new Exception( |
| 80 | + 'The ServerKey/ClientKey is invalid, as it is an empty string. Please double-check your ServerKey key. ' . |
| 81 | + 'You can check from the Midtrans Dashboard. ' . |
| 82 | + 'See https://docs.midtrans.com/en/midtrans-account/overview?id=retrieving-api-access-keys ' . |
| 83 | + 'for the details or contact support at support@midtrans.com if you have any questions.' |
| 84 | + ); |
| 85 | + } elseif (preg_match('/\s/',$server_key)) { |
| 86 | + throw new Exception( |
| 87 | + 'The ServerKey/ClientKey is contains white-space. Please double-check your API key. Please double-check your ServerKey key. ' . |
| 88 | + 'You can check from the Midtrans Dashboard. ' . |
| 89 | + 'See https://docs.midtrans.com/en/midtrans-account/overview?id=retrieving-api-access-keys ' . |
| 90 | + 'for the details or contact support at support@midtrans.com if you have any questions.' |
| 91 | + ); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + |
| 96 | + $curl_options = array( |
| 97 | + CURLOPT_URL => $url, |
| 98 | + CURLOPT_HTTPHEADER => array( |
| 99 | + 'Content-Type: application/json', |
| 100 | + 'Accept: application/json', |
| 101 | + 'User-Agent: midtrans-php-v2.6.1', |
| 102 | + 'Authorization: Basic ' . base64_encode($server_key . ':') |
| 103 | + ), |
| 104 | + CURLOPT_RETURNTRANSFER => 1 |
| 105 | + ); |
| 106 | + |
| 107 | + // Set append notification to header |
| 108 | + if (Config::$appendNotifUrl) Config::$curlOptions[CURLOPT_HTTPHEADER][] = 'X-Append-Notification: ' . Config::$appendNotifUrl; |
| 109 | + // Set override notification to header |
| 110 | + if (Config::$overrideNotifUrl) Config::$curlOptions[CURLOPT_HTTPHEADER][] = 'X-Override-Notification: ' . Config::$overrideNotifUrl; |
| 111 | + // Set payment idempotency-key to header |
| 112 | + if (Config::$paymentIdempotencyKey) Config::$curlOptions[CURLOPT_HTTPHEADER][] = 'Idempotency-Key: ' . Config::$paymentIdempotencyKey; |
| 113 | + |
| 114 | + // merging with Config::$curlOptions |
| 115 | + if (count(Config::$curlOptions)) { |
| 116 | + // We need to combine headers manually, because it's array and it will no be merged |
| 117 | + if (Config::$curlOptions[CURLOPT_HTTPHEADER]) { |
| 118 | + $mergedHeaders = array_merge($curl_options[CURLOPT_HTTPHEADER], Config::$curlOptions[CURLOPT_HTTPHEADER]); |
| 119 | + $headerOptions = array(CURLOPT_HTTPHEADER => $mergedHeaders); |
| 120 | + } else { |
| 121 | + $mergedHeaders = array(); |
| 122 | + $headerOptions = array(CURLOPT_HTTPHEADER => $mergedHeaders); |
| 123 | + } |
| 124 | + |
| 125 | + $curl_options = array_replace_recursive($curl_options, Config::$curlOptions, $headerOptions); |
| 126 | + } |
| 127 | + |
| 128 | + if ($method != 'GET') { |
| 129 | + |
| 130 | + if ($data_hash) { |
| 131 | + $body = json_encode($data_hash); |
| 132 | + $curl_options[CURLOPT_POSTFIELDS] = $body; |
| 133 | + } else { |
| 134 | + $curl_options[CURLOPT_POSTFIELDS] = ''; |
| 135 | + } |
| 136 | + |
| 137 | + if ($method == 'POST') { |
| 138 | + $curl_options[CURLOPT_POST] = 1; |
| 139 | + } elseif ($method == 'PATCH') { |
| 140 | + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH'); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + curl_setopt_array($ch, $curl_options); |
| 145 | + |
| 146 | + // For testing purpose |
| 147 | + if (class_exists('\Midtrans\MT_Tests') && MT_Tests::$stubHttp) { |
| 148 | + $result = self::processStubed($curl_options, $url, $server_key, $data_hash, $method); |
| 149 | + } else { |
| 150 | + $result = curl_exec($ch); |
| 151 | + // curl_close($ch); |
| 152 | + } |
| 153 | + |
| 154 | + |
| 155 | + if ($result === false) { |
| 156 | + throw new Exception('CURL Error: ' . curl_error($ch), curl_errno($ch)); |
| 157 | + } else { |
| 158 | + try { |
| 159 | + $result_array = json_decode($result); |
| 160 | + } catch (Exception $e) { |
| 161 | + throw new Exception("API Request Error unable to json_decode API response: ".$result . ' | Request url: '.$url); |
| 162 | + } |
| 163 | + $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
| 164 | + if (isset($result_array->status_code) && $result_array->status_code >= 401 && $result_array->status_code != 407) { |
| 165 | + throw new Exception('Midtrans API is returning API error. HTTP status code: ' . $result_array->status_code . ' API response: ' . $result, $result_array->status_code); |
| 166 | + } elseif ($httpCode >= 400) { |
| 167 | + throw new Exception('Midtrans API is returning API error. HTTP status code: ' . $httpCode . ' API response: ' . $result, $httpCode); |
| 168 | + } else { |
| 169 | + return $result_array; |
| 170 | + } |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + private static function processStubed($curl, $url, $server_key, $data_hash, $method) |
| 175 | + { |
| 176 | + MT_Tests::$lastHttpRequest = array( |
| 177 | + "url" => $url, |
| 178 | + "server_key" => $server_key, |
| 179 | + "data_hash" => $data_hash, |
| 180 | + $method => $method, |
| 181 | + "curl" => $curl |
| 182 | + ); |
| 183 | + |
| 184 | + return MT_Tests::$stubHttpResponse; |
| 185 | + } |
| 186 | +} |
0 commit comments