Update catalog/model/payment/hutko.php

This commit is contained in:
2025-12-14 11:42:49 +02:00
parent 5353123921
commit 71b0a98c3f

View File

@@ -1,76 +1,86 @@
<?php <?php
namespace Opencart\Catalog\Model\Extension\Hutko\Payment; namespace Opencart\Catalog\Model\Extension\Hutko\Payment;
class Hutko extends \Opencart\System\Engine\Model class Hutko extends \Opencart\System\Engine\Model {
{
public function getMethods(array $address = []): array public function getMethods(array $address = []): array {
{
$method_data = $this->getMethod($address); $method_data = $this->getMethod($address);
// Only return the method if it actually has data
if ($method_data) { if ($method_data) {
return $method_data; return $method_data;
} }
return []; return [];
} }
public function getMethod(array $address = []): array public function getMethod(array $address = []): array {
{
$this->load->language('extension/hutko/payment/hutko'); $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; $status = true;
// 2. Check Geo Zone // 1. Currency Check
if ($this->config->get('payment_hutko_geo_zone_id')) { $allowed_currencies = ['UAH', 'USD', 'EUR', 'GBP', 'CZK', 'PLN'];
$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')"); 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) { if (!$query->num_rows) {
$status = false; $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()) { if ($this->config->get('payment_hutko_total') > 0 && $this->config->get('payment_hutko_total') > $this->cart->getTotal()) {
$status = false; $status = false;
} }
// 4. Return Data
$method_data = []; $method_data = [];
if ($status && $this->config->get('payment_hutko_status')) { if ($status && $this->config->get('payment_hutko_status')) {
$option_data = [];
$option_data['hutko'] = [ $option_data['hutko'] = [
'code' => 'hutko.hutko', 'code' => 'hutko.hutko',
'name' => $this->language->get('text_title') '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 = [ $method_data = [
'code' => 'hutko', 'code' => 'hutko',
'name' => $this->language->get('text_title'), 'name' => $this->language->get('text_title'),
'option' => $option_data, 'option' => $option_data,
'sort_order' => $sort_order 'sort_order' => (int)$this->config->get('payment_hutko_sort_order')
]; ];
} }
return $method_data; return $method_data;
} }
public function addHutkoOrder($order_id, $ref) /**
{ * Log a transaction event to the database
$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) . "'"); *
* @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;
} }
} }