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;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user