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 Refund class.
18 */
19
20 namespace Klarna\Rest\OrderManagement;
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 * Order Management: Refund resource.
29 *
30 * @example docs/examples/OrderManagementAPI/Refunds/fetch_refund.php Read information on a refund
31 * @example docs/examples/OrderManagementAPI/Refunds/refund_order.php Refund an amount of a captured order
32 */
33 class Refund extends Resource
34 {
35 /**
36 * {@inheritDoc}
37 */
38 const ID_FIELD = 'refund_id';
39
40 /**
41 * {@inheritDoc}
42 */
43 public static $path = '/refunds';
44
45 /**
46 * Constructs a Refund instance.
47 *
48 * @param ConnectorInterface $connector HTTP transport connector
49 * @param string $orderUrl Parent order resource url
50 * @param string $refundId Refund ID
51 */
52 public function __construct(ConnectorInterface $connector, $orderUrl, $refundId = null)
53 {
54 parent::__construct($connector);
55
56 $url = $orderUrl . self::$path;
57 if ($refundId !== null) {
58 $url = "{$url}/{$refundId}";
59 $this[static::ID_FIELD] = $refundId;
60 }
61
62 $this->setLocation($url);
63 }
64
65 /**
66 * Creates the resource.
67 *
68 * @param array $data Creation data
69 *
70 * @throws ConnectorException When the API replies with an error response
71 * @throws RequestException When an error is encountered
72 * @throws \RuntimeException If the location header is missing
73 * @throws \RuntimeException If the API replies with an unexpected response
74 * @throws \LogicException When Guzzle cannot populate the response
75 *
76 * @return self
77 */
78 public function create(array $data)
79 {
80 $url = $this->post($this->getLocation(), $data)
81 ->expectSuccessfull()
82 ->status('201')
83 ->getLocation();
84
85 $this->setLocation($url);
86
87 return $this;
88 }
89 }
90