- changed order pk column to id - refactored and fixed inShop - using php Money for transaction
242 lines
7 KiB
PHP
242 lines
7 KiB
PHP
<?php
|
|
|
|
namespace App\Models\Payment;
|
|
|
|
use App\Enums\Order\PaymentModeEnum;
|
|
use App\Enums\Order\PaymentStatus;
|
|
use App\Enums\Order\TransactionStatus;
|
|
use App\Enums\Order\TransactionType;
|
|
use App\Lib\Helpers\ToolboxHelper;
|
|
use App\Lib\Payments\PaymentAdapter;
|
|
use App\Models\Shop\Order;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Support\Facades\Session;
|
|
use Illuminate\Support\Str;
|
|
use Kwaadpepper\Enum\Traits\CastsEnums;
|
|
use Money\Currency;
|
|
use Money\Money;
|
|
|
|
/**
|
|
* Transaction
|
|
*
|
|
* @property-read string $reference The transaction unique reference string.
|
|
* @property-read \Money\Currency $currency The transaction currency.
|
|
* @property \Money\Money $amount The transaction amount.
|
|
*
|
|
* @property \App\Enums\Order\PaymentModeEnum $payment_mode The Payment mode for this transaction.
|
|
* @property \App\Enums\Order\TransactionStatus $status The transaction status.
|
|
* @property \App\Enums\Order\TransactionType $type The transaction type (debit/credit) for customer.
|
|
*
|
|
* @package App\Models
|
|
*
|
|
*/
|
|
class Transaction extends Model
|
|
{
|
|
use CastsEnums;
|
|
use HasFactory;
|
|
|
|
/**
|
|
* The attributes that should be cast to Enums.
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $enumCasts = [
|
|
'payment_mode' => PaymentModeEnum::class,
|
|
'status' => TransactionStatus::class,
|
|
'type' => TransactionType::class,
|
|
];
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* ! No attributes can be filled, TAKE THIS SERIOUSLY !
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $fillable = [];
|
|
|
|
/**
|
|
* Perform any actions required after the model boots.
|
|
*
|
|
* @return void
|
|
*/
|
|
protected static function booted()
|
|
{
|
|
// * EVENT CLOSURES.
|
|
static::creating(function (self $model) {
|
|
// * Référence unique par jour.
|
|
$model->reference = ToolboxHelper::getUniqueReference(self::class);
|
|
$model->mutateOrderStatus();
|
|
});
|
|
static::updating(function (self $model) {
|
|
if (!$model->reference) {
|
|
throw new \RuntimeException('Missing reference');
|
|
}
|
|
$model->mutateOrderStatus();
|
|
});
|
|
static::deleting(function () {
|
|
throw new \RuntimeException('Removing a transaction is not allowed');
|
|
});
|
|
}
|
|
|
|
// * METHODS
|
|
|
|
/**
|
|
* Create a new instance of the given model.
|
|
*
|
|
* @param \App\Enums\Order\PaymentModeEnum $mode
|
|
* @param \App\Enums\Order\TransactionType $type
|
|
* @param integer $amount
|
|
* @param array $attributes
|
|
* @return static
|
|
* @throws \RuntimeException If amount is below 0.
|
|
*/
|
|
public static function getNewInstance(
|
|
PaymentModeEnum $mode,
|
|
TransactionType $type,
|
|
int $amount = 0,
|
|
array $attributes = []
|
|
): self {
|
|
// phpcs:enable
|
|
if ($amount < 0) {
|
|
throw new \RuntimeException('Amount cannot be negative');
|
|
}
|
|
|
|
$transaction = new self($attributes);
|
|
$transaction->amount = new Money($amount, new Currency(Str::lower(env('CASHIER_CURRENCY', 'eur'))));
|
|
$transaction->status = TransactionStatus::pending();
|
|
$transaction->type = $type;
|
|
$transaction->payment_mode = $mode;
|
|
return $transaction;
|
|
}
|
|
|
|
/**
|
|
* Return payment adpater
|
|
*
|
|
* @return \App\Lib\Payments\PaymentAdapter
|
|
*/
|
|
public function getAdapter(): PaymentAdapter
|
|
{
|
|
// * Singleton adpater
|
|
if (!app($this->payment_mode->adapter)) {
|
|
\app()->singleton($this->payment_mode->adapter, function () {
|
|
return ($this->payment_mode->adapter)();
|
|
});
|
|
}
|
|
|
|
return app($this->payment_mode->adapter);
|
|
}
|
|
|
|
/**
|
|
* Get updated current session order
|
|
*
|
|
* @return null|\App\Models\Payment\Transaction
|
|
*/
|
|
public static function getCurrentTransaction(): ?self
|
|
{
|
|
/** @var \App\Models\Payment\Transaction|null $transaction */
|
|
$transaction = Session::get('currentTransaction');
|
|
return $transaction;
|
|
}
|
|
|
|
/**
|
|
* Fetch the associated order.
|
|
*
|
|
* @return \App\Models\Shop\Order
|
|
*/
|
|
protected function fetchOrder(): Order
|
|
{
|
|
return $this->order;
|
|
}
|
|
|
|
/**
|
|
* Set related Order payment status using this transaction actual status.
|
|
*
|
|
* @return void
|
|
* @throws \RuntimeException If this transaction is not associated with and order.
|
|
*/
|
|
public function mutateOrderStatus(): void
|
|
{
|
|
/** @var \App\Models\Shop\Order $relatedOrder */
|
|
if (!($relatedOrder = $this->order)) {
|
|
throw new \RuntimeException("Any transaction has to be associated with an order, id '{$this->id}'");
|
|
}
|
|
// * Ignore pending here as default status
|
|
switch ($this->status) {
|
|
case TransactionStatus::validated():
|
|
// * Make distinction between payment and refund (Full/Partial)
|
|
$paymentStatus = PaymentStatus::payed();
|
|
if ($this->type->equals(TransactionType::credit())) {
|
|
$paymentStatus = $relatedOrder->isFullyRefunded() ?
|
|
PaymentStatus::refunded() : PaymentStatus::refundedPartial();
|
|
}
|
|
$relatedOrder->payment_status = $paymentStatus;
|
|
break;
|
|
case TransactionStatus::failed():
|
|
// * Just set failure on any failed transaction.
|
|
$relatedOrder->payment_status = PaymentStatus::failed();
|
|
break;
|
|
}
|
|
$relatedOrder->save();
|
|
}
|
|
|
|
// * GETTER
|
|
|
|
/**
|
|
* Get the transation amount
|
|
*
|
|
* @return \Money\Money
|
|
*/
|
|
public function getAmountAttribute(): Money
|
|
{
|
|
return new Money($this->attributes['amount'], $this->currency);
|
|
}
|
|
|
|
/**
|
|
* Get the transation currency
|
|
*
|
|
* @return \Money\Currency
|
|
*/
|
|
public function getCurrencyAttribute(): Currency
|
|
{
|
|
return new Currency(Str::upper($this->attributes['currency']));
|
|
}
|
|
|
|
// * SETTER
|
|
|
|
/**
|
|
* Set the transation amount
|
|
*
|
|
* @param \Money\Money $value
|
|
* @return void
|
|
*/
|
|
public function setAmountAttribute(Money $value): void
|
|
{
|
|
$this->attributes['amount'] = \intval($value->getAmount());
|
|
$this->attributes['currency'] = \strval($value->getCurrency());
|
|
}
|
|
|
|
/**
|
|
* Set the transation currency
|
|
*
|
|
* @return void
|
|
* @throws \Error In any case, this is not allowed.
|
|
*/
|
|
public function setCurrencyAttribute(): void
|
|
{
|
|
throw new \Error('Settings manually currency is not allowed');
|
|
}
|
|
|
|
// * RELATIONSHIPS
|
|
|
|
/**
|
|
* BelongsTo order relation
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
*/
|
|
public function order()
|
|
{
|
|
return $this->belongsTo(Order::class);
|
|
}
|
|
}
|