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\ConnectorInterface;
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 ConnectorInterface $connector HTTP transport connector
51 * @param string $orderUrl Parent order resource url
52 * @param string $captureId Capture ID
53 */
54 public function __construct(ConnectorInterface $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 ->expectSuccessfull()
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 ->expectSuccessfull()
108 ->status('204');
109
110 return $this;
111 }
112
113 /**
114 * Updates the customers details.
115 *
116 * @param array $data Customer details data
117 *
118 * @throws ConnectorException When the API replies with an error response
119 * @throws RequestException When an error is encountered
120 * @throws \RuntimeException If the API replies with an unexpected response
121 * @throws \LogicException When Guzzle cannot populate the response
122 *
123 * @return self
124 */
125 public function updateCustomerDetails(array $data)
126 {
127 $this->patch($this->getLocation() . '/customer-details', $data)
128 ->expectSuccessfull()
129 ->status('204');
130
131 return $this;
132 }
133
134 /**
135 * Trigger send outs for this capture.
136 *
137 * @throws ConnectorException When the API replies with an error response
138 * @throws RequestException When an error is encountered
139 * @throws \RuntimeException If the API replies with an unexpected response
140 * @throws \LogicException When Guzzle cannot populate the response
141 *
142 * @return self
143 */
144 public function triggerSendout()
145 {
146 $this->post($this->getLocation() . '/trigger-send-out')
147 ->expectSuccessfull()
148 ->status('204');
149
150 return $this;
151 }
152 }
153