diff --git a/catalog/model/payment/hutko.php b/catalog/model/payment/hutko.php index 910e227..bcc12c5 100644 --- a/catalog/model/payment/hutko.php +++ b/catalog/model/payment/hutko.php @@ -1,76 +1,86 @@ getMethod($address); - - // Only return the method if it actually has data if ($method_data) { - return $method_data; + return $method_data; } - return []; } - public function getMethod(array $address = []): array - { + public function getMethod(array $address = []): array { $this->load->language('extension/hutko/payment/hutko'); - $allowed_currencies = ['UAH', 'USD', 'EUR', 'GBP', 'CZK']; - if (!in_array(strtoupper($this->session->data['currency']), $allowed_currencies)) { - $status = false; - } - // 1. Validate Address (Safeguard against undefined keys) - $country_id = isset($address['country_id']) ? (int)$address['country_id'] : 0; - $zone_id = isset($address['zone_id']) ? (int)$address['zone_id'] : 0; $status = true; - // 2. Check Geo Zone - if ($this->config->get('payment_hutko_geo_zone_id')) { - $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "zone_to_geo_zone WHERE geo_zone_id = '" . (int)$this->config->get('payment_hutko_geo_zone_id') . "' AND country_id = '" . $country_id . "' AND (zone_id = '" . $zone_id . "' OR zone_id = '0')"); + // 1. Currency Check + $allowed_currencies = ['UAH', 'USD', 'EUR', 'GBP', 'CZK', 'PLN']; + if (!in_array(strtoupper($this->session->data['currency']), $allowed_currencies)) { + $status = false; + } + // 2. Geo Zone Check + if ($this->config->get('payment_hutko_geo_zone_id')) { + $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "zone_to_geo_zone WHERE geo_zone_id = '" . (int)$this->config->get('payment_hutko_geo_zone_id') . "' AND country_id = '" . (int)$address['country_id'] . "' AND (zone_id = '" . (int)$address['zone_id'] . "' OR zone_id = '0')"); if (!$query->num_rows) { $status = false; } } - // 3. Check Order Total + // 3. Total Check if ($this->config->get('payment_hutko_total') > 0 && $this->config->get('payment_hutko_total') > $this->cart->getTotal()) { $status = false; } - // 4. Return Data $method_data = []; - if ($status && $this->config->get('payment_hutko_status')) { - $option_data = []; - $option_data['hutko'] = [ 'code' => 'hutko.hutko', 'name' => $this->language->get('text_title') ]; - // FORCE (int) casting and default value to prevent "Undefined array key" error - $sort_order = (int)$this->config->get('payment_hutko_sort_order') ?? 1; - $method_data = [ 'code' => 'hutko', 'name' => $this->language->get('text_title'), 'option' => $option_data, - 'sort_order' => $sort_order + 'sort_order' => (int)$this->config->get('payment_hutko_sort_order') ]; } return $method_data; } - public function addHutkoOrder($order_id, $ref) - { - $this->db->query("INSERT INTO `" . DB_PREFIX . "hutko_order` SET `order_id` = '" . (int)$order_id . "', `hutko_transaction_ref` = '" . $this->db->escape($ref) . "', `date_added` = NOW() ON DUPLICATE KEY UPDATE `hutko_transaction_ref` = '" . $this->db->escape($ref) . "'"); + /** + * Log a transaction event to the database + * + * @param int $order_id + * @param string $hutko_ref (The unique ID sent to Hutko, e.g. 101#12345678) + * @param string $type (payment, callback, refund) + * @param string $status (created, success, failed) + * @param float $amount + * @param string $currency + * @param array $payload_data (Will be json encoded) + */ + public function logTransaction($order_id, $hutko_ref, $type, $status, $amount, $currency, $payload_data = []) { + $json = json_encode($payload_data, JSON_UNESCAPED_UNICODE); + + $this->db->query("INSERT INTO `" . DB_PREFIX . "hutko_transaction` SET + `order_id` = '" . (int)$order_id . "', + `hutko_ref` = '" . $this->db->escape($hutko_ref) . "', + `type` = '" . $this->db->escape($type) . "', + `status` = '" . $this->db->escape($status) . "', + `amount` = '" . (float)$amount . "', + `currency` = '" . $this->db->escape($currency) . "', + `payload` = '" . $this->db->escape($json) . "', + `date_added` = NOW() + "); } -} + + public function getOrderLastSuccessTransaction($order_id) { + $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "hutko_transaction` WHERE `order_id` = '" . (int)$order_id . "' AND `status` = 'success' AND `type` = 'callback' ORDER BY `date_added` DESC LIMIT 1"); + return $query->row; + } +} \ No newline at end of file