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