Skip to content

Commit 366ce04

Browse files
committed
[10.x] fix refund
1 parent 022eb7a commit 366ce04

File tree

8 files changed

+177
-12
lines changed

8 files changed

+177
-12
lines changed

phpstan.tests.baseline.neon

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,8 @@ parameters:
1414
message: "#^Method Bavix\\\\Wallet\\\\Test\\\\Infra\\\\TestCase\\:\\:setUp\\(\\) is not final, but since the containing class is abstract, it should be\\.$#"
1515
count: 1
1616
path: tests/Infra/TestCase.php
17+
18+
-
19+
message: "#^Call to an undefined method Bavix\\\\Wallet\\\\Interfaces\\\\ProductInterface\\:\\:getWallet\\(\\)\\.$#"
20+
count: 1
21+
path: tests/Units/Domain/CartReceivingTest.php

src/Services/EagerLoaderService.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function loadWalletsByBasket(Customer $customer, BasketDtoInterface $bask
2727
$productGroupIds = [];
2828
foreach ($basketDto->items() as $index => $item) {
2929
// If the wallet is installed, then there is no need for lazy loading
30-
if ($item->getReceiving() !== null) {
30+
if ($item->getReceiving() instanceof \Bavix\Wallet\Interfaces\Wallet) {
3131
continue;
3232
}
3333

src/Services/PurchaseService.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function already(Customer $customer, BasketDtoInterface $basketDto, bool
2929
$productCounts = [];
3030
$query = $customer->transfers();
3131
foreach ($basketDto->items() as $itemDto) {
32-
$wallet = $this->castService->getWallet($itemDto->getProduct());
32+
$wallet = $this->castService->getWallet($itemDto->getReceiving() ?? $itemDto->getProduct());
3333
$wallets[$wallet->uuid] = $wallet;
3434

3535
$productCounts[$wallet->uuid] = ($productCounts[$wallet->uuid] ?? 0) + count($itemDto);

src/Traits/CartPay.php

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -212,19 +212,24 @@ public function refundCart(CartInterface $cart, bool $force = false, bool $gifts
212212
$objects = [];
213213
$transferIds = [];
214214
$transfers = array_values($transfers);
215+
$castService = app(CastServiceInterface::class);
215216
$prepareService = app(PrepareServiceInterface::class);
216217
$assistantService = app(AssistantServiceInterface::class);
217-
foreach ($basketDto->cursor() as $product) {
218-
$transferIds[] = $transfers[$index]->getKey();
219-
$objects[] = $prepareService->transferLazy(
220-
$product,
221-
$transfers[$index]->withdraw->wallet,
222-
Transfer::STATUS_TRANSFER,
223-
$transfers[$index]->deposit->amount,
224-
$assistantService->getMeta($basketDto, $product)
225-
);
218+
foreach ($basketDto->items() as $itemDto) {
219+
foreach ($itemDto->getItems() as $product) {
220+
$transferIds[] = $transfers[$index]->getKey();
221+
$objects[] = $prepareService->transferExtraLazy(
222+
$product,
223+
$castService->getWallet($itemDto->getReceiving() ?? $product),
224+
$transfers[$index]->withdraw->wallet,
225+
$transfers[$index]->withdraw->wallet,
226+
Transfer::STATUS_TRANSFER,
227+
$transfers[$index]->deposit->amount,
228+
$assistantService->getMeta($basketDto, $product)
229+
);
226230

227-
++$index;
231+
++$index;
232+
}
228233
}
229234

230235
if (! $force) {

tests/Infra/Models/Buyer.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Bavix\Wallet\Interfaces\Customer;
88
use Bavix\Wallet\Traits\CanPay;
9+
use Bavix\Wallet\Traits\HasWallets;
910
use Illuminate\Database\Eloquent\Model;
1011

1112
/**
@@ -17,6 +18,7 @@
1718
final class Buyer extends Model implements Customer
1819
{
1920
use CanPay;
21+
use HasWallets;
2022

2123
public function getTable(): string
2224
{

tests/Infra/Models/Item.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Bavix\Wallet\Services\CastServiceInterface;
1313
use Bavix\Wallet\Test\Infra\Helpers\Config;
1414
use Bavix\Wallet\Traits\HasWallet;
15+
use Bavix\Wallet\Traits\HasWallets;
1516
use Illuminate\Database\Eloquent\Model;
1617
use Illuminate\Database\Eloquent\Relations\MorphMany;
1718

@@ -25,6 +26,7 @@
2526
final class Item extends Model implements ProductLimitedInterface
2627
{
2728
use HasWallet;
29+
use HasWallets;
2830

2931
/**
3032
* @var string[]

tests/Infra/Models/ItemMeta.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Bavix\Wallet\Models\Wallet;
1010
use Bavix\Wallet\Services\CastService;
1111
use Bavix\Wallet\Traits\HasWallet;
12+
use Bavix\Wallet\Traits\HasWallets;
1213
use Illuminate\Database\Eloquent\Model;
1314

1415
/**
@@ -21,6 +22,7 @@
2122
final class ItemMeta extends Model implements ProductLimitedInterface
2223
{
2324
use HasWallet;
25+
use HasWallets;
2426

2527
/**
2628
* @var string[]
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Bavix\Wallet\Test\Units\Domain;
6+
7+
use Bavix\Wallet\Interfaces\ProductLimitedInterface;
8+
use Bavix\Wallet\Interfaces\Wallet;
9+
use Bavix\Wallet\Models\Transfer;
10+
use Bavix\Wallet\Objects\Cart;
11+
use Bavix\Wallet\Services\PurchaseServiceInterface;
12+
use Bavix\Wallet\Test\Infra\Factories\BuyerFactory;
13+
use Bavix\Wallet\Test\Infra\Factories\ItemFactory;
14+
use Bavix\Wallet\Test\Infra\Factories\ItemMetaFactory;
15+
use Bavix\Wallet\Test\Infra\Models\Buyer;
16+
use Bavix\Wallet\Test\Infra\Models\Item;
17+
use Bavix\Wallet\Test\Infra\Models\ItemMeta;
18+
use Bavix\Wallet\Test\Infra\PackageModels\Transaction;
19+
use Bavix\Wallet\Test\Infra\TestCase;
20+
use Bavix\Wallet\Traits\HasWallets;
21+
use function count;
22+
use Illuminate\Database\Eloquent\Collection;
23+
24+
/**
25+
* @internal
26+
*/
27+
final class CartReceivingTest extends TestCase
28+
{
29+
public function testCartMeta(): void
30+
{
31+
/** @var Buyer $buyer */
32+
$buyer = BuyerFactory::new()->create();
33+
/** @var ItemMeta $product */
34+
$product = ItemMetaFactory::new()->create([
35+
'quantity' => 1,
36+
]);
37+
38+
$expected = 'pay';
39+
40+
$payment = $buyer->createWallet([
41+
'name' => 'Dollar',
42+
'meta' => [
43+
'currency' => 'USD',
44+
],
45+
]);
46+
47+
$receiving = $product->createWallet([
48+
'name' => 'Dollar',
49+
'meta' => [
50+
'currency' => 'USD',
51+
],
52+
]);
53+
54+
$cart = app(Cart::class)
55+
->withItem($product, receiving: $receiving)
56+
->withMeta([
57+
'type' => $expected,
58+
])
59+
;
60+
61+
$amount = $cart->getTotal($buyer);
62+
63+
self::assertSame(0, $buyer->balanceInt);
64+
self::assertNotNull($payment->deposit($amount));
65+
66+
$transfers = $payment->payCart($cart);
67+
self::assertCount(1, $transfers);
68+
69+
$transfer = current($transfers);
70+
71+
/** @var Transaction[] $transactions */
72+
$transactions = [$transfer->deposit, $transfer->withdraw];
73+
foreach ($transactions as $transaction) {
74+
self::assertSame($product->price, $transaction->meta['price']);
75+
self::assertSame($product->name, $transaction->meta['name']);
76+
self::assertSame($expected, $transaction->meta['type']);
77+
}
78+
79+
self::assertSame((int) $amount, $receiving->balanceInt);
80+
self::assertSame('USD', $receiving->currency);
81+
82+
self::assertSame(0, $payment->balanceInt);
83+
self::assertSame('USD', $payment->currency);
84+
}
85+
86+
public function testPay(): void
87+
{
88+
/** @var Buyer $buyer */
89+
$buyer = BuyerFactory::new()->create();
90+
/** @var Collection<int, Item> $products */
91+
$products = ItemFactory::times(10)->create([
92+
'quantity' => 1,
93+
]);
94+
95+
$cart = app(Cart::class);
96+
foreach ($products as $product) {
97+
$receiving = $product->createWallet([
98+
'name' => 'Dollar',
99+
'meta' => [
100+
'currency' => 'USD',
101+
],
102+
]);
103+
104+
$cart = $cart->withItem($product, pricePerItem: 1, receiving: $receiving);
105+
}
106+
107+
self::assertCount(10, $cart->getItems());
108+
109+
foreach ($cart->getItems() as $product) {
110+
self::assertSame(0, $product->getWallet('dollar')?->balanceInt);
111+
}
112+
113+
$payment = $buyer->createWallet([
114+
'name' => 'Dollar',
115+
'meta' => [
116+
'currency' => 'USD',
117+
],
118+
]);
119+
120+
$payment->deposit($cart->getTotal($buyer));
121+
122+
self::assertSame(10, $payment->balanceInt);
123+
124+
$transfers = $payment->payCart($cart);
125+
126+
self::assertCount(count($cart), $transfers);
127+
self::assertTrue((bool) app(PurchaseServiceInterface::class)->already($payment, $cart->getBasketDto()));
128+
self::assertSame(0, $payment->balanceInt);
129+
130+
foreach ($transfers as $transfer) {
131+
self::assertSame(Transfer::STATUS_PAID, $transfer->status);
132+
self::assertNull($transfer->status_last);
133+
}
134+
135+
foreach ($cart->getItems() as $product) {
136+
/** @var Item $product */
137+
self::assertSame(1, $product->getWallet('dollar')?->balanceInt);
138+
}
139+
140+
self::assertTrue($payment->refundCart($cart));
141+
foreach ($transfers as $transfer) {
142+
$transfer->refresh();
143+
self::assertSame(Transfer::STATUS_REFUND, $transfer->status);
144+
self::assertSame(Transfer::STATUS_PAID, $transfer->status_last);
145+
}
146+
147+
self::assertSame(10, $payment->balanceInt);
148+
}
149+
}

0 commit comments

Comments
 (0)