| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class Rest_API\Shipping\Client file. |
| 4 |
* |
| 5 |
* @package PostNLWooCommerce\Rest_API\Legacy\Shipping |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace PostNLWooCommerce\Rest_API\Legacy\Shipping; |
| 9 |
|
| 10 |
use PostNLWooCommerce\Rest_API\Base; |
| 11 |
|
| 12 |
if ( ! defined( 'ABSPATH' ) ) { |
| 13 |
exit; |
| 14 |
} |
| 15 |
|
| 16 |
/** |
| 17 |
* Class Client |
| 18 |
* |
| 19 |
* @package PostNLWooCommerce\Rest_API\Legacy\Shipping |
| 20 |
*/ |
| 21 |
class Client extends Base { |
| 22 |
/** |
| 23 |
* API Endpoint. |
| 24 |
* |
| 25 |
* @var string |
| 26 |
*/ |
| 27 |
public $endpoint = '/v1/shipment'; |
| 28 |
|
| 29 |
/** |
| 30 |
* Function for composing API request in the URL for GET request. |
| 31 |
* |
| 32 |
* @return Array. |
| 33 |
*/ |
| 34 |
public function compose_url_params() { |
| 35 |
return array( |
| 36 |
'confirm' => 'true', |
| 37 |
); |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Function for composing API request. |
| 42 |
*/ |
| 43 |
public function compose_body_request() { |
| 44 |
return array( |
| 45 |
'Customer' => array( |
| 46 |
'Address' => $this->get_customer_address(), |
| 47 |
/* Temporarily hardcoded in Settings::get_location_code(). */ |
| 48 |
'CollectionLocation' => $this->item_info->customer['location_code'], |
| 49 |
'CustomerCode' => $this->item_info->customer['customer_code'], |
| 50 |
'CustomerNumber' => $this->item_info->customer['customer_num'], |
| 51 |
'ContactPerson' => $this->item_info->customer['company'], |
| 52 |
'Email' => $this->item_info->customer['email'], |
| 53 |
'Name' => $this->item_info->customer['company'], |
| 54 |
), |
| 55 |
/** Hardcoded */ |
| 56 |
'Message' => array( |
| 57 |
'MessageID' => '36209c3d-14d2-478f-85de-abccd84fa790', |
| 58 |
'MessageTimeStamp' => gmdate( 'd-m-Y H:i:s' ), |
| 59 |
'Printertype' => $this->item_info->shipment['printer_type'], |
| 60 |
), |
| 61 |
'Shipments' => $this->get_shipments(), |
| 62 |
); |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Get customer address information for Rest API. |
| 67 |
* |
| 68 |
* @return Array |
| 69 |
*/ |
| 70 |
public function get_customer_address() { |
| 71 |
return array( |
| 72 |
'AddressType' => '02', |
| 73 |
'City' => $this->item_info->shipper['city'], |
| 74 |
'CompanyName' => $this->item_info->shipper['company'], |
| 75 |
'Countrycode' => $this->item_info->shipper['country'], |
| 76 |
'HouseNr' => $this->item_info->shipper['address_2'], |
| 77 |
'Street' => $this->item_info->shipper['address_1'], |
| 78 |
'Zipcode' => $this->item_info->shipper['postcode'], |
| 79 |
); |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Get shipments data. |
| 84 |
*/ |
| 85 |
public function get_shipments() { |
| 86 |
$shipments = array(); |
| 87 |
|
| 88 |
$shipment = array( |
| 89 |
'Addresses' => $this->get_shipment_addresses(), |
| 90 |
'Barcode' => $this->item_info->shipment['main_barcode'], |
| 91 |
'Contacts' => array( |
| 92 |
array( |
| 93 |
'ContactType' => '01', |
| 94 |
'Email' => $this->item_info->shipment['email'], |
| 95 |
'SMSNr' => $this->item_info->shipment['phone'], |
| 96 |
), |
| 97 |
), |
| 98 |
'Dimension' => array( |
| 99 |
'Weight' => $this->item_info->shipment['total_weight'], |
| 100 |
), |
| 101 |
'Customs' => $this->get_customs(), |
| 102 |
'ProductCodeDelivery' => $this->item_info->shipment['shipping_product']['code'], |
| 103 |
'Reference' => $this->item_info->shipment['order_number'], |
| 104 |
); |
| 105 |
|
| 106 |
$confirmation_email = $this->item_info->shipper['shipping_confirmation_email']; |
| 107 |
if ( ! empty( $confirmation_email ) ) { |
| 108 |
$shipment['Contacts'][] = array( |
| 109 |
'ContactType' => '02', |
| 110 |
'Email' => $confirmation_email, |
| 111 |
); |
| 112 |
} |
| 113 |
|
| 114 |
if ( $this->item_info->is_delivery_day() ) { |
| 115 |
$shipment['DeliveryDate'] = $this->item_info->delivery_day['date'] . ' ' . $this->item_info->delivery_day['from']; |
| 116 |
} elseif ( $this->item_info->is_pickup_points() ) { |
| 117 |
$shipment['DeliveryDate'] = $this->item_info->pickup_points['date'] . ' ' . $this->item_info->pickup_points['time']; |
| 118 |
} |
| 119 |
|
| 120 |
if ( ! empty( $this->item_info->shipment['product_options']['characteristic'] ) ) { |
| 121 |
$shipment['ProductOptions'] = array( |
| 122 |
array( |
| 123 |
'Characteristic' => $this->item_info->shipment['product_options']['characteristic'], |
| 124 |
'Option' => $this->item_info->shipment['product_options']['option'], |
| 125 |
), |
| 126 |
); |
| 127 |
} |
| 128 |
|
| 129 |
// Add the required product options. |
| 130 |
if ( ! empty( $this->item_info->shipment['shipping_product']['options'] ) ) { |
| 131 |
$shipment['ProductOptions'] = $shipment['ProductOptions'] ?? array(); |
| 132 |
|
| 133 |
foreach ( $this->item_info->shipment['shipping_product']['options'] as $option ) { |
| 134 |
$shipment['ProductOptions'][] = array( |
| 135 |
'Characteristic' => $option['characteristic'], |
| 136 |
'Option' => $option['option'], |
| 137 |
); |
| 138 |
|
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
// Add return options. |
| 143 |
if ( ! empty( $this->item_info->shipment['return_options'] ) ) { |
| 144 |
$shipment['ProductOptions'] = $shipment['ProductOptions'] ?? array(); |
| 145 |
|
| 146 |
foreach ( $this->item_info->shipment['return_options'] as $option ) { |
| 147 |
$shipment['ProductOptions'][] = array( |
| 148 |
'Characteristic' => $option['characteristic'], |
| 149 |
'Option' => $option['option'], |
| 150 |
); |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
if ( ! empty( $this->item_info->shipment['return_barcode'] ) ) { |
| 155 |
$shipment['ReturnBarcode'] = $this->item_info->shipment['return_barcode']; |
| 156 |
} elseif ( ! empty( $this->item_info->shipment['shipping_return_barcode'] ) ) { |
| 157 |
$shipment['ReturnBarcode'] = $this->item_info->shipment['main_barcode']; |
| 158 |
} |
| 159 |
|
| 160 |
for ( $i = 1; $i <= $this->item_info->backend_data['num_labels']; $i++ ) { |
| 161 |
if ( $this->item_info->backend_data['num_labels'] > 1 ) { |
| 162 |
$shipment['Barcode'] = $this->item_info->shipment['barcodes'][ ( $i - 1 ) ]; |
| 163 |
$shipment['Groups'] = array( |
| 164 |
array( |
| 165 |
'GroupType' => '03', |
| 166 |
'GroupCount' => $this->item_info->backend_data['num_labels'], |
| 167 |
'GroupSequence' => $i, |
| 168 |
'MainBarcode' => $this->item_info->shipment['main_barcode'], |
| 169 |
), |
| 170 |
); |
| 171 |
} |
| 172 |
|
| 173 |
// Amounts (insurance) must only be present on the first/parent shipment. |
| 174 |
// Unset it for child shipments to avoid the "child shipment cannot contain an insurance amount" error. |
| 175 |
if ( $this->item_info->backend_data['insured_shipping'] ) { |
| 176 |
if ( 1 === $i ) { |
| 177 |
$shipment['Amounts'] = array( |
| 178 |
array( |
| 179 |
'AmountType' => '02', |
| 180 |
'Currency' => $this->item_info->shipment['currency'], |
| 181 |
'Value' => $this->item_info->shipment['subtotal'], |
| 182 |
), |
| 183 |
); |
| 184 |
} else { |
| 185 |
unset( $shipment['Amounts'] ); |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
$shipments[] = $shipment; |
| 190 |
} |
| 191 |
|
| 192 |
return $shipments; |
| 193 |
} |
| 194 |
|
| 195 |
/** |
| 196 |
* Create a customs segment in API request. |
| 197 |
* |
| 198 |
* @return Array. |
| 199 |
*/ |
| 200 |
public function get_customs() { |
| 201 |
$customs = array( |
| 202 |
'Certificate' => false, |
| 203 |
'Currency' => $this->item_info->shipment['currency'], |
| 204 |
'Invoice' => true, |
| 205 |
'InvoiceNr' => $this->item_info->shipment['order_id'], |
| 206 |
'License' => false, |
| 207 |
// Hardcoded. |
| 208 |
'TransactionCode' => '11', |
| 209 |
'TransactionDescription' => 'Sale of goods', |
| 210 |
'ShipmentType' => 'Commercial Goods', |
| 211 |
'Content' => $this->get_custom_contents(), |
| 212 |
); |
| 213 |
// Add TrustedShipperID if merchant code exists for non-EU destination. |
| 214 |
if ( ! empty( $this->item_info->shipment['merchant_code'] ) ) { |
| 215 |
$customs['TrustedShipperID'] = $this->item_info->shipment['merchant_code']; |
| 216 |
} |
| 217 |
|
| 218 |
return $customs; |
| 219 |
} |
| 220 |
|
| 221 |
/** |
| 222 |
* Create a custom contents segment in API request. |
| 223 |
* |
| 224 |
* @return Array. |
| 225 |
*/ |
| 226 |
public function get_custom_contents() { |
| 227 |
if ( empty( $this->item_info->contents ) ) { |
| 228 |
return array(); |
| 229 |
} |
| 230 |
|
| 231 |
$contents = array(); |
| 232 |
foreach ( $this->item_info->contents as $item ) { |
| 233 |
$contents[] = array( |
| 234 |
'Description' => $item['description'], |
| 235 |
'Quantity' => $item['qty'], |
| 236 |
'Weight' => $item['weight'], |
| 237 |
'Value' => $item['value'], |
| 238 |
'HSTariffNr' => $item['hs_code'], |
| 239 |
'CountryOfOrigin' => $item['origin'], |
| 240 |
); |
| 241 |
} |
| 242 |
|
| 243 |
return $contents; |
| 244 |
} |
| 245 |
|
| 246 |
/** |
| 247 |
* Get shipment addresses data. |
| 248 |
*/ |
| 249 |
public function get_shipment_addresses() { |
| 250 |
$addresses = array(); |
| 251 |
|
| 252 |
$addresses[] = array( |
| 253 |
'AddressType' => '01', |
| 254 |
'CompanyName' => $this->item_info->receiver['company'], |
| 255 |
'City' => $this->item_info->receiver['city'], |
| 256 |
'Countrycode' => $this->item_info->receiver['country'], |
| 257 |
'FirstName' => $this->item_info->receiver['first_name'], |
| 258 |
'HouseNr' => $this->item_info->receiver['house_number'], |
| 259 |
'HouseNrExt' => $this->item_info->receiver['address_2'], |
| 260 |
'Name' => $this->item_info->receiver['last_name'], |
| 261 |
'Street' => $this->item_info->receiver['address_1'], |
| 262 |
'Zipcode' => $this->item_info->receiver['postcode'], |
| 263 |
); |
| 264 |
|
| 265 |
if ( $this->item_info->is_pickup_points() ) { |
| 266 |
$addresses[] = array( |
| 267 |
'AddressType' => '09', |
| 268 |
'CompanyName' => $this->item_info->pickup_points['company'], |
| 269 |
'City' => $this->item_info->pickup_points['city'], |
| 270 |
'Countrycode' => $this->item_info->pickup_points['country'], |
| 271 |
'HouseNr' => $this->item_info->pickup_points['address_2'], |
| 272 |
'HouseNrExt' => '', |
| 273 |
'Street' => $this->item_info->pickup_points['address_1'], |
| 274 |
'Zipcode' => $this->item_info->pickup_points['postcode'], |
| 275 |
); |
| 276 |
} |
| 277 |
|
| 278 |
if ( ! empty( $this->item_info->shipment['return_barcode'] ) || |
| 279 |
! empty( $this->item_info->shipment['shipping_return_barcode'] ) ) { |
| 280 |
$addresses[] = array( |
| 281 |
'AddressType' => '08', |
| 282 |
'City' => $this->item_info->customer['return_address_city'], |
| 283 |
'CompanyName' => $this->item_info->customer['return_company'], |
| 284 |
'Countrycode' => $this->item_info->shipper['country'], |
| 285 |
'HouseNr' => $this->item_info->customer['return_address_2'], |
| 286 |
'HouseNrExt' => '', |
| 287 |
'Street' => $this->item_info->customer['return_address_1'], |
| 288 |
'Zipcode' => $this->item_info->customer['return_address_zip'], |
| 289 |
); |
| 290 |
} |
| 291 |
|
| 292 |
return apply_filters( 'postnl_shipment_addresses', $addresses, $this ); |
| 293 |
} |
| 294 |
} |
| 295 |
|