Код: Выделить всё
casino_bakiye) и xpgtransactions , где я сохраняю транзакции, поступающие из API. In the request, there is amount and username among the data sent.
So what I am to do:
[*]Get the user from the users table with the username that comes with the request
[*]Get that user's balance
уменьшение этого баланса < /p>
< /li>
возвращает ответ с текущим балансом этого пользователя. Баланс составляет 1000, а запрос возвращает сумму в 100, мне нужно уменьшить этот баланс на 1000 на 100 и вернуть полученную сумму в ответе. Это означает, что 1000 - 100 = 900, поэтому я должен вернуть 900, то есть после уменьшения баланса этого пользователя на 100. Затем сохранить сумму транзакции в XPGTransactions Таблица.
[b] Проблема [/b]
Теперь проблема заключается в том, что с несколькими транзакциями, логика уменьшения не происходит. Это просто, но сохраняет транзакцию в таблице XPGTransactions . Поэтому мне интересно, как он пропускает уменьшение количества, и сохраняет данные в XPGTransactions
$data = json_decode(file_get_contents('php://input'), true);
$username = $data['Login'];
$user = User::where('username', $username)->first();
if(!$user) {
$rivalaoResponse = ["d"=>["ErrorCode"=>-10,
"HasErrors"=>true,
"Message"=>"InvalidPlayer"]];
return response()->json($rivalaoResponse);
}
$type="debit";
$userId = $user->id;
$amount = $data['Amount'];
$gameId = $data['GameId'];
$roundId = $data['RoundId'];
$sequence = $data['Sequence'];
$game = XpgTransaction::where('gameId', $gameId)
->where('roundId', $roundId)
->where('sequence', $sequence)
->first();
DB::beginTransaction();
try {
if (!$game) {
$trxn = new XpgTransaction;
$trxn->user_id = $userId;
$trxn ->request=json_encode($data);
$trxn->type =$type;
$trxn->save();
if ($user->decrement('casino_bakiye', $amount)) {
$trxn->casino_balance = $user->refresh()->casino_bakiye;
$trxn->save();
DB::commit();
$currentBalance = $user->refresh()->casino_bakiye;
$rivalaoResponse = ["d"=>["Data"=>[(float)$currentBalance],
"ErrorCode"=>0,
"HasErrors"=>false,
"Message"=>""]];
return response()->json($rivalaoResponse);
} else {
$currentBalance = $user->refresh()->casino_bakiye;
$rivalaoResponse = ["d"=>["Data"=>
[(float)$currentBalance],
"ErrorCode"=>-1,
"HasErrors"=>true,
"Message"=>"Unknown error"]
];
return response()->json($rivalaoResponse);
DB::rollBack();
}
} else {
$rivalaoResponse = ["d"=>["ErrorCode"=>-21,
"HasErrors"=>true,
"Message"=>"Duplicate transaction"]
];
return response()->json($rivalaoResponse);
}
} catch (\Exception $e) {
$currentBalance = $user->refresh()->casino_bakiye;
$rivalaoResponse = ["d"=>["Data"=>[(float)$currentBalance],
"ErrorCode"=>-1,
"HasErrors"=>true,
"Message"=>"Unknown error"]
];
return response()->json($rivalaoResponse);
DB::rollBack();
}
}
Подробнее здесь: https://stackoverflow.com/questions/641 ... -the-other