owner_id = $owner_id; $this->owner_type = $owner_type; if (wallet::where([ ['owner', '=', $owner_id], ['owner_type', '=', $owner_type], ['fund_id', '=', $fund_id], ['product_id', '=', $product_id], ])->count()) { $this->wallet = wallet::where([ ['owner', '=', $owner_id], ['owner_type', '=', $owner_type], ['fund_id', '=', $fund_id], ['product_id', '=', $product_id], ])->first(); $this->validate(); } else { $this->wallet = Wallet::create([ 'owner' => $owner_id, 'owner_type' => $owner_type, 'gold' => 0, 'cash' => 0, 'fund_id'=>$fund_id, 'product_id'=>$product_id ]); $this->set_hash(); } } public function validate(): bool { // if ($this->wallet->hash !== $this->generate_hash()) { // throw new UnprocessableEntityHttpException('امکان شارز این کیف پول وجود ندارد'); // } return true; } private function generate_hash(): string { $last_transaction = Transaction::where('wallet', '=', $this->wallet->id)->orderBy('created_at', 'DESC')->first(); $last_transaction_hash = $last_transaction ? hash('sha256', $last_transaction->hash) : ''; return hash('sha256', hash('sha256', $this->wallet->gold) . hash('sha256', $this->wallet->cash) . hash('sha256', $this->wallet->owner) . $last_transaction_hash . "yhoenszeaf"); } public function set_hash(): bool { $this->wallet->hash = $this->generate_hash(); $this->wallet->save(); return true; } }