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(); // No event registration needed - we use the native 'order()' method now } public function uninstall(): void { $this->load->model('setting/event'); $this->model_setting_event->deleteEventByCode('hutko_order_info'); // Cleanup old events if any } /** * Native OpenCart Order Info hook. * OC4 calls this method automatically if the payment method is 'hutko'. * It renders the content into a Tab in the Order Info page. */ public function order(): string { $this->load->language('extension/hutko/payment/hutko'); // In OC4, load->controller calls for order info don't always pass arguments, // so we rely on the global request $order_id = isset($this->request->get['order_id']) ? (int)$this->request->get['order_id'] : 0; if (!$order_id) return ''; $this->load->model('extension/hutko/payment/hutko'); $transactions = $this->model_extension_hutko_payment_hutko->getTransactions($order_id); $data['transactions'] = []; foreach ($transactions as $t) { $payload_arr = json_decode($t['payload'], true); if (isset($payload_arr['request_data']['reservation_data'])) { $inner = json_decode(base64_decode($payload_arr['request_data']['reservation_data']), true); if ($inner) $payload_arr['request_data']['reservation_data'] = $inner; } // FIX: Pretty print with unescaped characters for better readability $pretty_payload = $payload_arr ? json_encode($payload_arr, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) : $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, 'can_refund'=> ($t['type'] == 'callback' && $t['status'] == 'success') ]; } $data['order_id'] = $order_id; $data['user_token'] = $this->session->data['user_token']; $data['refund_url'] = $this->url->link('extension/hutko/payment/hutko.refund', 'user_token=' . $this->session->data['user_token'] . '&order_id=' . $order_id, true); $data['status_url'] = $this->url->link('extension/hutko/payment/hutko.status', 'user_token=' . $this->session->data['user_token'], true); $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, true); $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'; $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'); $data['text_create_link_info'] = 'Create a new payment link using current order totals.'; return $this->load->view('extension/hutko/payment/hutko_order_info_panel', $data); } 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(); $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() ] ); if ((int)$order_info['order_status_id'] == 0) { $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_extension_hutko_payment_hutko->addOrderHistory($order_id, $new_status_id, 'Payment Link Created (Admin)', false); } $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 { $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->post['order_id'] ?? 0); $amount = (float)($this->request->post['refund_amount'] ?? 0); $comment = (string)($this->request->post['refund_comment'] ?? ''); $hutko_ref = (string)($this->request->post['hutko_ref'] ?? ''); if (empty($hutko_ref)) { // 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') { $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); $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 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); } // ========================================================================= // SHARED LOGIC START // MAINTENANCE WARNING: The following functions (buildRequest, getProducts, // sign, api, logOC) must remain identical in Admin and Catalog controllers. // ========================================================================= private function buildRequest($order, $hutko_ref) { $products_data = $this->getProducts($order['order_id'], $order); $total_products_sum = 0; foreach ($products_data as $p) { $total_products_sum += $p['total_amount']; } $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') { $shipping_cost += $this->currency->format($t['value'], $order['currency_code'], $order['currency_value'], false); } } $order_total_val = $this->currency->format($order['total'], $order['currency_code'], $order['currency_value'], false); if ($this->config->get('payment_hutko_include_discount_to_total')) { $amount_val = $order_total_val; if (!$this->config->get('payment_hutko_shipping_include')) { $amount_val -= $shipping_cost; } } else { $amount_val = $total_products_sum; } if ($amount_val < 0.01) $amount_val = 0.01; $total_cents = (int)round($amount_val * 100); $catalog_url = defined('HTTP_CATALOG') ? HTTP_CATALOG : HTTP_SERVER; $catalog_url = rtrim($catalog_url, '/') . '/'; $response_url = $catalog_url . 'index.php?route=checkout/success'; $callback_url = $catalog_url . 'index.php?route=extension/hutko/payment/hutko.callback'; $reservation_data = [ "cms_name" => "OpenCart", "cms_version" => VERSION, "shop_domain" => preg_replace("(^https?://)", "", $catalog_url), "phonemobile" => $order['telephone'], "customer_address" => $order['payment_address_1'] . ' ' . $order['payment_address_2'], "customer_country" => $order['shipping_iso_code_2'], "customer_name" => $order['firstname'] . ' ' . $order['lastname'], "customer_email" => $order['email'], "products" => $products_data ]; $data = [ 'order_id' => $hutko_ref, 'merchant_id' => $this->config->get('payment_hutko_merchant_id'), 'amount' => $total_cents, 'currency' => $order['currency_code'], 'order_desc' => 'Order #' . $order['order_id'], 'response_url' => $response_url, 'server_callback_url' => $callback_url, 'sender_email' => $order['email'], 'reservation_data' => base64_encode(json_encode($reservation_data)) ]; $data['signature'] = $this->sign($data); return $data; } private function getProducts(int $order_id, array $order_info): array { $products_data = []; $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "order_product` WHERE `order_id` = '" . (int)$order_id . "'"); foreach ($query->rows as $product) { $unit_price = $this->currency->format($product['price'] + $product['tax'], $order_info['currency_code'], $order_info['currency_value'], false); $total_price = $this->currency->format($product['total'] + ($product['tax'] * $product['quantity']), $order_info['currency_code'], $order_info['currency_value'], false); $products_data[] = [ "id" => $product['product_id'], "name" => $product['name'] . ' ' . $product['model'], "price" => round((float)$unit_price, 2), "total_amount" => round((float)$total_price, 2), "quantity" => (int)$product['quantity'], ]; } if ($this->config->get('payment_hutko_shipping_include')) { $totals = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_total WHERE order_id = '" . (int)$order_id . "' AND code = 'shipping'"); if ($totals->num_rows) { $shipping_val = $this->currency->format($totals->row['value'], $order_info['currency_code'], $order_info['currency_value'], false); if ($shipping_val > 0) { $products_data[] = [ "id" => $this->config->get('payment_hutko_shipping_product_code') ?: 'SHIPPING', "name" => $this->config->get('payment_hutko_shipping_product_name') ?: 'Shipping', "price" => round((float)$shipping_val, 2), "total_amount" => round((float)$shipping_val, 2), "quantity" => 1, ]; } } } return $products_data; } private function sign($data) { $key = $this->config->get('payment_hutko_secret_key'); $arr = array_filter($data, function($v){ return $v !== '' && $v !== null; }); ksort($arr); $str = $key; foreach($arr as $v) $str .= '|' . $v; return sha1($str); } private function api($url, $data) { 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']); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); curl_setopt($ch, CURLOPT_TIMEOUT, 30); $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: ' . $error); } return json_decode($res, true) ?: []; } private function logOC($msg) { $this->log->write("Hutko Payment: " . $msg); } // ========================================================================= // SHARED LOGIC END // ========================================================================= }