get_setting('base_url', '')); return $own !== '' ? $own : 'https://ws.colissimo.fr/sls-ws/SlsServiceWSRest/2.0'; } public function capabilities() { return array('create', 'track'); } public function settings_schema() { return array( 'contract_number' => array( 'type' => 'text', 'label' => __('Contract number', 'ai-eshop-optimizer'), 'required' => true, 'help' => __('Your Colissimo contract number — six digits on your contract letter.', 'ai-eshop-optimizer'), ), 'password' => array('type' => 'password', 'label' => __('Web services password', 'ai-eshop-optimizer'), 'required' => true), 'product_code' => array( 'type' => 'text', 'label' => __('Product code', 'ai-eshop-optimizer'), 'help' => __('DOM for France without signature, DOS with signature, COLI for Europe, COM for the rest of the world. Your contract lists what you hold.', 'ai-eshop-optimizer'), ), 'output_format' => array( 'type' => 'select', 'label' => __('Label format', 'ai-eshop-optimizer'), 'options' => array( 'PDF_A4_300dpi' => __('A4 PDF', 'ai-eshop-optimizer'), 'PDF_10x15_300dpi'=> __('10×15 PDF', 'ai-eshop-optimizer'), 'ZPL_10x15_300dpi'=> __('10×15 ZPL (thermal)', 'ai-eshop-optimizer'), ), ), 'tracking_api_key' => array( 'type' => 'password', 'label' => __('La Poste tracking key (X-Okapi-Key)', 'ai-eshop-optimizer'), 'help' => __('Optional, and a SEPARATE product from the shipping contract — request it at developer.laposte.fr. Without it labels still print; only automatic status polling is unavailable.', 'ai-eshop-optimizer'), ), 'base_url' => array( 'type' => 'url', 'label' => __('API base URL', 'ai-eshop-optimizer'), 'help' => __('Leave empty for the standard endpoint. Some contracts are issued against a different host.', 'ai-eshop-optimizer'), ), 'sender_name' => array('type' => 'text', 'label' => __('Sender name', 'ai-eshop-optimizer'), 'required' => true), 'sender_company' => array('type' => 'text', 'label' => __('Sender company', 'ai-eshop-optimizer')), 'sender_street' => array('type' => 'text', 'label' => __('Sender street', 'ai-eshop-optimizer'), 'required' => true), 'sender_zip' => array('type' => 'text', 'label' => __('Sender postcode', 'ai-eshop-optimizer'), 'required' => true), 'sender_city' => array('type' => 'text', 'label' => __('Sender city', 'ai-eshop-optimizer'), 'required' => true), 'sender_country' => array('type' => 'text', 'label' => __('Sender country code', 'ai-eshop-optimizer'), 'required' => true), 'sender_email' => array('type' => 'text', 'label' => __('Sender email', 'ai-eshop-optimizer')), 'sender_phone' => array('type' => 'text', 'label' => __('Sender phone', 'ai-eshop-optimizer')), ); } public function tracking_url($voucher_no) { $own = parent::tracking_url($voucher_no); if ($own !== '') { return $own; } return 'https://www.laposte.fr/outils/suivre-vos-envois?code=' . rawurlencode((string) $voucher_no); } public function authenticate(array $creds) { $cfg = array_merge($this->credentials(), $creds); $missing = array(); foreach (array('contract_number', 'password') as $req) { if (empty($cfg[$req])) { $missing[] = $req; } } if (!empty($missing)) { return array('ok' => false, 'error' => sprintf( __('Colissimo credentials missing: %s', 'ai-eshop-optimizer'), implode(', ', $missing) )); } return array('ok' => true, 'auth_token' => null, 'expires_at' => null); } private function credentials() { return class_exists('AIEO_Fulfillment_Settings') ? (array) AIEO_Fulfillment_Settings::get('courier_' . $this->code(), array()) : array(); } private function address(array $a) { list($first, $last) = $this->split_name((string) ($a['name'] ?? '')); $out = array( 'companyName' => mb_substr((string) ($a['company'] ?? ''), 0, 38), 'lastName' => mb_substr($last !== '' ? $last : $first, 0, 38), 'firstName' => mb_substr($first, 0, 29), 'line2' => mb_substr(trim((string) ($a['address'] ?? '')), 0, 38), 'countryCode' => strtoupper(substr((string) ($a['country'] ?? 'FR'), 0, 2)), 'city' => mb_substr((string) ($a['city'] ?? ''), 0, 35), 'zipCode' => (string) ($a['zip'] ?? ''), ); $line3 = trim((string) ($a['address_2'] ?? '')); if ($line3 !== '') { $out['line3'] = mb_substr($line3, 0, 38); } $email = trim((string) ($a['email'] ?? '')); if ($email !== '') { $out['email'] = $email; } $phone = $this->digits((string) ($a['phone'] ?? '')); if ($phone !== '') { $out['mobileNumber'] = $phone; } return $out; } public function create_voucher(array $payload) { $auth = $this->authenticate(array()); if (empty($auth['ok'])) { return $auth; } $cfg = $this->credentials(); $rec = isset($payload['recipient']) && is_array($payload['recipient']) ? $payload['recipient'] : array(); $weight = (float) ($payload['weight'] ?? 0.5); if ($weight <= 0) { $weight = 0.5; } $body = array( 'contractNumber' => (string) $cfg['contract_number'], 'password' => (string) $cfg['password'], 'outputFormat' => array( 'x' => 0, 'y' => 0, 'outputPrintingType' => (string) ($cfg['output_format'] ?: 'PDF_A4_300dpi'), ), 'letter' => array( 'service' => array( 'productCode' => (string) ($cfg['product_code'] ?: 'DOM'), 'depositDate' => wp_date('Y-m-d'), 'orderNumber' => $this->reference($payload, 30), ), 'parcel' => array( 'weight' => $weight, ), 'sender' => array( 'address' => $this->address(array( 'name' => $cfg['sender_name'] ?? '', 'company' => $cfg['sender_company'] ?? '', 'address' => $cfg['sender_street'] ?? '', 'zip' => $cfg['sender_zip'] ?? '', 'city' => $cfg['sender_city'] ?? '', 'country' => $cfg['sender_country'] ?? 'FR', 'email' => $cfg['sender_email'] ?? '', 'phone' => $cfg['sender_phone'] ?? '', )), ), 'addressee' => array( 'address' => $this->address($rec), ), ), ); $cod = (float) ($payload['cod_amount'] ?? 0); if ($cod > 0) { $body['letter']['parcel']['COD'] = true; $body['letter']['parcel']['CODAmount'] = $cod; } $raw = $this->request_raw('POST', '/generateLabel', $body); if (is_wp_error($raw)) { return array('ok' => false, 'error' => $raw->get_error_message()); } $json = null; $label = ''; $parts = $this->split_multipart($raw['body'], $raw['content_type']); if (empty($parts)) { $json = json_decode($raw['body'], true); } else { foreach ($parts as $part) { $decoded = json_decode($part['payload'], true); if (is_array($decoded)) { $json = $decoded; continue; } if ($part['payload'] !== '') { $label = $part['payload']; } } } $resp = is_array($json) ? $json : array(); $number = (string) ($resp['labelV2Response']['parcelNumber'] ?? ''); if ($number === '') { $msg = $this->extract_error($resp, $raw['body']); return array('ok' => false, 'error' => $msg ?: __('Colissimo did not return a parcel number.', 'ai-eshop-optimizer'), 'raw' => $resp); } return array( 'ok' => true, 'voucher_no' => $number, 'subvouchers' => array(), 'job_id' => null, 'raw' => array_merge($resp, array('aieo_label_b64' => $label !== '' ? base64_encode($label) : '')), ); } public function get_label_pdf($voucher_no, $format = 'A4') { return array( 'ok' => false, 'error' => __('Colissimo returns the label only when it is generated. Reprint it from the copy saved with the voucher.', 'ai-eshop-optimizer'), ); } public function label_from_create(array $create_response) { $b64 = (string) ($create_response['raw']['aieo_label_b64'] ?? ''); if ($b64 === '') { return array('ok' => false, 'pdf' => null, 'error' => __('No label in the Colissimo response.', 'ai-eshop-optimizer')); } return $this->decode_label($b64); } public function map_status($raw_code) { $map = array( 'DR1' => 'created', 'PC1' => 'in_transit', 'PC2' => 'in_transit', 'ET1' => 'in_transit', 'ET2' => 'in_transit', 'ET3' => 'in_transit', 'ET4' => 'in_transit', 'EP1' => 'out_for_delivery', 'DO1' => 'out_for_delivery', 'DO2' => 'out_for_delivery', 'DI1' => 'delivered', 'DI2' => 'returned', 'ND1' => 'attempted', 'RE1' => 'returning', 'AG1' => 'on_hold', ); $code = strtoupper((string) $raw_code); return isset($map[$code]) ? $map[$code] : 'unknown'; } public function track($voucher_no) { $key = trim((string) ($this->credentials()['tracking_api_key'] ?? '')); if ($key === '') { return array( 'ok' => false, 'error' => __('Colissimo tracking needs the separate La Poste tracking key. Labels and shipping work without it.', 'ai-eshop-optimizer'), ); } $resp = $this->request('GET', self::TRACK_URL . rawurlencode((string) $voucher_no), null, array( 'X-Okapi-Key' => $key, 'Accept' => 'application/json', )); if (is_wp_error($resp)) { return array('ok' => false, 'error' => $resp->get_error_message()); } $shipment = $resp['shipment'] ?? array(); $checkpoints = array(); $latest = ''; $delivered_at = null; foreach ((array) ($shipment['event'] ?? array()) as $e) { $ts = isset($e['date']) ? strtotime((string) $e['date']) : false; $code = (string) ($e['code'] ?? ''); $checkpoints[] = array( 'code' => $code, 'description' => (string) ($e['label'] ?? ''), 'location' => '', 'checkpoint_at' => $ts ?: null, ); if ($code !== '' && $latest === '') { $latest = $code; } if ('delivered' === $this->map_status($code) && $ts) { $delivered_at = $ts; } } return array( 'ok' => true, 'status_code' => $latest, 'checkpoints' => $checkpoints, 'consignee' => null, 'delivered_at' => $delivered_at, ); } } add_filter('aieo_fulfillment_couriers', static function (array $drivers) { if (!isset($drivers['colissimo']) && class_exists('AIEO_Courier_Driver_Colissimo')) { $drivers['colissimo'] = new AIEO_Courier_Driver_Colissimo(); } return $drivers; });