load->language('extension/hutko/payment/hutko'); $this->document->setTitle($this->language->get('heading_title')); $data['breadcrumbs'] = []; $data['breadcrumbs'][] = [ 'text' => $this->language->get('text_home'), 'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token']) ]; $data['breadcrumbs'][] = [ 'text' => $this->language->get('text_extension'), 'href' => $this->url->link('marketplace/extension', 'user_token=' . $this->session->data['user_token'] . '&type=payment') ]; $data['breadcrumbs'][] = [ 'text' => $this->language->get('heading_title'), 'href' => $this->url->link('extension/hutko/payment/hutko', 'user_token=' . $this->session->data['user_token']) ]; $data['save'] = $this->url->link('extension/hutko/payment/hutko.save', 'user_token=' . $this->session->data['user_token']); $data['back'] = $this->url->link('marketplace/extension', 'user_token=' . $this->session->data['user_token'] . '&type=payment'); // Config fields $fields = [ 'payment_hutko_merchant_id', 'payment_hutko_secret_key', 'payment_hutko_shipping_include', 'payment_hutko_shipping_product_name', 'payment_hutko_shipping_product_code', 'payment_hutko_new_order_status_id', 'payment_hutko_success_status_id', 'payment_hutko_declined_status_id', 'payment_hutko_expired_status_id', 'payment_hutko_refunded_status_id', 'payment_hutko_include_discount_to_total', 'payment_hutko_status', 'payment_hutko_sort_order', 'payment_hutko_geo_zone_id', 'payment_hutko_total', 'payment_hutko_save_logs' ]; foreach ($fields as $field) { $data[$field] = $this->config->get($field); } // Defaults if (is_null($data['payment_hutko_shipping_product_name'])) $data['payment_hutko_shipping_product_name'] = 'Shipping'; if (is_null($data['payment_hutko_shipping_product_code'])) $data['payment_hutko_shipping_product_code'] = 'SHIPPING_001'; if (is_null($data['payment_hutko_total'])) $data['payment_hutko_total'] = '0.01'; if (is_null($data['payment_hutko_shipping_include'])) $data['payment_hutko_shipping_include'] = 1; if (is_null($data['payment_hutko_include_discount_to_total'])) $data['payment_hutko_include_discount_to_total'] = 1; $this->load->model('localisation/order_status'); $data['order_statuses'] = $this->model_localisation_order_status->getOrderStatuses(); $this->load->model('localisation/geo_zone'); $data['geo_zones'] = $this->model_localisation_geo_zone->getGeoZones(); $data['log_content'] = $this->displayLastDayLog(); $data['header'] = $this->load->controller('common/header'); $data['column_left'] = $this->load->controller('common/column_left'); $data['footer'] = $this->load->controller('common/footer'); $this->response->setOutput($this->load->view('extension/hutko/payment/hutko', $data)); } public function save(): void { $this->load->language('extension/hutko/payment/hutko'); $json = []; if (!$this->user->hasPermission('modify', 'extension/hutko/payment/hutko')) { $json['error']['warning'] = $this->language->get('error_permission'); } if (empty($this->request->post['payment_hutko_merchant_id']) || !is_numeric($this->request->post['payment_hutko_merchant_id'])) { $json['error']['payment_hutko_merchant_id'] = $this->language->get('error_merchant_id_numeric'); } $key = $this->request->post['payment_hutko_secret_key'] ?? ''; if (empty($key) || ($key != 'test' && (strlen($key) < 10 || is_numeric($key)))) { $json['error']['payment_hutko_secret_key'] = $this->language->get('error_secret_key_invalid'); } if (!$json) { $this->load->model('setting/setting'); $this->model_setting_setting->editSetting('payment_hutko', $this->request->post); $json['success'] = $this->language->get('text_success'); } $this->response->addHeader('Content-Type: application/json'); $this->response->setOutput(json_encode($json)); } public function install(): void { $this->load->model('extension/hutko/payment/hutko'); $this->model_extension_hutko_payment_hutko->install(); // Register Event $this->load->model('setting/event'); // Remove if exists to prevent duplicates $this->model_setting_event->deleteEventByCode('hutko_order_info'); $event_data = [ 'code' => 'hutko_order_info', 'description' => 'Hutko Payment Info Panel', 'trigger' => 'admin/view/sale/order_info/after', 'action' => 'extension/hutko/payment/hutko.order_info', 'status' => 1, 'sort_order' => 1 ]; if (version_compare(VERSION, '4.0.0.0', '>=')) { // OC 4.0.2.0+ uses array, older 4.0.x uses params. // We try array first (modern way). try { $this->model_setting_event->addEvent($event_data); } catch (\Exception $e) { // Fallback for older 4.0.0.0 versions $this->model_setting_event->addEvent('hutko_order_info', 'admin/view/sale/order_info/after', 'extension/hutko/payment/hutko.order_info', 1, 1); } } } public function uninstall(): void { $this->load->model('setting/event'); $this->model_setting_event->deleteEventByCode('hutko_order_info'); } public function order_info(string &$route, array &$args, string &$output): void { $order_id = isset($args['order_id']) ? (int)$args['order_id'] : 0; if (!$order_id) return; $this->load->model('sale/order'); $order_info = $this->model_sale_order->getOrder($order_id); if ($order_info && isset($order_info['payment_code']) && ($order_info['payment_code'] == 'hutko' || $order_info['payment_code'] == 'hutko.hutko')) { $this->load->language('extension/hutko/payment/hutko'); $this->load->model('extension/hutko/payment/hutko'); // NEW: Get all transactions $transactions = $this->model_extension_hutko_payment_hutko->getTransactions($order_id); // Format transactions for view $data['transactions'] = []; foreach ($transactions as $t) { $payload_arr = json_decode($t['payload'], true); $pretty_payload = $payload_arr ? json_encode($payload_arr, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) : $t['payload']; $data['transactions'][] = [ 'date' => date($this->language->get('datetime_format'), strtotime($t['date_added'])), 'ref' => $t['hutko_ref'], 'type' => $t['type'], 'status' => $t['status'], 'amount' => $t['amount'] . ' ' . $t['currency'], 'payload' => $pretty_payload, // Only allow refund if it's a successful callback (approved payment) 'can_refund'=> ($t['type'] == 'callback' && $t['status'] == 'success') ]; } $data['order_id'] = $order_id; $data['user_token'] = $this->session->data['user_token']; // URLs $data['refund_url'] = $this->url->link('extension/hutko/payment/hutko.refund', 'user_token=' . $this->session->data['user_token'] . '&order_id=' . $order_id); $data['status_url'] = $this->url->link('extension/hutko/payment/hutko.status', 'user_token=' . $this->session->data['user_token']); // Translations $data['text_payment_information'] = $this->language->get('text_payment_information'); $data['text_hutko_refund_title'] = $this->language->get('text_hutko_refund_title'); $data['text_hutko_status_title'] = $this->language->get('text_hutko_status_title'); $data['entry_refund_amount'] = $this->language->get('entry_refund_amount'); $data['entry_refund_comment'] = $this->language->get('entry_refund_comment'); $data['button_hutko_refund'] = $this->language->get('button_hutko_refund'); $data['button_hutko_status_check'] = $this->language->get('button_hutko_status_check'); $data['text_confirm_refund'] = $this->language->get('text_confirm_refund'); $data['text_loading'] = $this->language->get('text_loading'); $data['text_no_transactions'] = 'No Hutko transactions recorded.'; $panel_html = $this->load->view('extension/hutko/payment/hutko_order_info_panel', $data); if (strpos($output, 'id="history"') !== false) { $output = str_replace('
load->language('extension/hutko/payment/hutko'); $this->load->model('extension/hutko/payment/hutko'); $this->load->model('sale/order'); $json = []; $order_id = (int)($this->request->post['order_id'] ?? 0); $amount = (float)($this->request->post['refund_amount'] ?? 0); $comment = (string)($this->request->post['refund_comment'] ?? ''); // NEW: Pass specific transaction ref if possible, or find the last successful one $hutko_ref = (string)($this->request->post['hutko_ref'] ?? ''); // If ref not passed via JS (which we will add), check DB if (empty($hutko_ref)) { // Find the successful payment $transactions = $this->model_extension_hutko_payment_hutko->getTransactions($order_id); foreach($transactions as $t) { if ($t['type'] == 'callback' && $t['status'] == 'success') { $hutko_ref = $t['hutko_ref']; break; } } } $order_info = $this->model_sale_order->getOrder($order_id); if ($hutko_ref && $order_info && $amount > 0) { $data = [ 'order_id' => $hutko_ref, 'merchant_id' => $this->config->get('payment_hutko_merchant_id'), 'version' => '1.0', 'amount' => round($amount * 100), 'currency' => $order_info['currency_code'], 'comment' => $comment ]; $data['signature'] = $this->sign($data); $response = $this->api($this->refund_url, $data); // LOG the refund attempt $this->model_extension_hutko_payment_hutko->logTransaction( $order_id, $hutko_ref, 'refund', (($response['response']['reverse_status'] ?? '') === 'approved') ? 'success' : 'failed', $amount, $order_info['currency_code'], $response ); if (($response['response']['reverse_status'] ?? '') === 'approved') { $json['success'] = $this->language->get('text_refund_success'); $rev_amt = isset($response['response']['reversal_amount']) ? $response['response']['reversal_amount']/100 : $amount; $msg = sprintf($this->language->get('text_refund_success_comment'), $hutko_ref, $this->currency->format($rev_amt, $order_info['currency_code'], $order_info['currency_value']), $comment ); $this->model_extension_hutko_payment_hutko->addOrderHistory($order_id, $this->config->get('payment_hutko_refunded_status_id'), $msg, true); } else { $err = $response['response']['error_message'] ?? 'Unknown Error'; $json['error'] = sprintf($this->language->get('text_refund_api_error'), $err); } } else { $json['error'] = $this->language->get('error_invalid_request'); } $this->response->addHeader('Content-Type: application/json'); $this->response->setOutput(json_encode($json)); } public function status(): void { $this->load->language('extension/hutko/payment/hutko'); $json = []; $ref = $this->request->post['hutko_transaction_ref'] ?? ''; if ($ref) { $data = [ 'order_id' => $ref, 'merchant_id' => $this->config->get('payment_hutko_merchant_id'), 'version' => '1.0', ]; $data['signature'] = $this->sign($data); $response = $this->api($this->status_url, $data); if (($response['response']['response_status'] ?? '') === 'success') { $json['success'] = $this->language->get('text_status_success'); unset($response['response']['response_signature_string'], $response['response']['signature']); $json['data'] = $response['response']; } else { $err = $response['response']['error_message'] ?? 'Unknown Error'; $json['error'] = sprintf($this->language->get('text_status_api_error'), $err); } } else { $json['error'] = $this->language->get('error_missing_params'); } $this->response->addHeader('Content-Type: application/json'); $this->response->setOutput(json_encode($json)); } private function sign(array $data): string { $key = $this->config->get('payment_hutko_secret_key'); $filtered = array_filter($data, function ($v) { return $v !== '' && $v !== null; }); ksort($filtered); $str = $key; foreach ($filtered as $v) $str .= '|' . $v; return sha1($str); } private function api(string $url, array $data): array { if ($this->config->get('payment_hutko_save_logs')) $this->logOC('Req: ' . json_encode($data)); $ch = curl_init($url); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(['request' => $data])); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']); $res = curl_exec($ch); $error = curl_error($ch); curl_close($ch); if ($this->config->get('payment_hutko_save_logs')) { $this->logOC('Res: ' . $res); if ($error) $this->logOC('CURL: ' . $error); } return json_decode($res, true) ?: []; } private function displayLastDayLog() { if (!$this->config->get('payment_hutko_save_logs')) return $this->language->get('text_logs_disabled'); $file = DIR_LOGS . 'error.log'; if (!file_exists($file)) return sprintf($this->language->get('text_log_file_not_found'), 'error.log'); $lines = file($file); $output = []; for ($i = count($lines) - 1; $i >= 0 && count($output) < 50; $i--) { if (strpos($lines[$i], 'Hutko Payment') !== false) $output[] = htmlspecialchars($lines[$i], ENT_QUOTES, 'UTF-8'); } return empty($output) ? $this->language->get('text_no_logs_found') : implode('
', $output); } private function logOC($message) { $this->log->write('Hutko Payment: ' . $message); } }