Skip to content

Commit

Permalink
Merge pull request #16 from HPWebdeveloper/types
Browse files Browse the repository at this point in the history
Correcting Types and Fix-Styling
  • Loading branch information
HPWebdeveloper authored Jan 14, 2024
2 parents aa174d1 + 174ab31 commit d661d59
Show file tree
Hide file tree
Showing 18 changed files with 139 additions and 77 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ LaravelPayPocket::deposit($user, 'wallet_1', 123.45);

Note: `wallet_1` and `wallet_2` must already be defined in the `WalletEnums`.

#### Transaction Notes ([#8][i8])
#### Transaction Info ([#8][i8])

When you need to add descriptions for a specific transaction, the `$notes` parameter enables you to provide details explaining the reason behind the transaction.

Expand Down
5 changes: 3 additions & 2 deletions config/pay-pocket.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/
return [
'log_reference_length' => 12,
'log_reference_prefix' => null,
'log_reference_generator' => null,
'log_reference_prefix' => '',
'log_reference_generator_class' => Illuminate\Support\Str::class,
'log_reference_generator_method' => 'random',
];
9 changes: 2 additions & 7 deletions src/Exceptions/InsufficientBalanceException.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,11 @@
namespace HPWebdeveloper\LaravelPayPocket\Exceptions;

use Exception;
use Throwable;

class InsufficientBalanceException extends Exception
{
/**
* Construct the exception.
*
* @param string $message
* @param int $code
*/
public function __construct($message = 'Insufficient balance to cover the order', $code = 0, ?\Throwable $previous = null)
public function __construct(string $message = 'Insufficient balance to cover the order', int $code = 0, ?Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
}
Expand Down
9 changes: 2 additions & 7 deletions src/Exceptions/InvalidDepositException.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,11 @@
namespace HPWebdeveloper\LaravelPayPocket\Exceptions;

use Exception;
use Throwable;

class InvalidDepositException extends Exception
{
/**
* Construct the exception.
*
* @param string $message
* @param int $code
*/
public function __construct($message = 'Invalid deposit operation', $code = 0, ?\Throwable $previous = null)
public function __construct(string $message = 'Invalid deposit operation', int $code = 0, ?Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
}
Expand Down
9 changes: 2 additions & 7 deletions src/Exceptions/InvalidValueException.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,11 @@
namespace HPWebdeveloper\LaravelPayPocket\Exceptions;

use Exception;
use Throwable;

class InvalidValueException extends Exception
{
/**
* Construct the exception.
*
* @param string $message
* @param int $code
*/
public function __construct($message = 'Invalie value to deposit', $code = 0, ?\Throwable $previous = null)
public function __construct(string $message = 'Invalie value to deposit', int $code = 0, ?Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
}
Expand Down
3 changes: 2 additions & 1 deletion src/Exceptions/InvalidWalletTypeException.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
namespace HPWebdeveloper\LaravelPayPocket\Exceptions;

use Exception;
use Throwable;

class InvalidWalletTypeException extends Exception
{
public function __construct($message = 'Invalid wallet type', $code = 0, ?\Throwable $previous = null)
public function __construct(string $message = 'Invalid wallet type', int $code = 0, ?Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
}
Expand Down
3 changes: 2 additions & 1 deletion src/Exceptions/WalletNotFoundException.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
namespace HPWebdeveloper\LaravelPayPocket\Exceptions;

use Exception;
use Throwable;

class WalletNotFoundException extends Exception
{
public function __construct($message = 'Wallet not found', $code = 0, ?\Throwable $previous = null)
public function __construct(string $message = 'Wallet not found', int $code = 0, ?Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
}
Expand Down
8 changes: 7 additions & 1 deletion src/Facades/LaravelPayPocket.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,20 @@

namespace HPWebdeveloper\LaravelPayPocket\Facades;

use HPWebdeveloper\LaravelPayPocket\Interfaces\WalletOperations;
use Illuminate\Support\Facades\Facade;

/**
* @see \HPWebdeveloper\LaravelPayPocket\Services\PocketServices
*
* @method static void pay(WalletOperations $user, int|float $orderValue, ?string $notes = null)
* @method static bool deposit(WalletOperations $user, string $type, int|float $amount, ?string $notes = null)
* @method static int|float checkBalance(WalletOperations $user)
* @method static int|float walletBalanceByType(WalletOperations $user, string $type)
*/
class LaravelPayPocket extends Facade
{
protected static function getFacadeAccessor()
protected static function getFacadeAccessor(): string
{
return \HPWebdeveloper\LaravelPayPocket\Services\PocketServices::class;
}
Expand Down
32 changes: 28 additions & 4 deletions src/Interfaces/WalletOperations.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,39 @@

namespace HPWebdeveloper\LaravelPayPocket\Interfaces;

use HPWebdeveloper\LaravelPayPocket\Exceptions\InsufficientBalanceException;

interface WalletOperations
{
public function getWalletBalanceAttribute();
/**
* Get User's Wallet Balance
*/
public function getWalletBalanceAttribute(): int|float;

public function getWalletBalanceByType(string $walletType);
/**
* Get the balance of a specific wallet type.
*/
public function getWalletBalanceByType(string $walletType): int|float;

public function hasSufficientBalance($value): bool;
/**
* Check if User's wallet balance is more than given value
*/
public function hasSufficientBalance(int|float $value): bool;

public function pay(int|float $orderValue, ?string $notes = null);
/**
* Pay the order value from the user's wallets.
*
* @throws InsufficientBalanceException
*/
public function pay(int|float $orderValue, ?string $notes = null): void;

/**
* Deposit an amount to the user's wallet of a specific type.
*/
public function deposit(string $type, int|float $amount, ?string $notes = null): bool;

/**
* Get user's wallet balance.
*/
public function getWalletBalance(): int|float;
}
8 changes: 8 additions & 0 deletions src/Models/WalletsLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@
* HPWebdeveloper\LaravelPayPocket\Models\WalletsLog
*
* @property string $status
* @property int|float $from
* @property int|float $to
* @property string $type
* @property string $ip
* @property int|float $value
* @property string $wallet_name
* @property string $notes
* @property string $reference
*/
class WalletsLog extends Model
{
Expand Down
29 changes: 23 additions & 6 deletions src/Services/PocketServices.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,41 @@

namespace HPWebdeveloper\LaravelPayPocket\Services;

use HPWebdeveloper\LaravelPayPocket\Exceptions\InsufficientBalanceException;
use HPWebdeveloper\LaravelPayPocket\Interfaces\WalletOperations;

class PocketServices
{
public function deposit($user, $type, $amount, $notes = null)
/**
* Deposit an amount to the user's wallet of a specific type.
*/
public function deposit(WalletOperations $user, string $type, int|float $amount, ?string $notes = null): bool
{
return $user->deposit($type, $amount, $notes);
}

public function pay($user, $orderValue, $notes = null)
/**
* Pay the order value from the user's wallets.
*
* @throws InsufficientBalanceException
*/
public function pay(WalletOperations $user, int|float $orderValue, ?string $notes = null): void
{
return $user->pay($orderValue, $notes);
$user->pay($orderValue, $notes);
}

public function checkBalance($user)
/**
* Get the balance of the user.
*/
public function checkBalance(WalletOperations $user): int|float
{
return $user->walletBalance;
return $user->getWalletBalance();
}

public function walletBalanceByType($user, $type)
/**
* Get the balance of a specific wallet type.
*/
public function walletBalanceByType(WalletOperations $user, string $type): int|float
{
return $user->getWalletBalanceByType($type);
}
Expand Down
50 changes: 31 additions & 19 deletions src/Traits/BalanceOperation.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,66 +2,78 @@

namespace HPWebdeveloper\LaravelPayPocket\Traits;

use Illuminate\Support\Str;
use HPWebdeveloper\LaravelPayPocket\Models\WalletsLog;
use InvalidArgumentException;

trait BalanceOperation
{
protected $createdLog;
protected WalletsLog $createdLog;

/**
* Check if Balance is more than zero.
* Check if Balance is more than zero.
*/
public function hasBalance(): bool
{
return $this->balance > 0;
}

/**
* Decrement Balance and create a log entry.
* Decrement Balance and create a log entry.
*/
public function decrementAndCreateLog($value, $notes = null): void
public function decrementAndCreateLog(int|float $value, ?string $notes = null): void
{
$this->createLog('dec', $value, $notes);
$this->decrement('balance', $value);
}

/**
* Increment Balance and create a log entry.
* Increment Balance and create a log entry.
*/
public function incrementAndCreateLog($value, $notes = null): void
public function incrementAndCreateLog(int|float $value, ?string $notes = null): void
{
$this->createLog('inc', $value, $notes);
$this->increment('balance', $value);
}

/**
* Create a new log record
* Create a new log record
*/
protected function createLog($logType, $value, $notes = null): void
protected function createLog(string $logType, int|float $value, ?string $notes = null): void
{
$currentBalance = $this->balance ?? 0;

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

$refGen = config('pay-pocket.log_reference_generator', [
Str::class, 'random', [config('pay-pocket.log_reference_length', 12)],
]);
$reference = config('pay-pocket.reference_string_prefix', '');
$reference .= isset($refGen[0], $refGen[1])
? $refGen[0]::{$refGen[1]}(...$refGen[2] ?? [])
: Str::random(config('pay-pocket.log_reference_length', 12));

$this->createdLog = $this->logs()->create([
'wallet_name' => $this->type->value,
'from' => $currentBalance,
'to' => $newBalance,
'type' => $logType,
'ip' => \Request::ip(),
'ip' => request()->ip(),
'value' => $value,
'notes' => $notes,
'reference' => $reference,
'reference' => $this->generateReference(),
]);

$this->createdLog->changeStatus('Done');
}

/**
* @throws InvalidArgumentException
*/
protected function generateReference(): string
{
$className = config('pay-pocket.log_reference_generator_class');
$methodName = config('pay-pocket.log_reference_generator_method');
$length = config('pay-pocket.log_reference_length');
$prefix = config('pay-pocket.log_reference_prefix');

if (! is_callable([$className, $methodName])) {
throw new InvalidArgumentException('Invalid configuration: The combination of log_reference_generator_class and log_reference_generator_method is not callable.');
}

$reference = call_user_func([$className, $methodName], $length);

return $prefix.$reference;
}
}
6 changes: 2 additions & 4 deletions src/Traits/GetWallets.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@

trait GetWallets
{
private function walletsInOrder()
private function walletsInOrder(): array
{
return array_map(
function ($enumCase) {
return $enumCase->value;
},
fn ($enumCase) => $enumCase->value,
WalletEnums::cases()
);
}
Expand Down
13 changes: 7 additions & 6 deletions src/Traits/HandlesDeposit.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
use HPWebdeveloper\LaravelPayPocket\Exceptions\InvalidWalletTypeException;
use Illuminate\Support\Facades\DB;

// Use your defined exception

trait HandlesDeposit
{
/**
* Deposit an amount to the user's wallet of a specific type.
*
* @throws InvalidDepositException
* @throws InvalidValueException
* @throws InvalidWalletTypeException
*/
public function deposit(string $type, int|float $amount, ?string $notes = null): bool
{
Expand Down Expand Up @@ -50,12 +52,11 @@ private function getDepositableTypes(): array
}

/**
* Check if the given tyep is valid.
* Check if the given type is valid.
*
* @param string $type
* @return bool
* @throws InvalidWalletTypeException
*/
private function isRequestValid($type, array $depositable)
private function isRequestValid($type, array $depositable): bool
{
if (! array_key_exists($type, $depositable)) {
throw new InvalidWalletTypeException('Invalid deposit type.');
Expand Down
4 changes: 3 additions & 1 deletion src/Traits/HandlesPayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ trait HandlesPayment
/**
* Pay the order value from the user's wallets.
*
*
* @throws InsufficientBalanceException
*/
public function pay(int|float $orderValue, ?string $notes = null): void
Expand All @@ -22,6 +21,9 @@ public function pay(int|float $orderValue, ?string $notes = null): void
DB::transaction(function () use ($orderValue, $notes) {
$remainingOrderValue = $orderValue;

/**
* @var \Illuminate\Support\Collection<TKey, \HPWebdeveloper\LaravelPayPocket\Models\Wallet>
*/
$walletsInOrder = $this->wallets()->whereIn('type', $this->walletsInOrder())->get();

foreach ($walletsInOrder as $wallet) {
Expand Down
Loading

0 comments on commit d661d59

Please sign in to comment.