/
itp_practice
/
itp_backend
Обзор
Документация
Войти
/
itp_practice
/
itp_backend
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
master
app/Models/Transaction.php
128 строк
3 KB
vivanenko
fix taps and others structure
27 май 2025, 18:06
27 май 2025, 18:06
8cdc7d9
Код
Авторство
О чём код?
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Factories\HasFactory; /** * @property int $id * @property int $order_id * @property string $gateway * @property string $transaction_id * @property float $amount * @property string $currency * @property string $status * @property string|null $detail * @property string|null $token * @property \DateTime|null $processed_at * @property \DateTime $created_at * @property \DateTime $updated_at */ class Transaction extends Model { use HasFactory; protected $fillable = [ 'order_id', 'gateway', 'transaction_id', 'amount', 'currency', 'status', 'detail', 'token', 'processed_at', ]; protected $casts = [ 'order_id' => 'integer', 'gateway' => 'string', 'transaction_id' => 'string', 'amount' => 'decimal:2', 'currency' => 'string', 'status' => 'string', 'detail' => 'string', 'token' => 'string', 'processed_at' => 'datetime', ]; // Константы платежных шлюзов public const GATEWAY_FREEKASSA = 'freekassa'; public const GATEWAY_FREEKASSA_MOCK = 'freekassa_mock'; // Константы статусов транзакций public const STATUS_PENDING = 'pending'; public const STATUS_COMPLETED = 'completed'; public const STATUS_FAILED = 'failed'; public const STATUS_CANCELLED = 'cancelled'; public function order(): BelongsTo { return $this->belongsTo(Order::class); } public function isFreekassa(): bool { return $this->gateway === self::GATEWAY_FREEKASSA; } public function isFreekassaMock(): bool { return $this->gateway === self::GATEWAY_FREEKASSA_MOCK; } public function getDetailAsArray(): array { return $this->detail ? json_decode($this->detail, true) : []; } public function setDetailFromArray(array $data): self { $this->detail = json_encode($data); return $this; } // Методы для работы со статусами public function isPending(): bool { return $this->status === self::STATUS_PENDING; } public function isCompleted(): bool { return $this->status === self::STATUS_COMPLETED; } public function isFailed(): bool { return $this->status === self::STATUS_FAILED; } public function isCancelled(): bool { return $this->status === self::STATUS_CANCELLED; } public function markAsCompleted(): self { $this->status = self::STATUS_COMPLETED; $this->processed_at = now(); return $this; } public function markAsFailed(): self { $this->status = self::STATUS_FAILED; $this->processed_at = now(); return $this; } public function markAsCancelled(): self { $this->status = self::STATUS_CANCELLED; $this->processed_at = now(); return $this; } }