Skip to content

Commit d8fe430

Browse files
committed
[10.x] Added a receiving wallet to the transfer code
1 parent ded0b04 commit d8fe430

File tree

4 files changed

+61
-14
lines changed

4 files changed

+61
-14
lines changed

src/Services/EagerLoaderService.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ public function loadWalletsByBasket(Customer $customer, BasketDtoInterface $bask
2626
/** @var array<array-key, array<array-key, int|string>> $productGroupIds */
2727
$productGroupIds = [];
2828
foreach ($basketDto->items() as $index => $item) {
29+
// If the wallet is installed, then there is no need for lazy loading
30+
if ($item->getReceiving() !== null) {
31+
continue;
32+
}
33+
2934
$model = $this->castService->getModel($item->getProduct());
3035
if (! $model->relationLoaded('wallet')) {
3136
$products[$index] = $item->getProduct();

src/Services/PrepareService.php

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Bavix\Wallet\Internal\Dto\TransferLazyDtoInterface;
1515
use Bavix\Wallet\Internal\Service\MathServiceInterface;
1616
use Bavix\Wallet\Models\Transaction;
17+
use Bavix\Wallet\Models\Wallet as WalletModel;
1718

1819
/**
1920
* @internal
@@ -89,21 +90,39 @@ public function transferLazy(
8990
string $status,
9091
float|int|string $amount,
9192
ExtraDtoInterface|array|null $meta = null
93+
): TransferLazyDtoInterface {
94+
return $this->transferExtraLazy(
95+
$from,
96+
$this->castService->getWallet($from),
97+
$to,
98+
$this->castService->getWallet($to),
99+
$status,
100+
$amount,
101+
$meta
102+
);
103+
}
104+
105+
public function transferExtraLazy(
106+
Wallet $from,
107+
WalletModel $fromWallet,
108+
Wallet $to,
109+
WalletModel $toWallet,
110+
string $status,
111+
float|int|string $amount,
112+
ExtraDtoInterface|array|null $meta = null
92113
): TransferLazyDtoInterface {
93114
$discount = $this->personalDiscountService->getDiscount($from, $to);
94-
$toWallet = $this->castService->getWallet($to);
95-
$from = $this->castService->getWallet($from);
96115
$fee = $this->taxService->getFee($to, $amount);
97116

98117
$amountWithoutDiscount = $this->mathService->sub($amount, $discount, $toWallet->decimal_places);
99118
$depositAmount = $this->mathService->compare($amountWithoutDiscount, 0) === -1 ? '0' : $amountWithoutDiscount;
100-
$withdrawAmount = $this->mathService->add($depositAmount, $fee, $from->decimal_places);
119+
$withdrawAmount = $this->mathService->add($depositAmount, $fee, $fromWallet->decimal_places);
101120
$extra = $this->extraDtoAssembler->create($meta);
102121
$withdrawOption = $extra->getWithdrawOption();
103122
$depositOption = $extra->getDepositOption();
104123

105124
$withdraw = $this->withdraw(
106-
$from,
125+
$fromWallet,
107126
$withdrawAmount,
108127
$withdrawOption->getMeta(),
109128
$withdrawOption->isConfirmed(),
@@ -119,7 +138,7 @@ public function transferLazy(
119138
);
120139

121140
return $this->transferLazyDtoAssembler->create(
122-
$from,
141+
$fromWallet,
123142
$toWallet,
124143
$discount,
125144
$fee,

src/Services/PrepareServiceInterface.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Bavix\Wallet\Interfaces\Wallet;
1010
use Bavix\Wallet\Internal\Dto\TransactionDtoInterface;
1111
use Bavix\Wallet\Internal\Dto\TransferLazyDtoInterface;
12+
use Bavix\Wallet\Models\Wallet as WalletModel;
1213

1314
/**
1415
* @api
@@ -53,4 +54,19 @@ public function transferLazy(
5354
float|int|string $amount,
5455
ExtraDtoInterface|array|null $meta = null
5556
): TransferLazyDtoInterface;
57+
58+
/**
59+
* @param ExtraDtoInterface|array<mixed>|null $meta
60+
*
61+
* @throws AmountInvalid
62+
*/
63+
public function transferExtraLazy(
64+
Wallet $from,
65+
WalletModel $fromWallet,
66+
Wallet $to,
67+
WalletModel $toWallet,
68+
string $status,
69+
float|int|string $amount,
70+
ExtraDtoInterface|array|null $meta = null
71+
): TransferLazyDtoInterface;
5672
}

src/Traits/CartPay.php

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,21 @@ public function payFreeCart(CartInterface $cart): array
6565
app(ConsistencyServiceInterface::class)->checkPotential($this, 0, true);
6666

6767
$transfers = [];
68+
$castService = app(CastServiceInterface::class);
6869
$prepareService = app(PrepareServiceInterface::class);
6970
$assistantService = app(AssistantServiceInterface::class);
70-
foreach ($basketDto->cursor() as $product) {
71-
$transfers[] = $prepareService->transferLazy(
72-
$this,
73-
$product,
74-
Transfer::STATUS_PAID,
75-
0,
76-
$assistantService->getMeta($basketDto, $product)
77-
);
71+
foreach ($basketDto->items() as $item) {
72+
foreach ($item->getItems() as $product) {
73+
$transfers[] = $prepareService->transferExtraLazy(
74+
$this,
75+
$castService->getWallet($this),
76+
$product,
77+
$castService->getWallet($item->getReceiving() ?? $product),
78+
Transfer::STATUS_PAID,
79+
0,
80+
$assistantService->getMeta($basketDto, $product)
81+
);
82+
}
7883
}
7984

8085
assert($transfers !== []);
@@ -134,9 +139,11 @@ public function payCart(CartInterface $cart, bool $force = false): array
134139
$pricePerItem = $prices[$productId];
135140
}
136141

137-
$transfers[] = $prepareService->transferLazy(
142+
$transfers[] = $prepareService->transferExtraLazy(
138143
$this,
144+
$castService->getWallet($this),
139145
$product,
146+
$castService->getWallet($item->getReceiving() ?? $product),
140147
Transfer::STATUS_PAID,
141148
$pricePerItem,
142149
$assistantService->getMeta($basketDto, $product)

0 commit comments

Comments
 (0)