fixed icon and status

This commit is contained in:
O K
2025-12-14 14:01:00 +02:00
parent 924e6a9fe0
commit d9f349db3b
3 changed files with 102 additions and 122 deletions

View File

@@ -2,6 +2,7 @@
namespace Opencart\Admin\Controller\Extension\Hutko\Payment;
class Hutko extends \Opencart\System\Engine\Controller {
private $checkout_url = 'https://pay.hutko.org/api/checkout/url/';
private $refund_url = 'https://pay.hutko.org/api/reverse/order_id';
private $status_url = 'https://pay.hutko.org/api/status/order_id';
@@ -92,12 +93,11 @@ class Hutko extends \Opencart\System\Engine\Controller {
$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');
// COMPATIBILITY FIX: Check version strictly for Event Registration
if (version_compare(VERSION, '4.0.2.0', '>=')) {
$event_data = [
'code' => 'hutko_order_info',
'description' => 'Hutko Payment Info Panel',
@@ -106,24 +106,20 @@ class Hutko extends \Opencart\System\Engine\Controller {
'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
} else {
$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');
}
/**
* Event Handler to inject Hutko Panel into 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;
@@ -131,6 +127,7 @@ 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
if ($order_info && isset($order_info['payment_code']) &&
($order_info['payment_code'] == 'hutko' || $order_info['payment_code'] == 'hutko.hutko')) {
@@ -158,33 +155,91 @@ class Hutko extends \Opencart\System\Engine\Controller {
$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']);
// NEW: Link creation URL
$data['create_link_url'] = $this->url->link('extension/hutko/payment/hutko.create_payment_link', 'user_token=' . $this->session->data['user_token'] . '&order_id=' . $order_id);
// Translations
$data['text_payment_information'] = $this->language->get('text_payment_information');
$data['text_hutko_refund_title'] = $this->language->get('text_hutko_refund_title');
$data['button_hutko_refund'] = $this->language->get('button_hutko_refund');
$data['button_hutko_status_check'] = $this->language->get('button_hutko_status_check');
$data['button_create_link'] = 'Create New Payment Link'; // Add to language file
$data['button_create_link'] = 'Create New Payment Link';
$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.';
$data['entry_refund_amount'] = $this->language->get('entry_refund_amount');
$data['entry_refund_comment'] = $this->language->get('entry_refund_comment');
// New Language keys for manual link
$data['text_create_link_info'] = 'Create a new payment link using current order totals.';
$panel_html = $this->load->view('extension/hutko/payment/hutko_order_info_panel', $data);
if (strpos($output, 'id="history"') !== false) {
$output = str_replace('<div id="history"', $panel_html . '<div id="history"', $output);
} else {
// FIX: Simply append the panel to the end of the output.
// This avoids regex failures if the 'history' ID is nested or named differently in themes.
$output .= $panel_html;
}
}
public function create_payment_link(): void {
$this->load->language('extension/hutko/payment/hutko');
$this->load->model('extension/hutko/payment/hutko');
$this->load->model('sale/order');
$json = [];
$order_id = (int)($this->request->get['order_id'] ?? 0);
$order_info = $this->model_sale_order->getOrder($order_id);
if ($order_info) {
$hutko_ref = $order_id . '#ADM' . time(); // Admin unique ref
// Call Shared Logic
$request_data = $this->buildRequest($order_info, $hutko_ref);
if (!$request_data) {
$json['error'] = $this->language->get('error_payment_data_build');
} else {
$response = $this->api($this->checkout_url, $request_data);
if (($response['response']['response_status'] ?? '') === 'success' && !empty($response['response']['checkout_url'])) {
$url = $response['response']['checkout_url'];
$this->model_extension_hutko_payment_hutko->logTransaction(
$order_id,
$hutko_ref,
'payment_request_admin',
'created',
$request_data['amount'] / 100,
$request_data['currency'],
[
'request_data' => $request_data,
'checkout_url' => $url,
'admin_user' => $this->user->getUserName()
]
);
$json['success'] = 'Payment Link Created Successfully';
$json['url'] = $url;
} else {
$err = $response['response']['error_message'] ?? 'API Error';
$json['error'] = $err;
$this->model_extension_hutko_payment_hutko->logTransaction(
$order_id,
$hutko_ref,
'payment_request_admin',
'failed',
$request_data['amount'] / 100,
$request_data['currency'],
['error' => $err, 'api_response' => $response]
);
}
}
} else {
$json['error'] = 'Order not found';
}
$this->response->addHeader('Content-Type: application/json');
$this->response->setOutput(json_encode($json));
}
public function refund(): void {
@@ -196,13 +251,10 @@ class Hutko extends \Opencart\System\Engine\Controller {
$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
// Find the successful payment if ref not provided
$transactions = $this->model_extension_hutko_payment_hutko->getTransactions($order_id);
foreach($transactions as $t) {
if ($t['type'] == 'callback' && $t['status'] == 'success') {
@@ -227,7 +279,6 @@ class Hutko extends \Opencart\System\Engine\Controller {
$response = $this->api($this->refund_url, $data);
// LOG the refund attempt
$this->model_extension_hutko_payment_hutko->logTransaction(
$order_id,
$hutko_ref,
@@ -264,7 +315,6 @@ class Hutko extends \Opencart\System\Engine\Controller {
public function status(): void {
$this->load->language('extension/hutko/payment/hutko');
$json = [];
$ref = $this->request->post['hutko_transaction_ref'] ?? '';
if ($ref) {
$data = [
@@ -286,18 +336,14 @@ class Hutko extends \Opencart\System\Engine\Controller {
} else {
$json['error'] = $this->language->get('error_missing_params');
}
$this->response->addHeader('Content-Type: application/json');
$this->response->setOutput(json_encode($json));
}
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--) {
@@ -306,79 +352,10 @@ class Hutko extends \Opencart\System\Engine\Controller {
return empty($output) ? $this->language->get('text_no_logs_found') : implode('<br>', $output);
}
public function create_payment_link(): void {
$this->load->language('extension/hutko/payment/hutko');
$this->load->model('extension/hutko/payment/hutko');
$this->load->model('sale/order');
$json = [];
$order_id = (int)($this->request->get['order_id'] ?? 0);
$order_info = $this->model_sale_order->getOrder($order_id);
if ($order_info) {
// 1. Generate new Ref (Admin initiated)
$hutko_ref = $order_id . '#ADM' . time(); // Mark as ADM to distinguish
// 2. Build Request (Uses shared logic)
$request_data = $this->buildRequest($order_info, $hutko_ref);
if (!$request_data) {
$json['error'] = $this->language->get('error_payment_data_build');
} else {
// 3. Call API
$response = $this->api($this->checkout_url, $request_data);
if (($response['response']['response_status'] ?? '') === 'success' && !empty($response['response']['checkout_url'])) {
$url = $response['response']['checkout_url'];
// 4. Log to DB
$this->model_extension_hutko_payment_hutko->logTransaction(
$order_id,
$hutko_ref,
'payment_request_admin', // Distinct type
'created',
$request_data['amount'] / 100,
$request_data['currency'],
[
'request_data' => $request_data,
'checkout_url' => $url,
'admin_user' => $this->user->getUserName()
]
);
$json['success'] = 'Payment Link Created Successfully';
$json['url'] = $url;
} else {
$err = $response['response']['error_message'] ?? 'API Error';
$json['error'] = $err;
// Log Failure
$this->model_extension_hutko_payment_hutko->logTransaction(
$order_id,
$hutko_ref,
'payment_request_admin',
'failed',
$request_data['amount'] / 100,
$request_data['currency'],
['error' => $err, 'api_response' => $response]
);
}
}
} else {
$json['error'] = 'Order not found';
}
$this->response->addHeader('Content-Type: application/json');
$this->response->setOutput(json_encode($json));
}
// =========================================================================
// SHARED LOGIC START
// MAINTENANCE WARNING: The following functions (buildRequest, getProducts,
// sign, api) must remain identical in Admin and Catalog controllers.
// sign, api, logOC) must remain identical in Admin and Catalog controllers.
// =========================================================================
private function buildRequest($order, $hutko_ref) {
@@ -389,12 +366,10 @@ class Hutko extends \Opencart\System\Engine\Controller {
$total_products_sum += $p['total_amount'];
}
// Re-fetch totals to ensure accuracy
$totals_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_total WHERE order_id = '" . (int)$order['order_id'] . "' ORDER BY sort_order ASC");
$shipping_cost = 0;
foreach ($totals_query->rows as $t) {
if ($t['code'] == 'shipping') {
// Format using order currency values
$shipping_cost += $this->currency->format($t['value'], $order['currency_code'], $order['currency_value'], false);
}
}
@@ -413,9 +388,7 @@ class Hutko extends \Opencart\System\Engine\Controller {
if ($amount_val < 0.01) $amount_val = 0.01;
$total_cents = (int)round($amount_val * 100);
// Use Catalog URL for response/callback, not Admin URL
$catalog_url = defined('HTTP_CATALOG') ? HTTP_CATALOG : HTTP_SERVER;
// Ensure no double trailing slash
$catalog_url = rtrim($catalog_url, '/') . '/';
$response_url = $catalog_url . 'index.php?route=checkout/success';

View File

@@ -52,12 +52,19 @@ class Hutko extends \Opencart\System\Engine\Controller {
]
);
// FIX: Ensure status is not 0 (Voided). Default to 1 (Pending) if config is missing.
if ($order_info['order_status_id'] == 0) {
$this->model_checkout_order->addHistory($order_info['order_id'], $this->config->get('payment_hutko_new_order_status_id'), $this->language->get('text_initiated_payment'), false);
$new_status_id = (int)$this->config->get('payment_hutko_new_order_status_id');
if ($new_status_id <= 0) {
$new_status_id = 1; // Default to Pending
}
$this->model_checkout_order->addHistory($order_info['order_id'], $new_status_id, $this->language->get('text_initiated_payment'), false);
}
$json['redirect'] = $url;
} else {
// ... error handling (same as before) ...
$err = $response['response']['error_message'] ?? $this->language->get('error_api_communication');
$json['error'] = $err;
$this->model_extension_hutko_payment_hutko->logTransaction(
@@ -77,7 +84,6 @@ class Hutko extends \Opencart\System\Engine\Controller {
$this->response->addHeader('Content-Type: application/json');
$this->response->setOutput(json_encode($json));
}
public function callback(): void {
$this->load->language('extension/hutko/payment/hutko');

View File

@@ -4,5 +4,6 @@
"author": "Hutko",
"link": "https://hutko.org",
"instruction": "",
"code": "hutko"
"code": "hutko",
"image": "admin/view/image/payment/hutko.png"
}