PluginProbe
PostNL for WooCommerce / trunk
PostNL for WooCommerce vtrunk
5.9.11 5.9.10 5.9.9 5.9.8 5.9.7 5.9.6 trunk 2.5.0 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 3.1.4 3.1.5 3.1.6 3.1.7 4.0.0 4.0.1 4.0.2 4.3.2 4.3.3 4.4.0 4.4.1 4.4.2 All 71 releases
woo-postnl / src / Rest_API / Base.php

Base.php in PostNL for WooCommerce trunk, at src/Rest_API/Base.php

296 lines 6.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Class Rest_API/Checkout file.
4 *
5 * @package PostNLWooCommerce\Rest_API
6 */
7
8 namespace PostNLWooCommerce\Rest_API;
9
10 use PostNLWooCommerce\Shipping_Method\Settings;
11 use PostNLWooCommerce\Main;
12
13 if ( ! defined( 'ABSPATH' ) ) {
14 exit;
15 }
16
17 /**
18 * Class Base
19 *
20 * @package PostNLWooCommerce\Rest_API
21 */
22 class Base {
23 /**
24 * Settings class instance.
25 *
26 * @var PostNLWooCommerce\Shipping_Method\Settings
27 */
28 protected $settings;
29
30 /**
31 * PostnL API URL.
32 *
33 * @var string
34 */
35 public $api_url;
36
37 /**
38 * PostnL API Method.
39 *
40 * @var string
41 */
42 public $method = 'POST';
43
44 /**
45 * PostnL Logger.
46 *
47 * @var Logger
48 */
49 public $logger;
50
51 /**
52 * API arguments.
53 *
54 * @var Array
55 */
56 public $api_args;
57
58 /**
59 * Item Info.
60 *
61 * @var Array
62 */
63 public $item_info;
64
65 /**
66 * PostnL API key value from the settings.
67 *
68 * @var string
69 */
70 public $api_key;
71
72 /**
73 * API Endpoint.
74 *
75 * @var string
76 */
77 public $endpoint;
78
79 /**
80 * Sandbox mode or not.
81 *
82 * @var boolean
83 */
84 public $is_sandbox;
85
86 /**
87 * Class constructor.
88 *
89 * @param Item_Info $item_info Set of Item_Info.
90 */
91 public function __construct( $item_info ) {
92 $this->settings = Settings::get_instance();
93 $this->logger = Main::get_logger();
94
95 $this->set_item_info( $item_info );
96 $this->check_api_mode();
97 $this->set_api_url();
98 }
99
100 /**
101 * Method to set API arguments as a class property.
102 *
103 * @param array $api_args Set of API arguments.
104 */
105 public function set_api_args( $api_args ) {
106 $this->api_args = $api_args;
107 }
108
109 /**
110 * Method to set API args to an item info.
111 *
112 * @param array $api_args Set of API arguments.
113 */
114 public function set_item_info( $api_args ) {
115 $this->item_info = $api_args;
116 }
117
118 /**
119 * Check the API mode from the settings.
120 */
121 public function check_api_mode() {
122 $this->is_sandbox = $this->settings->is_sandbox();
123 }
124
125 /**
126 * Set API Environment value.
127 */
128 public function set_api_url() {
129 $this->api_url = ( true === $this->is_sandbox ) ? POSTNL_WC_SANDBOX_API_URL : POSTNL_WC_PROD_API_URL;
130 $this->api_url .= $this->endpoint;
131
132 if ( ! empty( $this->compose_url_params() ) && is_array( $this->compose_url_params() ) ) {
133 $this->api_url = add_query_arg(
134 $this->compose_url_params(),
135 $this->api_url
136 );
137 }
138 }
139
140 /**
141 * Get API Environment value.
142 *
143 * @return String
144 */
145 public function get_api_url() {
146 if ( empty( $this->api_url ) ) {
147 $this->set_api_url();
148 }
149
150 return $this->api_url;
151 }
152
153 /**
154 * Set API key value.
155 */
156 public function set_api_key() {
157 $this->api_key = ( true === $this->is_sandbox ) ? $this->settings->get_api_key_sandbox() : $this->settings->get_api_key();
158 }
159
160 /**
161 * Get API Key value.
162 *
163 * @return String
164 */
165 public function get_api_key() {
166 if ( empty( $this->api_key ) ) {
167 $this->set_api_key();
168 }
169
170 return $this->api_key;
171 }
172
173 /**
174 * Get basic headers args for REST request.
175 *
176 * @return array
177 */
178 public function get_basic_headers_args() {
179 return array(
180 'apikey' => $this->get_api_key(),
181 'accept' => 'application/json',
182 'Content-Type' => 'application/json',
183 'SourceSystem' => '35',
184 );
185 }
186
187 /**
188 * Get headers args for REST request.
189 * We can manipulate this in child class if some class has different needs for API headers.
190 *
191 * @return Array
192 */
193 public function get_headers_args() {
194 return $this->get_basic_headers_args();
195 }
196
197 /**
198 * Function for composing API parameter in the URL for GET request.
199 */
200 public function compose_url_params() {
201 return array();
202 }
203
204 /**
205 * Function for composing API request.
206 */
207 public function compose_body_request() {
208 return array();
209 }
210
211 /**
212 * Send API request to PostNL Rest API.
213 *
214 * @throws \Exception Throw error if response has WP_Error.
215 */
216 public function send_request() {
217 $api_url = $this->get_api_url();
218 $request_args = array(
219 'method' => $this->method,
220 'headers' => $this->get_headers_args(),
221 );
222
223 if ( ! empty( $this->compose_body_request() ) && is_array( $this->compose_body_request() ) ) {
224 $request_args['body'] = wp_json_encode( $this->compose_body_request() );
225 }
226
227 $this->logger->write( sprintf( 'Begin send request to %1$s', $api_url ) );
228 $this->logger->write( 'API Request:' );
229 $this->logger->write( $request_args );
230
231 for ( $i = 1; $i <= 5; $i++ ) {
232 $response = wp_remote_request( $api_url, $request_args );
233
234 if ( ! is_wp_error( $response ) ) {
235 $this->logger->write( sprintf( 'Get response after %1$d attempts.', $i ) );
236 break;
237 }
238 }
239
240 if ( is_wp_error( $response ) ) {
241 $this->logger->write( 'Get WP Error response:' );
242 $this->logger->write( $response );
243
244 throw new \Exception( $response->get_error_message() );
245 }
246
247 $body_response = wp_remote_retrieve_body( $response );
248 $header_response = wp_remote_retrieve_headers( $response );
249
250 $this->logger->write( 'API Successful response:' );
251 $this->logger->write( $header_response );
252 $this->logger->write( $body_response );
253
254 $this->check_response_error( $body_response );
255
256 return json_decode( $body_response, true );
257 }
258
259 /**
260 * Check if the response value has error or not.
261 *
262 * @param Mixed $response response value from the API call.
263 *
264 * @throws \Exception Error when response has error.
265 */
266 public function check_response_error( $response ) {
267
268 if ( ! is_array( $response ) ) {
269 $response = json_decode( $response, true );
270 }
271
272 if ( ! empty( $response['fault'] ) ) {
273 $error_text = ! empty( $response['fault']['faultstring'] ) ? $response['fault']['faultstring'] : esc_html__( 'Unknown error!', 'postnl-for-woocommerce' );
274 throw new \Exception( $error_text );
275 }
276
277 if ( ! empty( $response['Errors'] ) ) {
278 $first_error = array_shift( $response['Errors'] );
279 $error_text = ! empty( $first_error['Description'] ) ? $first_error['Description'] : '';
280
281 if ( empty( $error_text ) ) {
282 $error_text = ! empty( $first_error['ErrorMsg'] ) ? $first_error['ErrorMsg'] : esc_html__( 'Unknown error!', 'postnl-for-woocommerce' );
283 }
284
285 throw new \Exception( $error_text );
286 }
287
288 if ( ! empty( $response['Error'] ) ) {
289 $first_error = $response['Error'];
290 $error_text = ! empty( $first_error['ErrorMessage'] ) ? $first_error['ErrorMessage'] : esc_html__( 'Unknown error!', 'postnl-for-woocommerce' );
291
292 throw new \Exception( $error_text );
293 }
294 }
295 }
296