| 1 |
<?php |
| 2 |
|
| 3 |
if ( ! defined('ABSPATH')) exit; // Exit if accessed directly |
| 4 |
|
| 5 |
if ( ! class_exists('WC_PostNL_API')) : |
| 6 |
|
| 7 |
class WC_PostNL_API extends WC_PostNL_REST_Client { |
| 8 |
|
| 9 |
public $APIURL = "https://api.myparcel.nl/"; |
| 10 |
private $key; |
| 11 |
|
| 12 |
/** |
| 13 |
* Default constructor |
| 14 |
* |
| 15 |
* @param string $key API Key provided by PostNL |
| 16 |
* |
| 17 |
* @return void |
| 18 |
*/ |
| 19 |
function __construct($key) { |
| 20 |
parent::__construct(); |
| 21 |
|
| 22 |
$this->user_agent = $this->getUserAgent(); |
| 23 |
|
| 24 |
$this->key = $key; |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Add shipment |
| 29 |
* |
| 30 |
* @param array $shipments array of shipments |
| 31 |
* @param string $type shipment type: standard/return/unrelated_return |
| 32 |
*/ |
| 33 |
public function add_shipments($shipments, $type = 'standard') { |
| 34 |
$endpoint = 'shipments'; |
| 35 |
|
| 36 |
// define content type |
| 37 |
switch($type) { |
| 38 |
case 'standard': |
| 39 |
default: |
| 40 |
$content_type = 'application/vnd.shipment+json'; |
| 41 |
$data_key = 'shipments'; |
| 42 |
break; |
| 43 |
case 'return': |
| 44 |
$content_type = 'application/vnd.return_shipment+json'; |
| 45 |
$data_key = 'return_shipments'; |
| 46 |
break; |
| 47 |
case 'unrelated_return': |
| 48 |
$content_type = 'application/vnd.unrelated_return_shipment+json'; |
| 49 |
$data_key = 'unrelated_return_shipments'; |
| 50 |
break; |
| 51 |
} |
| 52 |
|
| 53 |
$data = array( |
| 54 |
'data' => array( |
| 55 |
$data_key => $shipments, |
| 56 |
), |
| 57 |
); |
| 58 |
|
| 59 |
$json = json_encode($data); |
| 60 |
|
| 61 |
$headers = array( |
| 62 |
'Content-type' => $content_type . '; charset=UTF-8', |
| 63 |
'Authorization' => 'basic ' . base64_encode("{$this->key}"), |
| 64 |
'user-agent' => $this->user_agent |
| 65 |
); |
| 66 |
|
| 67 |
$request_url = $this->APIURL . $endpoint; |
| 68 |
$response = $this->post($request_url, $json, $headers); |
| 69 |
|
| 70 |
return $response; |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Delete Shipment |
| 75 |
* |
| 76 |
* @param array $ids shipment ids |
| 77 |
* |
| 78 |
* @return array response |
| 79 |
*/ |
| 80 |
public function delete_shipments($ids) { |
| 81 |
$endpoint = 'shipments'; |
| 82 |
|
| 83 |
$headers = array( |
| 84 |
'headers' => array( |
| 85 |
'Accept' => 'application/json; charset=UTF-8', |
| 86 |
'Authorization' => 'basic ' . base64_encode("{$this->key}"), |
| 87 |
'user-agent' => $this->user_agent |
| 88 |
) |
| 89 |
); |
| 90 |
|
| 91 |
$request_url = $this->APIURL . $endpoint . '/' . implode(';', $ids); |
| 92 |
$response = $this->delete($request_url, $headers); |
| 93 |
|
| 94 |
return $response; |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Unrelated return shipments |
| 99 |
* @return array response |
| 100 |
*/ |
| 101 |
public function unrelated_return_shipments() { |
| 102 |
$endpoint = 'return_shipments'; |
| 103 |
|
| 104 |
$headers = array( |
| 105 |
'Authorization: basic ' . base64_encode("{$this->key}"), |
| 106 |
); |
| 107 |
|
| 108 |
$request_url = $this->APIURL . $endpoint; |
| 109 |
$response = $this->post($request_url, '', $headers); |
| 110 |
|
| 111 |
return $response; |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Get shipments |
| 116 |
* |
| 117 |
* @param array $params request parameters |
| 118 |
* |
| 119 |
* @return array response |
| 120 |
*/ |
| 121 |
public function get_shipments($ids, $params = array()) { |
| 122 |
$endpoint = 'shipments'; |
| 123 |
|
| 124 |
$headers = array( |
| 125 |
'headers' => array( |
| 126 |
'Accept' => 'application/json; charset=UTF-8', |
| 127 |
'Authorization' => 'basic ' . base64_encode("{$this->key}"), |
| 128 |
'user-agent' => $this->user_agent |
| 129 |
) |
| 130 |
); |
| 131 |
|
| 132 |
$request_url = $this->APIURL . $endpoint . '/' . implode(';', (array) $ids); |
| 133 |
$request_url = add_query_arg($params, $request_url); |
| 134 |
$response = $this->get($request_url, $headers); |
| 135 |
|
| 136 |
return $response; |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Get shipment labels |
| 141 |
* |
| 142 |
* @param array $ids shipment ids |
| 143 |
* @param array $params request parameters |
| 144 |
* @param string $return pdf or json |
| 145 |
* |
| 146 |
* @return array response |
| 147 |
*/ |
| 148 |
public function get_shipment_labels($ids, $params = array(), $return = 'pdf') { |
| 149 |
$endpoint = 'shipment_labels'; |
| 150 |
|
| 151 |
if ($return == 'pdf') { |
| 152 |
$accept = 'application/pdf'; // (For the PDF binary. This is the default.) |
| 153 |
$raw = true; |
| 154 |
} else { |
| 155 |
$accept = 'application/json; charset=UTF-8'; // (For shipment download link) |
| 156 |
$raw = false; |
| 157 |
} |
| 158 |
|
| 159 |
$headers = array( |
| 160 |
'headers' => array( |
| 161 |
'Accept' => $accept, |
| 162 |
'Authorization' => 'basic ' . base64_encode("{$this->key}"), |
| 163 |
'user-agent' => $this->user_agent |
| 164 |
) |
| 165 |
); |
| 166 |
|
| 167 |
$positions = isset($params['positions']) ? $params['positions'] : null; |
| 168 |
|
| 169 |
$label_format_url = $this->get_label_format_url($positions); |
| 170 |
$request_url = $this->APIURL . $endpoint . '/' . implode(';', $ids) . '?' . $label_format_url; |
| 171 |
|
| 172 |
$response = $this->get($request_url, $headers, $raw); |
| 173 |
|
| 174 |
return $response; |
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
* Track shipments |
| 179 |
* |
| 180 |
* @param array $ids shipment ids |
| 181 |
* @param array $params request parameters |
| 182 |
* |
| 183 |
* @return array response |
| 184 |
*/ |
| 185 |
public function get_tracktraces($ids, $params = array()) { |
| 186 |
$endpoint = 'tracktraces'; |
| 187 |
|
| 188 |
$headers = array( |
| 189 |
'headers' => array( |
| 190 |
'Authorization' => 'basic ' . base64_encode("{$this->key}"), |
| 191 |
'user-agent' => $this->user_agent |
| 192 |
) |
| 193 |
); |
| 194 |
|
| 195 |
$request_url = add_query_arg($params, $this->APIURL . $endpoint . '/' . implode(';', $ids)); |
| 196 |
$response = $this->get($request_url, $headers, false); |
| 197 |
|
| 198 |
return $response; |
| 199 |
} |
| 200 |
|
| 201 |
/** |
| 202 |
* Get delivery options |
| 203 |
* @return array response |
| 204 |
*/ |
| 205 |
public function get_delivery_options($params = array(), $raw = false) { |
| 206 |
$endpoint = 'delivery_options'; |
| 207 |
if (isset(WooCommerce_PostNL()->checkout_settings['monday_delivery'])) { |
| 208 |
$params['monday_delivery'] = 1; |
| 209 |
} |
| 210 |
|
| 211 |
$request_url = add_query_arg($params, $this->APIURL . $endpoint); |
| 212 |
$response = $this->get($request_url, null, $raw); |
| 213 |
|
| 214 |
return $response; |
| 215 |
} |
| 216 |
|
| 217 |
/** |
| 218 |
* Get Wordpress, Woocommerce, PostNL version and place theme in a array. Implode the array to get an UserAgent. |
| 219 |
* @return string |
| 220 |
*/ |
| 221 |
private function getUserAgent() { |
| 222 |
$userAgents = array( |
| 223 |
'Wordpress/' . get_bloginfo('version'), |
| 224 |
'WooCommerce/' . WOOCOMMERCE_VERSION, |
| 225 |
'PostNL-WooCommerce/' . WC_POSTNL_VERSION, |
| 226 |
); |
| 227 |
|
| 228 |
//Place white space between the array elements |
| 229 |
$userAgent = implode(' ', $userAgents); |
| 230 |
|
| 231 |
return $userAgent; |
| 232 |
} |
| 233 |
|
| 234 |
private function get_label_format_url($positions) { |
| 235 |
$generalSettings = WooCommerce_PostNL()->general_settings; |
| 236 |
|
| 237 |
if ($generalSettings['label_format'] == 'A4') { |
| 238 |
return 'format=A4&positions=' . $positions; |
| 239 |
} |
| 240 |
|
| 241 |
if ($generalSettings['label_format'] == 'A6') { |
| 242 |
return 'format=A6'; |
| 243 |
} |
| 244 |
|
| 245 |
return ''; |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
endif; // class_exists |
| 250 |
|