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 Capture class.
18 */
19
20 namespace Klarna\Rest\OrderManagement;
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 * Order Management: Capture resource.
29 *
30 * @example docs/examples/OrderManagementAPI/Captures/add_shipping_info.php
31 * @example docs/examples/OrderManagementAPI/Captures/fetch_captures.php
32 * @example docs/examples/OrderManagementAPI/Captures/trigger_send_out.php
33 * @example docs/examples/OrderManagementAPI/Captures/update_customer_details.php
34 */
35 class Capture extends Resource
36 {
37 /**
38 * {@inheritDoc}
39 */
40 const ID_FIELD = 'capture_id';
41
42 /**
43 * {@inheritDoc}
44 */
45 public static $path = '/captures';
46
47 /**
48 * Constructs a Capture instance.
49 *
50 * @param Connector $connector HTTP transport connector
51 * @param string $orderUrl Parent order resource url
52 * @param string $captureId Capture ID
53 */
54 public function __construct(Connector $connector, $orderUrl, $captureId = null)
55 {
56 parent::__construct($connector);
57
58 $url = $orderUrl . self::$path;
59 if ($captureId !== null) {
60 $url = "{$url}/{$captureId}";
61 $this[static::ID_FIELD] = $captureId;
62 }
63
64 $this->setLocation($url);
65 }
66
67 /**
68 * Creates the resource.
69 *
70 * @param array $data Creation data
71 *
72 * @throws ConnectorException When the API replies with an error response
73 * @throws RequestException When an error is encountered
74 * @throws \RuntimeException If the location header is missing
75 * @throws \RuntimeException If the API replies with an unexpected response
76 * @throws \LogicException When Guzzle cannot populate the response
77 *
78 * @return self
79 */
80 public function create(array $data)
81 {
82 $url = $this->post($this->getLocation(), $data)
83 ->status('201')
84 ->getLocation();
85
86 $this->setLocation($url);
87
88 return $this;
89 }
90
91 /**
92 * Appends shipping information to the capture.
93 *
94 * @param array $data Shipping info data
95 *
96 * @throws ConnectorException When the API replies with an error response
97 * @throws RequestException When an error is encountered
98 * @throws \RuntimeException If the API replies with an unexpected response
99 * @throws \LogicException When Guzzle cannot populate the response
100 *
101 * @return self
102 */
103 public function addShippingInfo(array $data)
104 {
105 $this->post($this->getLocation() . '/shipping-info', $data)
106 ->status('204');
107
108 return $this;
109 }
110
111 /**
112 * Updates the customers details.
113 *
114 * @param array $data Customer details data
115 *
116 * @throws ConnectorException When the API replies with an error response
117 * @throws RequestException When an error is encountered
118 * @throws \RuntimeException If the API replies with an unexpected response
119 * @throws \LogicException When Guzzle cannot populate the response
120 *
121 * @return self
122 */
123 public function updateCustomerDetails(array $data)
124 {
125 $this->patch($this->getLocation() . '/customer-details', $data)
126 ->status('204');
127
128 return $this;
129 }
130
131 /**
132 * Trigger send outs for this capture.
133 *
134 * @throws ConnectorException When the API replies with an error response
135 * @throws RequestException When an error is encountered
136 * @throws \RuntimeException If the API replies with an unexpected response
137 * @throws \LogicException When Guzzle cannot populate the response
138 *
139 * @return self
140 */
141 public function triggerSendout()
142 {
143 $this->post($this->getLocation() . '/trigger-send-out')
144 ->status('204');
145
146 return $this;
147 }
148 }
149