Changeset 2796709
- Timestamp:
- 10/10/2022 03:12:48 PM (3 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
fastpicker/trunk/src/WooOrderpicker/API/ShippingRegistration.php
r2708206 r2796709 1 1 <?php namespace WooOrderpicker\API; 2 2 3 use KDZ\Data; 4 use KDZ\Order; 3 5 use MyParcelNL\Sdk\src\Helper\MyParcelCollection; 4 6 use WCPN_Export; 7 use WCMP_Export; 5 8 use WCPOST_Settings; 6 9 use WooOrderpicker\Utils\Helper; … … 8 11 class ShippingRegistration { 9 12 10 protected $ postNLMainObjectFields = [13 protected $myParcelMainObjectFields = [ 11 14 'package_type' => 'packageType' 12 15 ]; 13 16 14 protected $ postNLSubObjectFields = [17 protected $myParcelSubObjectFields = [ 15 18 'only_recipient' => 'only_recipient', 16 19 'signature_required' => 'signature', … … 41 44 42 45 if($body->plugin == 'postnl') { 43 if(!isset($body->fields) || !is_array($body->fields)) return $this->failed(); 44 45 $deliveryOptions = get_post_meta($body->id, '_postnl_delivery_options', true); 46 $shipmentOptions = get_post_meta($body->id, '_postnl_shipment_options_extra', true); 47 if(!$deliveryOptions || !$shipmentOptions) return $this->failed(false, 'No PostNL settings found on order.'); 48 49 $decodedDelivery = json_decode($deliveryOptions); 50 $decodedShipment = json_decode($shipmentOptions); 51 52 foreach($body->fields as $field) { 53 if($field->name == 'collo_amount') { 54 $decodedShipment->collo_amount = $field->value; 55 } else if(array_key_exists($field->name, $this->postNLMainObjectFields)) { 56 $decodedDelivery->{$this->postNLMainObjectFields[$field->name]} = $field->value; 57 } else if(array_key_exists($field->name, $this->postNLSubObjectFields)) { 58 $decodedDelivery->shipmentOptions->{$this->postNLSubObjectFields[$field->name]} = ($field->name === 'insured_amount') ? (int) $field->value : $field->value; 59 } 60 } 61 62 update_post_meta($body->id, '_postnl_delivery_options', json_encode($decodedDelivery)); 63 update_post_meta($body->id, '_postnl_shipment_options_extra', json_encode($decodedShipment)); 64 65 $orderIds = [$body->id]; 66 $export = new WCPN_Export(); 67 $result = $export->add_shipments($orderIds, false); 68 69 if(is_array($result) && array_key_exists('success_ids', $result) && count($result['success_ids']) !== 0) { 70 try { 71 return ['success' => true, 'success_ids' => $result['success_ids']]; 72 } catch(\Exception $e) { 73 return ['success' => false, 'msg' => $e->getMessage(), 'carrier_response' => null]; 74 } 46 if(!Helper::hasPostNL()) { 47 return $this->failed(false, 'PLUGIN_DISABLED'); 75 48 } else { 76 $error = (count($export->errors)) ? end($export->errors) : false; 77 return $this->failed($result, $error); 49 return $this->processPostNL($body); 50 } 51 52 } else if($body->plugin == 'myparcel') { 53 if(!Helper::hasMyParcel()) { 54 return $this->failed(false, 'PLUGIN_DISABLED'); 55 } else { 56 return $this->processMyParcel($body); 57 } 58 } else if($body->plugin == 'kdz') { 59 if(!Helper::hasKdz()) { 60 return $this->failed(false, 'PLUGIN_DISABLED'); 61 } else { 62 return $this->processKdz($body); 78 63 } 79 64 } … … 82 67 } 83 68 69 public function processPostNL($body) 70 { 71 if(!isset($body->fields) || !is_array($body->fields)) return $this->failed(); 72 73 $deliveryOptions = get_post_meta($body->id, '_postnl_delivery_options', true); 74 $shipmentOptions = get_post_meta($body->id, '_postnl_shipment_options_extra', true); 75 if(!$deliveryOptions || !$shipmentOptions) return $this->failed(false, 'No PostNL settings found on order.'); 76 77 $decodedDelivery = json_decode($deliveryOptions); 78 $decodedShipment = json_decode($shipmentOptions); 79 80 foreach($body->fields as $field) { 81 if($field->name == 'collo_amount') { 82 $decodedShipment->collo_amount = $field->value; 83 } else if(array_key_exists($field->name, $this->myParcelMainObjectFields)) { 84 $decodedDelivery->{$this->myParcelMainObjectFields[$field->name]} = $field->value; 85 } else if(array_key_exists($field->name, $this->myParcelSubObjectFields)) { 86 $decodedDelivery->shipmentOptions->{$this->myParcelSubObjectFields[$field->name]} = ($field->name === 'insured_amount') ? (int) $field->value : $field->value; 87 } 88 } 89 90 update_post_meta($body->id, '_postnl_delivery_options', json_encode($decodedDelivery)); 91 update_post_meta($body->id, '_postnl_shipment_options_extra', json_encode($decodedShipment)); 92 93 $orderIds = [$body->id]; 94 $export = new WCPN_Export(); 95 $result = $export->add_shipments($orderIds, false); 96 97 if(is_array($result) && array_key_exists('success_ids', $result) && count($result['success_ids']) !== 0) { 98 try { 99 return ['success' => true, 'success_ids' => $result['success_ids']]; 100 } catch(\Exception $e) { 101 return ['success' => false, 'msg' => $e->getMessage(), 'carrier_response' => null]; 102 } 103 } else { 104 $error = (count($export->errors)) ? end($export->errors) : false; 105 return $this->failed($result, $error); 106 } 107 } 108 109 110 public function processMyParcel($body) 111 { 112 if(!isset($body->fields) || !is_array($body->fields)) return $this->failed(); 113 114 $deliveryOptions = get_post_meta($body->id, '_myparcel_delivery_options', true); 115 $shipmentOptions = get_post_meta($body->id, '_myparcel_shipment_options_extra', true); 116 if(!$deliveryOptions || !$shipmentOptions) return $this->failed(false, 'No MyParcel settings found on order.'); 117 118 $decodedDelivery = json_decode($deliveryOptions); 119 $decodedShipment = json_decode($shipmentOptions); 120 121 foreach($body->fields as $field) { 122 if($field->name == 'colli_amount') { 123 $decodedShipment->colli_amount = $field->value; 124 } else if(array_key_exists($field->name, $this->myParcelMainObjectFields)) { 125 $decodedDelivery->{$this->myParcelMainObjectFields[$field->name]} = $field->value; 126 } else if(array_key_exists($field->name, $this->myParcelSubObjectFields)) { 127 $decodedDelivery->shipmentOptions->{$this->myParcelSubObjectFields[$field->name]} = ($field->name === 'insured_amount') ? (int) $field->value : $field->value; 128 } 129 } 130 131 update_post_meta($body->id, '_myparcel_delivery_options', json_encode($decodedDelivery)); 132 update_post_meta($body->id, '_myparcel_shipment_options_extra', json_encode($decodedShipment)); 133 134 $orderIds = [$body->id]; 135 $export = new WCMP_Export(); 136 $result = $export->addShipments($orderIds, false); 137 138 if(is_array($result) && array_key_exists('success_ids', $result) && count($result['success_ids']) !== 0) { 139 try { 140 return ['success' => true, 'success_ids' => $result['success_ids']]; 141 } catch(\Exception $e) { 142 return ['success' => false, 'msg' => $e->getMessage(), 'carrier_response' => null]; 143 } 144 } else { 145 $error = (count($export->errors)) ? end($export->errors) : false; 146 return $this->failed($result, $error); 147 } 148 } 149 84 150 protected function failed($result = false, $error = false) 85 151 { 86 return ['success' => false, 'carrier_response' => $error, 'test' => is_array($result)]; 152 return ['success' => false, 'carrier_response' => $error]; 153 } 154 155 private function processKdz($body) 156 { 157 $apiKey = get_option('wc_settings_tab_kdz_api_key'); 158 $products = [ 159 'Road Express' => 266, 160 'Priority Express < 17:00 - Goods' => 268, 161 'Priority Express < 12:00 - Goods' => 272, 162 'Priority Express < 9:00 - Goods' => 271 163 ]; 164 165 $data = new Data(); 166 $request = $data->get($body->id, $products[$this->getField('delivery_method', $body->fields)], $this->formatColli($this->getField('colli', $body->fields)), $this->getField('remark', $body->fields)); 167 168 $guzzle = new \GuzzleHttp\Client(); 169 $response = $guzzle->request( 170 "POST", 171 $this->url() . '/api/Woocommerce/post?t=' . $apiKey, 172 [ 173 'headers' => ['Content-Type' => 'application/json'], 174 'body' => json_encode($request) 175 ] 176 )->getBody()->getContents(); 177 178 $data = json_decode($response); 179 if($data->Success) { 180 $order = new Order($body->id); 181 $order->setIsSend(true); 182 183 return ['success' => true]; 184 } 185 186 return ['success' => false]; 187 } 188 189 private function getField($string, $fields) 190 { 191 foreach($fields as $field) { 192 if($field->name === $string) { 193 return (isset($field->subFields) && $field->subFields) ? $field->subFields : $field->value; 194 } 195 } 196 } 197 198 private function formatColli($subFields) 199 { 200 $result = []; 201 202 foreach($subFields as $i => $fields) { 203 $result[] = [ 204 'name' => 'Colli ' . ($i + 1), 205 'weight' => $this->getField('weight', $fields), 206 'dimensions' => [ 207 'height' => $this->getField('height', $fields), 208 'width' => $this->getField('width', $fields), 209 'length' => $this->getField('length', $fields) 210 ] 211 ]; 212 } 213 214 return $result; 215 } 216 217 private function url() 218 { 219 $kdzAPIUrl = get_option(WOO_ORDERPICKER_PLUGIN_KDZ_API_URL_OPTION); 220 221 if($kdzAPIUrl) { 222 return preg_replace('/\/$/', '', $kdzAPIUrl); 223 } 224 225 return 'https://transcon.nl'; 87 226 } 88 227 }
Note: See TracChangeset
for help on using the changeset viewer.