AAAATransport.php000066600000003147151374165310007270 0ustar00 * @copyright 2012 Klarna AB * @license http://www.apache.org/licenses/LICENSE-2.0 Apache license v2.0 * @link http://integration.klarna.com/ */ /** * Factory of HTTP Transport * * @category Payment * @package Payment_Klarna * @subpackage Unit_Tests * @author David K. * @copyright 2012 Klarna AB * @license http://www.apache.org/licenses/LICENSE-2.0 Apache license v2.0 * @link http://integration.klarna.com/ */ class Klarna_Checkout_HTTP_Transport { /** * Create a new transport instance * * @return Klarna_Checkout_HTTP_TransportInterface */ public static function create() { return new Klarna_Checkout_HTTP_CURLTransport( new Klarna_Checkout_HTTP_CURLFactory ); } } TransportInterface.php000066600000005153151374165310011110 0ustar00 * @copyright 2012 Klarna AB * @license http://www.apache.org/licenses/LICENSE-2.0 Apache license v2.0 * @link http://integration.klarna.com/ */ /** * Interface for a Klarna HTTP Transport object * * @category Payment * @package Payment_Klarna * @subpackage Interfaces * @author Klarna * @copyright 2012 Klarna AB * @license http://www.apache.org/licenses/LICENSE-2.0 Apache license v2.0 * @link http://integration.klarna.com/ */ interface Klarna_Checkout_HTTP_TransportInterface { /** * Specifies the number of seconds before the connection times out. * * @param int $timeout number of seconds * * @throws InvalidArgumentException If the specified argument * is not of type integer. * @return void */ public function setTimeout($timeout); /** * Gets the number of seconds before the connection times out. * * @return int timeout in number of seconds */ public function getTimeout(); /** * Performs a HTTP request. * * @param Klarna_Checkout_HTTP_Request $request the HTTP request to send. * * @throws Klarna_Checkout_ConnectionErrorException Thrown for unspecified * network or hardware issues. * @return Klarna_Checkout_HTTP_Response */ public function send(Klarna_Checkout_HTTP_Request $request); /** * Creates a HTTP request object. * * @param string $url the request URL. * * @throws InvalidArgumentException If the specified argument * is not of type string. * @return Klarna_Checkout_HTTP_Request */ public function createRequest($url); } index.html000066600000000000151374165310006541 0ustar00CURLHeaders.php000066600000004761151374165310007340 0ustar00 * @copyright 2012 Klarna AB * @license http://www.apache.org/licenses/LICENSE-2.0 Apache license v2.0 * @link http://integration.klarna.com/ */ /** * A simple class handling the header callback for cURL. * * @category Payment * @package Payment_Klarna * @subpackage HTTP * @author Klarna * @copyright 2012 Klarna AB * @license http://www.apache.org/licenses/LICENSE-2.0 Apache license v2.0 * @link http://integration.klarna.com/ */ class Klarna_Checkout_HTTP_CURLHeaders { /** * Response headers, cleared for each request. * * @var array */ protected $headers; /** * Initializes a new instance of the HTTP cURL class. */ public function __construct() { $this->headers = array(); } /** * Callback method to handle custom headers. * * @param resource $curl the cURL resource. * @param string $header the header data. * * @return int the number of bytes handled. */ public function processHeader($curl, $header) { $curl = null; //TODO replace with regexp, e.g. /^([^:]+):([^:]*)$/ ? $pos = strpos($header, ':'); // Didn't find a colon. if ($pos === false) { // Not real header, abort. return strlen($header); } $key = substr($header, 0, $pos); $value = trim(substr($header, $pos+1)); $this->headers[$key] = trim($value); return strlen($header); } /** * Gets the accumulated headers. * * @return array */ public function getHeaders() { return $this->headers; } } CURLTransport.php000066600000012411151374165310007750 0ustar00 * @copyright 2012 Klarna AB AB * @license http://www.apache.org/licenses/LICENSE-2.0 Apache license v2.0 * @link http://integration.klarna.com/ */ /** * Klarna HTTP transport implementation for cURL * * @category Payment * @package Payment_Klarna * @subpackage HTTP * @author Klarna * @copyright 2012 Klarna AB * @license http://www.apache.org/licenses/LICENSE-2.0 Apache license v2.0 * @link http://integration.klarna.com/ */ class Klarna_Checkout_HTTP_CURLTransport implements Klarna_Checkout_HTTP_TransportInterface { const DEFAULT_TIMEOUT = 10; /** * @var Klarna_Checkout_HTTP_CURLFactory */ protected $curl; /** * Number of seconds before the connection times out. * * @var int */ protected $timeout; /** * Initializes a new instance of the HTTP cURL class. * * @param Klarna_Checkout_HTTP_CURLFactory $curl factory to for curl handles */ public function __construct(Klarna_Checkout_HTTP_CURLFactory $curl) { $this->curl = $curl; $this->timeout = self::DEFAULT_TIMEOUT; } /** * Sets the number of seconds until a connection times out. * * @param int $timeout number of seconds * * @return void */ public function setTimeout($timeout) { $this->timeout = intval($timeout); } /** * Gets the number of seconds before the connection times out. * * @return int timeout in number of seconds */ public function getTimeout() { return $this->timeout; } /** * Performs a HTTP request. * * @param Klarna_Checkout_HTTP_Request $request the HTTP request to send. * * @throws RuntimeException Thrown if a cURL handle cannot * be initialized. * @throws Klarna_Checkout_ConnectionErrorException Thrown for unspecified * network or hardware issues. * @return Klarna_Checkout_HTTP_Response */ public function send(Klarna_Checkout_HTTP_Request $request) { $curl = $this->curl->handle(); if ($curl === false) { throw new RuntimeException( 'Failed to initialize a HTTP handle.' ); } $url = $request->getURL(); $curl->setOption(CURLOPT_URL, $url); $method = $request->getMethod(); if ($method === 'POST') { $curl->setOption(CURLOPT_POST, true); $curl->setOption(CURLOPT_POSTFIELDS, $request->getData()); } // Convert headers to cURL format. $requestHeaders = array(); foreach ($request->getHeaders() as $key => $value) { $requestHeaders[] = $key . ': ' . $value; } $curl->setOption(CURLOPT_HTTPHEADER, $requestHeaders); $curl->setOption(CURLOPT_RETURNTRANSFER, true); $curl->setOption(CURLOPT_CONNECTTIMEOUT, $this->timeout); $curlHeaders = new Klarna_Checkout_HTTP_CURLHeaders(); $curl->setOption( CURLOPT_HEADERFUNCTION, array(&$curlHeaders, 'processHeader') ); // TODO remove me when real cert is in place $curl->setOption(CURLOPT_SSL_VERIFYPEER, false); $payload = $curl->execute(); $info = $curl->getInfo(); $curl->close(); /* * A failure occured if: * payload is false (e.g. HTTP timeout?). * info is false, then it has no HTTP status code. */ if ($payload === false || $info === false) { throw new Klarna_Checkout_ConnectionErrorException( "Connection to '{$url}' failed." ); } $headers = $curlHeaders->getHeaders(); // Convert Content-Type into a normal header $headers['Content-Type'] = $info['content_type']; $response = new Klarna_Checkout_HTTP_Response( $request, $headers, intval($info['http_code']), strval($payload) ); return $response; } /** * Creates a HTTP request object. * * @param string $url the request URL. * * @throws InvalidArgumentException If the specified argument * is not of type string. * @return Klarna_Checkout_HTTP_Request */ public function createRequest($url) { return new Klarna_Checkout_HTTP_Request($url); } } CURLHandleInterface.php000066600000003661151374165310010777 0ustar00 * @copyright 2012 Klarna AB * @license http://www.apache.org/licenses/LICENSE-2.0 Apache license v2.0 * @link http://integration.klarna.com/ */ /** * Defines a cURL handle interface * * @category Payment * @package Payment_Klarna * @subpackage HTTP * @author David K. * @copyright 2012 Klarna AB * @license http://www.apache.org/licenses/LICENSE-2.0 Apache license v2.0 * @link http://integration.klarna.com/ */ interface Klarna_Checkout_HTTP_CURLHandleInterface { /** * Set an option for the cURL transfer * * @param int $name option the set * @param mixed $value the value to be set on option * * @return void */ public function setOption($name, $value); /** * Perform the cURL session * * @return mixed response */ public function execute(); /** * Get information regarding this transfer * * @return array */ public function getInfo(); /** * Close the cURL session * * @return void */ public function close(); } Request.php000066600000010605151374165310006721 0ustar00 * @copyright 2012 Klarna AB * @license http://www.apache.org/licenses/LICENSE-2.0 Apache license v2.0 * @link http://integration.klarna.com/ */ /** * Klarna HTTP Request class * * @category Payment * @package Payment_Klarna * @subpackage HTTP * @author Klarna * @copyright 2012 Klarna AB * @license http://www.apache.org/licenses/LICENSE-2.0 Apache license v2.0 * @link http://integration.klarna.com/ */ class Klarna_Checkout_HTTP_Request { /** * @var string */ protected $url; /** * @var string */ protected $method; /** * @var array */ protected $headers; /** * @var string */ protected $data; /** * Initializes a new instance of the HTTP request class. * * @param string $url the request URL. * * @throws InvalidArgumentException If the specified argument * is not of type string. */ public function __construct($url) { $this->url = $url; $this->method = 'GET'; $this->headers = array(); $this->data = ''; } /** * Gets the request URL. * * @return string the request URL. */ public function getURL() { return $this->url; } /** * Specifies the HTTP method used for the request. * * @param string $method a HTTP method. * * @throws InvalidArgumentException If the specified argument * is not of type string. * @return void */ public function setMethod($method) { $this->method = strtoupper($method); } /** * Gets the HTTP method used for the request. * * @return string a HTTP method */ public function getMethod() { return $this->method; } /** * Specifies a header for the request. * * @param string $name the header name * @param mixed $value the header value * * @throws InvalidArgumentException If the argument name is not of type * string or an empty string. * @return void */ public function setHeader($name, $value) { $this->headers[$name] = strval($value); } /** * Gets a specific header for the request. * * @param string $name the header name * * @throws InvalidArgumentException If the specified argument * is not of type string. * @return string|null the header value or null if it doesn't exist */ public function getHeader($name) { if (!array_key_exists($name, $this->headers)) { return null; } return $this->headers[$name]; } /** * Gets the headers specified for the request. * * @return array */ public function getHeaders() { return $this->headers; } /** * Sets the data (payload) for the request. * * \code * $request->setMethod('POST'); * $request->setData('some data'); * \endcode * * @param string $data the request payload * * @throws InvalidArgumentException If the specified argument * is not of type string. * @return void */ public function setData($data) { $this->data = $data; } /** * Gets the data (payload) for the request. * * @return string the request payload */ public function getData() { return $this->data; } } Response.php000066600000007040151374165310007066 0ustar00 * @copyright 2012 Klarna AB * @license http://www.apache.org/licenses/LICENSE-2.0 Apache license v2.0 * @link http://integration.klarna.com/ */ /** * Klarna HTTP Response class * * @category Payment * @package Payment_Klarna * @subpackage HTTP * @author Klarna * @copyright 2012 Klarna AB * @license http://www.apache.org/licenses/LICENSE-2.0 Apache license v2.0 * @link http://integration.klarna.com/ */ class Klarna_Checkout_HTTP_Response { /** * @var int */ protected $status; /** * @var Klarna_Checkout_HTTP_Request */ protected $request; /** * @var array */ protected $headers; /** * @var string */ protected $data; /** * Initializes a new instance of the HTTP response class. * * @param Klarna_Checkout_HTTP_Request $request the origin request. * @param array $headers the response HTTP headers. * @param int $status the HTTP status code. * @param string $data the response payload. */ public function __construct( Klarna_Checkout_HTTP_Request $request, array $headers, $status, $data ) { $this->request = $request; $this->headers = array(); foreach ($headers as $key => $value) { $this->headers[strtolower($key)] = $value; } $this->status = $status; $this->data = $data; } /** * Gets the HTTP status code. * * @return int HTTP status code. */ public function getStatus() { return $this->status; } /** * Gets the HTTP request this response originated from. * * @return Klarna_Checkout_HTTP_Request */ public function getRequest() { return $this->request; } /** * Gets specified HTTP header. * * @param string $name the header name. * * @throws InvalidArgumentException If the specified argument * is not of type string. * @return string|null Null if header doesn't exist, else header value. */ public function getHeader($name) { $name = strtolower($name); if (!array_key_exists($name, $this->headers)) { return null; } return $this->headers[$name]; } /** * Gets the headers specified for the response. * * @return array */ public function getHeaders() { return $this->headers; } /** * Gets the data (payload) for the response. * * @return string the response payload. */ public function getData() { return $this->data; } } CURLHandle.php000066600000005055151374165310007155 0ustar00 * @copyright 2012 Klarna AB * @license http://www.apache.org/licenses/LICENSE-2.0 Apache license v2.0 * @link http://integration.klarna.com/ */ /** * A wrapper around the cURL functions * * @category Payment * @package Payment_Klarna * @subpackage HTTP * @author David K. * @copyright 2012 Klarna AB * @license http://www.apache.org/licenses/LICENSE-2.0 Apache license v2.0 * @link http://integration.klarna.com/ */ class Klarna_Checkout_HTTP_CURLHandle implements Klarna_Checkout_HTTP_CURLHandleInterface { /** * cURL handle * @var resource */ private $_handle = null; /** * Create a new cURL handle */ public function __construct() { if (!extension_loaded('curl')) { throw new RuntimeException( 'cURL extension is requred.' ); } $this->_handle = curl_init(); } /** * Set an option for the cURL transfer * * @param int $name option the set * @param mixed $value the value to be set on option * * @return void */ public function setOption($name, $value) { curl_setopt($this->_handle, $name, $value); } /** * Perform the cURL session * * @return mixed response */ public function execute() { return curl_exec($this->_handle); } /** * Get information regarding this transfer * * @return array */ public function getInfo() { return curl_getinfo($this->_handle); } /** * Close the cURL session * * @return void */ public function close() { curl_close($this->_handle); } } .htaccess000066600000000177151374165310006361 0ustar00 Order allow,deny Deny from all CURLFactory.php000066600000003041151374165310007362 0ustar00 * @copyright 2012 Klarna AB * @license http://www.apache.org/licenses/LICENSE-2.0 Apache license v2.0 * @link http://integration.klarna.com/ */ /** * Factory of cURL handles * * @category Payment * @package Payment_Klarna * @subpackage Unit_Tests * @author Klarna * @copyright 2012 Klarna AB * @license http://www.apache.org/licenses/LICENSE-2.0 Apache license v2.0 * @link http://integration.klarna.com/ */ class Klarna_Checkout_HTTP_CURLFactory { /** * Create a new cURL handle * * @return Klarna_Checkout_HTTP_CURLHandle */ public function handle() { return new Klarna_Checkout_HTTP_CURLHandle(); } }