PluginProbe
PostNL for WooCommerce / 4.0.0
PostNL for WooCommerce v4.0.0
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.0.0, at includes/admin/class-wcpn-api.php

195 lines 5.5 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\WC_Core;
5 use WPO\WC\PostNL\Compatibility\Order as WC_Order;
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 PostNL 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', get_bloginfo('version').
134 'WooCommerce/' . WOOCOMMERCE_VERSION .
135 'PostNL-WooCommerce/' . WC_POSTNL_VERSION,
136 ];
137
138 // Place white space between the array elements
139 return implode(" ", $userAgents);
140 }
141
142 /**
143 * Get shipment labels, save them to the orders before showing them.
144 *
145 * @param array $shipment_ids Shipment ids.
146 * @param array $order_ids
147 * @param array $positions Print position(s).
148 * @param bool $display Download or display.
149 *
150 * @throws Exception
151 */
152 public function getShipmentLabels(array $shipment_ids, array $order_ids, array $positions = [], $display = true)
153 {
154 $collection = MyParcelCollection::findMany($shipment_ids, $this->key);
155
156 /**
157 * @see https://github.com/PostNL/Sdk#label-format-and-position
158 */
159 if (WCPOST()->setting_collection->getByName(WCPOST_Settings::SETTING_LABEL_FORMAT) === "A6") {
160 $positions = false;
161 }
162
163 if ($display) {
164 $collection->setPdfOfLabels($positions);
165 $this->updateOrderBarcode($order_ids, $collection);
166 $collection->downloadPdfOfLabels($display);
167 }
168
169 if (! $display) {
170 $collection->setLinkOfLabels($positions);
171 $this->updateOrderBarcode($order_ids, $collection);
172 echo $collection->getLinkOfLabels();
173 die();
174 }
175 }
176
177 /**
178 * @param array $orderIds
179 * @param MyParcelCollection $collection
180 * @throws Exception
181 */
182 private function updateOrderBarcode(array $orderIds, MyParcelCollection $collection) : void
183 {
184 foreach ($orderIds as $orderId) {
185 $order = WC_Core::get_order($orderId);
186 $lastShipmentIds = unserialize($order->get_meta('_postnl_last_shipment_ids'));
187 $shipmentData = (new WCPN_Export())->getShipmentData($lastShipmentIds, $order);
188 $trackTrace = $shipmentData["track_trace"] ?? null;
189 ChannelEngine::updateMetaOnExport($order, $trackTrace);
190 }
191
192 WCPN_Export::saveTrackTracesToOrders($collection, $orderIds);
193 }
194 }
195