| Current Path : /proc/thread-self/root/proc/thread-self/root/proc/1908984/root/proc/2411249/cwd/ |
| Current File : //proc/thread-self/root/proc/thread-self/root/proc/1908984/root/proc/2411249/cwd/kco_php.tar |
Checkout.php 0000644 00000004772 15235315310 0007032 0 ustar 00 <?php
/**
* Copyright 2015 Klarna AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Bootstrap file to include the klarna checkout library
*
* PHP version 5.3
*
* @category Payment
* @package Klarna_Checkout
* @author Klarna <support@klarna.com>
* @copyright 2015 Klarna AB
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache license v2.0
* @link http://developers.klarna.com/
*/
define('KLARNA_CHECKOUT_DIR', dirname(__FILE__) . '/Checkout');
require_once KLARNA_CHECKOUT_DIR . '/ConnectorInterface.php';
require_once KLARNA_CHECKOUT_DIR . '/ResourceInterface.php';
require_once KLARNA_CHECKOUT_DIR . '/Connector.php';
require_once KLARNA_CHECKOUT_DIR . '/BasicConnector.php';
require_once KLARNA_CHECKOUT_DIR . '/Resource.php';
require_once KLARNA_CHECKOUT_DIR . '/ResourceCreateableInterface.php';
require_once KLARNA_CHECKOUT_DIR . '/ResourceFetchableInterface.php';
require_once KLARNA_CHECKOUT_DIR . '/ResourceUpdateableInterface.php';
require_once KLARNA_CHECKOUT_DIR . '/Order.php';
require_once KLARNA_CHECKOUT_DIR . '/RecurringOrder.php';
require_once KLARNA_CHECKOUT_DIR . '/RecurringStatus.php';
require_once KLARNA_CHECKOUT_DIR . '/Digest.php';
require_once KLARNA_CHECKOUT_DIR . '/Exception.php';
require_once KLARNA_CHECKOUT_DIR . '/ConnectionErrorException.php';
require_once KLARNA_CHECKOUT_DIR . '/ApiErrorException.php';
require_once KLARNA_CHECKOUT_DIR . '/ConnectorException.php';
require_once KLARNA_CHECKOUT_DIR . '/UserAgent.php';
require_once KLARNA_CHECKOUT_DIR . '/HTTP/TransportInterface.php';
require_once KLARNA_CHECKOUT_DIR . '/HTTP/CURLHandleInterface.php';
require_once KLARNA_CHECKOUT_DIR . '/HTTP/Request.php';
require_once KLARNA_CHECKOUT_DIR . '/HTTP/Response.php';
require_once KLARNA_CHECKOUT_DIR . '/HTTP/Transport.php';
require_once KLARNA_CHECKOUT_DIR . '/HTTP/CURLTransport.php';
require_once KLARNA_CHECKOUT_DIR . '/HTTP/CURLHeaders.php';
require_once KLARNA_CHECKOUT_DIR . '/HTTP/CURLHandle.php';
require_once KLARNA_CHECKOUT_DIR . '/HTTP/CURLFactory.php';
Checkout/ResourceInterface.php 0000644 00000004736 15235315310 0012442 0 ustar 00 <?php
/**
* Copyright 2015 Klarna AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* File containing the Klarna_Checkout_Resource interface
*
* PHP version 5.3
*
* @category Payment
* @package Klarna_Checkout
* @author Klarna <support@klarna.com>
* @copyright 2015 Klarna AB
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache license v2.0
* @link http://developers.klarna.com/
*/
/**
* Interface for the resource object
*
* @category Payment
* @package Klarna_Checkout
* @author Majid G. <majid.garmaroudi@klarna.com>
* @author David K. <david.keijser@klarna.com>
* @copyright 2015 Klarna AB
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache license v2.0
* @link http://developers.klarna.com/
*/
interface Klarna_Checkout_ResourceInterface
{
/**
* Get the URL of the resource
*
* @return string
*/
public function getLocation();
/**
* Set the URL of the resource
*
* @param string $location URL of the resource
*
* @return void
*/
public function setLocation($location);
/**
* Return content type of the resource
*
* @return string Content type
*/
public function getContentType();
/**
* Set the content type
*
* @param string $contentType Content type
*
* @return void
*/
public function setContentType($contentType);
/**
* Return accept header of the resource
*
* @return string Accept header
*/
public function getAccept();
/**
* Set the accept type
*
* @param string $accept Accept type
*
* @return void
*/
public function setAccept($accept);
/**
* Update resource with the new data
*
* @param array $data data
*
* @return void
*/
public function parse(array $data);
/**
* Basic representation of the object
*
* @return array data
*/
public function marshal();
}
Checkout/RecurringOrder.php 0000644 00000005347 15235315310 0011765 0 ustar 00 <?php
/**
* Copyright 2015 Klarna AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* File containing the Klarna_Checkout_RecurringOrder class
*
* PHP version 5.3
*
* @category Payment
* @package Klarna_Checkout
* @author Klarna <support@klarna.com>
* @copyright 2015 Klarna AB
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache license v2.0
* @link http://developers.klarna.com/
*/
/**
* Implementation of the recurring order resource
*
* @category Payment
* @package Klarna_Checkout
* @author Matthias Feist <matthias.feist@klarna.com>
* @copyright 2015 Klarna AB
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache license v2.0
* @link http://developers.klarna.com/
*/
class Klarna_Checkout_RecurringOrder extends Klarna_Checkout_Resource implements
Klarna_Checkout_ResourceCreateableInterface
{
/**
* Path that is used to create resources
*
* @var string
*/
protected $relativePath = '/checkout/recurring/%s/orders';
/**
* Content Type to use
*
* @var string
*/
protected $contentType
= "application/vnd.klarna.checkout.recurring-order-v1+json";
/**
* Accept header to use
*
* @var string
*/
protected $accept
= 'application/vnd.klarna.checkout.recurring-order-accepted-v1+json';
/**
* Create a new recurring order object
*
* @param Klarna_Checkout_ConnectorInterface $connector connector to use
* @param string $recurringToken recurring token
*/
public function __construct(
Klarna_Checkout_ConnectorInterface $connector,
$recurringToken
) {
parent::__construct($connector);
$uri = $this->connector->getDomain() . sprintf(
$this->relativePath,
$recurringToken
);
$this->setLocation($uri);
}
/**
* Create a new order
*
* @param array $data data to initialise order resource with
*
* @return void
*/
public function create(array $data)
{
$options = array(
'url' => $this->location,
'data' => $data
);
$this->connector->apply('POST', $this, $options);
}
}
Checkout/ResourceUpdateableInterface.php 0000644 00000002712 15235315310 0014421 0 ustar 00 <?php
/**
* Copyright 2015 Klarna AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* File containing the Klarna_Checkout_Resource interface
*
* PHP version 5.3
*
* @category Payment
* @package Klarna_Checkout
* @author Klarna <support@klarna.com>
* @copyright 2015 Klarna AB
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache license v2.0
* @link http://developers.klarna.com/
*/
/**
* Interface for updateable resource objects
*
* @category Payment
* @package Klarna_Checkout
* @author Matthias Feist <matthias.feist@klarna.com>
* @copyright 2015 Klarna AB
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache license v2.0
* @link http://developers.klarna.com/
*/
interface Klarna_Checkout_ResourceUpdateableInterface
{
/**
* Update resource data
*
* @param array $data data to update the resource with
*
* @return void
*/
public function update(array $data);
}
Checkout/ConnectionErrorException.php 0000644 00000002555 15235315310 0014017 0 ustar 00 <?php
/**
* Copyright 2015 Klarna AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* File containing the Klarna_Checkout_ConnectionErrorException class
*
* PHP version 5.3
*
* @category Payment
* @package Klarna_Checkout
* @author Klarna <support@klarna.com>
* @copyright 2015 Klarna AB
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache license v2.0
* @link http://developers.klarna.com/
*/
/**
* Connection exception
*
* @category Payment
* @package Klarna_Checkout
* @author Rickard D. <rickard.dybeck@klarna.com>
* @author Christer G. <christer.gustavsson@klarna.com>
* @copyright 2015 Klarna AB
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache license v2.0
* @link http://developers.klarna.com/
*/
class Klarna_Checkout_ConnectionErrorException extends Klarna_Checkout_Exception
{
}
Checkout/ResourceFetchableInterface.php 0000644 00000002571 15235315310 0014233 0 ustar 00 <?php
/**
* Copyright 2015 Klarna AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* File containing the Klarna_Checkout_Resource interface
*
* PHP version 5.3
*
* @category Payment
* @package Klarna_Checkout
* @author Klarna <support@klarna.com>
* @copyright 2015 Klarna AB
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache license v2.0
* @link http://developers.klarna.com/
*/
/**
* Interface for fetchable resource objects
*
* @category Payment
* @package Klarna_Checkout
* @author Matthias Feist <matthias.feist@klarna.com>
* @copyright 2015 Klarna AB
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache license v2.0
* @link http://developers.klarna.com/
*/
interface Klarna_Checkout_ResourceFetchableInterface
{
/**
* Fetch resource data
*
* @return void
*/
public function fetch();
}
Checkout/Connector.php 0000644 00000004061 15235315310 0010753 0 ustar 00 <?php
/**
* Copyright 2015 Klarna AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* File containing the Klarna_Checkout_Connector facade class
*
* PHP version 5.3
*
* @category Payment
* @package Klarna_Checkout
* @author Klarna <support@klarna.com>
* @copyright 2015 Klarna AB
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache license v2.0
* @link http://developers.klarna.com/
*/
/**
* Connector factory
*
* @category Payment
* @package Klarna_Checkout
* @author Rickard D. <rickard.dybeck@klarna.com>
* @author Christer G. <christer.gustavsson@klarna.com>
* @author David K. <david.keijser@klarna.com>
* @copyright 2015 Klarna AB
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache license v2.0
* @link http://developers.klarna.com/
*/
class Klarna_Checkout_Connector
{
/**
* Domain of the testdrive
*/
const BASE_TEST_URL = "https://checkout.testdrive.klarna.com";
/**
* Domain of the live system
*/
const BASE_URL = "https://checkout.klarna.com";
/**
* Create a new Checkout Connector
*
* @param string $secret string used to sign requests
* @param string $domain the domain used for requests
*
* @return Klarna_Checkout_ConnectorInterface
*/
public static function create($secret, $domain = self::BASE_URL)
{
return new Klarna_Checkout_BasicConnector(
Klarna_Checkout_HTTP_Transport::create(),
new Klarna_Checkout_Digest,
$secret,
$domain
);
}
}
Checkout/Exception.php 0000644 00000002453 15235315310 0010762 0 ustar 00 <?php
/**
* Copyright 2015 Klarna AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* File containing the Klarna_Checkout_Exception class
*
* PHP version 5.3
*
* @category Payment
* @package Klarna_Checkout
* @author Klarna <support@klarna.com>
* @copyright 2015 Klarna AB
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache license v2.0
* @link http://developers.klarna.com/
*/
/**
* Basic exception class
*
* @category Payment
* @package Klarna_Checkout
* @author Majid G. <majid.garmaroudi@klarna.com>
* @author David K. <david.keijser@klarna.com>
* @copyright 2015 Klarna AB
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache license v2.0
* @link http://developers.klarna.com/
*/
class Klarna_Checkout_Exception extends Exception
{
} Checkout/Resource.php 0000644 00000012160 15235315310 0010607 0 ustar 00 <?php
/**
* Copyright 2015 Klarna AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* File containing the Klarna_Checkout_Resource class
*
* PHP version 5.3
*
* @category Payment
* @package Klarna_Checkout
* @author Klarna <support@klarna.com>
* @copyright 2015 Klarna AB
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache license v2.0
* @link http://developers.klarna.com/
*/
/**
* Implementation of the order resource
*
* @category Payment
* @package Klarna_Checkout
* @author Majid G. <majid.garmaroudi@klarna.com>
* @author David K. <david.keijser@klarna.com>
* @author Matthias Feist <matthias.feist@klarna.com>
* @copyright 2015 Klarna AB
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache license v2.0
* @link http://developers.klarna.com/
*/
abstract class Klarna_Checkout_Resource
implements Klarna_Checkout_ResourceInterface, ArrayAccess
{
/**
* Path that is used to create resources
*
* @var string
*/
protected $relativePath = null;
/**
* Content Type to use
*
* @var string
*/
protected $contentType = null;
/**
* Accept header to use
*
* @var string
*/
protected $accept = null;
/**
* URI of remote resource
*
* @var string
*/
protected $location;
/**
* Order data
*
* @var array
*/
protected $data = array();
/**
* Connector
*
* @var Klarna_Checkout_ConnectorInterface
*/
protected $connector;
/**
* Create a new Resource object
*
* @param Klarna_Checkout_ConnectorInterface $connector connector to use
*/
public function __construct(Klarna_Checkout_ConnectorInterface $connector)
{
$this->connector = $connector;
}
/**
* Get the URL of the resource
*
* @return string
*/
public function getLocation()
{
return $this->location;
}
/**
* Set the URL of the resource
*
* @param string $location URL of the resource
*
* @return void
*/
public function setLocation($location)
{
$this->location = strval($location);
}
/**
* Return content type of the resource
*
* @return string Content type
*/
public function getContentType()
{
return $this->contentType;
}
/**
* Return accept header of the resource
*
* @return string Accept header
*/
public function getAccept()
{
return $this->accept;
}
/**
* Set the content type
*
* @param string $contentType Content type
*
* @return void
*/
public function setContentType($contentType)
{
$this->contentType = $contentType;
}
/**
* Set the accept type
*
* @param string $accept Accept type
*
* @return void
*/
public function setAccept($accept)
{
$this->accept = $accept;
}
/**
* Replace resource data
*
* @param array $data data
*
* @return void
*/
public function parse(array $data)
{
$this->data = $data;
}
/**
* Basic representation of the object
*
* @return array Data
*/
public function marshal()
{
return $this->data;
}
/**
* Get value of a key
*
* @param string $key Key
*
* @return mixed data
*/
public function offsetGet($key)
{
if (!is_string($key)) {
throw new InvalidArgumentException("Key must be string");
}
return isset($this->data[$key]) ? $this->data[$key] : null;
}
/**
* Set value of a key
*
* @param string $key Key
* @param mixed $value Value of the key
*
* @return void
*/
public function offsetSet($key, $value)
{
if (!is_string($key)) {
throw new InvalidArgumentException("Key must be string");
}
$value = print_r($value, true);
throw new RuntimeException(
"Use update function to change values. trying to set $key to $value"
);
}
/**
* Check if a key exists in the resource
*
* @param string $key key
*
* @return boolean
*/
public function offsetExists($key)
{
return array_key_exists($key, $this->data);
}
/**
* Unset the value of a key
*
* @param string $key key
*
* @return void
*/
public function offsetUnset($key)
{
throw new RuntimeException(
"unset of fields not supported. trying to unset $key"
);
}
}
Checkout/UserAgent.php 0000644 00000005530 15235315310 0010720 0 ustar 00 <?php
/**
* Copyright 2015 Klarna AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* File containing the Klarna_Checkout_UserAgent class
*
* PHP version 5.3
*
* @category Payment
* @package Klarna_Checkout
* @author Klarna <support@klarna.com>
* @copyright 2015 Klarna AB
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache license v2.0
* @link http://developers.klarna.com/
*/
/**
* UserAgent string builder
*
* @category Payment
* @package Klarna_Checkout
* @author David K. <david.keijser@klarna.com>
* @copyright 2015 Klarna AB
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache license v2.0
* @link http://developers.klarna.com/
*/
class Klarna_Checkout_UserAgent
{
/**
* Components of the user-agent
*
* @var array
*/
private $_fields;
/**
* Initialise user-agent with default fields
*/
public function __construct()
{
$this->_fields = array(
'Library' => array(
'name' => 'Klarna.ApiWrapper',
'version' => '4.0.0',
),
'OS' => array(
'name' => php_uname('s'),
'version' => php_uname('r')
),
'Language' => array(
'name' => 'PHP',
'version' => phpversion()
)
);
}
/**
* Add a new field to the user agent
*
* @param string $field Name of field
* @param array $data data array with name, version and possibly options
*
* @return void
* @throws Klarna_Checkout_Exception
*/
public function addField($field, array $data)
{
if (array_key_exists($field, $this->_fields)) {
throw new Klarna_Checkout_Exception(
"Unable to redefine field {$field}"
);
}
$this->_fields[$field] = $data;
}
/**
* Serialise fields to a user agent string
*
* @return string
*/
public function __toString()
{
$parts = array();
foreach ($this->_fields as $key => $value) {
$parts[] = "$key/{$value['name']}_{$value['version']}";
if (array_key_exists('options', $value)) {
$parts[] = '(' . implode(' ; ', $value['options']) . ')';
}
}
return implode(' ', $parts);
}
}
Checkout/BasicConnector.php 0000644 00000024501 15235315310 0011716 0 ustar 00 <?php
/**
* Copyright 2015 Klarna AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* File containing the Klarna_Checkout_BasicConnector class
*
* PHP version 5.3
*
* @category Payment
* @package Klarna_Checkout
* @author Klarna <support@klarna.com>
* @copyright 2015 Klarna AB
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache license v2.0
* @link http://developers.klarna.com/
*/
/**
* Basic implementation of the connector interface
*
* @category Payment
* @package Klarna_Checkout
* @author Rickard D. <rickard.dybeck@klarna.com>
* @author Christer G. <christer.gustavsson@klarna.com>
* @author David K. <david.keijser@klarna.com>
* @copyright 2015 Klarna AB
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache license v2.0
* @link http://developers.klarna.com/
*/
class Klarna_Checkout_BasicConnector implements Klarna_Checkout_ConnectorInterface
{
/**
* Klarna_Checkout_HTTP_TransportInterface Implementation
*
* @var Klarna_Checkout_HTTP_TransportInterface
*/
protected $http;
/**
* Digester class
*
* @var Klarna_Checkout_Digest
*/
protected $digester;
/**
* The domain for the request
*
* @var string
*/
protected $domain;
/**
* Shared Secret used to sign requests
*
* @var string
*/
private $_secret;
/**
* Create a new Checkout Connector
*
* @param Klarna_Checkout_HTTP_TransportInterface $http Transport
* @param Klarna_Checkout_Digest $digester Digest Generator
* @param string $secret Shared secret
* @param string $domain Domain of the request
*/
public function __construct(
Klarna_Checkout_HTTP_TransportInterface $http,
Klarna_Checkout_Digest $digester,
$secret,
$domain = Klarna_Checkout_Connector::BASE_URL
) {
$this->http = $http;
$this->digester = $digester;
$this->_secret = $secret;
$this->domain = $domain;
}
/**
* Create the user agent identifier to use
*
* @return Klarna_Checkout_UserAgent
*/
protected function userAgent()
{
return new Klarna_Checkout_UserAgent();
}
/**
* Applying the method on the specific resource
*
* @param string $method Http methods
* @param Klarna_Checkout_ResourceInterface $resource resource
* @param array $options Options
*
* @return mixed
*/
public function apply(
$method,
Klarna_Checkout_ResourceInterface $resource,
array $options = null
) {
switch ($method) {
case 'GET':
case 'POST':
return $this->handle($method, $resource, $options, array());
default:
throw new InvalidArgumentException(
"{$method} is not a valid HTTP method"
);
}
}
/**
* Gets the underlying transport object
*
* @return Klarna_Checkout_HTTP_TransportInterface Transport object
*/
public function getTransport()
{
return $this->http;
}
/**
* Set content (headers, payload) on a request
*
* @param Klarna_Checkout_ResourceInterface $resource Klarna Checkout Resource
* @param string $method HTTP Method
* @param string $payload Payload to send with the
* request
* @param string $url URL for request
*
* @return Klarna_Checkout_HTTP_Request
*/
protected function createRequest(
Klarna_Checkout_ResourceInterface $resource,
$method,
$payload,
$url
) {
// Generate the digest string
$digest = $this->digester->create($payload . $this->_secret);
$request = $this->http->createRequest($url);
$request->setMethod($method);
// Set HTTP Headers
$accept = $resource->getAccept();
$contentType = $resource->getContentType();
$request->setHeader('User-Agent', (string)$this->userAgent());
$request->setHeader('Authorization', "Klarna {$digest}");
$request->setHeader('Accept', $accept ? $accept : $contentType);
if (strlen($payload) > 0) {
$request->setHeader('Content-Type', $contentType);
$request->setData($payload);
}
return $request;
}
/**
* Get the current domain
*
* @return string
*/
public function getDomain()
{
return $this->domain;
}
/**
* Get the url to use
*
* @param Klarna_Checkout_ResourceInterface $resource resource
* @param array $options Options
*
* @return string Url to use for HTTP requests
*/
protected function getUrl(
Klarna_Checkout_ResourceInterface $resource, array $options
) {
if (array_key_exists('url', $options)) {
return $options['url'];
}
return $resource->getLocation();
}
/**
* Get the data to use
*
* @param Klarna_Checkout_ResourceInterface $resource resource
* @param array $options Options
*
* @return array data to use for HTTP requests
*/
protected function getData(
Klarna_Checkout_ResourceInterface $resource, array $options
) {
if (array_key_exists('data', $options)) {
return $options['data'];
}
return $resource->marshal();
}
/**
* Throw an exception if the server responds with an error code.
*
* @param Klarna_Checkout_HTTP_Response $result HTTP Response object
*
* @throws Klarna_Checkout_ApiErrorException
* @return void
*/
protected function verifyResponse(Klarna_Checkout_HTTP_Response $result)
{
// Error Status Code recieved. Throw an exception.
if ($result->getStatus() >= 400 && $result->getStatus() <= 599) {
$json = json_decode($result->getData(), true);
$payload = ($json && is_array($json)) ? $json : array();
throw new Klarna_Checkout_ApiErrorException(
"API Error", $result->getStatus(), $payload
);
}
}
/**
* Act upon the status of a response
*
* @param Klarna_Checkout_HTTP_Response $result response from server
* @param Klarna_Checkout_ResourceInterface $resource associated resource
* @param array $visited list of visited locations
*
* @return Klarna_Checkout_HTTP_Response
* @throws Klarna_Checkout_ConnectorException
*/
protected function handleResponse(
Klarna_Checkout_HTTP_Response $result,
Klarna_Checkout_ResourceInterface $resource,
array $visited = array()
) {
// Check if we got an Error status code back
$this->verifyResponse($result);
$url = $result->getHeader('Location');
switch ($result->getStatus()) {
case 301:
// Update location and fallthrough
$resource->setLocation($url);
case 302:
// Don't fallthrough for other than GET
if ($result->getRequest()->getMethod() !== 'GET') {
break;
}
case 303:
// Detect eternal loops
if (in_array($url, $visited)) {
throw new Klarna_Checkout_ConnectorException(
'Infinite redirect loop detected.',
-1
);
}
$visited[] = $url;
// Follow redirect
return $this->handle(
'GET',
$resource,
array('url' => $url),
$visited
);
case 201:
// Update Location
$resource->setLocation($url);
break;
case 200:
// Update Data on resource
$json = json_decode($result->getData(), true);
if ($json === null) {
throw new Klarna_Checkout_ConnectorException(
'Bad format on response content.',
-2
);
}
$resource->parse($json);
}
return $result;
}
/**
* Perform a HTTP Call on the supplied resource using the wanted method.
*
* @param string $method HTTP Method
* @param Klarna_Checkout_ResourceInterface $resource Klarna Order
* @param array $options Options
* @param array $visited list of visited locations
*
* @throws Klarna_Checkout_Exception if 4xx or 5xx response code.
*
* @return Klarna_Checkout_HTTP_Response Result object containing status code
* and payload
*/
protected function handle(
$method,
Klarna_Checkout_ResourceInterface $resource,
array $options = null,
array $visited = array()
) {
if ($options === null) {
$options = array();
}
// Define the target URL
$url = $this->getUrl($resource, $options);
// Set a payload if it is a POST call.
$payload = '';
if ($method === 'POST') {
$payload = json_encode($this->getData($resource, $options));
}
// Create a HTTP Request object
$request = $this->createRequest($resource, $method, $payload, $url);
// Execute the HTTP Request
$result = $this->http->send($request);
// Handle statuses appropriately.
return $this->handleResponse($result, $resource, $visited);
}
}
Checkout/ResourceCreateableInterface.php 0000644 00000002717 15235315310 0014407 0 ustar 00 <?php
/**
* Copyright 2015 Klarna AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* File containing the Klarna_Checkout_Resource interface
*
* PHP version 5.3
*
* @category Payment
* @package Klarna_Checkout
* @author Klarna <support@klarna.com>
* @copyright 2015 Klarna AB
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache license v2.0
* @link http://developers.klarna.com/
*/
/**
* Interface for createable resource objects
*
* @category Payment
* @package Klarna_Checkout
* @author Matthias Feist <matthias.feist@klarna.com>
* @copyright 2015 Klarna AB
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache license v2.0
* @link http://developers.klarna.com/
*/
interface Klarna_Checkout_ResourceCreateableInterface
{
/**
* Create a new resource
*
* @param array $data data to initialise the resource with
*
* @return void
*/
public function create(array $data);
}
Checkout/ConnectorException.php 0000644 00000002513 15235315310 0012632 0 ustar 00 <?php
/**
* Copyright 2015 Klarna AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* File containing the Klarna_Checkout_ConnectorException class
*
* PHP version 5.3
*
* @category Payment
* @package Klarna_Checkout
* @author Klarna <support@klarna.com>
* @copyright 2015 Klarna AB
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache license v2.0
* @link http://developers.klarna.com/
*/
/**
* Connector exception
*
* @category Payment
* @package Klarna_Checkout
* @author Rickard D. <rickard.dybeck@klarna.com>
* @author David K. <david.keijser@klarna.com>
* @copyright 2015 Klarna AB
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache license v2.0
* @link http://developers.klarna.com/
*/
class Klarna_Checkout_ConnectorException extends Klarna_Checkout_Exception
{
} Checkout/ApiErrorException.php 0000644 00000003766 15235315310 0012436 0 ustar 00 <?php
/**
* Copyright 2015 Klarna AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* File containing the Klarna_Checkout_ConnectorException class
*
* PHP version 5.3
*
* @category Payment
* @package Klarna_Checkout
* @author Klarna <support@klarna.com>
* @copyright 2015 Klarna AB
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache license v2.0
* @link http://developers.klarna.com/
*/
/**
* Api Error exception
*
* @category Payment
* @package Klarna_Checkout
* @author Matthias Feist <matthias.feist@klarna.com>
* @copyright 2015 Klarna AB
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache license v2.0
* @link http://developers.klarna.com/
*/
class Klarna_Checkout_ApiErrorException extends Klarna_Checkout_Exception
{
/**
* Payload of the error
*
* @var array
*/
protected $payload = array();
/**
* Custom contructor
*
* @param string $message Error message
* @param int $code Error code
* @param Array $payload Payload
* @param Exception $previous Previous Exception
*/
public function __construct(
$message,
$code,
Array $payload = array(),
Exception $previous = null
) {
parent::__construct($message, $code, $previous);
$this->payload = $payload;
}
/**
* Gets the payload
*
* @return array
*/
public function getPayload()
{
return $this->payload;
}
} Checkout/RecurringStatus.php 0000644 00000004702 15235315310 0012167 0 ustar 00 <?php
/**
* Copyright 2015 Klarna AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* File containing the Klarna_Checkout_RecurringStatus class
*
* PHP version 5.3
*
* @category Payment
* @package Klarna_Checkout
* @author Klarna <support@klarna.com>
* @copyright 2015 Klarna AB
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache license v2.0
* @link http://developers.klarna.com/
*/
/**
* Implementation of the recurring order resource
*
* @category Payment
* @package Klarna_Checkout
* @author Matthias Feist <matthias.feist@klarna.com>
* @copyright 2015 Klarna AB
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache license v2.0
* @link http://developers.klarna.com/
*/
class Klarna_Checkout_RecurringStatus extends Klarna_Checkout_Resource implements
Klarna_Checkout_ResourceFetchableInterface
{
/**
* Path that is used to create resources
*
* @var string
*/
protected $relativePath = '/checkout/recurring/%s';
/**
* Content Type to use
*
* @var string
*/
protected $contentType
= "application/vnd.klarna.checkout.recurring-status-v1+json";
/**
* Create a new recurring status object
*
* @param Klarna_Checkout_ConnectorInterface $connector connector to use
* @param string $recurringToken recurring token
*/
public function __construct(
Klarna_Checkout_ConnectorInterface $connector,
$recurringToken
) {
parent::__construct($connector);
$uri = $this->connector->getDomain() . sprintf(
$this->relativePath,
$recurringToken
);
$this->setLocation($uri);
}
/**
* Fetch order data
*
* @return void
*/
public function fetch()
{
$options = array(
'url' => $this->location
);
$this->connector->apply('GET', $this, $options);
}
}
Checkout/Order.php 0000644 00000006453 15235315310 0010103 0 ustar 00 <?php
/**
* Copyright 2015 Klarna AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* File containing the Klarna_Checkout_Order class
*
* PHP version 5.3
*
* @category Payment
* @package Klarna_Checkout
* @author Klarna <support@klarna.com>
* @copyright 2015 Klarna AB
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache license v2.0
* @link http://developers.klarna.com/
*/
/**
* Implementation of the order resource
*
* @category Payment
* @package Klarna_Checkout
* @author Majid G. <majid.garmaroudi@klarna.com>
* @author David K. <david.keijser@klarna.com>
* @author Matthias Feist <matthias.feist@klarna.com>
* @copyright 2015 Klarna AB
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache license v2.0
* @link http://developers.klarna.com/
*/
class Klarna_Checkout_Order extends Klarna_Checkout_Resource implements
Klarna_Checkout_ResourceCreateableInterface,
Klarna_Checkout_ResourceFetchableInterface,
Klarna_Checkout_ResourceUpdateableInterface
{
/**
* Path that is used to create resources
*
* @var string
*/
protected $relativePath = '/checkout/orders';
/**
* Content Type to use
*
* @var string
*/
protected $contentType
= "application/vnd.klarna.checkout.aggregated-order-v2+json";
/**
* Create a new order.
*
* @param Klarna_Checkout_ConnectorInterface $connector connector to use
* @param string $id Order id
*/
public function __construct(
Klarna_Checkout_ConnectorInterface $connector,
$id = null
) {
parent::__construct($connector);
if ($id !== null) {
$uri = $this->connector->getDomain() . "{$this->relativePath}/{$id}";
$this->setLocation($uri);
}
}
/**
* Create a new order
*
* @param array $data data to initialise order resource with
*
* @return void
*/
public function create(array $data)
{
$options = array(
'url' => $this->connector->getDomain() . $this->relativePath,
'data' => $data
);
$this->connector->apply('POST', $this, $options);
}
/**
* Fetch order data
*
* @return void
*/
public function fetch()
{
$options = array(
'url' => $this->location
);
$this->connector->apply('GET', $this, $options);
}
/**
* Update order data
*
* @param array $data data to update order resource with
*
* @return void
*/
public function update(
array $data
) {
$options = array(
'url' => $this->location,
'data' => $data
);
$this->connector->apply('POST', $this, $options);
}
}
Checkout/Digest.php 0000644 00000003142 15235315310 0010237 0 ustar 00 <?php
/**
* Copyright 2015 Klarna AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* File containing the Klarna_Checkout_Digester class
*
* PHP version 5.3
*
* @category Payment
* @package Klarna_Checkout
* @author Klarna <support@klarna.com>
* @copyright 2015 Klarna AB
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache license v2.0
* @link http://developers.klarna.com/
*/
/**
* Class to handle the digesting of hash string
*
* @category Payment
* @package Klarna_Checkout
* @author Rickard D. <rickard.dybeck@klarna.com>
* @author Christer G. <christer.gustavsson@klarna.com>
* @copyright 2015 Klarna AB
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache license v2.0
* @link http://developers.klarna.com/
*/
class Klarna_Checkout_Digest
{
/**
* Create a digest from a supplied string
*
* @param string $digestString string to hash
*
* @return string Base64 and SHA256 hashed string
*/
public function create($digestString)
{
return base64_encode(hash('sha256', $digestString, true));
}
}
Checkout/ConnectorInterface.php 0000644 00000004013 15235315310 0012571 0 ustar 00 <?php
/**
* Copyright 2015 Klarna AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* File containing the Klarna_Checkout_Connector interface
*
* PHP version 5.3
*
* @category Payment
* @package Klarna_Checkout
* @author Klarna <support@klarna.com>
* @copyright 2015 Klarna AB
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache license v2.0
* @link http://developers.klarna.com/
*/
/**
* Interface for the resource object
*
* @category Payment
* @package Klarna_Checkout
* @author Majid G. <majid.garmaroudi@klarna.com>
* @author David K. <david.keijser@klarna.com>
* @copyright 2015 Klarna AB
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache license v2.0
* @link http://developers.klarna.com/
*/
interface Klarna_Checkout_ConnectorInterface
{
/**
* Applying the method on the specific resource
*
* @param string $method Http methods
* @param Klarna_Checkout_ResourceInterface $resource resource
* @param array $options Options
*
* @return void
*/
public function apply(
$method,
Klarna_Checkout_ResourceInterface $resource,
array $options = null
);
/**
* Gets the underlying transport object
*
* @return Klarna_Checkout_HTTP_TransportInterface Transport object
*/
public function getTransport();
/**
* Get the current domain
*
* @return string
*/
public function getDomain();
}
Checkout/HTTP/Response.php 0000644 00000007140 15235315310 0011377 0 ustar 00 <?php
/**
* Copyright 2015 Klarna AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* File containing the Klarna_HTTP_Response class
*
* PHP version 5.3
*
* @category Payment
* @package Payment_Klarna
* @subpackage HTTP
* @author Klarna <support@klarna.com>
* @copyright 2015 Klarna AB
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache license v2.0
* @link http://developers.klarna.com/
*/
/**
* Klarna HTTP Response class
*
* @category Payment
* @package Payment_Klarna
* @subpackage HTTP
* @author Klarna <support@klarna.com>
* @copyright 2015 Klarna AB
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache license v2.0
* @link http://developers.klarna.com/
*/
class Klarna_Checkout_HTTP_Response
{
/**
* HTTP response status code
*
* @var int
*/
protected $status;
/**
* Request object
*
* @var Klarna_Checkout_HTTP_Request
*/
protected $request;
/**
* HTTP header
*
* @var array
*/
protected $headers;
/**
* Data
*
* @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;
}
}
Checkout/HTTP/Transport.php 0000644 00000003065 15235315310 0011577 0 ustar 00 <?php
/**
* Copyright 2015 Klarna AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* File containing the Transport factory
*
* PHP version 5.3
*
* @category Payment
* @package Payment_Klarna
* @subpackage Unit_Tests
* @author Klarna <support@klarna.com>
* @copyright 2015 Klarna AB
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache license v2.0
* @link http://developers.klarna.com/
*/
/**
* Factory of HTTP Transport
*
* @category Payment
* @package Payment_Klarna
* @subpackage Unit_Tests
* @author David K. <david.keijser@klarna.com>
* @copyright 2015 Klarna AB
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache license v2.0
* @link http://developers.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
);
}
}
Checkout/HTTP/TransportInterface.php 0000644 00000005071 15235315310 0013417 0 ustar 00 <?php
/**
* Copyright 2015 Klarna AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* File containing the Klarna_Checkout_HTTP_TransportInterface interface
*
* PHP version 5.3
*
* @category Payment
* @package Payment_Klarna
* @subpackage Interfaces
* @author Klarna <support@klarna.com>
* @copyright 2015 Klarna AB
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache license v2.0
* @link http://developers.klarna.com/
*/
/**
* Interface for a Klarna HTTP Transport object
*
* @category Payment
* @package Payment_Klarna
* @subpackage Interfaces
* @author Klarna <support@klarna.com>
* @copyright 2015 Klarna AB
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache license v2.0
* @link http://developers.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);
}
Checkout/HTTP/Request.php 0000644 00000010665 15235315310 0011237 0 ustar 00 <?php
/**
* Copyright 2015 Klarna AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* File containing the Klarna_Checkout_HTTP_Request class
*
* PHP version 5.3
*
* @category Payment
* @package Payment_Klarna
* @subpackage HTTP
* @author Klarna <support@klarna.com>
* @copyright 2015 Klarna AB
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache license v2.0
* @link http://developers.klarna.com/
*/
/**
* Klarna HTTP Request class
*
* @category Payment
* @package Payment_Klarna
* @subpackage HTTP
* @author Klarna <support@klarna.com>
* @copyright 2015 Klarna AB
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache license v2.0
* @link http://developers.klarna.com/
*/
class Klarna_Checkout_HTTP_Request
{
/**
* HTTP url
*
* @var string
*/
protected $url;
/**
* HTTP method
*
* @var string
*/
protected $method;
/**
* HTTP headers
*
* @var array
*/
protected $headers;
/**
* Payload
*
* @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;
}
}
Checkout/HTTP/CURLFactory.php 0000644 00000002757 15235315310 0011707 0 ustar 00 <?php
/**
* Copyright 2015 Klarna AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* File containing the PHPUnit Klarna_HTTP_CURLTest test case
*
* PHP version 5.3
*
* @category Payment
* @package Payment_Klarna
* @subpackage Unit_Tests
* @author Klarna <support@klarna.com>
* @copyright 2015 Klarna AB
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache license v2.0
* @link http://developers.klarna.com/
*/
/**
* Factory of cURL handles
*
* @category Payment
* @package Payment_Klarna
* @subpackage Unit_Tests
* @author Klarna <support@klarna.com>
* @copyright 2015 Klarna AB
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache license v2.0
* @link http://developers.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();
}
}
Checkout/HTTP/CURLHandle.php 0000644 00000005312 15235315310 0011461 0 ustar 00 <?php
/**
* Copyright 2015 Klarna AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* File containing the Klarna_Checkout_HTTP_CURLHeaders class
*
* PHP version 5.3
*
* @category Payment
* @package Payment_Klarna
* @subpackage HTTP
* @author Klarna <support@klarna.com>
* @copyright 2015 Klarna AB
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache license v2.0
* @link http://developers.klarna.com/
*/
/**
* A wrapper around the cURL functions
*
* @category Payment
* @package Payment_Klarna
* @subpackage HTTP
* @author David K. <david.keijser@klarna.com>
* @copyright 2015 Klarna AB
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache license v2.0
* @link http://developers.klarna.com/
*/
class Klarna_Checkout_HTTP_CURLHandle implements
Klarna_Checkout_HTTP_CURLHandleInterface
{
/**
* Handle for cURL.
*
* @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);
}
/**
* Get error message regarding this transfer
*
* @return string Error message
*/
public function getError()
{
return curl_error($this->_handle);
}
/**
* Close the cURL session
*
* @return void
*/
public function close()
{
curl_close($this->_handle);
}
}
Checkout/HTTP/CURLTransport.php 0000644 00000013616 15235315310 0012270 0 ustar 00 <?php
/**
* Copyright 2015 Klarna AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* File containing the Klarna_Checkout_HTTP_CURLTransport class
*
* PHP version 5.3
*
* @category Payment
* @package Payment_Klarna
* @subpackage HTTP
* @author Klarna <support@klarna.com>
* @copyright 2015 Klarna AB AB
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache license v2.0
* @link http://developers.klarna.com/
*/
/**
* Klarna HTTP transport implementation for cURL
*
* @category Payment
* @package Payment_Klarna
* @subpackage HTTP
* @author Klarna <support@klarna.com>
* @copyright 2015 Klarna AB
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache license v2.0
* @link http://developers.klarna.com/
*/
class Klarna_Checkout_HTTP_CURLTransport implements
Klarna_Checkout_HTTP_TransportInterface
{
const DEFAULT_TIMEOUT = 10;
/**
* Factory for cUrl
*
* @var Klarna_Checkout_HTTP_CURLFactory
*/
protected $curl;
/**
* Number of seconds before the connection times out.
*
* @var int
*/
protected $timeout;
/**
* Options for cURL
*
* @var array
*/
protected $options;
/**
* 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;
$this->options = array();
}
/**
* Set specific cURL options.
*
* @param int $option cURL option constant
* @param mixed $value cURL option value
*
* @return void
*/
public function setOption($option, $value)
{
$this->options[$option] = $value;
}
/**
* 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);
$curl->setOption(CURLOPT_TIMEOUT, $this->timeout);
$curlHeaders = new Klarna_Checkout_HTTP_CURLHeaders();
$curl->setOption(
CURLOPT_HEADERFUNCTION,
array(&$curlHeaders, 'processHeader')
);
$curl->setOption(CURLOPT_SSL_VERIFYHOST, 2);
$curl->setOption(CURLOPT_SSL_VERIFYPEER, true);
// Override specific set options
foreach ($this->options as $option => $value) {
$curl->setOption($option, $value);
}
$payload = $curl->execute();
$info = $curl->getInfo();
$error = $curl->getError();
$curl->close();
/*
* A failure occurred 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: {$error}"
);
}
$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);
}
}
Checkout/HTTP/CURLHandleInterface.php 0000644 00000004014 15235315310 0013300 0 ustar 00 <?php
/**
* Copyright 2015 Klarna AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* File containing the Klarna_Checkout_HTTP_CURLHeaders class
*
* PHP version 5.3
*
* @category Payment
* @package Payment_Klarna
* @subpackage HTTP
* @author Klarna <support@klarna.com>
* @copyright 2015 Klarna AB
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache license v2.0
* @link http://developers.klarna.com/
*/
/**
* Defines a cURL handle interface
*
* @category Payment
* @package Payment_Klarna
* @subpackage HTTP
* @author David K. <david.keijser@klarna.com>
* @copyright 2015 Klarna AB
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache license v2.0
* @link http://developers.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();
/**
* Get error message regarding this transfer
*
* @return string Error message
*/
public function getError();
/**
* Close the cURL session
*
* @return void
*/
public function close();
}
Checkout/HTTP/CURLHeaders.php 0000644 00000004677 15235315310 0011656 0 ustar 00 <?php
/**
* Copyright 2015 Klarna AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* File containing the Klarna_Checkout_HTTP_CURLHeaders class
*
* PHP version 5.3
*
* @category Payment
* @package Payment_Klarna
* @subpackage HTTP
* @author Klarna <support@klarna.com>
* @copyright 2015 Klarna AB
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache license v2.0
* @link http://developers.klarna.com/
*/
/**
* A simple class handling the header callback for cURL.
*
* @category Payment
* @package Payment_Klarna
* @subpackage HTTP
* @author Klarna <support@klarna.com>
* @copyright 2015 Klarna AB
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache license v2.0
* @link http://developers.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;
}
}