Overview
  • Package
  • Class

Packages

  • Klarna
    • Checkout
  • Payment
    • Klarna
      • HTTP
      • Interfaces
      • Unit
        • Tests

Classes

  • Klarna_Checkout_HTTP_CURLFactory
  • Klarna_Checkout_HTTP_Transport
  1 <?php
  2 /**
  3  * Copyright 2015 Klarna AB
  4  *
  5  * Licensed under the Apache License, Version 2.0 (the "License");
  6  * you may not use this file except in compliance with the License.
  7  * You may obtain a copy of the License at
  8  *
  9  *     http://www.apache.org/licenses/LICENSE-2.0
 10  *
 11  * Unless required by applicable law or agreed to in writing, software
 12  * distributed under the License is distributed on an "AS IS" BASIS,
 13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  * See the License for the specific language governing permissions and
 15  * limitations under the License.
 16  *
 17  * File containing the Klarna_Checkout_HTTP_CURLTransport class
 18  *
 19  * PHP version 5.3
 20  *
 21  * @category   Payment
 22  * @package    Payment_Klarna
 23  * @subpackage HTTP
 24  * @author     Klarna <support@klarna.com>
 25  * @copyright  2015 Klarna AB AB
 26  * @license    http://www.apache.org/licenses/LICENSE-2.0 Apache license v2.0
 27  * @link       http://developers.klarna.com/
 28  */
 29 
 30 /**
 31  * Klarna HTTP transport implementation for cURL
 32  *
 33  * @category   Payment
 34  * @package    Payment_Klarna
 35  * @subpackage HTTP
 36  * @author     Klarna <support@klarna.com>
 37  * @copyright  2015 Klarna AB
 38  * @license    http://www.apache.org/licenses/LICENSE-2.0 Apache license v2.0
 39  * @link       http://developers.klarna.com/
 40  */
 41 class Klarna_Checkout_HTTP_CURLTransport implements
 42     Klarna_Checkout_HTTP_TransportInterface
 43 {
 44     const DEFAULT_TIMEOUT = 10;
 45 
 46     /**
 47      * Factory for cUrl
 48      *
 49      * @var Klarna_Checkout_HTTP_CURLFactory
 50      */
 51     protected $curl;
 52 
 53     /**
 54      * Number of seconds before the connection times out.
 55      *
 56      * @var int
 57      */
 58     protected $timeout;
 59 
 60     /**
 61      * Options for cURL
 62      *
 63      * @var array
 64      */
 65     protected $options;
 66 
 67     /**
 68      * Initializes a new instance of the HTTP cURL class.
 69      *
 70      * @param Klarna_Checkout_HTTP_CURLFactory $curl factory to for curl handles
 71      */
 72     public function __construct(Klarna_Checkout_HTTP_CURLFactory $curl)
 73     {
 74         $this->curl = $curl;
 75         $this->timeout = self::DEFAULT_TIMEOUT;
 76         $this->options = array();
 77     }
 78 
 79     /**
 80      * Set specific cURL options.
 81      *
 82      * @param int   $option cURL option constant
 83      * @param mixed $value  cURL option value
 84      *
 85      * @return void
 86      */
 87     public function setOption($option, $value)
 88     {
 89         $this->options[$option] = $value;
 90     }
 91 
 92     /**
 93      * Sets the number of seconds until a connection times out.
 94      *
 95      * @param int $timeout number of seconds
 96      *
 97      * @return void
 98      */
 99     public function setTimeout($timeout)
100     {
101         $this->timeout = intval($timeout);
102     }
103 
104     /**
105      * Gets the number of seconds before the connection times out.
106      *
107      * @return int timeout in number of seconds
108      */
109     public function getTimeout()
110     {
111         return $this->timeout;
112     }
113 
114     /**
115      * Performs a HTTP request.
116      *
117      * @param Klarna_Checkout_HTTP_Request $request the HTTP request to send.
118      *
119      * @throws RuntimeException                Thrown if a cURL handle cannot
120      *                                         be initialized.
121      * @throws Klarna_Checkout_ConnectionErrorException Thrown for unspecified
122      *                                                  network or hardware issues.
123      * @return Klarna_Checkout_HTTP_Response
124      */
125     public function send(Klarna_Checkout_HTTP_Request $request)
126     {
127         $curl = $this->curl->handle();
128         if ($curl === false) {
129             throw new RuntimeException(
130                 'Failed to initialize a HTTP handle.'
131             );
132         }
133 
134         $url = $request->getURL();
135         $curl->setOption(CURLOPT_URL, $url);
136 
137         $method = $request->getMethod();
138         if ($method === 'POST') {
139             $curl->setOption(CURLOPT_POST, true);
140             $curl->setOption(CURLOPT_POSTFIELDS, $request->getData());
141         }
142 
143         // Convert headers to cURL format.
144         $requestHeaders = array();
145         foreach ($request->getHeaders() as $key => $value) {
146             $requestHeaders[] = $key . ': ' . $value;
147         }
148 
149         $curl->setOption(CURLOPT_HTTPHEADER, $requestHeaders);
150 
151         $curl->setOption(CURLOPT_RETURNTRANSFER, true);
152         $curl->setOption(CURLOPT_CONNECTTIMEOUT, $this->timeout);
153         $curl->setOption(CURLOPT_TIMEOUT, $this->timeout);
154 
155         $curlHeaders = new Klarna_Checkout_HTTP_CURLHeaders();
156         $curl->setOption(
157             CURLOPT_HEADERFUNCTION,
158             array(&$curlHeaders, 'processHeader')
159         );
160 
161         $curl->setOption(CURLOPT_SSL_VERIFYHOST, 2);
162         $curl->setOption(CURLOPT_SSL_VERIFYPEER, true);
163 
164         // Override specific set options
165         foreach ($this->options as $option => $value) {
166             $curl->setOption($option, $value);
167         }
168 
169         $payload = $curl->execute();
170         $info = $curl->getInfo();
171         $error = $curl->getError();
172 
173         $curl->close();
174 
175         /*
176          * A failure occurred if:
177          * payload is false (e.g. HTTP timeout?).
178          * info is false, then it has no HTTP status code.
179          */
180         if ($payload === false || $info === false) {
181             throw new Klarna_Checkout_ConnectionErrorException(
182                 "Connection to '{$url}' failed: {$error}"
183             );
184         }
185 
186         $headers = $curlHeaders->getHeaders();
187 
188         // Convert Content-Type into a normal header
189         $headers['Content-Type'] = $info['content_type'];
190 
191         $response = new Klarna_Checkout_HTTP_Response(
192             $request, $headers, intval($info['http_code']), strval($payload)
193         );
194 
195         return $response;
196     }
197 
198     /**
199      * Creates a HTTP request object.
200      *
201      * @param string $url the request URL.
202      *
203      * @throws InvalidArgumentException If the specified argument
204      *                                  is not of type string.
205      * @return Klarna_Checkout_HTTP_Request
206      */
207     public function createRequest($url)
208     {
209         return new Klarna_Checkout_HTTP_Request($url);
210     }
211 }
212 
API documentation generated by ApiGen