order = $order; $this->login = $login; $this->client = new AbankPay_Payment_Api_Client($token, $is_production); $this->success_status = $success_status; } protected function getAccessToken(): string { return $this->client->fetchToken($this->login); } public function createInvoice(AbankPay_Order $orderObject): array { $client = new AbankPay_Payment_Api_Client($this->getAccessToken(), $this->client->isProduction); $payload = [ 'amount' => $orderObject->getAmount(), 'description' => 'Замовлення #' . $orderObject->getId(), 'items' => $orderObject->getBasketOrder(), 'callback_url' => $orderObject->getWebHookUrl(), 'redirect_url' => $orderObject->getRedirectUrl(), ]; $response = $client->createPreAuthInvoice($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 { $client = new AbankPay_Payment_Api_Client($this->getAccessToken(), $this->client->isProduction); $result = $client->getInvoiceStatus($transaction_id); $status = $result->status ?? null; if ($status === 'paid') { // Перевірка, чи нотатка вже існує $notes = wc_get_order_notes([ 'order_id' => $this->order->get_id(), 'type' => 'system', ]); $note_exists = false; foreach ($notes as $note) { if (strpos($note->content, "àPay: Платіж було підтверджено вручну") !== false) { $note_exists = true; break; } } if (!$note_exists) { $this->order->add_order_note("àPay: Платіж було підтверджено вручну (PREAUTH)"); } update_option('abankpay_admin_notice', [ 'type' => 'success', 'text' => "✅ Платіж підтверджено (PREAUTH)" ]); return; } if ($status === 'hold') { update_option('abankpay_admin_notice', [ 'type' => 'warning', 'text' => '⏳ Платіж знаходиться на утриманні. Очікує підтвердження.' ]); return; } // Всі інші статуси вважаємо помилковими $message = 'àPay: ❌ Статус інвойсу: ' . ($status ?? 'невідомий'); $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' ]); exit; } $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' ]); exit; } $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' ]); exit; } 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' ]); exit; } } catch (\Exception $e) { $this->order->add_order_note("àPay: помилка при поверненні: {$e->getMessage()}"); wp_send_json_error([ 'message' => $e->getMessage(), 'notice_type' => 'error' ]); exit; } } public function confirmHold(): bool { $transaction_id = $this->order->get_transaction_id(); if (!$transaction_id) { $this->order->add_order_note('àPay: не знайдено ID транзакції для підтвердження.'); return false; } // Перевіряємо, чи вже підтверджений $notes = wc_get_order_notes([ 'order_id' => $this->order->get_id(), 'type' => 'system', ]); foreach ($notes as $note) { if (strpos($note->content, 'àPay: Платіж підтверджено через confirmHold') !== false) { return true; } } try { $client = new AbankPay_Payment_Api_Client($this->getAccessToken(), $this->client->isProduction); $result = $client->confirmHold([ 'invoice_id' => $transaction_id, 'amount' => $this->order->get_total() - $this->order->get_total_refunded() ]); if (isset($result->resultCode) && $result->resultCode === '100') { $this->order->add_order_note('àPay: Платіж підтверджено через confirmHold'); return true; } $message = $result->error->message ?? 'Невідома помилка при confirmHold'; $this->order->add_order_note("àPay: ❌ Помилка при confirmHold: {$message}"); return false; } catch (\Exception $e) { $this->order->add_order_note("àPay: ❌ Внутрішня помилка confirmHold: {$e->getMessage()}"); return false; } } }