"https://test-epay-oauth.epayment.kz/oauth2/token", "status" => "https://test-epay-api.epayment.kz/check-status/payment/transaction/", "script" => "https://test-epay.epayment.kz/payform/payment-api.js", ]; private const LIVE = [ "oauth" => "https://epay-oauth.homebank.kz/oauth2/token", "status" => "https://epay-api.homebank.kz/check-status/payment/transaction/", "script" => "https://epay.homebank.kz/payform/payment-api.js", ]; private const LANGUAGES = ["ru" => "rus", "kz" => "kaz"]; public function getCurrencyList() { return ["RUB", "KZT"]; } public function getPaymentIdFromRequest(Request $request) { return (int)$request->get("payment"); } public function initiatePay(Payment $payment, Request $request = null) { $result = new ServiceResult(); $returnUrl = (string)($request ? $request->get("return_url") : ""); $cancelUrl = (string)($request ? $request->get("cancel_url") : ""); if ($returnUrl === "" || $cancelUrl === "") { $result->addError(new Error("Halyk ePay return addresses are missing")); return $result; } $invoice = "7" . str_pad((string)$payment->getId(), 5, "0", STR_PAD_LEFT) . str_pad((string)(time() % 1000000), 6, "0", STR_PAD_LEFT); $amount = $this->amount($payment); if ($amount === null) { $result->addError(new Error("Halyk ePay exchange rate is not set")); return $result; } $terminal = trim((string)$this->getBusinessValue($payment, "HALYK_TERMINAL")); $secret = trim((string)$this->getBusinessValue($payment, "HALYK_SECRET")); $token = $this->token($payment, [ "scope" => "payment", "invoiceID" => $invoice, "secret_hash" => hash_hmac("sha256", $invoice, $secret), "amount" => $amount, "currency" => self::CURRENCY, "terminal" => $terminal, ]); if ($token === null) { $result->addError(new Error("Halyk ePay authorization failed")); return $result; } $payment->setField("PS_INVOICE_ID", $invoice); $payment->setField("PS_SUM", $amount); $payment->setField("PS_CURRENCY", self::CURRENCY); $result->setData([ "widget" => [ "script" => $this->urls($payment)["script"], "payment" => [ "invoiceId" => $invoice, "backLink" => $returnUrl, "failureBackLink" => $cancelUrl, "language" => self::LANGUAGES[defined("LANGUAGE_ID") ? LANGUAGE_ID : "en"] ?? "eng", "description" => "Order " . (string)$payment->getOrder()->getField("ACCOUNT_NUMBER"), "terminal" => $terminal, "amount" => $amount, "currency" => self::CURRENCY, "auth" => $token, ], ], ]); return $result; } public function processRequest(Payment $payment, Request $request) { $result = new ServiceResult(); if ($payment->isPaid()) { return $result; } $invoice = (string)$payment->getField("PS_INVOICE_ID"); if ($invoice === "") { $result->addError(new Error("Halyk ePay payment was not started")); return $result; } $token = $this->token($payment, [ "scope" => "webapi usermanagement email_send verification statement statistics payment", "terminal" => trim((string)$this->getBusinessValue($payment, "HALYK_TERMINAL")), ]); if ($token === null) { $result->addError(new Error("Halyk ePay authorization failed")); return $result; } $client = new HttpClient(["socketTimeout" => 15, "streamTimeout" => 25]); $client->setHeader("Authorization", "Bearer " . (string)$token["access_token"]); $body = $client->get($this->urls($payment)["status"] . rawurlencode($invoice)); $data = is_string($body) && $body !== "" ? json_decode($body, true) : null; $transaction = is_array($data) ? ($data["transaction"] ?? null) : null; if (!is_array($transaction) || (string)($data["resultCode"] ?? "") !== "100") { $result->addError(new Error("Halyk ePay payment is not found yet")); return $result; } if (($transaction["statusName"] ?? "") !== "CHARGE") { $result->addError(new Error("Halyk ePay payment is not completed: " . (string)($transaction["statusName"] ?? ""))); return $result; } $expected = (float)$payment->getField("PS_SUM"); if ($expected <= 0 || $payment->getField("PS_CURRENCY") !== self::CURRENCY || (string)($transaction["invoiceID"] ?? "") !== $invoice || ($transaction["currency"] ?? "") !== self::CURRENCY || abs((float)($transaction["amount"] ?? 0) - $expected) > 0.01) { $result->addError(new Error("Halyk ePay amount does not match the order")); return $result; } $result->setPsData([ "PS_INVOICE_ID" => $invoice, "PS_STATUS" => "Y", "PS_STATUS_CODE" => "CHARGE", "PS_STATUS_DESCRIPTION" => "Halyk ePay " . (string)($transaction["reference"] ?? ""), "PS_SUM" => $expected, "PS_CURRENCY" => self::CURRENCY, "PS_RESPONSE_DATE" => new DateTime(), ]); $result->setOperationType(ServiceResult::MONEY_COMING); return $result; } private function amount(Payment $payment): ?float { $sum = PriceMaths::roundByFormatCurrency($payment->getSum(), $payment->getCurrency()); if ($payment->getCurrency() === self::CURRENCY) { return round($sum); } $rate = (float)str_replace(",", ".", (string)$this->getBusinessValue($payment, "HALYK_RATE")); if ($rate <= 0) { return null; } return max(1.0, round($sum * $rate)); } private function urls(Payment $payment): array { return $this->getBusinessValue($payment, "HALYK_TEST") === "N" ? self::LIVE : self::TEST; } private function token(Payment $payment, array $fields): ?array { $client = new HttpClient(["socketTimeout" => 15, "streamTimeout" => 25]); $body = $client->post($this->urls($payment)["oauth"], [ "grant_type" => "client_credentials", "client_id" => trim((string)$this->getBusinessValue($payment, "HALYK_CLIENT_ID")), "client_secret" => trim((string)$this->getBusinessValue($payment, "HALYK_SECRET")), ] + $fields); $data = is_string($body) && $body !== "" ? json_decode($body, true) : null; if (!is_array($data) || empty($data["access_token"])) { PaySystem\Logger::addError(__CLASS__ . ": token request failed, HTTP " . $client->getStatus()); return null; } return $data; } }