first commit

This commit is contained in:
O K
2025-12-12 09:28:40 +02:00
commit b1b2ef5949
15 changed files with 1303 additions and 0 deletions

View File

@@ -0,0 +1,128 @@
<?php
namespace Opencart\Catalog\Controller\Extension\Hutko\Payment;
class Hutko extends \Opencart\System\Engine\Controller {
private $checkout_url = 'https://pay.hutko.org/api/checkout/url/';
public function index(): string {
$this->load->language('extension/hutko/payment/hutko');
return $this->load->view('extension/hutko/payment/hutko', ['language' => $this->config->get('config_language')]);
}
public function confirm(): void {
$this->load->language('extension/hutko/payment/hutko');
$this->load->model('checkout/order');
$json = [];
if (!isset($this->session->data['order_id'])) {
$json['error'] = 'Session missing';
$json['redirect'] = $this->url->link('checkout/failure', 'language=' . $this->config->get('config_language'), true);
} else {
$order_info = $this->model_checkout_order->getOrder($this->session->data['order_id']);
if (!$order_info) {
$json['error'] = 'Order missing';
} else {
// Build API Payload
$request_data = $this->buildRequest($order_info);
// Save Ref
$this->load->model('extension/hutko/payment/hutko');
$this->model_extension_hutko_payment_hutko->addHutkoOrder($order_info['order_id'], $request_data['order_id']);
// API Call
$response = $this->api($this->checkout_url, $request_data);
if (($response['response']['response_status'] ?? '') === 'success' && !empty($response['response']['checkout_url'])) {
// Set to Pending/Initiated
$this->model_checkout_order->addHistory($order_info['order_id'], $this->config->get('payment_hutko_new_order_status_id'), 'Redirecting to Hutko', false);
// Return Redirect URL to frontend JS
$json['redirect'] = $response['response']['checkout_url'];
} else {
$json['error'] = $response['response']['error_message'] ?? $this->language->get('error_api_communication');
}
}
}
$this->response->addHeader('Content-Type: application/json');
$this->response->setOutput(json_encode($json));
}
public function callback(): void {
$input = file_get_contents("php://input");
$data = json_decode($input, true);
if (!$data || !$this->validate($data)) {
http_response_code(400);
exit('Invalid Request');
}
$parts = explode('#', $data['order_id']);
$order_id = (int)$parts[0];
$this->load->model('checkout/order');
$order_info = $this->model_checkout_order->getOrder($order_id);
if ($order_info) {
$status = $data['order_status'] ?? '';
// Map statuses
if ($status === 'approved') {
$this->model_checkout_order->addHistory($order_id, $this->config->get('payment_hutko_success_status_id'), 'Hutko Confirmed', true);
echo "OK";
} elseif ($status === 'declined') {
$this->model_checkout_order->addHistory($order_id, $this->config->get('payment_hutko_declined_status_id'), 'Declined', true);
echo "Declined";
} else {
echo "Status update received";
}
}
}
private function buildRequest($order) {
$ref = $order['order_id'] . '#' . time();
$total = (int)round($order['total'] * 100); // Send in cents
$data = [
'order_id' => $ref,
'merchant_id' => $this->config->get('payment_hutko_merchant_id'),
'amount' => $total,
'currency' => $order['currency_code'],
'order_desc' => 'Order #' . $order['order_id'],
'response_url' => $this->url->link('checkout/success', 'language=' . $this->config->get('config_language'), true),
'server_callback_url' => $this->url->link('extension/hutko/payment/hutko.callback', '', true),
'reservation_data' => base64_encode(json_encode(['products' => []])) // simplified for brevity
];
$data['signature'] = $this->sign($data);
return $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 validate($data) {
$sig = $data['signature'] ?? '';
unset($data['signature'], $data['response_signature_string']);
return hash_equals($this->sign($data), $sig);
}
private function api($url, $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']);
$res = curl_exec($ch);
curl_close($ch);
return json_decode($res, true) ?: [];
}
}

View File

@@ -0,0 +1,14 @@
<?php
// Text
$_['text_title'] = 'Credit Card / Debit Card (Hutko)';
$_['text_initiated_payment'] = 'Customer initiated payment via Hutko.';
$_['text_order'] = 'Order';
$_['text_loading'] = 'Loading';
$_['error_payment_data_build'] = 'Error: Could not prepare payment data. Please try again or contact support.';
$_['error_api_communication'] = 'Error: Could not communicate with the payment gateway. Please try again.';
$_['text_redirecting_comment'] = 'Redirecting to Hutko. Hutko Order ID: %s. URL: %s';
// For callback
$_['text_payment_approved'] = 'Payment Approved by Hutko.';
$_['text_payment_declined'] = 'Payment Declined by Hutko.';
$_['text_payment_expired'] = 'Payment Expired at Hutko.';
$_['text_payment_processing'] = 'Payment is Processing at Hutko.';

View File

@@ -0,0 +1,16 @@
<?php
// Hutko translation file
$_['text_title'] = 'Оплата картой через Hutko';
$_['text_initiated_payment'] = 'Клиент инициировал платеж через Hutko.';
$_['text_order'] = 'Заказ';
$_['text_loading'] = 'Загрузка';
$_['error_payment_data_build'] = 'Ошибка: не удалось подготовить данные платежа. Повторите попытку или обратитесь в службу поддержки.';
$_['error_api_communication'] = 'Ошибка: не удалось связаться с платежным шлюзом. Повторите попытку.';
$_['text_redirecting_comment'] = 'Перенаправление на Hutko. Идентификатор заказа Hutko: %s. URL: %s';
$_['text_payment_approved'] = 'Платеж одобрен Hutko.';
$_['text_payment_declined'] = 'Платеж отклонен Hutko.';
$_['text_payment_expired'] = 'Срок действия платежа истек в Hutko.';
$_['text_payment_processing'] = 'Платеж обрабатывается в Hutko.';

View File

@@ -0,0 +1,16 @@
<?php
// Hutko translation file
$_['text_title'] = 'Оплата карткою через Hutko';
$_['text_initiated_payment'] = 'Клієнт ініціював платіж через Hutko.';
$_['text_order'] = 'Замовлення';
$_['text_loading'] = 'Завантаження';
$_['error_payment_data_build'] = 'Помилка: не вдалося підготувати дані платежу. Повторіть спробу або зверніться до служби підтримки.';
$_['error_api_communication'] = 'Помилка: не вдалося зв\'язатися з платіжним шлюзом. Повторіть спробу.';
$_['text_redirecting_comment'] = 'Перенаправлення на Hutko. Ідентифікатор замовлення Hutko: %s. URL: %s';
$_['text_payment_approved'] = 'Платіж схвалений Hutko.';
$_['text_payment_declined'] = 'Платіж відхилений Hutko.';
$_['text_payment_expired'] = 'Термін дії платежу минув у Hutko.';
$_['text_payment_processing'] = 'Платіж обробляється в Hutko.';

View File

@@ -0,0 +1,76 @@
<?php
namespace Opencart\Catalog\Model\Extension\Hutko\Payment;
class Hutko extends \Opencart\System\Engine\Model
{
public function getMethods(array $address = []): array
{
$method_data = $this->getMethod($address);
// Only return the method if it actually has data
if ($method_data) {
return $method_data;
}
return [];
}
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')");
if (!$query->num_rows) {
$status = false;
}
}
// 3. Check Order Total
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
];
}
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) . "'");
}
}

View File

@@ -0,0 +1,30 @@
<div class="d-inline-block pt-2 pd-2 w-100 text-end">
<button type="button" id="button-confirm" class="btn btn-primary">{{ button_confirm }}</button>
</div>
<script type="text/javascript"><!--
$('#button-confirm').on('click', function() {
var element = this;
$.ajax({
url: 'index.php?route=extension/hutko/payment/hutko.confirm&language={{ language }}',
type: 'get',
dataType: 'json',
beforeSend: function() {
$(element).prop('disabled', true).addClass('loading');
},
complete: function() {
$(element).prop('disabled', false).removeClass('loading');
},
success: function(json) {
if (json['error']) {
alert(json['error']);
}
if (json['redirect']) {
location = json['redirect'];
}
},
error: function(xhr, ajaxOptions, thrownError) {
console.log(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
}
});
});
//--></script>