order = $order; $this->client = new AbankPay_Payment_Api_Client('', $is_production); $this->client->fetchToken($login, $token); $this->success_status = $success_status; } public function createInvoice(AbankPay_Order $orderObject): array { $payload = [ 'amount' => $orderObject->getAmount(), 'description' => 'Замовлення #' . $orderObject->getId(), 'order_id_system' => (string) $orderObject->getId(), 'items' => $orderObject->getBasketOrder(), 'callback_url' => $orderObject->getRedirectUrl(), 'callback_system' => $orderObject->getWebHookUrl(), ]; $response = $this->client->createInvoice($payload); if (isset($response->invoice_url)) { $this->order->set_transaction_id($response->invoice_id ?? ''); $this->order->save(); return ['result' => 'success', 'redirect_url' => $response->invoice_url]; } return ['result' => 'error', 'message' => 'Не вдалося створити інвойс']; } public function checkStatus() { $transaction_id = $this->order->get_transaction_id(); if (!$transaction_id) { $this->order->add_order_note('àPay: не знайдено ID транзакції.'); update_option('abankpay_admin_notice', [ 'type' => 'error', 'text' => '❌ Немає transaction ID' ]); return; } try { $result = $this->client->getInvoiceStatus($transaction_id); // Перевіримо, чи нотатка вже існує $notes = wc_get_order_notes([ 'order_id' => $this->order->get_id(), 'type' => 'system', ]); if (isset($result->resultCode) && $result->resultCode === '100') { $amount = isset($result->oper_data[0]->AMOUNT) ? $result->oper_data[0]->AMOUNT : $this->order->get_total(); $note_exists = false; foreach ($notes as $note) { if (strpos($note->content, "àPay: Було отримано оплату") !== false) { $note_exists = true; break; } } $final_statuses = ['completed', 'refunded', 'cancelled', 'failed']; $current_status = $this->order->get_status(); if (!$note_exists) { $this->order->add_order_note("àPay: Було отримано оплату {$amount} грн."); } if (!in_array($current_status, $final_statuses) && isset($result->status) && $result->status === 'paid') { $this->order->update_status(str_replace('wc-', '', $this->success_status)); } update_option('abankpay_admin_notice', [ 'type' => 'success', 'text' => "✅ Платіж підтверджено на суму {$amount} грн." ]); return; } if (isset($result->resultCode) && in_array($result->resultCode, ['400', '401', '406', '212'])) { update_option('abankpay_admin_notice', [ 'type' => 'warning', 'text' => '⏳ Платіж очікує оплати' ]); return; } $message = 'àPay: ❌ '; $message .= $result->resultDesc ?? $result->error->message ?? 'Невідома помилка.'; $note_exists = false; foreach ($notes as $note) { if (strpos($note->content, $message) !== false) { $note_exists = true; break; } } if (!$note_exists) { $this->order->add_order_note($message); } update_option('abankpay_admin_notice', [ 'type' => 'error', 'text' => $message ]); } catch (\Exception $e) { $message = 'àPay: ❌ Внутрішня помилка: ' . $e->getMessage(); $this->order->add_order_note($message); update_option('abankpay_admin_notice', [ 'type' => 'error', 'text' => $message ]); } } public function handleRefund($amount = '', $reason = '') { $result = $this->client->getInvoiceStatus($this->order->get_transaction_id()); $tran_id = ''; if (isset($result->resultCode) && $result->resultCode === '100') { $tran_id = $result->oper_data[0]->TRANID ?? ''; } if (empty($tran_id)) { wp_send_json_error([ 'message' => 'àPay: помилка при поверненні. Інвойс не знайдено.', 'notice_type' => 'error' ]); } $refund_order = ["tran_id" => $tran_id]; if (!empty($reason)) { $refund_order["details"] = $reason; } try { $result = $this->client->refund($refund_order); $amount = $this->order->get_total(); if (is_wp_error($result)) { wp_send_json_error([ 'message' => $result->get_error_message(), 'notice_type' => 'error' ]); return; } $notes = wc_get_order_notes(['order_id' => $this->order->get_id()]); if (is_object($result) && isset($result->result) && $result->result === 'ok') { $note_exists = false; foreach ($notes as $note) { if (strpos($note->content, "Клієнту було повернено {$amount} грн.") !== false) { $note_exists = true; break; } } if (!$note_exists) { $this->order->add_order_note("Клієнту було повернено {$amount} грн."); } wp_send_json_success([ 'status' => 'success', 'message' => "✅ Клієнту було повернено {$amount} грн.", 'notice_type' => 'success' ]); return; } else { $message = isset($result->error->message) ? $result->error->message : 'Невідома помилка від API àPay.'; $note_exists = false; foreach ($notes as $note) { if (strpos($note->content, "àPay: {$message}") !== false) { $note_exists = true; break; } } if (!$note_exists) { $this->order->add_order_note("àPay: {$message}"); } wp_send_json_error([ 'message' => $message, 'notice_type' => 'error' ]); return; } } catch (\Exception $e) { $this->order->add_order_note("àPay: помилка при поверненні: {$e->getMessage()}"); wp_send_json_error([ 'message' => $e->getMessage(), 'notice_type' => 'error' ]); return; } } public function confirmHold() { return true; } }