Интеграция платёжных шлюзов PrestaShop
PrestaShop 8.x использует обновлённый Payment API по сравнению с 1.7.x — основные классы те же, но появился PaymentOption с поддержкой iframe. Кастомный модуль оплаты регистрируется через хуки и реализует несколько обязательных методов. Срок разработки — 2–4 рабочих дня.
Структура модуля
modules/mypay/
├── controllers/front/
│ ├── payment.php # Инициализация платежа
│ └── callback.php # Webhook от провайдера
├── views/templates/front/
│ └── payment_infos.tpl
├── mypay.php # Основной класс модуля
└── logo.png
Основной класс модуля
class MyPay extends PaymentModule
{
public function __construct()
{
$this->name = 'mypay';
$this->tab = 'payments_gateways';
$this->version = '1.0.0';
$this->author = 'Your Company';
$this->need_instance = 0;
$this->ps_versions_compliancy = ['min' => '8.0.0', 'max' => _PS_VERSION_];
$this->bootstrap = true;
parent::__construct();
$this->displayName = $this->trans('MyPay', [], 'Modules.Mypay.Admin');
$this->description = $this->trans('Оплата картой через MyPay', [], 'Modules.Mypay.Admin');
}
public function install(): bool
{
return parent::install()
&& $this->registerHook('paymentOptions')
&& $this->registerHook('paymentReturn')
&& $this->registerHook('actionOrderStatusUpdate');
}
public function hookPaymentOptions(array $params): array
{
if (!$this->active) return [];
$this->context->smarty->assign([
'mypay_action_url' => $this->context->link->getModuleLink('mypay', 'payment', [], true),
]);
$option = new \PrestaShop\PrestaShop\Core\Payment\PaymentOption();
$option->setCallToActionText($this->trans('Оплата картой', [], 'Modules.Mypay.Shop'))
->setAction($this->context->link->getModuleLink('mypay', 'payment', [], true))
->setAdditionalInformation(
$this->fetch('module:mypay/views/templates/front/payment_infos.tpl')
);
return [$option];
}
}
Контроллер инициализации платежа
// controllers/front/payment.php
class MyPayPaymentModuleFrontController extends ModuleFrontController
{
public function postProcess(): void
{
$cart = $this->context->cart;
if (!$this->module->checkCurrency($cart)) {
Tools::redirect('index.php?controller=order');
}
$total = (int) round($cart->getOrderTotal(true) * 100);
$customer = new Customer($cart->id_customer);
$currency = new Currency($cart->id_currency);
// Валидируем заказ до редиректа
$this->module->validateOrder(
$cart->id,
Configuration::get('MYPAY_OS_PENDING'),
$cart->getOrderTotal(true),
$this->module->displayName,
null,
[],
(int) $currency->id,
false,
$customer->secure_key
);
$orderId = Order::getIdByCartId($cart->id);
$client = new MyPayApiClient(
Configuration::get('MYPAY_API_KEY'),
Configuration::get('MYPAY_SECRET_KEY')
);
$payment = $client->createPayment([
'amount' => $total,
'currency' => $currency->iso_code,
'order_id' => $orderId,
'callback_url' => $this->context->link->getModuleLink('mypay', 'callback', [], true),
'success_url' => $this->context->link->getPageLink('order-confirmation', true, null, [
'id_cart' => $cart->id,
'id_module' => $this->module->id,
'id_order' => $orderId,
'key' => $customer->secure_key,
]),
]);
$order = new Order($orderId);
$order->reference = $payment['payment_id'];
$order->save();
Tools::redirect($payment['payment_url']);
}
}
Обработка callback
// controllers/front/callback.php
class MyPayCallbackModuleFrontController extends ModuleFrontController
{
public function postProcess(): void
{
$raw = file_get_contents('php://input');
$data = json_decode($raw, true);
$secret = Configuration::get('MYPAY_SECRET_KEY');
if (!hash_equals(hash_hmac('sha256', $raw, $secret), $_SERVER['HTTP_X_SIGNATURE'] ?? '')) {
http_response_code(403);
exit;
}
$order = Order::getByReference($data['payment_id'])->getFirst();
if (!$order) {
http_response_code(404);
exit('Order not found');
}
$statusMap = [
'succeeded' => Configuration::get('MYPAY_OS_PAID'),
'failed' => Configuration::get('PS_OS_ERROR'),
'cancelled' => Configuration::get('PS_OS_CANCELED'),
];
if (isset($statusMap[$data['status']])) {
$history = new OrderHistory();
$history->id_order = $order->id;
$history->changeIdOrderState((int) $statusMap[$data['status']], $order);
$history->addWithemail(true);
}
http_response_code(200);
echo 'OK';
exit;
}
}
Возвраты через admin
PrestaShop не вызывает модуль автоматически при возврате — нужно подписаться на хук actionOrderStatusUpdate:
public function hookActionOrderStatusUpdate(array $params): void
{
$newStatus = $params['newOrderStatus'];
$order = $params['order'];
if ($newStatus->id !== (int) Configuration::get('PS_OS_REFUND')) {
return;
}
$client = new MyPayApiClient(
Configuration::get('MYPAY_API_KEY'),
Configuration::get('MYPAY_SECRET_KEY')
);
$client->refund($order->reference, (int) round($order->total_paid * 100));
}
Настройки в admin
Страница конфигурации модуля (getContent()) использует HelperForm для рендеринга формы с полями API-ключей, тестового режима, маппинга статусов заказа. Статусы заказа (Pending, Paid, Failed) создаются при установке модуля и хранятся в таблице ps_order_state.







