改进 扣费 API
This commit is contained in:
parent
1539daa106
commit
bfdd436332
@ -88,7 +88,7 @@ public function update(Request $request, User $user): JsonResponse
|
|||||||
return $this->error('用户余额不足。');
|
return $this->error('用户余额不足。');
|
||||||
}
|
}
|
||||||
|
|
||||||
$user->reduce($balance, $request->description, true);
|
$trans = $user->reduce($balance, $request->description, true);
|
||||||
$module->charge($balance, 'balance', $request->description, [
|
$module->charge($balance, 'balance', $request->description, [
|
||||||
'user_id' => $user->id,
|
'user_id' => $user->id,
|
||||||
]);
|
]);
|
||||||
@ -101,11 +101,12 @@ public function update(Request $request, User $user): JsonResponse
|
|||||||
'user_id' => $user->id,
|
'user_id' => $user->id,
|
||||||
'payment' => 'module_balance',
|
'payment' => 'module_balance',
|
||||||
]);
|
]);
|
||||||
|
$trans = $user->charge($balance, 'module_balance', $request->description, [
|
||||||
$user->charge($balance, 'module_balance', $request->description);
|
'module_id' => $module->id
|
||||||
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->updated();
|
return $this->success($trans);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function auth($token): JsonResponse
|
public function auth($token): JsonResponse
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
|
|
||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
use Jenssegers\Mongodb\Eloquent\Model;
|
use Jenssegers\Mongodb\Eloquent\Model;
|
||||||
|
use Symfony\Component\Uid\Ulid;
|
||||||
|
|
||||||
class Transaction extends Model
|
class Transaction extends Model
|
||||||
{
|
{
|
||||||
|
@ -170,15 +170,16 @@ public function startTransfer(self $to, string $amount, string|null $description
|
|||||||
* @param string $description
|
* @param string $description
|
||||||
* @param bool $fail
|
* @param bool $fail
|
||||||
* @param array $options
|
* @param array $options
|
||||||
* @return string
|
*
|
||||||
|
* @return Transaction
|
||||||
*/
|
*/
|
||||||
public function reduce(string|null $amount = '0', string $description = '消费', bool $fail = false, array $options = []): string
|
public function reduce(string|null $amount = '0', string $description = '消费', bool $fail = false, array $options = []): Transaction
|
||||||
{
|
{
|
||||||
if ($amount === null || $amount === '') {
|
if ($amount === null || $amount === '') {
|
||||||
return $this->balance;
|
return $this->balance;
|
||||||
}
|
}
|
||||||
|
|
||||||
Cache::lock('user_balance_'.$this->id, 10)->block(10, function () use ($amount, $fail, $description, $options) {
|
return Cache::lock('user_balance_'.$this->id, 10)->block(10, function () use ($amount, $fail, $description, $options) {
|
||||||
$this->refresh();
|
$this->refresh();
|
||||||
|
|
||||||
if ($this->balance < $amount) {
|
if ($this->balance < $amount) {
|
||||||
@ -202,12 +203,10 @@ public function reduce(string|null $amount = '0', string $description = '消费'
|
|||||||
$data = array_merge($data, $options);
|
$data = array_merge($data, $options);
|
||||||
}
|
}
|
||||||
|
|
||||||
(new Transaction)->create($data);
|
|
||||||
|
|
||||||
broadcast(new Users($this, 'balances.amount.reduced', $this));
|
broadcast(new Users($this, 'balances.amount.reduced', $this));
|
||||||
});
|
|
||||||
|
|
||||||
return $this->balance;
|
return (new Transaction)->create($data);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -217,15 +216,16 @@ public function reduce(string|null $amount = '0', string $description = '消费'
|
|||||||
* @param string $payment
|
* @param string $payment
|
||||||
* @param string $description
|
* @param string $description
|
||||||
* @param array $options
|
* @param array $options
|
||||||
* @return string
|
*
|
||||||
|
* @return Transaction
|
||||||
*/
|
*/
|
||||||
public function charge(string|null $amount = '0', string $payment = 'console', string $description = '充值', array $options = []): string
|
public function charge(string|null $amount = '0', string $payment = 'console', string $description = '充值', array $options = []): Transaction
|
||||||
{
|
{
|
||||||
if ($amount === null || $amount === '') {
|
if ($amount === null || $amount === '') {
|
||||||
return $this->balance;
|
return $this->balance;
|
||||||
}
|
}
|
||||||
|
|
||||||
Cache::lock('user_balance_'.$this->id, 10)->block(10, function () use ($amount, $description, $payment, $options) {
|
return Cache::lock('user_balance_'.$this->id, 10)->block(10, function () use ($amount, $description, $payment, $options) {
|
||||||
$this->refresh();
|
$this->refresh();
|
||||||
$this->balance = bcadd($this->balance, $amount, 4);
|
$this->balance = bcadd($this->balance, $amount, 4);
|
||||||
$this->save();
|
$this->save();
|
||||||
@ -242,8 +242,6 @@ public function charge(string|null $amount = '0', string $payment = 'console', s
|
|||||||
$data = array_merge($data, $options);
|
$data = array_merge($data, $options);
|
||||||
}
|
}
|
||||||
|
|
||||||
(new Transaction)->create($data);
|
|
||||||
|
|
||||||
if (isset($options['add_balances_log']) && $options['add_balances_log'] === true) {
|
if (isset($options['add_balances_log']) && $options['add_balances_log'] === true) {
|
||||||
(new Balance)->create([
|
(new Balance)->create([
|
||||||
'user_id' => $this->id,
|
'user_id' => $this->id,
|
||||||
@ -253,9 +251,9 @@ public function charge(string|null $amount = '0', string $payment = 'console', s
|
|||||||
'paid_at' => now(),
|
'paid_at' => now(),
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
|
||||||
return $this->balance;
|
return (new Transaction)->create($data);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getCostPrice(string $price): string
|
public function getCostPrice(string $price): string
|
||||||
|
Loading…
Reference in New Issue
Block a user