Skip to content

Commit 8110c2e

Browse files
committed
Add Reference and Detail to Logs
1 parent b15069e commit 8110c2e

File tree

6 files changed

+48
-23
lines changed

6 files changed

+48
-23
lines changed

config/pay-pocket.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,11 @@
22

33
// config for HPWebdeveloper/LaravelPayPocket
44
return [
5-
6-
];
5+
'log_reference_length' => 12,
6+
'log_reference_prefix' => null,
7+
/**
8+
* The log reference generator should be static
9+
* The third array item should contain optional parameters to pass to the generator
10+
*/
11+
'log_reference_generator' => [\Illuminate\Support\Str::class, 'random', [15]],
12+
];

src/LaravelPayPocketServiceProvider.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,16 @@ public function bootingPackage()
3030
$this->publishes([
3131
__DIR__ . '/../Enums/' => app_path('Enums'),
3232
], 'pay-pocket-wallets');
33+
34+
35+
$this->publishes([
36+
__DIR__ . '/../config/pay-pocket.php' => config_path('pay-pocket.php'),
37+
], 'config');
38+
}
39+
40+
public function registeringPackage()
41+
{
42+
// Automatically apply the package configuration
43+
$this->mergeConfigFrom(__DIR__ . '/../config/pay-pocket.php', 'pay-pocket');
3344
}
3445
}

src/Traits/BalanceOperation.php

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,54 +2,64 @@
22

33
namespace HPWebdeveloper\LaravelPayPocket\Traits;
44

5+
use Illuminate\Support\Str;
6+
57
trait BalanceOperation
68
{
79
protected $createdLog;
810

911
/**
10-
* Check if Balance is more than zero.
12+
* Check if Balance is more than zero.
1113
*/
1214
public function hasBalance(): bool
1315
{
1416
return $this->balance > 0;
1517
}
1618

1719
/**
18-
* Decrement Balance and create a log entry.
20+
* Decrement Balance and create a log entry.
1921
*/
20-
public function decrementAndCreateLog($value): void
22+
public function decrementAndCreateLog($value, $detail = null): void
2123
{
22-
$this->createLog('dec', $value);
24+
$this->createLog('dec', $value, $detail);
2325
$this->decrement('balance', $value);
2426
}
2527

2628
/**
27-
* Increment Balance and create a log entry.
29+
* Increment Balance and create a log entry.
2830
*/
29-
public function incrementAndCreateLog($value): void
31+
public function incrementAndCreateLog($value, $detail = null): void
3032
{
31-
$this->createLog('inc', $value);
33+
$this->createLog('inc', $value, $detail);
3234
$this->increment('balance', $value);
3335
}
3436

3537
/**
36-
* Create a new log record
38+
* Create a new log record
3739
*/
38-
protected function createLog($logType, $value): void
40+
protected function createLog($logType, $value, $detail = null): void
3941
{
4042
$currentBalance = $this->balance ?? 0;
4143

4244
$newBalance = $logType === 'dec' ? $currentBalance - $value : $currentBalance + $value;
4345

46+
$refGen = config('pay-pocket.log_reference_generator', [Str::class, 'random', [12]]);
47+
$reference = config('pay-pocket.reference_string_prefix', '');
48+
$reference .= isset($refGen[0], $refGen[1])
49+
? $refGen[0]::{$refGen[1]}(...$refGen[2] ?? [])
50+
: Str::random(config('pay-pocket.log_reference_length', 12));
51+
4452
$this->createdLog = $this->logs()->create([
4553
'wallet_name' => $this->type->value,
4654
'from' => $currentBalance,
4755
'to' => $newBalance,
4856
'type' => $logType,
4957
'ip' => \Request::ip(),
5058
'value' => $value,
59+
'detail' => $detail,
60+
'reference' => $reference
5161
]);
5262

5363
$this->createdLog->changeStatus('Done');
5464
}
55-
}
65+
}

src/Traits/HandlesDeposit.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ trait HandlesDeposit
1515
/**
1616
* Deposit an amount to the user's wallet of a specific type.
1717
*/
18-
public function deposit(string $type, int|float $amount): bool
18+
public function deposit(string $type, int|float $amount, $detail = null): bool
1919
{
2020
$depositable = $this->getDepositableTypes();
2121

@@ -27,10 +27,10 @@ public function deposit(string $type, int|float $amount): bool
2727
throw new InvalidValueException();
2828
}
2929

30-
DB::transaction(function () use ($type, $amount) {
30+
DB::transaction(function () use ($type, $amount, $detail) {
3131
$type = WalletEnums::tryFrom($type);
3232
$wallet = $this->wallets()->firstOrCreate(['type' => $type]);
33-
$wallet->incrementAndCreateLog($amount);
33+
$wallet->incrementAndCreateLog($amount, $detail);
3434
});
3535

3636
return true;
@@ -63,4 +63,4 @@ private function isRequestValid($type, array $depositable)
6363

6464
return true;
6565
}
66-
}
66+
}

src/Traits/HandlesPayment.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ trait HandlesPayment
1313
*
1414
* @throws InsufficientBalanceException
1515
*/
16-
public function pay(int|float $orderValue): void
16+
public function pay(int|float $orderValue, $detail = null): void
1717
{
1818
if (! $this->hasSufficientBalance($orderValue)) {
1919
throw new InsufficientBalanceException('Insufficient balance to cover the order.');
2020
}
2121

22-
DB::transaction(function () use ($orderValue) {
22+
DB::transaction(function () use ($orderValue, $detail) {
2323
$remainingOrderValue = $orderValue;
2424

2525
$walletsInOrder = $this->wallets()->whereIn('type', $this->walletsInOrder())->get();
@@ -30,7 +30,7 @@ public function pay(int|float $orderValue): void
3030
}
3131

3232
$amountToDeduct = min($wallet->balance, $remainingOrderValue);
33-
$wallet->decrementAndCreateLog($amountToDeduct);
33+
$wallet->decrementAndCreateLog($amountToDeduct, $detail);
3434
$remainingOrderValue -= $amountToDeduct;
3535

3636
if ($remainingOrderValue <= 0) {
@@ -43,4 +43,4 @@ public function pay(int|float $orderValue): void
4343
}
4444
});
4545
}
46-
}
46+
}

tests/OperationsWithFacadeTest.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
expect(LaravelPayPocket::walletBalanceByType($user, 'wallet_2'))->toBeFloat(234.56);
1919

2020
expect(LaravelPayPocket::checkBalance($user))->toBeFloat(234.56);
21-
2221
});
2322

2423
test('user can deposit two times', function () {
@@ -34,7 +33,6 @@
3433
expect(LaravelPayPocket::walletBalanceByType($user, 'wallet_2'))->toBeFloat(1023.68);
3534

3635
expect(LaravelPayPocket::checkBalance($user))->toBeFloat(1023.68);
37-
3836
});
3937

4038
test('user can pay order', function () {
@@ -96,4 +94,4 @@
9694
expect(LaravelPayPocket::walletBalanceByType($user, 'wallet_2'))->toBeFloat(0.12);
9795

9896
expect(LaravelPayPocket::checkBalance($user))->toBeFloat(0.12);
99-
});
97+
});

0 commit comments

Comments
 (0)