PluginProbe
PostNL for WooCommerce / 4.4.1
PostNL for WooCommerce v4.4.1
5.9.12 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 All 72 releases
woo-postnl / includes / admin / class-wcpn-api.php

class-wcpn-api.php in PostNL for WooCommerce 4.4.1, at includes/admin/class-wcpn-api.php

229 lines 6.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 use MyParcelNL\Sdk\src\Helper\MyParcelCollection;
4 use WPO\WC\PostNL\Compatibility\Order as WCX_Order;
5 use WPO\WC\PostNL\Compatibility\WC_Core;
6 use WPO\WC\PostNL\Compatibility\WCPN_ChannelEngine_Compatibility as ChannelEngine;
7
8 if (! defined("ABSPATH")) {
9 exit;
10 } // Exit if accessed directly
11
12 if (class_exists('WCPN_API')) {
13 return;
14 }
15
16 class WCPN_API extends WCPN_Rest
17 {
18 /**
19 * @var string
20 */
21 public $apiUrl = "https://api.myparcel.nl/";
22
23 /**
24 * @var string
25 */
26 private $key;
27
28 /**
29 * @var string
30 */
31 private $userAgent;
32
33 /**
34 * Default constructor
35 *
36 * @param string $key API Key provided by PostNL
37 *
38 * @throws Exception
39 */
40 public function __construct($key)
41 {
42 parent::__construct();
43
44 $this->apiUrl = WCPN_Data::API_URL;
45 $this->userAgent = $this->getUserAgent();
46 $this->key = (string) $key;
47 }
48
49 /**
50 * Add shipment
51 *
52 * @param array $shipments array of shipments
53 * @param string $type shipment type: standard/return/unrelated_return
54 *
55 * @return array
56 * @throws Exception
57 * @deprecated Use MyParcel SDK instead
58 */
59 public function add_shipments(array $shipments, string $type = "standard"): array
60 {
61 $endpoint = "shipments";
62
63 // define content type
64 switch ($type) {
65 case "return":
66 $content_type = "application/vnd.return_shipment+json";
67 $data_key = "return_shipments";
68 break;
69 case "unrelated_return":
70 $content_type = "application/vnd.unrelated_return_shipment+json";
71 $data_key = "unrelated_return_shipments";
72 break;
73 default:
74 $content_type = "application/vnd.shipment+json";
75 $data_key = "shipments";
76 break;
77 }
78
79 $data = [
80 "data" => [
81 $data_key => $shipments,
82 ],
83 ];
84
85 $json = json_encode($data);
86
87 $headers = [
88 "Content-type" => $content_type . "; charset=UTF-8",
89 "Authorization" => "basic " . base64_encode("{$this->key}"),
90 "user-agent" => $this->userAgent,
91 ];
92
93 $request_url = $this->apiUrl . $endpoint;
94
95 return $this->post($request_url, $json, $headers);
96 }
97
98 /**
99 * Get shipments
100 *
101 * @param int|array $ids
102 * @param array $params request parameters
103 *
104 * @return array response
105 * @throws Exception
106 */
107 public function get_shipments($ids, array $params = []): array
108 {
109 $endpoint = "shipments";
110
111 $headers = [
112 "headers" => [
113 "Accept" => "application/json; charset=UTF-8",
114 "Authorization" => "basic " . base64_encode("{$this->key}"),
115 "user-agent" => $this->userAgent,
116 ],
117 ];
118
119 $request_url = $this->apiUrl . $endpoint . "/" . implode(";", (array) $ids);
120 $request_url = add_query_arg($params, $request_url);
121
122 return $this->get($request_url, $headers);
123 }
124
125 /**
126 * Get Wordpress, WooCommerce, PostNL version and place theme in a array. Implode the array to get an UserAgent.
127 *
128 * @return string
129 */
130 private function getUserAgent(): string
131 {
132 $userAgents = [
133 'Wordpress',
134 get_bloginfo('version')
135 . 'WooCommerce/'
136 . WOOCOMMERCE_VERSION
137 . 'PostNL-WooCommerce/'
138 . WC_POSTNL_VERSION,
139 ];
140
141 // Place white space between the array elements
142 return implode(" ", $userAgents);
143 }
144
145 /**
146 * Get shipment labels, save them to the orders before showing them.
147 *
148 * @param array $shipment_ids Shipment ids.
149 * @param array $order_ids
150 * @param array $positions Print position(s).
151 * @param bool $display Download or display.
152 *
153 * @throws Exception
154 */
155 public function getShipmentLabels(array $shipment_ids, array $order_ids, array $positions = [], $display = true)
156 {
157 $collection = MyParcelCollection::findMany($shipment_ids, $this->key);
158
159 /**
160 * @see https://github.com/MyParcelNL/Sdk#label-format-and-position
161 */
162 if (WCPOST()->setting_collection->getByName(WCPOST_Settings::SETTING_LABEL_FORMAT) === "A6") {
163 $positions = false;
164 }
165
166 if ($display) {
167 $collection->setPdfOfLabels($positions);
168 $this->updateOrderBarcode($order_ids, $collection);
169 $collection->downloadPdfOfLabels($display);
170 }
171
172 if (! $display) {
173 $collection->setLinkOfLabels($positions);
174 $this->updateOrderBarcode($order_ids, $collection);
175 echo $collection->getLinkOfLabels();
176 die();
177 }
178 }
179
180 /**
181 * Update the status of given order based on the automatic order status settings.
182 *
183 * @param WC_Order $order
184 * @param string $changeStatusAtExportOrPrint
185 */
186 public function updateOrderStatus(WC_Order $order, string $changeStatusAtExportOrPrint): void
187 {
188 $statusAutomation = WCPOST()->setting_collection->isEnabled(WCPOST_Settings::SETTING_ORDER_STATUS_AUTOMATION);
189 $momentOfStatusChange = WCPOST()->setting_collection->getByName(WCPOST_Settings::SETTING_CHANGE_ORDER_STATUS_AFTER);
190 $newStatus = WCPOST()->setting_collection->getByName(WCPOST_Settings::SETTING_AUTOMATIC_ORDER_STATUS);
191
192 if ($statusAutomation && $changeStatusAtExportOrPrint === $momentOfStatusChange) {
193 $order->update_status(
194 $newStatus,
195 __("postnl_shipment_created", "woocommerce-postnl")
196 );
197
198 WCPN_Log::add("Status of order {$order->get_id()} updated to \"$newStatus\"");
199 }
200 }
201
202 /**
203 * @param array $orderIds
204 * @param MyParcelCollection $collection
205 *
206 * @throws Exception
207 */
208 private function updateOrderBarcode(array $orderIds, MyParcelCollection $collection): void
209 {
210 foreach ($orderIds as $orderId) {
211 $order = WC_Core::get_order($orderId);
212 $lastShipmentIds = WCX_Order::get_meta($order, WCPOST_Admin::META_LAST_SHIPMENT_IDS);
213
214 if (empty($lastShipmentIds)) {
215 continue;
216 }
217
218 $shipmentData = (new WCPN_Export())->getShipmentData($lastShipmentIds, $order);
219 $trackTrace = $shipmentData["track_trace"] ?? null;
220
221 $this->updateOrderStatus($order, WCPN_Settings_Data::CHANGE_STATUS_AFTER_PRINTING);
222
223 ChannelEngine::updateMetaOnExport($order, $trackTrace);
224 }
225
226 WCPN_Export::saveTrackTracesToOrders($collection, $orderIds);
227 }
228 }
229