main
This commit is contained in:
@@ -0,0 +1,118 @@
|
||||
<?php
|
||||
|
||||
namespace Rooyesh\Wallet\interfaces;
|
||||
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Rooyesh\Wallet\models\TransactionMeta;
|
||||
use Rooyesh\Wallet\models\Wallet;
|
||||
use Rooyesh\Wallet\models\Transaction;
|
||||
use Rooyesh\Wallet\Exceptions;
|
||||
use \Illuminate\Validation\ValidationException;
|
||||
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
|
||||
|
||||
class TransactionInterface
|
||||
{
|
||||
|
||||
private $_inp;
|
||||
private $_type;
|
||||
private $wallet_interface;
|
||||
private $transaction;
|
||||
|
||||
function __construct($wallet_interface = null, $_inp = null, $_type = null,$deposit_gold = false,$deposit_cash=false, $gold_block = 0,$meta = [])
|
||||
|
||||
{
|
||||
$this->transaction = TransactionInterface::apply_transaction($wallet_interface, $_inp, $_type,$deposit_gold ,$deposit_cash,$gold_block,$meta);
|
||||
}
|
||||
|
||||
static function validate_last_transaction($wallet_id)
|
||||
{
|
||||
// $last_transaction = Transaction::where('wallet', '=', $wallet_id)->orderBy('created_at', 'DESC')->first();
|
||||
// if ($last_transaction and $last_transaction->hash !== TransactionInterface::generate_hash($last_transaction)) {
|
||||
// throw new Exceptions\LastTransactionValidateFailed();
|
||||
// }
|
||||
}
|
||||
|
||||
private static function generate_hash($transaction)
|
||||
{
|
||||
$last_transaction_hash = '';
|
||||
$last_user_transaction = Transaction::where('wallet', '=', $transaction->wallet)->orderBy('created_at', 'DESC');
|
||||
if ($last_user_transaction->count() and $last_user_transaction->first() != $transaction) {
|
||||
$last_transaction_hash = $last_user_transaction->first()->hash;
|
||||
}
|
||||
return hash('sha256',
|
||||
hash('sha256', $transaction->gold) .
|
||||
hash('sha256', $transaction->cash) .
|
||||
hash('sha256', $transaction->wallet) .
|
||||
"yhoenszeaf" . $last_transaction_hash
|
||||
);
|
||||
}
|
||||
|
||||
private static function set_hash($transaction)
|
||||
{
|
||||
$transaction->hash = TransactionInterface::generate_hash($transaction);
|
||||
$transaction->save();
|
||||
}
|
||||
|
||||
public static function apply_transaction($wallet_interface, $_inp, $_type,$deposit_gold,$deposit_cash,$gold_block, $meta = [])
|
||||
{
|
||||
$wallet = $wallet_interface->wallet;
|
||||
TransactionInterface::validate_last_transaction($wallet->id);
|
||||
#-------------Check Balance --------
|
||||
if (!$deposit_gold) {
|
||||
// if ($_type === 1 && $wallet->cash < $_inp['value']) {
|
||||
// throw new Exceptions\NotEnoughCashBalance();
|
||||
//
|
||||
// }
|
||||
if ($_type === 2 && $wallet->gold < $_inp['gold']) {
|
||||
throw new UnprocessableEntityHttpException('مقدار وارد شده کمتر از موجودی شماست');
|
||||
|
||||
}
|
||||
}
|
||||
// $_inp['value'] = abs($_inp['value']) * ($deposit ? 1 : -1);
|
||||
if (isset($_inp['gold'])){
|
||||
$_inp['gold'] = abs($_inp['gold']) * ($deposit_gold ? 1 : -1);
|
||||
$wallet->gold += $_inp['gold'];
|
||||
$_inp['balance_gold'] = $wallet->gold;
|
||||
}
|
||||
if (isset($_inp['cash'])){
|
||||
$_inp['cash'] = abs($_inp['cash']) * ($deposit_cash ? 1 : -1);
|
||||
$wallet->cash += $_inp['cash'];
|
||||
$_inp['balance_cash'] = $wallet->cash;
|
||||
}
|
||||
if($gold_block != 0){
|
||||
$wallet->gold_block += $gold_block;
|
||||
}
|
||||
$_inp['wallet'] = $wallet->id;
|
||||
$_inp['type'] = $_type;
|
||||
// $_inp['transaction_type'] = $_type;
|
||||
$_inp['customer_id'] = $wallet->owner;
|
||||
// $_inp['balance'] = $wallet->cash + $_inp['cash'];
|
||||
$transaction_validate_rules = [
|
||||
'wallet' => 'required',
|
||||
'type' => 'required',
|
||||
'expire_at' => 'date',
|
||||
'cash' => 'integer',
|
||||
'note' => 'string',
|
||||
'balance' => 'integer',
|
||||
];
|
||||
$transaction_validator = Validator::make($_inp, $transaction_validate_rules);
|
||||
if ($transaction_validator->failed()) {
|
||||
throw ValidationException::withMessages($transaction_validator->errors()->all());
|
||||
}
|
||||
$transaction = Transaction::create($_inp);
|
||||
$transaction->save();
|
||||
TransactionInterface::set_hash($transaction);
|
||||
$meta['transaction'] = $transaction->id;
|
||||
$transaction_metas = TransactionMeta::create($meta);
|
||||
|
||||
// if ($_inp['type'] == 1) {
|
||||
// $wallet->cash += $_inp['value'];
|
||||
// } elseif ($_inp['type'] == 2) {
|
||||
// $wallet->gold += $_inp['value'];
|
||||
// }
|
||||
$wallet->save();
|
||||
$wallet_interface->set_hash();
|
||||
return $transaction;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user