Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Commands/RefreshBalance.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class RefreshBalance extends Command
*/
public function handle(): void
{
DB::transaction(function() {
DB::transaction(static function() {
$wallet = config('wallet.wallet.table');
$trans = config('wallet.transaction.table');

Expand Down
5 changes: 3 additions & 2 deletions src/Services/CommonService.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,9 @@ public function multiOperation(Wallet $self, array $operations): array
*/
public function assemble(array $brings): array
{
return DB::transaction(function() use ($brings) {
return $this->multiBrings($brings);
$self = $this;
return DB::transaction(static function() use ($self, $brings) {
return $self->multiBrings($brings);
});
}

Expand Down
12 changes: 7 additions & 5 deletions src/Traits/CanConfirm.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ trait CanConfirm
*/
public function confirm(Transaction $transaction): bool
{
return DB::transaction(function() use ($transaction) {
$self = $this;
return DB::transaction(static function() use ($self, $transaction) {
$wallet = app(WalletService::class)
->getWallet($this);
->getWallet($self);

if (!$wallet->refreshBalance()) {
// @codeCoverageIgnoreStart
Expand All @@ -36,7 +37,7 @@ public function confirm(Transaction $transaction): bool
);
}

return $this->forceConfirm($transaction);
return $self->forceConfirm($transaction);
});
}

Expand All @@ -61,10 +62,11 @@ public function safeConfirm(Transaction $transaction): bool
*/
public function forceConfirm(Transaction $transaction): bool
{
return DB::transaction(function() use ($transaction) {
$self = $this;
return DB::transaction(static function() use ($self, $transaction) {

$wallet = app(WalletService::class)
->getWallet($this);
->getWallet($self);

if ($transaction->confirmed) {
throw new ConfirmedInvalid(); // todo
Expand Down
4 changes: 2 additions & 2 deletions src/Traits/CanExchange.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ public function forceExchange(Wallet $to, int $amount, ?array $meta = null): Tra
*/
$from = app(WalletService::class)->getWallet($this);

return DB::transaction(function() use ($from, $to, $amount, $meta) {
return DB::transaction(static function() use ($from, $to, $amount, $meta) {
$rate = app(ExchangeService::class)->rate($from, $to);
$fee = app(WalletService::class)->fee($to, $amount);

$withdraw = app(CommonService::class)
->forceWithdraw($from, $amount + $fee, $meta);

$deposit = app(CommonService::class)
->deposit($to, $amount * $rate, $meta);

Expand Down
19 changes: 11 additions & 8 deletions src/Traits/CartPay.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@ public function payFreeCart(Cart $cart): array
app(CommonService::class)
->verifyWithdraw($this, 0);

return DB::transaction(function() use ($cart) {
$self = $this;
return DB::transaction(static function() use ($self, $cart) {
$results = [];
foreach ($cart->getItems() as $product) {
$results[] = app(CommonService::class)->forceTransfer(
$this,
$self,
$product,
0,
$product->getMetaProduct(),
Expand Down Expand Up @@ -73,12 +74,13 @@ public function payCart(Cart $cart, bool $force = null): array
throw new ProductEnded(trans('wallet::errors.product_stock'));
}

return DB::transaction(function() use ($cart, $force) {
$self = $this;
return DB::transaction(static function() use ($self, $cart, $force) {
$results = [];
foreach ($cart->getItems() as $product) {
if ($force) {
$results[] = app(CommonService::class)->forceTransfer(
$this,
$self,
$product,
$product->getAmountProduct(),
$product->getMetaProduct(),
Expand All @@ -89,7 +91,7 @@ public function payCart(Cart $cart, bool $force = null): array
}

$results[] = app(CommonService::class)->transfer(
$this,
$self,
$product,
$product->getAmountProduct(),
$product->getMetaProduct(),
Expand Down Expand Up @@ -135,12 +137,13 @@ public function safeRefundCart(Cart $cart, bool $force = null, bool $gifts = nul
*/
public function refundCart(Cart $cart, bool $force = null, bool $gifts = null): bool
{
return DB::transaction(function() use ($cart, $force, $gifts) {
$self = $this;
return DB::transaction(static function() use ($self, $cart, $force, $gifts) {
$results = [];
$transfers = $cart->alreadyBuy($this, $gifts);
$transfers = $cart->alreadyBuy($self, $gifts);
if (count($transfers) !== count($cart)) {
throw (new ModelNotFoundException())
->setModel($this->transfers()->getMorphClass());
->setModel($self->transfers()->getMorphClass());
}

foreach ($cart->getItems() as $key => $product) {
Expand Down
15 changes: 9 additions & 6 deletions src/Traits/HasWallet.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,10 @@ trait HasWallet
*/
public function deposit(int $amount, ?array $meta = null, bool $confirmed = true): Transaction
{
return DB::transaction(function() use ($amount, $meta, $confirmed) {
$self = $this;
return DB::transaction(static function() use ($self, $amount, $meta, $confirmed) {
return app(CommonService::class)
->deposit($this, $amount, $meta, $confirmed);
->deposit($self, $amount, $meta, $confirmed);
});
}

Expand Down Expand Up @@ -160,9 +161,10 @@ public function canWithdraw(int $amount): bool
*/
public function forceWithdraw(int $amount, ?array $meta = null, bool $confirmed = true): Transaction
{
return DB::transaction(function() use ($amount, $meta, $confirmed) {
$self = $this;
return DB::transaction(static function() use ($self, $amount, $meta, $confirmed) {
return app(CommonService::class)
->forceWithdraw($this, $amount, $meta, $confirmed);
->forceWithdraw($self, $amount, $meta, $confirmed);
});
}

Expand All @@ -177,9 +179,10 @@ public function forceWithdraw(int $amount, ?array $meta = null, bool $confirmed
*/
public function forceTransfer(Wallet $wallet, int $amount, ?array $meta = null): Transfer
{
return DB::transaction(function() use ($amount, $wallet, $meta) {
$self = $this;
return DB::transaction(static function() use ($self, $amount, $wallet, $meta) {
return app(CommonService::class)
->forceTransfer($this, $wallet, $amount, $meta);
->forceTransfer($self, $wallet, $amount, $meta);
});
}

Expand Down