From 8da37f4bb3001e0ff5dd4f597b7154cb69a5200f Mon Sep 17 00:00:00 2001 From: panariga Date: Sun, 14 Dec 2025 11:47:39 +0200 Subject: [PATCH] Update admin/controller/payment/hutko.php --- admin/controller/payment/hutko.php | 112 ++++++++++++++--------------- 1 file changed, 53 insertions(+), 59 deletions(-) diff --git a/admin/controller/payment/hutko.php b/admin/controller/payment/hutko.php index da11582..1033966 100644 --- a/admin/controller/payment/hutko.php +++ b/admin/controller/payment/hutko.php @@ -124,9 +124,6 @@ class Hutko extends \Opencart\System\Engine\Controller { $this->model_setting_event->deleteEventByCode('hutko_order_info'); } - /** - * Event handler: Injects the Hutko panel into Order Info page - */ 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; @@ -134,19 +131,35 @@ class Hutko extends \Opencart\System\Engine\Controller { $this->load->model('sale/order'); $order_info = $this->model_sale_order->getOrder($order_id); - // Check if payment method is Hutko (code can vary slightly depending on how it was saved) 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'); - $hutko_order = $this->model_extension_hutko_payment_hutko->getHutkoOrder($order_id); + // NEW: Get all transactions + $transactions = $this->model_extension_hutko_payment_hutko->getTransactions($order_id); - // Prepare Data - $data['user_token'] = $this->session->data['user_token']; + // 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['hutko_transaction_ref'] = $hutko_order['hutko_transaction_ref'] ?? ''; + $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); @@ -154,7 +167,6 @@ class Hutko extends \Opencart\System\Engine\Controller { // Translations $data['text_payment_information'] = $this->language->get('text_payment_information'); - $data['text_hutko_transaction_ref_label'] = $this->language->get('text_hutko_transaction_ref_label'); $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'); @@ -162,56 +174,14 @@ class Hutko extends \Opencart\System\Engine\Controller { $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_not_available'] = $this->language->get('text_not_available'); $data['text_loading'] = $this->language->get('text_loading'); + $data['text_no_transactions'] = 'No Hutko transactions recorded.'; - $data['hutko_transaction_ref_display'] = $data['hutko_transaction_ref'] ?: $data['text_not_available']; - - // Load View $panel_html = $this->load->view('extension/hutko/payment/hutko_order_info_panel', $data); - // Injection Logic: Try to place it before the History card - // We look for the "Order History" text or the history div ID. - // OC4 typically uses id="history" for the history list, but we want to be above the card containing it. - - $markers = [ - '
' // Generic card start, risky but fallback - ]; - - $injected = false; - - // 1. Try to find the specific "History" ID and inject BEFORE the container card usually wrapping it - // Regex looks for the card containing id="history" - // This is complex, so let's try a simpler reliable marker: The closing of the previous card? - - // 2. Best bet: Inject before the div that contains the history load logic if (strpos($output, 'id="history"') !== false) { - // Attempt to find the CARD that holds the history. - // Usually:
...History...
- - // Let's just prepend it to the history div itself for simplicity, ensuring it renders. - // Or better, find the header "text_history" usually rendered. - - $search = '
' . $panel_html . '
Order History
' . $search, $output); - - // Note: The replace above assumes a specific structure which might break layout. - // Safer approach: Append to the "Payment Details" tab if it exists, or just prepend to the whole output? No. - - // SAFE INJECTION: Look for the closing of the "Order Details" card (usually the first big card) - // Or just search for the specific history ID and prepend. - - // Let's go with a simpler replace: $output = str_replace('
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; + } + } + } - $hutko_order = $this->model_extension_hutko_payment_hutko->getHutkoOrder($order_id); $order_info = $this->model_sale_order->getOrder($order_id); - if ($hutko_order && $order_info && $amount > 0) { + if ($hutko_ref && $order_info && $amount > 0) { $data = [ - 'order_id' => $hutko_order['hutko_transaction_ref'], + 'order_id' => $hutko_ref, 'merchant_id' => $this->config->get('payment_hutko_merchant_id'), 'version' => '1.0', 'amount' => round($amount * 100), @@ -242,22 +226,32 @@ class Hutko extends \Opencart\System\Engine\Controller { $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_order['hutko_transaction_ref'], + $hutko_ref, $this->currency->format($rev_amt, $order_info['currency_code'], $order_info['currency_value']), $comment ); - $this->model_sale_order->addHistory($order_id, $this->config->get('payment_hutko_refunded_status_id'), $msg, true); + $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); - $this->logOC("Refund Failed: " . json_encode($response)); } } else { $json['error'] = $this->language->get('error_invalid_request');