• Namespace
  • Class

Namespaces

  • Klarna
    • Exceptions
    • Rest
      • Checkout
      • CustomerToken
      • HostedPaymentPage
      • OrderManagement
      • Payments
      • Settlements
      • Transport
        • Exception

Interfaces

  • Klarna\Rest\Transport\Exception\ConnectorException
  1 <?php
  2 /**
  3  * Copyright 2014 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 Sessions class.
 18  */
 19 
 20 namespace Klarna\Rest\Payments;
 21 
 22 use GuzzleHttp\Exception\RequestException;
 23 use Klarna\Rest\Resource;
 24 use Klarna\Rest\Transport\Connector;
 25 use Klarna\Rest\Transport\Exception\ConnectorException;
 26 
 27 /**
 28  * Payments session resource.
 29  *
 30  * @example docs/examples/PaymentsAPI/Sessions/create_new_credit_session.php
 31  * @example docs/examples/PaymentsAPI/Sessions/read_credit_session.php
 32  * @example docs/examples/PaymentsAPI/Sessions/update_credit_session.php
 33  */
 34 class Sessions extends Resource
 35 {
 36     /**
 37      * {@inheritDoc}
 38      */
 39     const ID_FIELD = 'session_id';
 40 
 41     /**
 42      * {@inheritDoc}
 43      */
 44     public static $path = '/payments/v1/sessions';
 45 
 46     /**
 47      * Constructs a session instance.
 48      *
 49      * @param Connector $connector HTTP transport connector
 50      * @param string    $sessionId   Session ID
 51      */
 52     public function __construct(Connector $connector, $sessionId = null)
 53     {
 54         parent::__construct($connector);
 55 
 56         if ($sessionId !== null) {
 57             $this->setLocation(self::$path . "/{$sessionId}");
 58             $this[static::ID_FIELD] = $sessionId;
 59         }
 60     }
 61 
 62     /**
 63      * Creates the resource.
 64      *
 65      * @param array $data Creation data
 66      *
 67      * @throws ConnectorException When the API replies with an error response
 68      * @throws RequestException   When an error is encountered
 69      * @throws \RuntimeException  If the location header is missing
 70      * @throws \RuntimeException  If the API replies with an unexpected response
 71      * @throws \LogicException    When Guzzle cannot populate the response
 72      *
 73      * @return self
 74      */
 75     public function create(array $data)
 76     {
 77         $data = $this->post(self::$path, $data)
 78             ->status('200')
 79             ->contentType('application/json');
 80 
 81         $this->exchangeArray($data->getJson());
 82 
 83         // Payments API does not send Location header after creating a new session.
 84         // Use workaround to set new location.
 85         $this->setLocation(self::$path . '/' . $this->getId());
 86 
 87         return $this;
 88     }
 89 
 90     /**
 91      * Updates the resource.
 92      *
 93      * @param array $data Update data
 94      *
 95      * @throws ConnectorException        When the API replies with an error response
 96      * @throws RequestException          When an error is encountered
 97      * @throws \RuntimeException         On an unexpected API response
 98      * @throws \RuntimeException         If the response content type is not JSON
 99      * @throws \InvalidArgumentException If the JSON cannot be parsed
100      * @throws \LogicException           When Guzzle cannot populate the response
101      *
102      * @return self
103      */
104     public function update(array $data)
105     {
106         $this->post($this->getLocation(), $data)
107             ->status('204');
108             // ->contentType('application/json');
109             // TODO: We cannot check the Content-type here because of an inconsistency
110             // between service and documentation. The real Content-Type is
111             // "application/octet-stream but not the "application/json" as in the docs.
112 
113         return $this;
114     }
115 }
116 
API documentation generated by ApiGen