/** * create the cURL request and execute it */ private static function make_request($endpoint, $values, $headers = array()) { self::$ch = curl_init(); $headers = array_merge(array("Content-Type: application/json"), $headers); // always pass the correct Content-Type header // send Api Version header if (!empty(self::$api_version)) { $headers[] = "Api-Version: " . self::$api_version; } curl_setopt(self::$ch, CURLOPT_USERAGENT, 'WePay v2 PHP SDK v' . self::VERSION); curl_setopt(self::$ch, CURLOPT_RETURNTRANSFER, true); curl_setopt(self::$ch, CURLOPT_HTTPHEADER, $headers); curl_setopt(self::$ch, CURLOPT_TIMEOUT, 30); // 30-second timeout, adjust to taste curl_setopt(self::$ch, CURLOPT_POST, !empty($values)); // WePay's API is not strictly RESTful, so all requests are sent as POST unless there are no request values $uri = self::getDomain() . $endpoint; curl_setopt(self::$ch, CURLOPT_URL, $uri); if (!empty($values)) { curl_setopt(self::$ch, CURLOPT_POSTFIELDS, json_encode($values)); } $raw = curl_exec(self::$ch); if ($errno = curl_errno(self::$ch)) { // Set up special handling for request timeouts if ($errno == CURLE_OPERATION_TIMEOUTED) { throw new WePayServerException("Timeout occurred while trying to connect to WePay"); } throw new Exception('cURL error while making API call to WePay: ' . curl_error(self::$ch), $errno); } $result = json_decode($raw); $httpCode = curl_getinfo(self::$ch, CURLINFO_HTTP_CODE); if ($httpCode >= 400) { if (!isset($result->error_code)) { throw new WePayServerException("WePay returned an error response with no error_code, please alert api@wepay.com. Original message: {$result->error_description}", $httpCode, $result, 0); } if ($httpCode >= 500) { throw new WePayServerException($result->error_description, $httpCode, $result, $result->error_code); } switch ($result->error) { case 'invalid_request': throw new WePayRequestException($result->error_description, $httpCode, $result, $result->error_code); case 'access_denied': default: throw new WePayPermissionException($result->error_description, $httpCode, $result, $result->error_code); } } return $result; }