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