fixed cart clearance
This commit is contained in:
@@ -1,12 +1,15 @@
|
||||
<?php
|
||||
|
||||
namespace Opencart\Admin\Controller\Extension\Hutko\Payment;
|
||||
|
||||
class Hutko extends \Opencart\System\Engine\Controller {
|
||||
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';
|
||||
|
||||
public function index(): void {
|
||||
public function index(): void
|
||||
{
|
||||
$this->load->language('extension/hutko/payment/hutko');
|
||||
$this->document->setTitle($this->language->get('heading_title'));
|
||||
|
||||
@@ -29,11 +32,22 @@ class Hutko extends \Opencart\System\Engine\Controller {
|
||||
|
||||
// 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'
|
||||
'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) {
|
||||
@@ -62,7 +76,8 @@ class Hutko extends \Opencart\System\Engine\Controller {
|
||||
$this->response->setOutput($this->load->view('extension/hutko/payment/hutko', $data));
|
||||
}
|
||||
|
||||
public function save(): void {
|
||||
public function save(): void
|
||||
{
|
||||
$this->load->language('extension/hutko/payment/hutko');
|
||||
$json = [];
|
||||
|
||||
@@ -89,13 +104,15 @@ class Hutko extends \Opencart\System\Engine\Controller {
|
||||
$this->response->setOutput(json_encode($json));
|
||||
}
|
||||
|
||||
public function install(): void {
|
||||
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 {
|
||||
public function uninstall(): void
|
||||
{
|
||||
$this->load->model('setting/event');
|
||||
$this->model_setting_event->deleteEventByCode('hutko_order_info'); // Cleanup old events if any
|
||||
}
|
||||
@@ -105,7 +122,8 @@ class Hutko extends \Opencart\System\Engine\Controller {
|
||||
* 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 {
|
||||
public function order(): string
|
||||
{
|
||||
$this->load->language('extension/hutko/payment/hutko');
|
||||
|
||||
// In OC4, load->controller calls for order info don't always pass arguments,
|
||||
@@ -115,17 +133,26 @@ class Hutko extends \Opencart\System\Engine\Controller {
|
||||
|
||||
$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'])) {
|
||||
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'];
|
||||
if (isset($payload_arr['additional_info'])) {
|
||||
$inner = json_decode($payload_arr['additional_info'], true);
|
||||
if ($inner) $payload_arr['additional_info'] = $inner;
|
||||
if (isset($payload_arr['additional_info']['reservation_data'])) {
|
||||
$inner = json_decode($payload_arr['additional_info']['reservation_data'], true);
|
||||
if ($inner) $payload_arr['additional_info']['reservation_data'] = $inner;
|
||||
}
|
||||
}
|
||||
if (isset($payload_arr['response_signature_string'])) {
|
||||
unset($payload_arr['response_signature_string']);
|
||||
}
|
||||
$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'],
|
||||
@@ -133,13 +160,13 @@ class Hutko extends \Opencart\System\Engine\Controller {
|
||||
'status' => $t['status'],
|
||||
'amount' => $t['amount'] . ' ' . $t['currency'],
|
||||
'payload' => $pretty_payload,
|
||||
'can_refund'=> ($t['type'] == 'callback' && $t['status'] == 'success')
|
||||
'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);
|
||||
@@ -154,13 +181,14 @@ class Hutko extends \Opencart\System\Engine\Controller {
|
||||
$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 {
|
||||
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');
|
||||
@@ -170,8 +198,8 @@ public function create_payment_link(): void {
|
||||
$order_info = $this->model_sale_order->getOrder($order_id);
|
||||
|
||||
if ($order_info) {
|
||||
$hutko_ref = $order_id . '#ADM' . time();
|
||||
|
||||
$hutko_ref = $order_id . '#ADM' . time();
|
||||
|
||||
$request_data = $this->buildRequest($order_info, $hutko_ref);
|
||||
|
||||
if (!$request_data) {
|
||||
@@ -181,7 +209,7 @@ public function create_payment_link(): void {
|
||||
|
||||
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,
|
||||
@@ -199,7 +227,7 @@ public function create_payment_link(): void {
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -208,7 +236,7 @@ public function create_payment_link(): void {
|
||||
} else {
|
||||
$err = $response['response']['error_message'] ?? 'API Error';
|
||||
$json['error'] = $err;
|
||||
|
||||
|
||||
$this->model_extension_hutko_payment_hutko->logTransaction(
|
||||
$order_id,
|
||||
$hutko_ref,
|
||||
@@ -228,21 +256,22 @@ public function create_payment_link(): void {
|
||||
$this->response->setOutput(json_encode($json));
|
||||
}
|
||||
|
||||
public function refund(): void {
|
||||
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'] ?? '');
|
||||
|
||||
$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) {
|
||||
foreach ($transactions as $t) {
|
||||
if ($t['type'] == 'callback' && $t['status'] == 'success') {
|
||||
$hutko_ref = $t['hutko_ref'];
|
||||
break;
|
||||
@@ -262,9 +291,9 @@ public function create_payment_link(): void {
|
||||
'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,
|
||||
@@ -277,14 +306,15 @@ public function create_payment_link(): void {
|
||||
|
||||
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']),
|
||||
$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';
|
||||
@@ -298,7 +328,8 @@ public function create_payment_link(): void {
|
||||
$this->response->setOutput(json_encode($json));
|
||||
}
|
||||
|
||||
public function status(): void {
|
||||
public function status(): void
|
||||
{
|
||||
$this->load->language('extension/hutko/payment/hutko');
|
||||
$json = [];
|
||||
$ref = $this->request->post['hutko_transaction_ref'] ?? '';
|
||||
@@ -310,7 +341,7 @@ public function create_payment_link(): void {
|
||||
];
|
||||
$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']);
|
||||
@@ -326,7 +357,8 @@ public function create_payment_link(): void {
|
||||
$this->response->setOutput(json_encode($json));
|
||||
}
|
||||
|
||||
private function displayLastDayLog() {
|
||||
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');
|
||||
@@ -337,16 +369,17 @@ public function create_payment_link(): void {
|
||||
}
|
||||
return empty($output) ? $this->language->get('text_no_logs_found') : implode('<br>', $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) {
|
||||
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'];
|
||||
@@ -361,7 +394,7 @@ public function create_payment_link(): void {
|
||||
}
|
||||
|
||||
$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')) {
|
||||
@@ -403,12 +436,13 @@ public function create_payment_link(): void {
|
||||
'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 {
|
||||
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 . "'");
|
||||
|
||||
@@ -443,16 +477,20 @@ public function create_payment_link(): void {
|
||||
return $products_data;
|
||||
}
|
||||
|
||||
private function sign($data) {
|
||||
private function sign($data)
|
||||
{
|
||||
$key = $this->config->get('payment_hutko_secret_key');
|
||||
$arr = array_filter($data, function($v){ return $v !== '' && $v !== null; });
|
||||
$arr = array_filter($data, function ($v) {
|
||||
return $v !== '' && $v !== null;
|
||||
});
|
||||
ksort($arr);
|
||||
$str = $key;
|
||||
foreach($arr as $v) $str .= '|' . $v;
|
||||
foreach ($arr as $v) $str .= '|' . $v;
|
||||
return sha1($str);
|
||||
}
|
||||
|
||||
private function api($url, $data) {
|
||||
private function api($url, $data)
|
||||
{
|
||||
if ($this->config->get('payment_hutko_save_logs')) $this->logOC('Req: ' . json_encode($data));
|
||||
|
||||
$ch = curl_init($url);
|
||||
@@ -462,7 +500,7 @@ public function create_payment_link(): void {
|
||||
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);
|
||||
@@ -471,14 +509,15 @@ public function create_payment_link(): void {
|
||||
$this->logOC('Res: ' . $res);
|
||||
if ($error) $this->logOC('Curl Error: ' . $error);
|
||||
}
|
||||
|
||||
|
||||
return json_decode($res, true) ?: [];
|
||||
}
|
||||
|
||||
private function logOC($msg) {
|
||||
private function logOC($msg)
|
||||
{
|
||||
$this->log->write("Hutko Payment: " . $msg);
|
||||
}
|
||||
// =========================================================================
|
||||
// SHARED LOGIC END
|
||||
// =========================================================================
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user