ver. 0.269: ShopPaymentMethod refactor + Apilo keepalive

This commit is contained in:
2026-02-14 15:22:02 +01:00
parent 5e5d3d068a
commit 818cd7f2c0
31 changed files with 1832 additions and 269 deletions

View File

@@ -12,23 +12,23 @@ class IntegrationsController
$this->repository = $repository;
}
// ── Apilo settings ──────────────────────────────────────────
public function apilo_settings(): string
{
return \Tpl::view( 'integrations/apilo-settings', [
'settings' => $this->repository->getSettings( 'apilo' ),
'apilo_status' => $this->repository->apiloIntegrationStatus(),
] );
}
public function apilo_settings_save(): void
{
$response = [ 'status' => 'error', 'msg' => 'Podczas zapisywania ustawień wystąpił błąd. Proszę spróbować ponownie.' ];
$response = [ 'status' => 'error', 'msg' => 'Podczas zapisywania ustawien wystapil blad. Prosze sprobowac ponownie.' ];
$fieldId = \S::get( 'field_id' );
$value = \S::get( 'value' );
if ( $this->repository->saveSetting( 'apilo', $fieldId, $value ) )
$response = [ 'status' => 'ok', 'msg' => 'Ustawienia zostały zapisane.', 'value' => $value ];
if ( $this->repository->saveSetting( 'apilo', $fieldId, $value ) ) {
$response = [ 'status' => 'ok', 'msg' => 'Ustawienia zostaly zapisane.', 'value' => $value ];
}
echo json_encode( $response );
exit;
@@ -36,70 +36,55 @@ class IntegrationsController
public function apilo_authorization(): void
{
$response = [ 'status' => 'error', 'msg' => 'Podczas autoryzacji wystąpił błąd. Proszę spróbować ponownie.' ];
$settings = $this->repository->getSettings( 'apilo' );
if ( $this->repository->apiloAuthorize( $settings['client-id'], $settings['client-secret'], $settings['authorization-code'] ) )
$response = [ 'status' => 'ok', 'msg' => 'Autoryzacja przebiegła pomyślnie.' ];
if ( $this->repository->apiloAuthorize(
(string)($settings['client-id'] ?? ''),
(string)($settings['client-secret'] ?? ''),
(string)($settings['authorization-code'] ?? '')
) ) {
echo json_encode( [ 'status' => 'ok', 'msg' => 'Autoryzacja przebiegla pomyslnie.' ] );
exit;
}
echo json_encode( $response );
$status = $this->repository->apiloIntegrationStatus();
$message = trim( (string)($status['message'] ?? '') );
if ( $message === '' ) {
$message = 'Podczas autoryzacji wystapil blad. Prosze sprawdzic dane i sprobowac ponownie.';
} else {
$message = 'Autoryzacja nieudana. ' . $message;
}
echo json_encode( [ 'status' => 'error', 'msg' => $message ] );
exit;
}
// ── Apilo data fetch ────────────────────────────────────────
public function get_platform_list(): void
{
if ( $this->repository->apiloFetchList( 'platform' ) )
\S::alert( 'Lista platform została pobrana.' );
else
\S::alert( 'Brak wyników.' );
header( 'Location: /admin/integrations/apilo_settings/' );
exit;
$this->fetchApiloListWithFeedback( 'platform', 'Liste platform' );
}
public function get_status_types_list(): void
{
if ( $this->repository->apiloFetchList( 'status' ) )
\S::alert( 'Lista statusów została pobrana.' );
else
\S::alert( 'Brak wyników.' );
header( 'Location: /admin/integrations/apilo_settings/' );
exit;
$this->fetchApiloListWithFeedback( 'status', 'Liste statusow' );
}
public function get_carrier_account_list(): void
{
if ( $this->repository->apiloFetchList( 'carrier' ) )
\S::alert( 'Lista kont przewoźników została pobrana.' );
else
\S::alert( 'Brak wyników.' );
header( 'Location: /admin/integrations/apilo_settings/' );
exit;
$this->fetchApiloListWithFeedback( 'carrier', 'Liste kont przewoznikow' );
}
public function get_payment_types_list(): void
{
if ( $this->repository->apiloFetchList( 'payment' ) )
\S::alert( 'Lista metod płatności została pobrana.' );
else
\S::alert( 'Brak wyników.' );
header( 'Location: /admin/integrations/apilo_settings/' );
exit;
$this->fetchApiloListWithFeedback( 'payment', 'Liste metod platnosci' );
}
// ── Apilo product operations ────────────────────────────────
public function apilo_create_product(): void
{
$productId = (int) \S::get( 'product_id' );
$result = $this->repository->apiloCreateProduct( $productId );
\S::alert( $result['message'] );
\S::alert( (string)($result['message'] ?? 'Wystapil blad podczas tworzenia produktu w Apilo.') );
header( 'Location: /admin/shop_product/view_list/' );
exit;
}
@@ -120,24 +105,26 @@ class IntegrationsController
public function apilo_product_select_save(): void
{
if ( $this->repository->linkProduct( (int) \S::get( 'product_id' ), \S::get( 'apilo_product_id' ), \S::get( 'apilo_product_name' ) ) )
if ( $this->repository->linkProduct( (int) \S::get( 'product_id' ), \S::get( 'apilo_product_id' ), \S::get( 'apilo_product_name' ) ) ) {
echo json_encode( [ 'status' => 'ok' ] );
else
echo json_encode( [ 'status' => 'error', 'msg' => 'Podczas zapisywania produktu wystąpił błąd. Proszę spróbować ponownie.' ] );
} else {
echo json_encode( [ 'status' => 'error', 'msg' => 'Podczas zapisywania produktu wystapil blad. Prosze sprobowac ponownie.' ] );
}
exit;
}
public function apilo_product_select_delete(): void
{
if ( $this->repository->unlinkProduct( (int) \S::get( 'product_id' ) ) )
if ( $this->repository->unlinkProduct( (int) \S::get( 'product_id' ) ) ) {
echo json_encode( [ 'status' => 'ok' ] );
else
echo json_encode( [ 'status' => 'error', 'msg' => 'Podczas usuwania produktu wystąpił błąd. Proszę spróbować ponownie.' ] );
} else {
echo json_encode( [ 'status' => 'error', 'msg' => 'Podczas usuwania produktu wystapil blad. Prosze sprobowac ponownie.' ] );
}
exit;
}
// ── ShopPRO settings ────────────────────────────────────────
public function shoppro_settings(): string
{
return \Tpl::view( 'integrations/shoppro-settings', [
@@ -147,26 +134,45 @@ class IntegrationsController
public function shoppro_settings_save(): void
{
$response = [ 'status' => 'error', 'msg' => 'Podczas zapisywania ustawień wystąpił błąd. Proszę spróbować ponownie.' ];
$response = [ 'status' => 'error', 'msg' => 'Podczas zapisywania ustawien wystapil blad. Prosze sprobowac ponownie.' ];
$fieldId = \S::get( 'field_id' );
$value = \S::get( 'value' );
if ( $this->repository->saveSetting( 'shoppro', $fieldId, $value ) )
$response = [ 'status' => 'ok', 'msg' => 'Ustawienia zostały zapisane.', 'value' => $value ];
if ( $this->repository->saveSetting( 'shoppro', $fieldId, $value ) ) {
$response = [ 'status' => 'ok', 'msg' => 'Ustawienia zostaly zapisane.', 'value' => $value ];
}
echo json_encode( $response );
exit;
}
// ── ShopPRO product import ──────────────────────────────────
public function shoppro_product_import(): void
{
$productId = (int) \S::get( 'product_id' );
$result = $this->repository->shopproImportProduct( $productId );
\S::alert( $result['message'] );
\S::alert( (string)($result['message'] ?? 'Wystapil blad podczas importu produktu.') );
header( 'Location: /admin/shop_product/view_list/' );
exit;
}
private function fetchApiloListWithFeedback( string $type, string $label ): void
{
$result = $this->repository->apiloFetchListResult( $type );
if ( !empty( $result['success'] ) ) {
$count = (int)($result['count'] ?? 0);
\S::alert( $label . ' zostala pobrana. Liczba rekordow: ' . $count . '.' );
} else {
$details = trim( (string)($result['message'] ?? 'Nieznany blad.') );
\S::alert(
'Nie udalo sie pobrac ' . strtolower( $label ) . '. '
. $details
. ' Co zrobic: sprawdz konfiguracje Apilo, wykonaj autoryzacje i ponow pobranie listy.'
);
}
header( 'Location: /admin/integrations/apilo_settings/' );
exit;
}
}

View File

@@ -0,0 +1,290 @@
<?php
namespace admin\Controllers;
use Domain\PaymentMethod\PaymentMethodRepository;
use admin\ViewModels\Common\PaginatedTableViewModel;
use admin\ViewModels\Forms\FormAction;
use admin\ViewModels\Forms\FormEditViewModel;
use admin\ViewModels\Forms\FormField;
use admin\ViewModels\Forms\FormTab;
class ShopPaymentMethodController
{
private PaymentMethodRepository $repository;
public function __construct(PaymentMethodRepository $repository)
{
$this->repository = $repository;
}
public function list(): string
{
$sortableColumns = ['id', 'name', 'status', 'apilo_payment_type_id'];
$filterDefinitions = [
[
'key' => 'name',
'label' => 'Nazwa',
'type' => 'text',
],
[
'key' => 'status',
'label' => 'Aktywny',
'type' => 'select',
'options' => [
'' => '- aktywny -',
'1' => 'tak',
'0' => 'nie',
],
],
];
$listRequest = \admin\Support\TableListRequestFactory::fromRequest(
$filterDefinitions,
$sortableColumns,
'id'
);
$sortDir = $listRequest['sortDir'];
if (trim((string)\S::get('sort')) === '') {
$sortDir = 'ASC';
}
$result = $this->repository->listForAdmin(
$listRequest['filters'],
$listRequest['sortColumn'],
$sortDir,
$listRequest['page'],
$listRequest['perPage']
);
$apiloPaymentTypes = $this->getApiloPaymentTypes();
$rows = [];
$lp = ($listRequest['page'] - 1) * $listRequest['perPage'] + 1;
foreach ($result['items'] as $item) {
$id = (int)($item['id'] ?? 0);
$name = trim((string)($item['name'] ?? ''));
$status = (int)($item['status'] ?? 0);
$apiloPaymentTypeId = $item['apilo_payment_type_id'] ?? null;
$apiloLabel = '-';
if ($apiloPaymentTypeId !== null) {
$apiloKey = (string)$apiloPaymentTypeId;
if (isset($apiloPaymentTypes[$apiloKey])) {
$apiloLabel = $apiloPaymentTypes[$apiloKey];
}
}
$rows[] = [
'lp' => $lp++ . '.',
'name' => '<a href="/admin/shop_payment_method/edit/id=' . $id . '">' . htmlspecialchars($name, ENT_QUOTES, 'UTF-8') . '</a>',
'status' => $status === 1 ? 'tak' : '<span style="color: #FF0000;">nie</span>',
'apilo_payment_type' => htmlspecialchars((string)$apiloLabel, ENT_QUOTES, 'UTF-8'),
'_actions' => [
[
'label' => 'Edytuj',
'url' => '/admin/shop_payment_method/edit/id=' . $id,
'class' => 'btn btn-xs btn-primary',
],
],
];
}
$total = (int)$result['total'];
$totalPages = max(1, (int)ceil($total / $listRequest['perPage']));
$viewModel = new PaginatedTableViewModel(
[
['key' => 'lp', 'label' => 'Lp.', 'class' => 'text-center', 'sortable' => false],
['key' => 'name', 'sort_key' => 'name', 'label' => 'Nazwa', 'sortable' => true, 'raw' => true],
['key' => 'status', 'sort_key' => 'status', 'label' => 'Aktywny', 'class' => 'text-center', 'sortable' => true, 'raw' => true],
['key' => 'apilo_payment_type', 'sort_key' => 'apilo_payment_type_id', 'label' => 'Typ platnosci Apilo', 'class' => 'text-center', 'sortable' => true],
],
$rows,
$listRequest['viewFilters'],
[
'column' => $listRequest['sortColumn'],
'dir' => $sortDir,
],
[
'page' => $listRequest['page'],
'per_page' => $listRequest['perPage'],
'total' => $total,
'total_pages' => $totalPages,
],
array_merge($listRequest['queryFilters'], [
'sort' => $listRequest['sortColumn'],
'dir' => $sortDir,
'per_page' => $listRequest['perPage'],
]),
$listRequest['perPageOptions'],
$sortableColumns,
'/admin/shop_payment_method/list/',
'Brak danych w tabeli.'
);
return \Tpl::view('shop-payment-method/payment-methods-list', [
'viewModel' => $viewModel,
]);
}
public function edit(): string
{
$paymentMethod = $this->repository->find((int)\S::get('id'));
if ($paymentMethod === null) {
\S::alert('Metoda platnosci nie zostala znaleziona.');
header('Location: /admin/shop_payment_method/list/');
exit;
}
return \Tpl::view('shop-payment-method/payment-method-edit', [
'form' => $this->buildFormViewModel($paymentMethod, $this->getApiloPaymentTypes()),
]);
}
public function save(): void
{
$payload = $_POST;
$paymentMethodId = isset($payload['id']) && $payload['id'] !== ''
? (int)$payload['id']
: (int)\S::get('id');
$id = $this->repository->save($paymentMethodId, $payload);
if ($id !== null) {
echo json_encode([
'success' => true,
'id' => (int)$id,
'message' => 'Metoda platnosci zostala zapisana.',
]);
exit;
}
echo json_encode([
'success' => false,
'errors' => ['general' => 'Podczas zapisywania metody platnosci wystapil blad.'],
]);
exit;
}
private function buildFormViewModel(array $paymentMethod, array $apiloPaymentTypes): FormEditViewModel
{
$id = (int)($paymentMethod['id'] ?? 0);
$name = (string)($paymentMethod['name'] ?? '');
$apiloOptions = ['' => '--- wybierz typ platnosci apilo.com ---'];
foreach ($apiloPaymentTypes as $apiloId => $apiloName) {
$apiloOptions[(string)$apiloId] = $apiloName;
}
$data = [
'id' => $id,
'description' => (string)($paymentMethod['description'] ?? ''),
'status' => (int)($paymentMethod['status'] ?? 0),
'apilo_payment_type_id' => $paymentMethod['apilo_payment_type_id'] ?? '',
];
$fields = [
FormField::hidden('id', $id),
FormField::custom(
'name_preview',
\Html::input([
'label' => 'Nazwa',
'name' => 'name_preview',
'id' => 'name_preview',
'value' => $name,
'type' => 'text',
'readonly' => true,
]),
['tab' => 'settings']
),
FormField::textarea('description', [
'label' => 'Opis',
'tab' => 'settings',
'rows' => 5,
]),
FormField::select('apilo_payment_type_id', [
'label' => 'Typ platnosci Apilo',
'tab' => 'settings',
'options' => $apiloOptions,
]),
FormField::switch('status', [
'label' => 'Aktywny',
'tab' => 'settings',
]),
];
$tabs = [
new FormTab('settings', 'Ustawienia', 'fa-wrench'),
];
$actionUrl = '/admin/shop_payment_method/save/id=' . $id;
$actions = [
FormAction::save($actionUrl, '/admin/shop_payment_method/list/'),
FormAction::cancel('/admin/shop_payment_method/list/'),
];
return new FormEditViewModel(
'shop-payment-method-edit',
'Edycja metody platnosci: ' . $name,
$data,
$fields,
$tabs,
$actions,
'POST',
$actionUrl,
'/admin/shop_payment_method/list/',
true,
['id' => $id]
);
}
private function getApiloPaymentTypes(): array
{
$rawSetting = \admin\factory\Integrations::apilo_settings('payment-types-list');
$raw = null;
if (is_array($rawSetting)) {
$raw = $rawSetting;
} elseif (is_string($rawSetting)) {
$decoded = @unserialize($rawSetting);
if (is_array($decoded)) {
$raw = $decoded;
} else {
$decodedJson = json_decode($rawSetting, true);
if (is_array($decodedJson)) {
$raw = $decodedJson;
}
}
}
if (!is_array($raw)) {
return [];
}
if (isset($raw['message']) && isset($raw['code'])) {
return [];
}
if (isset($raw['items']) && is_array($raw['items'])) {
$raw = $raw['items'];
} elseif (isset($raw['data']) && is_array($raw['data'])) {
$raw = $raw['data'];
}
$list = [];
foreach ($raw as $key => $paymentType) {
if (is_array($paymentType)) {
if (isset($paymentType['id'], $paymentType['name'])) {
$list[(string)$paymentType['id']] = (string)$paymentType['name'];
continue;
}
} elseif (is_scalar($paymentType)) {
if (is_int($key) || (is_string($key) && preg_match('/^-?\d+$/', $key) === 1)) {
$list[(string)$key] = (string)$paymentType;
}
}
}
return $list;
}
}

View File

@@ -316,6 +316,13 @@ class Site
new \Domain\Coupon\CouponRepository( $mdb )
);
},
'ShopPaymentMethod' => function() {
global $mdb;
return new \admin\Controllers\ShopPaymentMethodController(
new \Domain\PaymentMethod\PaymentMethodRepository( $mdb )
);
},
'Pages' => function() {
global $mdb;

View File

@@ -1,11 +0,0 @@
<?php
namespace admin\controls;
class ShopPaymentMethod
{
public static function view_list()
{
return \Tpl::view( 'shop-payment-method/view-list', [
'apilo_payment_types_list' => unserialize( \admin\factory\Integrations::apilo_settings( 'payment-types-list' ) ),
] );
}
}

View File

@@ -18,9 +18,12 @@ class ShopTransport
public static function transport_edit()
{
global $mdb;
$paymentMethodRepository = new \Domain\PaymentMethod\PaymentMethodRepository( $mdb );
return \Tpl::view( 'shop-transport/transport-edit', [
'transport_details' => \admin\factory\ShopTransport::transport_details( \S::get( 'id' ) ),
'payments_list' => \admin\factory\ShopPaymentMethod::payments_list(),
'payments_list' => $paymentMethodRepository -> allForAdmin(),
'apilo_carrier_account_list' => unserialize( \admin\factory\Integrations::apilo_settings( 'carrier-account-list' ) ),
] );
}

View File

@@ -4,7 +4,7 @@ namespace admin\factory;
/**
* Fasada kompatybilnosci wstecznej.
* Deleguje do Domain\Integrations\IntegrationsRepository.
* Uzywane przez: cron.php, shop\Order, admin\Controllers\ShopStatusesController, admin\controls\ShopTransport, admin\controls\ShopPaymentMethod, admin\controls\ShopProduct.
* Uzywane przez: cron.php, shop\Order, admin\Controllers\ShopStatusesController, admin\controls\ShopTransport, admin\controls\ShopProduct, admin\Controllers\ShopPaymentMethodController.
*/
class Integrations {
@@ -32,6 +32,11 @@ class Integrations {
return self::repo()->apiloGetAccessToken();
}
static public function apilo_keepalive( int $refresh_lead_seconds = 300 )
{
return self::repo()->apiloKeepalive( $refresh_lead_seconds );
}
static public function apilo_authorization( $client_id, $client_secret, $authorization_code )
{
return self::repo()->apiloAuthorize( $client_id, $client_secret, $authorization_code );

View File

@@ -1,10 +0,0 @@
<?php
namespace admin\factory;
class ShopPaymentMethod
{
public static function payments_list()
{
global $mdb;
return $mdb -> select( 'pp_shop_payment_methods', '*', [ 'ORDER' => [ 'name' => 'ASC'] ] );
}
}

View File

@@ -1,6 +0,0 @@
<?php
namespace admin\view;
class ShopPaymentMethod
{
}