update
This commit is contained in:
@@ -16,6 +16,8 @@ use App\Modules\Email\EmailSendingService;
|
||||
use App\Modules\Settings\EmailMailboxRepository;
|
||||
use App\Modules\Settings\EmailTemplateRepository;
|
||||
use App\Modules\Settings\ReceiptConfigRepository;
|
||||
use App\Modules\Settings\ShopproApiClient;
|
||||
use App\Modules\Settings\ShopproIntegrationsRepository;
|
||||
use App\Modules\Shipments\ShipmentPackageRepository;
|
||||
|
||||
final class OrdersController
|
||||
@@ -32,7 +34,8 @@ final class OrdersController
|
||||
private readonly ?EmailTemplateRepository $emailTemplateRepo = null,
|
||||
private readonly ?EmailMailboxRepository $emailMailboxRepo = null,
|
||||
private readonly string $storagePath = '',
|
||||
private readonly ?\App\Modules\Printing\PrintJobRepository $printJobRepo = null
|
||||
private readonly ?\App\Modules\Printing\PrintJobRepository $printJobRepo = null,
|
||||
private readonly ?ShopproIntegrationsRepository $shopproIntegrations = null
|
||||
) {
|
||||
}
|
||||
|
||||
@@ -781,4 +784,102 @@ final class OrdersController
|
||||
return Response::json($preview);
|
||||
}
|
||||
|
||||
public function addPayment(Request $request): Response
|
||||
{
|
||||
$orderId = max(0, (int) $request->input('id', 0));
|
||||
if ($orderId <= 0) {
|
||||
return Response::json(['ok' => false, 'error' => 'Nieprawidłowe ID zamówienia.'], 400);
|
||||
}
|
||||
|
||||
if (!Csrf::verify((string) $request->input('_token', ''))) {
|
||||
return Response::json(['ok' => false, 'error' => 'Nieprawidłowy token CSRF.'], 403);
|
||||
}
|
||||
|
||||
$amount = (float) $request->input('amount', 0);
|
||||
$paymentTypeId = trim((string) $request->input('payment_type_id', ''));
|
||||
$paymentDate = trim((string) $request->input('payment_date', ''));
|
||||
$comment = trim((string) $request->input('comment', ''));
|
||||
|
||||
if ($amount <= 0) {
|
||||
return Response::json(['ok' => false, 'error' => 'Kwota musi być większa od 0.'], 422);
|
||||
}
|
||||
if ($paymentTypeId === '') {
|
||||
return Response::json(['ok' => false, 'error' => 'Wybierz typ płatności.'], 422);
|
||||
}
|
||||
|
||||
$result = $this->orders->addPayment($orderId, [
|
||||
'amount' => $amount,
|
||||
'payment_type_id' => $paymentTypeId,
|
||||
'payment_date' => $paymentDate !== '' ? $paymentDate . ' ' . date('H:i:s') : '',
|
||||
'comment' => $comment,
|
||||
]);
|
||||
|
||||
if ($result === null) {
|
||||
return Response::json(['ok' => false, 'error' => 'Nie udało się zapisać płatności.'], 500);
|
||||
}
|
||||
|
||||
$this->orders->recordActivity(
|
||||
$orderId,
|
||||
'payment',
|
||||
'Dodano płatność: ' . number_format($amount, 2, '.', ' ') . ' PLN (' . $paymentTypeId . ')',
|
||||
['payment_id' => $result['id'], 'amount' => $amount, 'type' => $paymentTypeId],
|
||||
'user',
|
||||
$this->auth->user()['name'] ?? null
|
||||
);
|
||||
|
||||
$this->pushPaymentToShoppro($orderId, $result['payment_status']);
|
||||
|
||||
return Response::json([
|
||||
'ok' => true,
|
||||
'payment_id' => $result['id'],
|
||||
'payment_status' => $result['payment_status'],
|
||||
'total_paid' => $result['total_paid'],
|
||||
]);
|
||||
}
|
||||
|
||||
private function pushPaymentToShoppro(int $orderId, int $paymentStatus): void
|
||||
{
|
||||
if ($paymentStatus !== 2 || $this->shopproIntegrations === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
$orderStmt = $this->orders->findOrderSourceInfo($orderId);
|
||||
if ($orderStmt === null || ($orderStmt['source'] ?? '') !== 'shoppro') {
|
||||
return;
|
||||
}
|
||||
|
||||
$integrationId = (int) ($orderStmt['integration_id'] ?? 0);
|
||||
$sourceOrderId = trim((string) ($orderStmt['source_order_id'] ?? ''));
|
||||
if ($integrationId <= 0 || $sourceOrderId === '') {
|
||||
return;
|
||||
}
|
||||
|
||||
$integration = $this->shopproIntegrations->findIntegration($integrationId);
|
||||
if ($integration === null || empty($integration['is_active']) || empty($integration['has_api_key'])) {
|
||||
return;
|
||||
}
|
||||
|
||||
$baseUrl = trim((string) ($integration['base_url'] ?? ''));
|
||||
$apiKey = $this->shopproIntegrations->getApiKeyDecrypted($integrationId);
|
||||
if ($baseUrl === '' || $apiKey === null || trim($apiKey) === '') {
|
||||
return;
|
||||
}
|
||||
|
||||
$client = new ShopproApiClient();
|
||||
$pushResult = $client->setOrderPaid($baseUrl, $apiKey, 10, $sourceOrderId);
|
||||
|
||||
$this->orders->recordActivity(
|
||||
$orderId,
|
||||
'sync',
|
||||
$pushResult['ok']
|
||||
? 'Wysłano status płatności do shopPRO (opłacone)'
|
||||
: 'Błąd push płatności do shopPRO: ' . ($pushResult['message'] ?? 'unknown'),
|
||||
['direction' => 'push', 'target' => 'shoppro', 'ok' => $pushResult['ok']],
|
||||
'system'
|
||||
);
|
||||
} catch (\Throwable) {
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user