| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- <?php
- namespace common\models;
- use common\components\ActiveRecord;
- use common\helpers\GuestDeclarationDraftStatus;
- use Yii;
- /**
- * Durable state for an unauthenticated registration order awaiting payment.
- */
- class GuestDeclarationDraft extends ActiveRecord
- {
- public static function tableName()
- {
- return '{{%GUEST_DECLARATION_DRAFT}}';
- }
- public function rules()
- {
- return [
- [['ORDER_SN', 'ACCESS_TOKEN_HASH', 'PAYLOAD', 'ORDER_SNAPSHOT', 'STATUS', 'PAY_AMOUNT', 'CURRENCY', 'CREATED_AT', 'EXPIRES_AT'], 'required'],
- [['PAYLOAD', 'ORDER_SNAPSHOT', 'PAYMENT_NOTE'], 'string'],
- [['PAY_AMOUNT'], 'number'],
- [['CREATED_AT', 'EXPIRES_AT', 'PAID_AT', 'COMPLETED_AT'], 'integer'],
- [['STATUS'], 'in', 'range' => GuestDeclarationDraftStatus::all()],
- [['ORDER_SN'], 'string', 'max' => 32],
- [['ACCESS_TOKEN_HASH'], 'string', 'max' => 64],
- [['CURRENCY'], 'string', 'max' => 8],
- [['PAYMENT_REFERENCE'], 'string', 'max' => 128],
- [['USER_ID'], 'string', 'max' => 32],
- [['ORDER_SN'], 'unique'],
- [['ACCESS_TOKEN_HASH'], 'unique'],
- [['PAYMENT_REFERENCE'], 'unique'],
- ];
- }
- public function transitionTo(string $newStatus): bool
- {
- if (!GuestDeclarationDraftStatus::canTransition($this->STATUS, $newStatus)) {
- $this->addError('STATUS', 'Invalid guest declaration draft status transition.');
- return false;
- }
- $this->STATUS = $newStatus;
- return $this->save(false, ['STATUS', 'UPDATED_AT']);
- }
- public static function claimForProcessing(string $orderSn): bool
- {
- return self::updateAll(
- ['STATUS' => GuestDeclarationDraftStatus::PROCESSING, 'UPDATED_AT' => time()],
- [
- 'AND',
- ['ORDER_SN' => $orderSn],
- ['STATUS' => GuestDeclarationDraftStatus::PENDING_PAYMENT],
- ['>', 'EXPIRES_AT', time()],
- ]
- ) === 1;
- }
- public function accessTokenMatches(string $accessToken): bool
- {
- return $accessToken !== '' && hash_equals($this->ACCESS_TOKEN_HASH, hash('sha256', $accessToken));
- }
- public function isExpired(?int $now = null): bool
- {
- return GuestDeclarationDraftStatus::isExpired((int) $this->EXPIRES_AT, $now);
- }
- public function getPayload(): array
- {
- $payload = json_decode($this->PAYLOAD, true);
- return is_array($payload) ? $payload : [];
- }
- public function getOrderSnapshot(): array
- {
- $snapshot = json_decode($this->ORDER_SNAPSHOT, true);
- return is_array($snapshot) ? $snapshot : [];
- }
- }
|