Update admin/controller/payment/hutko.php

This commit is contained in:
2025-12-14 11:47:39 +02:00
parent 2aac88f953
commit 8da37f4bb3

View File

@@ -124,9 +124,6 @@ class Hutko extends \Opencart\System\Engine\Controller {
$this->model_setting_event->deleteEventByCode('hutko_order_info'); $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 { public function order_info(string &$route, array &$args, string &$output): void {
$order_id = isset($args['order_id']) ? (int)$args['order_id'] : 0; $order_id = isset($args['order_id']) ? (int)$args['order_id'] : 0;
if (!$order_id) return; if (!$order_id) return;
@@ -134,19 +131,35 @@ class Hutko extends \Opencart\System\Engine\Controller {
$this->load->model('sale/order'); $this->load->model('sale/order');
$order_info = $this->model_sale_order->getOrder($order_id); $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']) && if ($order_info && isset($order_info['payment_code']) &&
($order_info['payment_code'] == 'hutko' || $order_info['payment_code'] == 'hutko.hutko')) { ($order_info['payment_code'] == 'hutko' || $order_info['payment_code'] == 'hutko.hutko')) {
$this->load->language('extension/hutko/payment/hutko'); $this->load->language('extension/hutko/payment/hutko');
$this->load->model('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 // Format transactions for view
$data['user_token'] = $this->session->data['user_token']; $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['order_id'] = $order_id;
$data['hutko_transaction_ref'] = $hutko_order['hutko_transaction_ref'] ?? ''; $data['user_token'] = $this->session->data['user_token'];
// URLs // URLs
$data['refund_url'] = $this->url->link('extension/hutko/payment/hutko.refund', 'user_token=' . $this->session->data['user_token'] . '&order_id=' . $order_id); $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 // Translations
$data['text_payment_information'] = $this->language->get('text_payment_information'); $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_refund_title'] = $this->language->get('text_hutko_refund_title');
$data['text_hutko_status_title'] = $this->language->get('text_hutko_status_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_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_refund'] = $this->language->get('button_hutko_refund');
$data['button_hutko_status_check'] = $this->language->get('button_hutko_status_check'); $data['button_hutko_status_check'] = $this->language->get('button_hutko_status_check');
$data['text_confirm_refund'] = $this->language->get('text_confirm_refund'); $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_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); $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 = [
'<div id="history"', // Common OC4 marker
'id="tab-history"', // Older/Alternative themes
'<div class="card mb-3">' // 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) { if (strpos($output, 'id="history"') !== false) {
// Attempt to find the CARD that holds the history.
// Usually: <div class="card"><div class="card-header">...History...</div><div class="card-body"><div id="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 = '<div id="history"';
// Inject our panel immediately before the history container
// We wrap our panel in a div to ensure spacing
$output = str_replace($search, '</div></div>' . $panel_html . '<div class="card"><div class="card-header"><i class="fa-solid fa-clock-rotate-left"></i> Order History</div><div class="card-body">' . $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('<div id="history"', $panel_html . '<div id="history"', $output); $output = str_replace('<div id="history"', $panel_html . '<div id="history"', $output);
$injected = true; } else {
}
if (!$injected) {
// Fallback: Append to the end of the output (inside the main container usually)
$output .= $panel_html; $output .= $panel_html;
} }
} }
@@ -226,13 +196,27 @@ class Hutko extends \Opencart\System\Engine\Controller {
$order_id = (int)($this->request->post['order_id'] ?? 0); $order_id = (int)($this->request->post['order_id'] ?? 0);
$amount = (float)($this->request->post['refund_amount'] ?? 0); $amount = (float)($this->request->post['refund_amount'] ?? 0);
$comment = (string)($this->request->post['refund_comment'] ?? ''); $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); $order_info = $this->model_sale_order->getOrder($order_id);
if ($hutko_order && $order_info && $amount > 0) { if ($hutko_ref && $order_info && $amount > 0) {
$data = [ $data = [
'order_id' => $hutko_order['hutko_transaction_ref'], 'order_id' => $hutko_ref,
'merchant_id' => $this->config->get('payment_hutko_merchant_id'), 'merchant_id' => $this->config->get('payment_hutko_merchant_id'),
'version' => '1.0', 'version' => '1.0',
'amount' => round($amount * 100), 'amount' => round($amount * 100),
@@ -242,22 +226,32 @@ class Hutko extends \Opencart\System\Engine\Controller {
$data['signature'] = $this->sign($data); $data['signature'] = $this->sign($data);
$response = $this->api($this->refund_url, $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') { if (($response['response']['reverse_status'] ?? '') === 'approved') {
$json['success'] = $this->language->get('text_refund_success'); $json['success'] = $this->language->get('text_refund_success');
$rev_amt = isset($response['response']['reversal_amount']) ? $response['response']['reversal_amount']/100 : $amount; $rev_amt = isset($response['response']['reversal_amount']) ? $response['response']['reversal_amount']/100 : $amount;
$msg = sprintf($this->language->get('text_refund_success_comment'), $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']), $this->currency->format($rev_amt, $order_info['currency_code'], $order_info['currency_value']),
$comment $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 { } else {
$err = $response['response']['error_message'] ?? 'Unknown Error'; $err = $response['response']['error_message'] ?? 'Unknown Error';
$json['error'] = sprintf($this->language->get('text_refund_api_error'), $err); $json['error'] = sprintf($this->language->get('text_refund_api_error'), $err);
$this->logOC("Refund Failed: " . json_encode($response));
} }
} else { } else {
$json['error'] = $this->language->get('error_invalid_request'); $json['error'] = $this->language->get('error_invalid_request');