230 lines
6 KiB
PHP
230 lines
6 KiB
PHP
<?php
|
|
|
|
namespace App\Lib\Payments;
|
|
|
|
use App\Enums\Order\PaymentModeEnum;
|
|
use Illuminate\Support\Facades\Cache;
|
|
use Money\Money;
|
|
|
|
/**
|
|
* Payment balance state
|
|
*
|
|
* @property-read boolean $test_mode Is it in test mode ?
|
|
* @property-read boolean $has_been_initialized The state has been initialized
|
|
* with a real value.
|
|
* @property-read \Money\Money $balance The last balance as sended by the IPN.
|
|
* @property-read \Money\Money $pending_balance The last pending balance as sended by the IPN.
|
|
* @property-read \App\Enums\Order\PaymentModeEnum $mode The state payment mode.
|
|
*
|
|
* @package App\Lib\Payments
|
|
*/
|
|
class PaymentState
|
|
{
|
|
/**
|
|
* Attributes that can be read via __get.
|
|
* @var string[]
|
|
*/
|
|
private $readableAttributes = [
|
|
'balance', 'pending_balance',
|
|
'mode', 'test_mode',
|
|
];
|
|
|
|
/**
|
|
* The account balance
|
|
* @var \Money\Money
|
|
*/
|
|
private $balance = null;
|
|
|
|
/**
|
|
* The account pending balance
|
|
* @var \Money\Money|null
|
|
*/
|
|
private $pending_balance;
|
|
|
|
/**
|
|
* The payment method used
|
|
*
|
|
* @var \App\Enums\Order\PaymentModeEnum
|
|
*/
|
|
private $mode;
|
|
|
|
/**
|
|
* The state represents test mode state
|
|
*
|
|
* @var boolean
|
|
*/
|
|
private $test_mode;
|
|
|
|
/**
|
|
* The state has been initialized with a real value.
|
|
*
|
|
* @var boolean
|
|
*/
|
|
private $has_been_initialized = false;
|
|
|
|
/**
|
|
* PaymentState
|
|
*
|
|
* @param \App\Enums\Order\PaymentModeEnum $paymentMode
|
|
* @param boolean $is_test_mode
|
|
* @return void
|
|
*/
|
|
final public function __construct(PaymentModeEnum $paymentMode, bool $is_test_mode = false)
|
|
{
|
|
$this->mode = $paymentMode;
|
|
$this->balance = new Money(0, app('phpMoneyCurrency'));
|
|
$this->pending_balance = null;
|
|
$this->test_mode = $is_test_mode;
|
|
}
|
|
|
|
/**
|
|
* Get a property
|
|
*
|
|
* @param string $name
|
|
* @return mixed
|
|
* @throws \Error If the property is undefined.
|
|
*/
|
|
public function __get(string $name)
|
|
{
|
|
if ($this->__isset($name)) {
|
|
return $this->{$name};
|
|
}
|
|
throw new \Error("Undefined property {$name}");
|
|
}
|
|
|
|
/**
|
|
* Allow checking or props are set.
|
|
*
|
|
* @param string $name
|
|
* @return boolean
|
|
*/
|
|
public function __isset(string $name): bool
|
|
{
|
|
return \in_array($name, $this->readableAttributes);
|
|
}
|
|
|
|
// * STATIC METHODS
|
|
|
|
/**
|
|
* Fetch or create a balance state
|
|
*
|
|
* @param \App\Enums\Order\PaymentModeEnum $paymentMode
|
|
* @param boolean $is_test_mode
|
|
* @return \App\Lib\Payments\PaymentState
|
|
*/
|
|
public static function fetchBalance(PaymentModeEnum $paymentMode, bool $is_test_mode): self
|
|
{
|
|
return static::getCachedPaymentState($paymentMode, $is_test_mode);
|
|
}
|
|
|
|
/**
|
|
* Get a specific payment mode state
|
|
*
|
|
* @param \App\Enums\Order\PaymentModeEnum $paymentMode
|
|
* @param boolean $is_test_mode
|
|
* @return \App\Lib\Payments\PaymentState
|
|
*/
|
|
private static function getCachedPaymentState(PaymentModeEnum $paymentMode, bool $is_test_mode): self
|
|
{
|
|
$staticKey = static::getPaymentStateKey($paymentMode, $is_test_mode);
|
|
if (!Cache::has($staticKey)) {
|
|
self::setCachedPaymentMode($paymentMode, $is_test_mode);
|
|
}
|
|
return Cache::get($staticKey);
|
|
}
|
|
|
|
/**
|
|
* Create a specific payment mode state
|
|
*
|
|
* @param \App\Enums\Order\PaymentModeEnum $paymentMode
|
|
* @param boolean $is_test_mode
|
|
* @return void
|
|
*/
|
|
private static function setCachedPaymentMode(PaymentModeEnum $paymentMode, bool $is_test_mode): void
|
|
{
|
|
Cache::put(static::getPaymentStateKey($paymentMode, $is_test_mode), new self($paymentMode, $is_test_mode));
|
|
}
|
|
|
|
/**
|
|
* Save a specific payment mode state
|
|
*
|
|
* @param self $paymentState
|
|
* @return void
|
|
*/
|
|
private static function setCachedPaymentState(self $paymentState): void
|
|
{
|
|
Cache::put(static::getPaymentStateKey($paymentState->mode, $paymentState->test_mode), $paymentState);
|
|
}
|
|
|
|
/**
|
|
* Get payment state mode
|
|
*
|
|
* @param \App\Enums\Order\PaymentModeEnum $paymentMode
|
|
* @param boolean $is_test_mode
|
|
* @return string
|
|
*/
|
|
private static function getPaymentStateKey(PaymentModeEnum $paymentMode, bool $is_test_mode): string
|
|
{
|
|
$suffix = $is_test_mode ? '_TEST' : '';
|
|
return "{$paymentMode->value}_PaymentState{$suffix}";
|
|
}
|
|
|
|
// * METHODS
|
|
|
|
/**
|
|
* Set the payment state balance
|
|
*
|
|
* @param \Money\Money $amount
|
|
* @return void
|
|
*/
|
|
public function setBalance(Money $amount)
|
|
{
|
|
$this->balance = $amount;
|
|
$this->has_been_initialized = true;
|
|
$this->save();
|
|
}
|
|
|
|
/**
|
|
* Set the payment state pending balance
|
|
*
|
|
* @param \Money\Money $amount
|
|
* @return void
|
|
*/
|
|
public function setPendingBalance(Money $amount)
|
|
{
|
|
$this->pending_balance = $amount;
|
|
$this->has_been_initialized = true;
|
|
$this->save();
|
|
}
|
|
|
|
/**
|
|
* Has this state been initialized with a real IPN value
|
|
*
|
|
* @return boolean
|
|
*/
|
|
public function hasBeenInitWithIpn(): bool
|
|
{
|
|
return $this->has_been_initialized;
|
|
}
|
|
|
|
/**
|
|
* Save this payement state
|
|
*
|
|
* @return void
|
|
*/
|
|
private function save(): void
|
|
{
|
|
static::setCachedPaymentState($this);
|
|
}
|
|
|
|
/**
|
|
* Throw an Error exception because not allowed.
|
|
*
|
|
* @return void
|
|
* @throws \Error Always.
|
|
*/
|
|
private function denyAccess(): void
|
|
{
|
|
throw new \Error('This is not allowed');
|
|
}
|
|
}
|