Vinoteca: модуль, шаблон и компоненты интернет-магазина вина на 1С-Битрикс

This commit is contained in:
jimagie committed 2026-10-22 03:32:28 +05:00
commit f6ba0d2174
470 files changed
+26454

No files matched your search

@@ -0,0 +1,97 @@
<?php
declare(strict_types=1);
namespace Jinove\Vinoteca\Controller;
use Bitrix\Main\Application;
use Bitrix\Main\Engine\ActionFilter\Csrf;
use Bitrix\Main\Engine\ActionFilter\HttpMethod;
use Bitrix\Main\Engine\Controller;
use Bitrix\Main\Error;
use Bitrix\Sale\DiscountCouponsManager;
use Jinove\Vinoteca\Cart;
use Jinove\Vinoteca\Checkout;
use Jinove\Vinoteca\Controller\Filter\FormGuard;
use Jinove\Vinoteca\Phone;
class Order extends Controller
{
private const RETRY_SECONDS = 20;
public function configureActions(): array
{
return [
"create" => ["prefilters" => [new HttpMethod([HttpMethod::METHOD_POST]), new Csrf(), new FormGuard()]],
"pay" => ["prefilters" => [new HttpMethod([HttpMethod::METHOD_POST]), new Csrf(), new FormGuard(FormGuard::TIMING_NONE, FormGuard::MIN_FILL_MS, false)]],
];
}
public function createAction(string $name, string $phone, string $email, string $pay, string $consent = "", string $comment = "", string $website = "", string $altcha = "", string $page = "", string $base = "/"): ?array
{
if ($website !== "") {
return ["ok" => true, "redirect" => ""];
}
$name = trim((string)preg_replace('/\s+/u', " ", $name));
$email = mb_strtolower(trim($email));
$comment = trim($comment);
$phone = Phone::format($phone);
if ($consent !== "Y" || preg_match('/^\/[\w\-\/]{0,198}$/', $page) !== 1 || mb_strlen($name) < 2 || mb_strlen($name) > 100 || $phone === "" || !check_email($email) || mb_strlen($email) > 254 || mb_strlen($comment) > 500 || (int)$pay <= 0) {
return $this->fail("Invalid fields", "fields");
}
$session = Application::getInstance()->getSession();
if (time() - (int)$session->get("vino_order_last") < self::RETRY_SECONDS) {
return $this->fail("Too many orders", "rate");
}
$session->set("vino_order_last", time());
$basket = Cart::load();
if ($basket === null) {
$session->set("vino_order_last", 0);
return $this->fail("Cart is unavailable", "cart");
}
DiscountCouponsManager::init();
$created = Checkout::create($basket, ["name" => $name, "phone" => $phone, "email" => $email, "comment" => $comment], (int)$pay, Checkout::consentProof($page));
if (isset($created["error"])) {
$session->set("vino_order_last", 0);
return $this->fail("Order was not created", (string)$created["error"]);
}
$dir = Checkout::dir($base);
$order = $created["order"];
$redirect = Checkout::doneUrl($order, $dir);
if ($created["method"]["code"] === "halyk") {
$redirect = Checkout::doneUrl($order, $dir, ["autopay" => "Y"]);
} elseif ($created["method"]["code"] !== "cash") {
$started = Checkout::startPayment($order, $dir);
$redirect = $started["url"] !== "" ? $started["url"] : Checkout::doneUrl($order, $dir, ["pay" => "failed"]);
}
return ["ok" => true, "redirect" => $redirect];
}
public function payAction(int $id, string $hash, string $base = "/"): ?array
{
$order = Checkout::find($id, $hash);
if ($order === null || $order->isCanceled()) {
return $this->fail("Order not found", "order");
}
$dir = Checkout::dir($base);
$started = Checkout::startPayment($order, $dir);
if (is_array($started["widget"] ?? null)) {
return ["ok" => true, "widget" => $started["widget"]];
}
if ($started["url"] === "") {
return $this->fail("Payment is unavailable", "pay");
}
return ["ok" => true, "redirect" => $started["url"]];
}
private function fail(string $message, string $code): ?array
{
$this->addError(new Error($message, $code));
return null;
}
}