• Namespace
  • Class

Namespaces

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

Classes

  • Klarna\Rest\Checkout\Order
  • Klarna\Rest\CustomerToken\Tokens
  • Klarna\Rest\HostedPaymentPage\Sessions
  • Klarna\Rest\InstantShopping\ButtonKeys
  • Klarna\Rest\InstantShopping\Orders
  • Klarna\Rest\MerchantCardService\VCCSettlements
  • Klarna\Rest\OrderManagement\Capture
  • Klarna\Rest\OrderManagement\Order
  • Klarna\Rest\OrderManagement\Refund
  • Klarna\Rest\Payments\Orders
  • Klarna\Rest\Payments\Sessions
  • Klarna\Rest\Resource
  • Klarna\Rest\Settlements\Payouts
  • Klarna\Rest\Settlements\Reports
  • Klarna\Rest\Settlements\Transactions
  • Klarna\Rest\Transport\ApiResponse
  • Klarna\Rest\Transport\Connector
  • Klarna\Rest\Transport\CURLConnector
  • Klarna\Rest\Transport\GuzzleConnector
  • Klarna\Rest\Transport\Method
  • Klarna\Rest\Transport\ResponseValidator
  • Klarna\Rest\Transport\UserAgent

Interfaces

  • Klarna\Rest\Transport\ConnectorInterface
  • Klarna\Rest\Transport\UserAgentInterface

Exceptions

  • Klarna\Exceptions\NotApplicableException
  • 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 Tokens class.
 18  */
 19 
 20 namespace Klarna\Rest\CustomerToken;
 21 
 22 use GuzzleHttp\Exception\RequestException;
 23 use Klarna\Rest\Resource;
 24 use Klarna\Rest\Transport\ConnectorInterface;
 25 use Klarna\Rest\Transport\Exception\ConnectorException;
 26 
 27 /**
 28  * Tokens resource.
 29  *
 30  * The Customer Token API is used to charge customers with a tokenized Klarna payment method
 31  * and can be used for recurring purchases, subscriptions and for storing a customer's payment method. Tokens are
 32  * created using the generate a customer token call in the payments API.
 33  *
 34  * @example docs/examples/CustomerTokenAPI/Tokens/create_order.php Create a new order using customer token
 35  * @example docs/examples/CustomerTokenAPI/Tokens/read_token_details.php Read customer token details
 36  */
 37 class Tokens extends Resource
 38 {
 39     /**
 40      * {@inheritDoc}
 41      */
 42     const ID_FIELD = 'customerToken';
 43 
 44     /**
 45      * {@inheritDoc}
 46      */
 47     public static $path = '/customer-token/v1/tokens';
 48 
 49     /**
 50      * Constructs a Tokens instance.
 51      *
 52      * @param ConnectorInterface $connector HTTP transport connector
 53      * @param string    $customerToken   Customer Token
 54      */
 55     public function __construct(ConnectorInterface $connector, $customerToken)
 56     {
 57         parent::__construct($connector);
 58 
 59         $this->setLocation(self::$path . "/{$customerToken}");
 60         $this[static::ID_FIELD] = $customerToken;
 61     }
 62 
 63     /**
 64      * Creates order using Customer Token.
 65      *
 66      * @param array  $data Order data
 67      * @param string $klarnaIdempotencyKey Idempotency Key
 68      *
 69      * @throws ConnectorException When the API replies with an error response
 70      * @throws RequestException   When an error is encountered
 71      * @throws \RuntimeException  If the location header is missing
 72      * @throws \RuntimeException  If the API replies with an unexpected response
 73      * @throws \LogicException    When Guzzle cannot populate the response
 74      *
 75      * @return array created order data
 76      */
 77     public function createOrder(array $data, $klarnaIdempotencyKey = null)
 78     {
 79         $headers = ['Content-Type' => 'application/json'];
 80         if (!is_null($klarnaIdempotencyKey)) {
 81             $headers['Klarna-Idempotency-Key'] = $klarnaIdempotencyKey;
 82         }
 83 
 84         return $this->request(
 85             'POST',
 86             $this->getLocation() . '/order',
 87             $headers,
 88             $data !== null ? \json_encode($data) : null
 89         )
 90         ->expectSuccessfull()
 91         ->status('200')
 92         ->contentType('application/json')
 93         ->getJson();
 94     }
 95 
 96     /**
 97      * Update the status of a customer token.
 98      *
 99      * @param array $data Customer token data
100      *
101      * @throws ConnectorException When the API replies with an error response
102      * @throws RequestException   When an error is encountered
103      * @throws \RuntimeException  If the API replies with an unexpected response
104      * @throws \LogicException    When Guzzle cannot populate the response
105      *
106      * @return self
107      */
108     public function updateTokenStatus(array $data)
109     {
110         $this->patch($this->getLocation() . '/status', $data)
111             ->expectSuccessfull()
112             ->status('202');
113 
114         return $this;
115     }
116 }
117 
API documentation generated by ApiGen