bavix / laravel-wallet

It's easy to work with a virtual wallet
https://bavix.github.io/laravel-wallet/
MIT License
1.15k stars 230 forks source link

Unable to perform refund with wallet version 9.6 #597

Closed HilalLko closed 2 years ago

HilalLko commented 2 years ago

Describe the bug Whenever am trying to refund an order (Purchase) am getting the error below:

Bavix\Wallet\Internal\Exceptions\TransactionFailedException

Transaction failed. Message: SQLSTATE[21S01]: Insert value list does not match column list: 1136 Column count doesn't match value count at row 2

insert into `transactions` (`amount`, `confirmed`, `created_at`, `meta`, `payable_id`, `payable_type`, `type`, `updated_at`, `uuid`, `wallet_id`) 
values (-1150.00, 1, 2022-11-07 06:00:41, {"title":"Order from B2C-pickup::CO0057","description":"Purchase of Order #CO0057"}, 369, App\Models\Order, withdraw, 2022-11-07 06:00:41, 413b7a9d-1abe-49a6-baa1-35a34093bd05, 874), 
(1150.00, 0, 1, 2022-11-07 06:00:41, 2023-11-07 06:00:41, {"title":"Order from B2C-pickup::CO0057","description":"Purchase of Order #CO0057"}, 1037, App\Models\User, deposit, 2022-11-07 06:00:41, bc76ac59-400b-44c5-b6ab-a4368075517d, 470)

Am using below to refund a purchase.

$order=Order::find(369);
$ouser = User::without(['role','wallet','lastTransaction','defaultAddress'])->with('wallets')->find($order->user_id);
$walletFull = $ouser->wallets[0];
$walletZero = $ouser->wallets[1];
$fullValue = $order->full_credit_used ?? 0;
$zeroValue = $order->zero_credit_used ?? 0;
$orderWallet = $order->wallet;
if($walletFull->refund($order)) {
    $walletFull->refreshBalance();
    if ($zeroValue > 0) {                
        $walletFull->transferFloat($walletZero, $zeroValue);
        $walletFull->refreshBalance();
        $walletZero->refreshBalance();
        $this->manageLastTransaction($walletZero,$this->order);
    }
    $this->manageLastTransaction($walletFull,$this->order,$fullValue);
}

Furthermore, a transfer is also giving the same error, i.e, "1136 Column count doesn't match value count at row 2", Transfer logic is stated below

$metaDeposit =  [
          'description' => 'Refund from Order #'.$this->order->order_number,
          'credit_type' => 'Refunded credits',
          'expire_on'   => Carbon::now()->addYear()
      ];
  $metaWithdraw =  [
          'description' => 'Refund for Order #'.$this->order->order_number,
      ];
  $transferMeta = new Extra(
      deposit: $metaDeposit,
      withdraw: new Option(meta: $metaWithdraw)
  );
  $orderWallet->transfer($walletFull, $fullValue, $transferMeta);

Trace Error

[2022-11-07 06:34:33] local.ERROR: Transaction failed. Message: SQLSTATE[21S01]: Insert value list does not match column list: 1136 Column count doesn't match value count at row 2 (SQL: insert into `transactions` (`amount`, `confirmed`, `created_at`, `meta`, `payable_id`, `payable_type`, `type`, `updated_at`, `uuid`, `wallet_id`) values (-1150.00, 1, 2022-11-07 06:34:33, {"title":"Order from B2C-pickup::CO0057","description":"Purchase of Order #CO0057"}, 369, App\Models\Order, withdraw, 2022-11-07 06:34:33, 5240b919-5bb9-4448-976b-828c532eb09a, 874), (1150.00, 0, 1, 2022-11-07 06:34:33, 2023-11-07 06:34:33, {"title":"Order from B2C-pickup::CO0057","description":"Purchase of Order #CO0057"}, 1037, App\Models\User, deposit, 2022-11-07 06:34:33, a90558e5-6234-47ce-a3f9-2daa8d790c2d, 470)) {"exception":"[object] (Bavix\\Wallet\\Internal\\Exceptions\\TransactionFailedException(code: 1024): Transaction failed. Message: SQLSTATE[21S01]: Insert value list does not match column list: 1136 Column count doesn't match value count at row 2 (SQL: insert into `transactions` (`amount`, `confirmed`, `created_at`, `meta`, `payable_id`, `payable_type`, `type`, `updated_at`, `uuid`, `wallet_id`) values (-1150.00, 1, 2022-11-07 06:34:33, {\"title\":\"Order from B2C-pickup::CO0057\",\"description\":\"Purchase of Order #CO0057\"}, 369, App\\Models\\Order, withdraw, 2022-11-07 06:34:33, 5240b919-5bb9-4448-976b-828c532eb09a, 874), (1150.00, 0, 1, 2022-11-07 06:34:33, 2023-11-07 06:34:33, {\"title\":\"Order from B2C-pickup::CO0057\",\"description\":\"Purchase of Order #CO0057\"}, 1037, App\\Models\\User, deposit, 2022-11-07 06:34:33, a90558e5-6234-47ce-a3f9-2daa8d790c2d, 470)) at /var/www/html/better_cloud/vendor/bavix/laravel-wallet/src/Internal/Service/DatabaseService.php:47)
[stacktrace]
#0 /var/www/html/better_cloud/vendor/bavix/laravel-wallet/src/Services/TransferService.php(107): Bavix\\Wallet\\Internal\\Service\\DatabaseService->transaction()
#1 /var/www/html/better_cloud/vendor/bavix/laravel-wallet/src/Traits/CartPay.php(235): Bavix\\Wallet\\Services\\TransferService->apply()
#2 /var/www/html/better_cloud/vendor/bavix/laravel-wallet/src/Internal/Service/DatabaseService.php(34): Bavix\\Wallet\\Models\\Wallet->Bavix\\Wallet\\Traits\\{closure}()
#3 /var/www/html/better_cloud/vendor/laravel/framework/src/Illuminate/Database/Concerns/ManagesTransactions.php(30): Bavix\\Wallet\\Internal\\Service\\DatabaseService->Bavix\\Wallet\\Internal\\Service\\{closure}()
#4 /var/www/html/better_cloud/vendor/bavix/laravel-wallet/src/Internal/Service/DatabaseService.php(41): Illuminate\\Database\\Connection->transaction()
#5 /var/www/html/better_cloud/vendor/bavix/laravel-wallet/src/Services/AtomicService.php(59): Bavix\\Wallet\\Internal\\Service\\DatabaseService->transaction()
#6 /var/www/html/better_cloud/vendor/laravel/framework/src/Illuminate/Cache/Lock.php(126): Bavix\\Wallet\\Services\\AtomicService->Bavix\\Wallet\\Services\\{closure}()
#7 /var/www/html/better_cloud/vendor/bavix/laravel-wallet/src/Internal/Service/LockService.php(55): Illuminate\\Cache\\Lock->block()
#8 /var/www/html/better_cloud/vendor/bavix/laravel-wallet/src/Internal/Service/LockService.php(75): Bavix\\Wallet\\Internal\\Service\\LockService->block()
#9 /var/www/html/better_cloud/vendor/bavix/laravel-wallet/src/Internal/Service/LockService.php(79): Bavix\\Wallet\\Internal\\Service\\LockService->Bavix\\Wallet\\Internal\\Service\\{closure}()
#10 /var/www/html/better_cloud/vendor/bavix/laravel-wallet/src/Services/AtomicService.php(63): Bavix\\Wallet\\Internal\\Service\\LockService->blocks()
#11 /var/www/html/better_cloud/vendor/bavix/laravel-wallet/src/Services/AtomicService.php(79): Bavix\\Wallet\\Services\\AtomicService->blocks()
#12 /var/www/html/better_cloud/vendor/bavix/laravel-wallet/src/Traits/CartPay.php(240): Bavix\\Wallet\\Services\\AtomicService->block()
#13 /var/www/html/better_cloud/vendor/bavix/laravel-wallet/src/Traits/CanPay.php(94): Bavix\\Wallet\\Models\\Wallet->refundCart()
#14 /var/www/html/better_cloud/app/Http/Controllers/HomeController.php(95): Bavix\\Wallet\\Models\\Wallet->refund()
#15 /var/www/html/better_cloud/vendor/laravel/framework/src/Illuminate/Routing/Controller.php(54): App\\Http\\Controllers\\HomeController->jugad()
#16 /var/www/html/better_cloud/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(43): Illuminate\\Routing\\Controller->callAction()
#17 /var/www/html/better_cloud/vendor/laravel/framework/src/Illuminate/Routing/Route.php(260): Illuminate\\Routing\\ControllerDispatcher->dispatch()
#18 /var/www/html/better_cloud/vendor/laravel/framework/src/Illuminate/Routing/Route.php(205): Illuminate\\Routing\\Route->runController()
#19 /var/www/html/better_cloud/vendor/laravel/framework/src/Illuminate/Routing/Router.php(725): Illuminate\\Routing\\Route->run()
#20 /var/www/html/better_cloud/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(141): Illuminate\\Routing\\Router->Illuminate\\Routing\\{closure}()
#21 /var/www/html/better_cloud/vendor/laravel/framework/src/Illuminate/Routing/Middleware/SubstituteBindings.php(50): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#22 /var/www/html/better_cloud/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(180): Illuminate\\Routing\\Middleware\\SubstituteBindings->handle()
#23 /var/www/html/better_cloud/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/VerifyCsrfToken.php(78): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#24 /var/www/html/better_cloud/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(180): Illuminate\\Foundation\\Http\\Middleware\\VerifyCsrfToken->handle()
#25 /var/www/html/better_cloud/vendor/laravel/framework/src/Illuminate/View/Middleware/ShareErrorsFromSession.php(49): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#26 /var/www/html/better_cloud/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(180): Illuminate\\View\\Middleware\\ShareErrorsFromSession->handle()
#27 /var/www/html/better_cloud/vendor/laravel/framework/src/Illuminate/Session/Middleware/StartSession.php(121): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#28 /var/www/html/better_cloud/vendor/laravel/framework/src/Illuminate/Session/Middleware/StartSession.php(64): Illuminate\\Session\\Middleware\\StartSession->handleStatefulRequest()
#29 /var/www/html/better_cloud/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(180): Illuminate\\Session\\Middleware\\StartSession->handle()
#30 /var/www/html/better_cloud/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/AddQueuedCookiesToResponse.php(37): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#31 /var/www/html/better_cloud/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(180): Illuminate\\Cookie\\Middleware\\AddQueuedCookiesToResponse->handle()
#32 /var/www/html/better_cloud/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/EncryptCookies.php(67): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#33 /var/www/html/better_cloud/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(180): Illuminate\\Cookie\\Middleware\\EncryptCookies->handle()
#34 /var/www/html/better_cloud/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(116): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#35 /var/www/html/better_cloud/vendor/laravel/framework/src/Illuminate/Routing/Router.php(726): Illuminate\\Pipeline\\Pipeline->then()
#36 /var/www/html/better_cloud/vendor/laravel/framework/src/Illuminate/Routing/Router.php(703): Illuminate\\Routing\\Router->runRouteWithinStack()
#37 /var/www/html/better_cloud/vendor/laravel/framework/src/Illuminate/Routing/Router.php(667): Illuminate\\Routing\\Router->runRoute()
#38 /var/www/html/better_cloud/vendor/laravel/framework/src/Illuminate/Routing/Router.php(656): Illuminate\\Routing\\Router->dispatchToRoute()
#39 /var/www/html/better_cloud/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(190): Illuminate\\Routing\\Router->dispatch()
#40 /var/www/html/better_cloud/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(141): Illuminate\\Foundation\\Http\\Kernel->Illuminate\\Foundation\\Http\\{closure}()
#41 /var/www/html/better_cloud/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(21): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#42 /var/www/html/better_cloud/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ConvertEmptyStringsToNull.php(31): Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest->handle()
#43 /var/www/html/better_cloud/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(180): Illuminate\\Foundation\\Http\\Middleware\\ConvertEmptyStringsToNull->handle()
#44 /var/www/html/better_cloud/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(21): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#45 /var/www/html/better_cloud/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TrimStrings.php(40): Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest->handle()
#46 /var/www/html/better_cloud/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(180): Illuminate\\Foundation\\Http\\Middleware\\TrimStrings->handle()
#47 /var/www/html/better_cloud/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ValidatePostSize.php(27): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#48 /var/www/html/better_cloud/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(180): Illuminate\\Foundation\\Http\\Middleware\\ValidatePostSize->handle()
#49 /var/www/html/better_cloud/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/PreventRequestsDuringMaintenance.php(86): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#50 /var/www/html/better_cloud/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(180): Illuminate\\Foundation\\Http\\Middleware\\PreventRequestsDuringMaintenance->handle()
#51 /var/www/html/better_cloud/vendor/laravel/framework/src/Illuminate/Http/Middleware/HandleCors.php(49): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#52 /var/www/html/better_cloud/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(180): Illuminate\\Http\\Middleware\\HandleCors->handle()
#53 /var/www/html/better_cloud/vendor/laravel/framework/src/Illuminate/Http/Middleware/TrustProxies.php(39): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#54 /var/www/html/better_cloud/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(180): Illuminate\\Http\\Middleware\\TrustProxies->handle()
#55 /var/www/html/better_cloud/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(116): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#56 /var/www/html/better_cloud/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(165): Illuminate\\Pipeline\\Pipeline->then()
#57 /var/www/html/better_cloud/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(134): Illuminate\\Foundation\\Http\\Kernel->sendRequestThroughRouter()
#58 /var/www/html/better_cloud/public/index.php(52): Illuminate\\Foundation\\Http\\Kernel->handle()
#59 /var/www/html/better_cloud/vendor/laravel/framework/src/Illuminate/Foundation/resources/server.php(16): require_once('...')
#60 {main}

Server:

rez1dent3 commented 2 years ago

@hilal-hipster Hello. An interesting case, I see that an incorrect query is being built in the database from your log. I will try to reproduce the problem. Thank you. Tomorrow I will unsubscribe on the result, it will not work quickly to check.

HilalLko commented 2 years ago

@hilal-hipster Hello. An interesting case, I see that an incorrect query is being built in the database from your log. I will try to reproduce the problem. Thank you. Tomorrow I will unsubscribe on the result, it will not work quickly to check.

Sure, @rez1dent3

Furthermore, while changing the cache & lock driver from 'array' to 'Redis' am seeing a difference between balances. Balance in original is "11.50" but after changing cache & lock driver from array to Redis, the balance is getting changed to "5.00" Old cache & lock drivers

'cache' => [
    'driver' => array,
],
'lock' => [
    'driver' => 'array',
    'seconds' => 1,
],

New & updated one

'cache' => [
    'driver' => redis,
],
'lock' => [
    'driver' => 'redis',
    'seconds' => 1,
],

Sorry, for asking this here instead of creating a new one.

HilalLko commented 2 years ago

@hilal-hipster Hello. An interesting case, I see that an incorrect query is being built in the database from your log. I will try to reproduce the problem. Thank you. Tomorrow I will unsubscribe on the result, it will not work quickly to check.

Just to let you know am using custom "TransactionDtoTransformer", in order to handle credit lifespan (validity/expiry), which contains

declare(strict_types=1);

namespace App\Bavix\Wallet\Transform;

use Bavix\Wallet\Internal\Dto\TransactionDtoInterface;
use Bavix\Wallet\Internal\Transform\TransactionDtoTransformer;
use Bavix\Wallet\Internal\Transform\TransactionDtoTransformerInterface;
use Carbon\Carbon;

final class CustomTransactionDtoTransformer implements TransactionDtoTransformerInterface
{
    public function __construct(
        private TransactionDtoTransformer $transactionDtoTransformer
    ) {
    }

    public function extract(TransactionDtoInterface $dto): array
    {
        if ($dto->getMeta() !== null && isset($dto->getMeta()['expire_on'])) {
            return array_merge($this->transactionDtoTransformer->extract($dto), [
                'expiring_at' => $dto->getMeta()['expire_on'],
                'amount_used' => 0,
            ]);
        }
        return $this->transactionDtoTransformer->extract($dto);
    }
}
rez1dent3 commented 2 years ago

Old cache & lock drivers

Most likely, you have already switched to redis before and then changed to array. And now we decided to switch back to redis. In this case, you need to completely reset the redis cache, as it is not relevant to you.

Array cache is not recommended for use, because does not protect against race conditions.

Just to let you know am using custom "TransactionDtoTransformer", in order to handle credit lifespan (validity/expiry), which contains

Now the problem is clear to me. Yes, the problem is not in the package, but in the custom assembler class. You need to add parameters not by condition, but always. Those put down the default-value.

I don't know your data and I'm assuming it's null there. This is what your assembler class should look like.

            return array_merge($this->transactionDtoTransformer->extract($dto), [
                'expiring_at' => $dto->getMeta()['expire_on'] ?? null,
                'amount_used' => 0,
            ]);

@hilal-hipster FYI

rez1dent3 commented 2 years ago

By the way, after changing the cache, you can recalculate the balance using the bavix/laravel-wallet-warmup package.

composer req bavix/laravel-wallet-warmup
artisan wallet:warm-up

The package consists of a single console command that runs through all wallets and applies refreshBalance.

HilalLko commented 2 years ago

Old cache & lock drivers

Most likely, you have already switched to redis before and then changed to array. And now we decided to switch back to redis. In this case, you need to completely reset the redis cache, as it is not relevant to you.

Array cache is not recommended for use, because does not protect against race conditions.

Just to let you know am using custom "TransactionDtoTransformer", in order to handle credit lifespan (validity/expiry), which contains

Now the problem is clear to me. Yes, the problem is not in the package, but in the custom assembler class. You need to add parameters not by condition, but always. Those put down the default-value.

I don't know your data and I'm assuming it's null there. This is what your assembler class should look like.

            return array_merge($this->transactionDtoTransformer->extract($dto), [
                'expiring_at' => $dto->getMeta()['expire_on'] ?? null,
                'amount_used' => 0,
            ]);

@hilal-hipster FYI

But @rez1dent3 here am isset($dto->getMeta()['expire_on']) am already checking existence of the key which contains expiration datetime

HilalLko commented 2 years ago

Old cache & lock drivers

Most likely, you have already switched to redis before and then changed to array. And now we decided to switch back to redis. In this case, you need to completely reset the redis cache, as it is not relevant to you.

Array cache is not recommended for use, because does not protect against race conditions.

Just to let you know am using custom "TransactionDtoTransformer", in order to handle credit lifespan (validity/expiry), which contains

Now the problem is clear to me. Yes, the problem is not in the package, but in the custom assembler class. You need to add parameters not by condition, but always. Those put down the default-value.

I don't know your data and I'm assuming it's null there. This is what your assembler class should look like.

            return array_merge($this->transactionDtoTransformer->extract($dto), [
                'expiring_at' => $dto->getMeta()['expire_on'] ?? null,
                'amount_used' => 0,
            ]);

@hilal-hipster FYI

if ($dto->getMeta() !== null && isset($dto->getMeta()['expire_on'])) {
    return array_merge($this->transactionDtoTransformer->extract($dto), [
        'expiring_at' => $dto->getMeta()['expire_on'],
        'amount_used' => 0,
    ]);
}

With this inside custom "TransactionDtoTransformer" everything like withdrawal, deposit, and purchase works as expected but now am facing this weird thing as I need to add the same "expiring_at" for a refund too.

rez1dent3 commented 2 years ago

@hilal-hipster My code also has this check ??. Because of your assembler class, an incorrect query is being built in the database:

-- first query (amount-1150.00, confirmed1, created_at2022-11-07 06:00:41, meta{"title":"Order from B2C-pickup::CO0057","description":"Purchase of Order #CO0057"}, payable_id369, payable_typeApp\Models\Order, typewithdraw, updated_at2022-11-07 06:00:41, uuid413b7a9d-1abe-49a6-baa1-35a34093bd05, wallet_id874),

-- second query (amount1150.00, confirmed0, created_at1, meta2022-11-07 06:00:41, 2023-11-07 06:00:41, payable_id{"title":"Order from B2C-pickup::CO0057","description":"Purchase of Order #CO0057"}, payable_type1037, typeApp\Models\User, updated_atdeposit, uuid2022-11-07 06:00:41, wallet_idbc76ac59-400b-44c5-b6ab-a4368075517d, 470)

If you look carefully, you will see that the values ​​fly to the database are not correct. In the first case, we have fewer parameters than in the second.

rez1dent3 commented 2 years ago

With this inside custom "TransactionDtoTransformer" everything like withdrawal, deposit, and purchase works as expected but now am facing this weird thing as I need to add the same "expiring_at" for a refund too.

Inside the database, the field will still be filled in some way (default value, for example). You need to put this value in your assembler class and everything will work for you.

HilalLko commented 2 years ago

Well @rez1dent3 second query here was the result of another check

-- second query
(amount1150.00, confirmed0, created_at1, meta2022-11-07 06:00:41, 2023-11-07 06:00:41, payable_id{"title":"Order from B2C-pickup::CO0057","description":"Purchase of Order #CO0057"}, payable_type1037, typeApp\Models\User, updated_atdeposit, uuid2022-11-07 06:00:41, wallet_idbc76ac59-400b-44c5-b6ab-a4368075517d, 470)

which was like this

if ($dto->getMeta() !== null && (isset($dto->getMeta()['title']) && str_contains($dto->getMeta()['title'],'Order from'))) {
if ($dto->getPayableType() == 'App\Models\User') {
return array_merge($this->transactionDtoTransformer->extract($dto), [
'expiring_at' => Carbon::now()->addYear(),
'amount_used' => 0,
]);
}
}

actually was trying to add "expiring_at" for refund transactions also.

rez1dent3 commented 2 years ago

One way or another, but the problem is clearly not on the side of the package. You need to solve the problem in your domain/application layer.

HilalLko commented 2 years ago

With this inside custom "TransactionDtoTransformer" everything like withdrawal, deposit, and purchase works as expected but now am facing this weird thing as I need to add the same "expiring_at" for a refund too.

Inside the database, the field will still be filled in some way (default value, for example). You need to put this value in your assembler class and everything will work for you.

@rez1dent3 assembler class Transaction Assembler,

'transaction' => TransactionDtoAssembler::class,

need to override ?

HilalLko commented 2 years ago

One way or another, but the problem is clearly not on the side of the package. You need to solve the problem in your domain/application layer.

Understood boss 😎

rez1dent3 commented 2 years ago

need to override ?

All right.

    'assemblers' => [
        ...
        'transaction' => TransactionDtoAssembler::class,
        ....
rez1dent3 commented 2 years ago

I wrote about the Assembler class, but I meant Transformer. Those, you need to return always up-to-date data for the table inside the Transformer class, without IF. You should put down the default values if the data did not come.

@hilal-hipster FYI^

HilalLko commented 2 years ago

@rez1dent3 Will not go for any big change as of now, i.e, changing TransactionDtoAssembler as of now. Instead, I will change my logic inside "TransactionDtoTransformer" now will use

return array_merge($this->transactionDtoTransformer->extract($dto), [
    'expiring_at' => $dto->getMeta()['expire_on'] ?? Carbon::now()->addYear(),
    'amount_used' => 0,
]);

In short, will add "expiring_at" & "amount_used" every time. As expiration of transaction will always be managed for transaction type "deposit" 😃

HilalLko commented 2 years ago

By the way, after changing the cache, you can recalculate the balance using the bavix/laravel-wallet-warmup package.

composer req bavix/laravel-wallet-warmup
artisan wallet:warm-up

The package consists of a single console command that runs through all wallets and applies refreshBalance.

This works awesome, Thanks @rez1dent3