get_setting('base_url', '')); return $own !== '' ? $own : 'https://serviciosconcentrador.correosexpress.com/servicios'; } public function capabilities() { return array('create', 'track'); } public function settings_schema() { return array( 'username' => array('type' => 'text', 'label' => __('Web services user', 'ai-eshop-optimizer'), 'required' => true), 'password' => array('type' => 'password', 'label' => __('Web services password', 'ai-eshop-optimizer'), 'required' => true), 'customer_code' => array( 'type' => 'text', 'label' => __('Customer code (código de cliente)', 'ai-eshop-optimizer'), 'required' => true, 'help' => __('From your Correos Express contract. Freight settles against this.', 'ai-eshop-optimizer'), ), 'product_code' => array( 'type' => 'text', 'label' => __('Product code', 'ai-eshop-optimizer'), 'help' => __('e.g. 51 for Paq 24, 93 for ePaq 24. Your contract lists the ones you hold.', 'ai-eshop-optimizer'), ), 'label_format' => array( 'type' => 'select', 'label' => __('Label format', 'ai-eshop-optimizer'), 'options' => array( 'PDF' => __('PDF', 'ai-eshop-optimizer'), 'ZPL' => __('ZPL (thermal)', 'ai-eshop-optimizer'), ), ), 'base_url' => array( 'type' => 'url', 'label' => __('API base URL', 'ai-eshop-optimizer'), 'help' => __('Leave empty for the standard endpoint. Correos Express issues per-customer hosts, so check your contract letter before assuming this is wrong.', 'ai-eshop-optimizer'), ), 'sender_name' => array('type' => 'text', 'label' => __('Sender name', 'ai-eshop-optimizer'), 'required' => true), '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_province' => array('type' => 'text', 'label' => __('Sender province', 'ai-eshop-optimizer')), 'sender_country' => array('type' => 'text', 'label' => __('Sender country code', 'ai-eshop-optimizer'), 'required' => true), '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.correosexpress.com/web/correosexpress/seguimiento-de-envios?numeroEnvio=' . rawurlencode((string) $voucher_no); } public function authenticate(array $creds) { $cfg = array_merge($this->credentials(), $creds); $missing = array(); foreach (array('username', 'password', 'customer_code') as $req) { if (empty($cfg[$req])) { $missing[] = $req; } } if (!empty($missing)) { return array('ok' => false, 'error' => sprintf( __('Correos Express credentials missing: %s. Note these are the CORREOS EXPRESS web-services details, not a Correos postal login — the two are separate contracts.', '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 auth_headers() { $c = $this->credentials(); return array( 'Authorization' => 'Basic ' . base64_encode(($c['username'] ?? '') . ':' . ($c['password'] ?? '')), ); } 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; } $pieces = max(1, (int) ($payload['pieces'] ?? 1)); $body = array( 'solicitante' => (string) ($cfg['customer_code'] ?? ''), 'canalEntrada' => 'WEB', 'tipoServicio' => (string) ($cfg['product_code'] ?: '51'), 'referenciaCliente' => $this->reference($payload, 20), 'nomOri' => mb_substr((string) ($cfg['sender_name'] ?? ''), 0, 40), 'dirOri' => mb_substr((string) ($cfg['sender_street'] ?? ''), 0, 300), 'cpOri' => (string) ($cfg['sender_zip'] ?? ''), 'pobOri' => mb_substr((string) ($cfg['sender_city'] ?? ''), 0, 40), 'provOri' => mb_substr((string) ($cfg['sender_province'] ?? ''), 0, 40), 'paisOri' => strtoupper(substr((string) ($cfg['sender_country'] ?? 'ES'), 0, 2)), 'telefonoOri' => $this->digits((string) ($cfg['sender_phone'] ?? '')), 'nomDes' => mb_substr((string) ($rec['name'] ?? ''), 0, 40), 'dirDes' => mb_substr(trim((string) ($rec['address'] ?? '')), 0, 300), 'cpDes' => (string) ($rec['zip'] ?? ''), 'pobDes' => mb_substr((string) ($rec['city'] ?? ''), 0, 40), 'provDes' => mb_substr((string) ($rec['state'] ?? ''), 0, 40), 'paisDes' => strtoupper(substr((string) ($rec['country'] ?? 'ES'), 0, 2)), 'telefonoDes' => $this->digits((string) ($rec['phone'] ?? '')), 'emailDes' => (string) ($rec['email'] ?? ''), 'bultos' => $pieces, 'kilos' => number_format($weight, 2, '.', ''), 'listaBultos' => array(), 'etiquetaPDF' => ('ZPL' === (string) ($cfg['label_format'] ?? '')) ? 'N' : 'S', 'tipoEtiqueta' => ('ZPL' === (string) ($cfg['label_format'] ?? '')) ? '2' : '1', ); $cod = (float) ($payload['cod_amount'] ?? 0); if ($cod > 0) { $body['reembolso'] = number_format($cod, 2, '.', ''); } $resp = $this->request('POST', '/rest/altaEnvio', $body, $this->auth_headers()); if (is_wp_error($resp)) { return array('ok' => false, 'error' => $resp->get_error_message()); } $ret = $resp['codigoRetorno'] ?? ($resp['datosResultado']['codigoRetorno'] ?? 0); if ((int) $ret !== 0) { $msg = trim((string) ($resp['mensajeRetorno'] ?? '')); return array('ok' => false, 'error' => $msg !== '' ? $msg : sprintf( __('Correos Express rejected the shipment (code %d).', 'ai-eshop-optimizer'), (int) $ret ), 'raw' => $resp); } $data = $resp['datosResultado'] ?? array(); $tracking = (string) (is_array($data) ? ($data['codigoBarras'] ?? '') : $data); if ($tracking === '') { $tracking = (string) ($resp['codigoBarras'] ?? ''); } if ($tracking === '') { return array('ok' => false, 'error' => __('Correos Express accepted the shipment but returned no barcode.', 'ai-eshop-optimizer'), 'raw' => $resp); } $b64 = ''; if (is_array($data)) { $b64 = (string) ($data['etiqueta'][0]['etiqueta1'] ?? ($data['etiqueta'] ?? '')); } return array( 'ok' => true, 'voucher_no' => $tracking, 'subvouchers' => array(), 'job_id' => null, 'raw' => array_merge(is_array($resp) ? $resp : array(), array('aieo_label_b64' => $b64)), ); } public function get_label_pdf($voucher_no, $format = 'A4') { return array( 'ok' => false, 'error' => __('Correos Express returns the label with the shipment. 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 Correos Express response.', 'ai-eshop-optimizer')); } return $this->decode_label($b64); } public function map_status($raw_code) { $raw = mb_strtoupper((string) $raw_code); if ($raw === '') { return 'unknown'; } if (mb_strpos($raw, 'ENTREGAD') !== false) { return 'delivered'; } if (mb_strpos($raw, 'REPARTO') !== false) { return 'out_for_delivery'; } if (mb_strpos($raw, 'TRANSITO') !== false) { return 'in_transit'; } if (mb_strpos($raw, 'RECOGID') !== false) { return 'in_transit'; } if (mb_strpos($raw, 'ADMITID') !== false) { return 'created'; } if (mb_strpos($raw, 'INCIDENCIA') !== false) { return 'attempted'; } if (mb_strpos($raw, 'DEVUELT') !== false) { return 'returned'; } if (mb_strpos($raw, 'ANULAD') !== false) { return 'canceled'; } return 'unknown'; } public function track($voucher_no) { $auth = $this->authenticate(array()); if (empty($auth['ok'])) { return $auth; } $resp = $this->request('POST', '/rest/consultaEstados', array( 'solicitante' => (string) ($this->credentials()['customer_code'] ?? ''), 'codigoBarras' => (string) $voucher_no, ), $this->auth_headers()); if (is_wp_error($resp)) { return array('ok' => false, 'error' => $resp->get_error_message()); } $ret = $resp['codigoRetorno'] ?? 0; if ((int) $ret !== 0) { return array('ok' => false, 'error' => trim((string) ($resp['mensajeRetorno'] ?? '')) ?: __('Correos Express does not recognise that barcode.', 'ai-eshop-optimizer')); } $checkpoints = array(); $latest = ''; $delivered_at = null; foreach ((array) ($resp['datosResultado'] ?? array()) as $e) { if (!is_array($e)) { continue; } $stamp = trim((string) ($e['fecha'] ?? '') . ' ' . (string) ($e['hora'] ?? '')); $ts = $stamp !== '' ? strtotime($stamp) : false; $desc = (string) ($e['evento'] ?? ($e['descripcion'] ?? '')); $checkpoints[] = array( 'code' => (string) ($e['codEvento'] ?? ''), 'description' => $desc, 'location' => (string) ($e['delegacion'] ?? ''), 'checkpoint_at' => $ts ?: null, ); if ($desc !== '') { $latest = $desc; } if ('delivered' === $this->map_status($desc) && $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['correos_express']) && class_exists('AIEO_Courier_Driver_Correos')) { $drivers['correos_express'] = new AIEO_Courier_Driver_Correos(); } return $drivers; });