Files
boursecu_server/packages/rooyesh/wallet/src/interfaces/WalletInterface.php
T

73 lines
2.2 KiB
PHP
Raw Normal View History

2026-01-24 14:03:11 +00:00
<?php
namespace Rooyesh\Wallet\interfaces;
use Rooyesh\Wallet\models\Wallet;
use Rooyesh\Wallet\models\Transaction;
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
class WalletInterface
{
public $wallet = null;
public $owner_id = null;
public $owner_type = null;
public $fund_id = null;
public $product_id = null;
function __construct($owner_id, $owner_type, $fund_id=null,$product_id=null)
{
$this->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;
}
}