array( 'type' => 'password', 'label' => __('API key (username)', 'ai-eshop-optimizer'), 'required' => true, 'help' => __('From the DHL Developer Portal, once MyDHL API access is granted for your account.', 'ai-eshop-optimizer'), ), 'api_secret' => array('type' => 'password', 'label' => __('API secret (password)', 'ai-eshop-optimizer'), 'required' => true), 'account_number' => array('type' => 'text', 'label' => __('DHL account number', 'ai-eshop-optimizer'), 'required' => true), 'product_code' => array( 'type' => 'text', 'label' => __('Product code', 'ai-eshop-optimizer'), 'help' => __('Two letters, e.g. N for domestic Express, P for Worldwide Express. Your DHL contract lists the ones you may use.', 'ai-eshop-optimizer'), ), 'label_format' => array( 'type' => 'select', 'label' => __('Label size', 'ai-eshop-optimizer'), 'options' => array( 'ECOM26_84_A4_001' => __('A4 sheet', 'ai-eshop-optimizer'), 'ECOM26_84_001' => __('Thermal 10×15', 'ai-eshop-optimizer'), ), ), 'incoterm' => array( 'type' => 'text', 'label' => __('Incoterm', 'ai-eshop-optimizer'), 'help' => __('For shipments that leave the customs union — DAP is the usual choice for consumer orders.', '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'), 'required' => true), 'sender_phone' => array('type' => 'text', 'label' => __('Sender phone', 'ai-eshop-optimizer'), 'required' => true), 'sender_email' => array('type' => 'text', 'label' => __('Sender email', 'ai-eshop-optimizer')), 'sender_street' => array('type' => 'text', 'label' => __('Sender street', 'ai-eshop-optimizer'), 'required' => true), 'sender_city' => array('type' => 'text', 'label' => __('Sender city', 'ai-eshop-optimizer'), 'required' => true), 'sender_zip' => array('type' => 'text', 'label' => __('Sender postcode', 'ai-eshop-optimizer'), 'required' => true), 'sender_country' => array('type' => 'text', 'label' => __('Sender country code', 'ai-eshop-optimizer'), 'required' => true), ), $this->common_schema()); } public function tracking_url($voucher_no) { $own = parent::tracking_url($voucher_no); if ($own !== '') { return $own; } return 'https://www.dhl.com/global-en/home/tracking/tracking-express.html?submit=1&tracking-id=' . rawurlencode((string) $voucher_no); } public function authenticate(array $creds) { $cfg = array_merge($this->credentials(), $creds); $missing = array(); foreach (array('api_key', 'api_secret', 'account_number') as $req) { if (empty($cfg[$req])) { $missing[] = $req; } } if (!empty($missing)) { return array('ok' => false, 'error' => sprintf( __('DHL 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 auth_headers() { $c = $this->credentials(); return array( 'Authorization' => 'Basic ' . base64_encode(($c['api_key'] ?? '') . ':' . ($c['api_secret'] ?? '')), 'Message-Reference' => wp_generate_uuid4(), ); } private function party(array $a) { $out = array( 'postalAddress' => array( 'cityName' => (string) ($a['city'] ?? ''), 'countryCode' => strtoupper((string) ($a['country'] ?? 'GR')), 'postalCode' => (string) ($a['zip'] ?? ''), 'addressLine1' => mb_substr(trim((string) ($a['address'] ?? '')), 0, 45), ), 'contactInformation' => array( 'phone' => $this->digits((string) ($a['phone'] ?? '')), 'companyName' => mb_substr((string) ($a['company'] ?? ($a['name'] ?? '')), 0, 60), 'fullName' => mb_substr((string) ($a['name'] ?? ''), 0, 45), ), ); $line2 = trim((string) ($a['address_2'] ?? '')); if ($line2 !== '') { $out['postalAddress']['addressLine2'] = mb_substr($line2, 0, 45); } $email = trim((string) ($a['email'] ?? '')); if ($email !== '') { $out['contactInformation']['email'] = $email; } return $out; } private function sender_party() { $c = $this->credentials(); return $this->party(array( 'name' => $c['sender_name'] ?? '', 'company' => $c['sender_company'] ?? ($c['sender_name'] ?? ''), 'phone' => $c['sender_phone'] ?? '', 'email' => $c['sender_email'] ?? '', 'address' => $c['sender_street'] ?? '', 'city' => $c['sender_city'] ?? '', 'zip' => $c['sender_zip'] ?? '', 'country' => $c['sender_country'] ?? '', )); } private function accounts(array $payload) { $ours = (string) ($this->credentials()['account_number'] ?? ''); $party = $this->charge_party($payload); if (self::CHARGE_SENDER === $party) { return array(array('typeCode' => 'shipper', 'number' => $ours)); } $theirs = trim((string) ($payload['billing_account'] ?? '')); if ($theirs === '') { return array(array('typeCode' => 'shipper', 'number' => $ours)); } return array(array('typeCode' => 'receiver', 'number' => $theirs)); } 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)); $per = round($weight / $pieces, 3); if ($per <= 0) { $per = $weight; } $desc = trim((string) ($payload['content_description'] ?? '')); if ($desc === '') { $desc = 'Goods'; } $packages = array(); for ($i = 0; $i < $pieces; $i++) { $packages[] = array( 'weight' => $per, 'dimensions' => array('length' => 20, 'width' => 15, 'height' => 10), ); } $sender_country = strtoupper((string) ($cfg['sender_country'] ?? '')); $dest_country = strtoupper((string) ($rec['country'] ?? 'GR')); $customs = ($sender_country !== '' && $dest_country !== '' && $sender_country !== $dest_country); $body = array( 'plannedShippingDateAndTime' => wp_date('Y-m-d\TH:i:s') . ' GMT' . wp_date('P'), 'pickup' => array('isRequested' => false), 'productCode' => (string) ($cfg['product_code'] ?: 'P'), 'accounts' => $this->accounts($payload), 'customerDetails' => array( 'shipperDetails' => $this->sender_party(), 'receiverDetails' => $this->party($rec), ), 'content' => array( 'packages' => $packages, 'isCustomsDeclarable' => $customs, 'description' => mb_substr($desc, 0, 70), 'incoterm' => (string) ($cfg['incoterm'] ?: 'DAP'), 'unitOfMeasurement' => 'metric', ), 'outputImageProperties' => array( 'imageOptions' => array(array( 'typeCode' => 'label', 'templateName' => (string) ($cfg['label_format'] ?: 'ECOM26_84_A4_001'), 'isRequested' => true, )), ), ); $ref = $this->reference($payload, 35); if ($ref !== '') { $body['customerReferences'] = array(array('value' => $ref, 'typeCode' => 'CU')); } $resp = $this->request('POST', '/shipments', $body, $this->auth_headers()); if (is_wp_error($resp)) { return array('ok' => false, 'error' => $resp->get_error_message()); } $tracking = (string) ($resp['shipmentTrackingNumber'] ?? ''); if ($tracking === '') { return array('ok' => false, 'error' => __('DHL accepted the request but returned no tracking number.', 'ai-eshop-optimizer'), 'raw' => $resp); } $subs = array(); foreach ((array) ($resp['packages'] ?? array()) as $pkg) { $n = (string) ($pkg['trackingNumber'] ?? ''); if ($n !== '' && $n !== $tracking) { $subs[] = $n; } } return array( 'ok' => true, 'voucher_no' => $tracking, 'subvouchers' => $subs, 'job_id' => null, 'raw' => $resp, ); } public function get_label_pdf($voucher_no, $format = 'A4') { return array( 'ok' => false, 'error' => __('DHL only returns a label at the moment the shipment is created. Reprint it from the copy saved with the voucher, or cancel the shipment and create it again.', 'ai-eshop-optimizer'), ); } public function label_from_create(array $create_response) { foreach ((array) ($create_response['raw']['documents'] ?? array()) as $doc) { if ('label' === (string) ($doc['typeCode'] ?? '') && !empty($doc['content'])) { return $this->decode_label((string) $doc['content']); } } return array('ok' => false, 'pdf' => null, 'error' => __('No label in the DHL response.', 'ai-eshop-optimizer')); } protected function code_map() { return array( 'PU' => 'in_transit', 'PL' => 'in_transit', 'DF' => 'in_transit', 'AF' => 'in_transit', 'PR' => 'in_transit', 'CC' => 'in_transit', 'WC' => 'out_for_delivery', 'OK' => 'delivered', 'WX' => 'attempted', 'HP' => 'on_hold', 'RT' => 'returned', 'CA' => 'canceled', ); } public function track($voucher_no) { $auth = $this->authenticate(array()); if (empty($auth['ok'])) { return $auth; } $resp = $this->request( 'GET', '/shipments/' . rawurlencode((string) $voucher_no) . '/tracking', null, $this->auth_headers() ); if (is_wp_error($resp)) { return array('ok' => false, 'error' => $resp->get_error_message()); } $shipment = $resp['shipments'][0] ?? array(); $checkpoints = array(); $latest = ''; $delivered_at = null; $returned = false; foreach ((array) ($shipment['events'] ?? array()) as $e) { $stamp = trim((string) ($e['date'] ?? '') . ' ' . (string) ($e['time'] ?? '')); $ts = $stamp !== '' ? strtotime($stamp) : false; $code = (string) ($e['typeCode'] ?? ''); $checkpoints[] = array( 'code' => $code, 'description' => (string) ($e['description'] ?? ''), 'location' => (string) ($e['serviceArea'][0]['description'] ?? ''), 'checkpoint_at' => $ts ?: null, ); if ($code !== '') { $latest = $code; } if ('RT' === $code) { $returned = true; } if ('OK' === $code && $ts) { $delivered_at = $ts; } } if ($returned) { $latest = 'RT'; $delivered_at = null; } $status = $shipment['status'] ?? ''; return array( 'ok' => true, 'status_code' => $latest !== '' ? $latest : (is_scalar($status) ? (string) $status : ''), 'checkpoints' => $checkpoints, 'consignee' => (string) ($shipment['details']['proofOfDelivery']['signatory'] ?? '') ?: null, 'delivered_at' => $delivered_at, ); } public function cancel($voucher_no) { $auth = $this->authenticate(array()); if (empty($auth['ok'])) { return $auth; } $resp = $this->request( 'DELETE', '/shipments/' . rawurlencode((string) $voucher_no), null, $this->auth_headers() ); if (is_wp_error($resp)) { return array('ok' => false, 'error' => $resp->get_error_message()); } return array('ok' => true); } } add_filter('aieo_fulfillment_couriers', static function (array $drivers) { if (!isset($drivers['dhl']) && class_exists('AIEO_Courier_Driver_DHL')) { $drivers['dhl'] = new AIEO_Courier_Driver_DHL(); } return $drivers; });