public function setHeaders($name, $value = null)
{
// If we got an array, go recursive!
if (is_array($name)) {
foreach ($name as $k => $v) {
if (is_string($k)) {
$this->setHeaders($k, $v);
} else {
$this->setHeaders($v, null);
}
}
} else {
// Check if $name needs to be split
if ($value === null && strpos($name, ':') > 0) {
list($name, $value) = explode(':', $name, 2);
}
// Make sure the name is valid if we are in strict mode
if ($this->config['strict'] && !preg_match('/^[a-zA-Z0-9-]+$/', $name)) {
/** @see Zend_Http_Client_Exception */
require_once 'Zend/Http/Client/Exception.php';
throw new Zend_Http_Client_Exception("{$name} is not a valid HTTP header name");
}
$normalized_name = strtolower($name);
// If $value is null or false, unset the header
if ($value === null || $value === false) {
unset($this->headers[$normalized_name]);
// Else, set the header
} else {
// Header names are stored lowercase internally.
if (is_string($value)) {
$value = trim($value);
}
$this->headers[$normalized_name] = array($name, $value);
}
}
return $this;
}
protected function _api_request($method, $path, $data = null) { $url = trim($this->getBaseApiUrl(), "/") . self::API_CHECKOUT_PATH . $path; $client = new Zend_Http_Client($url); if (in_array($method, array(Zend_Http_Client::POST, Zend_Http_Client::PUT, 'PATCH')) && $data) { $client->setHeaders('Content-type: application/json'); $client->setRawData(json_encode($data), 'application/json'); } $client->setHeaders('Authorization: Bearer ' . Mage::getStoreConfig('payment/aplazame/secret_api_key')); $client->setHeaders('User-Agent: ' . self::USER_AGENT); $client->setHeaders('Accept: ' . 'application/vnd.aplazame' . (Mage::getStoreConfig('payment/aplazame/sandbox') ? '.sandbox-' : '-') . Mage::getStoreConfig('payment/aplazame/version') . '+json'); $response = $client->request($method); $raw_result = $response->getBody(); $status_code = $response->getStatus(); if ($status_code >= 500) { Mage::throwException(Mage::helper('aplazame')->__('Aplazame error code: ' . $status_code)); } try { $ret_json = Zend_Json::decode($raw_result, Zend_Json::TYPE_ARRAY); } catch (Zend_Json_Exception $e) { Mage::throwException(Mage::helper('aplazame')->__('Invalid api response: ' . $raw_result)); } if ($status_code >= 400) { Mage::throwException(Mage::helper('aplazame')->__('Aplazame error code ' . $status_code . ': ' . $ret_json['error']['message'])); } return $ret_json; }