2017-01-30 20:40:43 +01:00
|
|
|
<?php
|
2015-03-16 22:45:25 +01:00
|
|
|
|
2017-01-30 20:40:43 +01:00
|
|
|
namespace App\Models;
|
|
|
|
|
|
|
|
use App\Events\PaymentCompleted;
|
|
|
|
use App\Events\PaymentFailed;
|
2015-10-28 20:22:07 +01:00
|
|
|
use App\Events\PaymentWasCreated;
|
2016-04-23 22:40:19 +02:00
|
|
|
use App\Events\PaymentWasRefunded;
|
2016-05-06 23:05:42 +02:00
|
|
|
use App\Events\PaymentWasVoided;
|
2017-01-30 20:40:43 +01:00
|
|
|
use Event;
|
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
2015-11-12 21:36:28 +01:00
|
|
|
use Laracasts\Presenter\PresentableTrait;
|
2015-03-31 11:38:24 +02:00
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
2017-01-30 20:40:43 +01:00
|
|
|
* Class Payment.
|
2016-07-03 18:11:58 +02:00
|
|
|
*/
|
2015-03-16 22:45:25 +01:00
|
|
|
class Payment extends EntityModel
|
|
|
|
{
|
2015-11-12 21:36:28 +01:00
|
|
|
use PresentableTrait;
|
2015-03-31 11:38:24 +02:00
|
|
|
use SoftDeletes;
|
2015-11-12 21:36:28 +01:00
|
|
|
|
2016-12-26 18:45:38 +01:00
|
|
|
public static $statusClasses = [
|
|
|
|
PAYMENT_STATUS_PENDING => 'info',
|
|
|
|
PAYMENT_STATUS_COMPLETED => 'success',
|
|
|
|
PAYMENT_STATUS_FAILED => 'danger',
|
|
|
|
PAYMENT_STATUS_PARTIALLY_REFUNDED => 'primary',
|
|
|
|
PAYMENT_STATUS_VOIDED => 'default',
|
|
|
|
PAYMENT_STATUS_REFUNDED => 'default',
|
|
|
|
];
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2015-03-31 11:38:24 +02:00
|
|
|
protected $dates = ['deleted_at'];
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2015-11-12 21:36:28 +01:00
|
|
|
protected $presenter = 'App\Ninja\Presenters\PaymentPresenter';
|
2015-03-31 11:38:24 +02:00
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2015-03-16 22:45:25 +01:00
|
|
|
public function invoice()
|
|
|
|
{
|
2015-03-31 11:38:24 +02:00
|
|
|
return $this->belongsTo('App\Models\Invoice')->withTrashed();
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
|
|
*/
|
2015-03-16 22:45:25 +01:00
|
|
|
public function invitation()
|
|
|
|
{
|
2015-03-31 11:38:24 +02:00
|
|
|
return $this->belongsTo('App\Models\Invitation');
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2015-03-16 22:45:25 +01:00
|
|
|
public function client()
|
|
|
|
{
|
2015-03-31 11:38:24 +02:00
|
|
|
return $this->belongsTo('App\Models\Client')->withTrashed();
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2015-06-14 19:30:01 +02:00
|
|
|
public function user()
|
|
|
|
{
|
|
|
|
return $this->belongsTo('App\Models\User')->withTrashed();
|
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
|
|
*/
|
2015-03-16 22:45:25 +01:00
|
|
|
public function account()
|
|
|
|
{
|
2015-03-31 11:38:24 +02:00
|
|
|
return $this->belongsTo('App\Models\Account');
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
|
|
*/
|
2015-03-16 22:45:25 +01:00
|
|
|
public function contact()
|
|
|
|
{
|
2015-03-31 11:38:24 +02:00
|
|
|
return $this->belongsTo('App\Models\Contact');
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
|
|
*/
|
2015-11-12 21:36:28 +01:00
|
|
|
public function account_gateway()
|
|
|
|
{
|
2016-08-02 12:06:04 +02:00
|
|
|
return $this->belongsTo('App\Models\AccountGateway')->withTrashed();
|
2015-11-12 21:36:28 +01:00
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
|
|
*/
|
2015-11-12 21:36:28 +01:00
|
|
|
public function payment_type()
|
|
|
|
{
|
|
|
|
return $this->belongsTo('App\Models\PaymentType');
|
2016-05-11 00:46:32 +02:00
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
|
|
*/
|
2016-05-11 00:46:32 +02:00
|
|
|
public function payment_method()
|
|
|
|
{
|
|
|
|
return $this->belongsTo('App\Models\PaymentMethod');
|
|
|
|
}
|
2016-04-23 22:40:19 +02:00
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
|
|
*/
|
2016-04-23 22:40:19 +02:00
|
|
|
public function payment_status()
|
|
|
|
{
|
|
|
|
return $this->belongsTo('App\Models\PaymentStatus');
|
2015-11-12 21:36:28 +01:00
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2015-10-28 20:22:07 +01:00
|
|
|
public function getRoute()
|
|
|
|
{
|
|
|
|
return "/payments/{$this->public_id}/edit";
|
|
|
|
}
|
|
|
|
|
2015-12-07 14:34:55 +01:00
|
|
|
/*
|
2015-03-16 22:45:25 +01:00
|
|
|
public function getAmount()
|
|
|
|
{
|
2015-06-10 10:34:20 +02:00
|
|
|
return Utils::formatMoney($this->amount, $this->client->getCurrencyId());
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
2015-12-07 14:34:55 +01:00
|
|
|
*/
|
2015-03-16 22:45:25 +01:00
|
|
|
|
2016-08-28 12:20:16 +02:00
|
|
|
public function scopeExcludeFailed($query)
|
|
|
|
{
|
|
|
|
$query->whereNotIn('payment_status_id', [PAYMENT_STATUS_VOIDED, PAYMENT_STATUS_FAILED]);
|
|
|
|
|
|
|
|
return $query;
|
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2015-03-16 22:45:25 +01:00
|
|
|
public function getName()
|
|
|
|
{
|
|
|
|
return trim("payment {$this->transaction_reference}");
|
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
2016-04-23 22:40:19 +02:00
|
|
|
public function isPending()
|
|
|
|
{
|
2016-06-23 19:17:02 +02:00
|
|
|
return $this->payment_status_id == PAYMENT_STATUS_PENDING;
|
2016-04-23 22:40:19 +02:00
|
|
|
}
|
2016-05-17 13:55:53 +02:00
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
2016-04-23 22:40:19 +02:00
|
|
|
public function isFailed()
|
|
|
|
{
|
2016-06-23 19:17:02 +02:00
|
|
|
return $this->payment_status_id == PAYMENT_STATUS_FAILED;
|
2016-04-23 22:40:19 +02:00
|
|
|
}
|
2016-05-17 13:55:53 +02:00
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
2016-04-23 22:40:19 +02:00
|
|
|
public function isCompleted()
|
|
|
|
{
|
2016-05-06 23:05:42 +02:00
|
|
|
return $this->payment_status_id == PAYMENT_STATUS_COMPLETED;
|
2016-04-23 22:40:19 +02:00
|
|
|
}
|
2016-05-17 13:55:53 +02:00
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
2016-04-23 22:40:19 +02:00
|
|
|
public function isPartiallyRefunded()
|
|
|
|
{
|
|
|
|
return $this->payment_status_id == PAYMENT_STATUS_PARTIALLY_REFUNDED;
|
|
|
|
}
|
2016-05-17 13:55:53 +02:00
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
2016-04-23 22:40:19 +02:00
|
|
|
public function isRefunded()
|
|
|
|
{
|
|
|
|
return $this->payment_status_id == PAYMENT_STATUS_REFUNDED;
|
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
2016-05-06 23:05:42 +02:00
|
|
|
public function isVoided()
|
|
|
|
{
|
|
|
|
return $this->payment_status_id == PAYMENT_STATUS_VOIDED;
|
|
|
|
}
|
|
|
|
|
2016-08-28 13:55:53 +02:00
|
|
|
public function isFailedOrVoided()
|
|
|
|
{
|
|
|
|
return $this->isFailed() || $this->isVoided();
|
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @param null $amount
|
2017-01-30 20:40:43 +01:00
|
|
|
*
|
2016-07-03 18:11:58 +02:00
|
|
|
* @return bool
|
|
|
|
*/
|
2016-04-23 22:40:19 +02:00
|
|
|
public function recordRefund($amount = null)
|
|
|
|
{
|
2016-06-20 16:14:43 +02:00
|
|
|
if ($this->isRefunded() || $this->isVoided()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-01-30 20:40:43 +01:00
|
|
|
if (! $amount) {
|
2016-06-20 16:14:43 +02:00
|
|
|
$amount = $this->amount;
|
|
|
|
}
|
2016-05-17 13:55:53 +02:00
|
|
|
|
2016-06-20 16:14:43 +02:00
|
|
|
$new_refund = min($this->amount, $this->refunded + $amount);
|
|
|
|
$refund_change = $new_refund - $this->refunded;
|
2016-05-17 13:55:53 +02:00
|
|
|
|
2016-06-20 16:14:43 +02:00
|
|
|
if ($refund_change) {
|
|
|
|
$this->refunded = $new_refund;
|
|
|
|
$this->payment_status_id = $this->refunded == $this->amount ? PAYMENT_STATUS_REFUNDED : PAYMENT_STATUS_PARTIALLY_REFUNDED;
|
|
|
|
$this->save();
|
2016-05-17 13:55:53 +02:00
|
|
|
|
2016-06-20 16:14:43 +02:00
|
|
|
Event::fire(new PaymentWasRefunded($this, $refund_change));
|
2016-04-23 22:40:19 +02:00
|
|
|
}
|
2016-06-20 16:14:43 +02:00
|
|
|
|
|
|
|
return true;
|
2016-04-23 22:40:19 +02:00
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
2016-05-06 23:05:42 +02:00
|
|
|
public function markVoided()
|
|
|
|
{
|
2016-06-20 16:14:43 +02:00
|
|
|
if ($this->isVoided() || $this->isPartiallyRefunded() || $this->isRefunded()) {
|
|
|
|
return false;
|
2016-05-06 23:05:42 +02:00
|
|
|
}
|
2016-06-20 16:14:43 +02:00
|
|
|
|
2017-04-03 16:21:12 +02:00
|
|
|
Event::fire(new PaymentWasVoided($this));
|
|
|
|
|
2016-06-20 16:14:43 +02:00
|
|
|
$this->refunded = $this->amount;
|
|
|
|
$this->payment_status_id = PAYMENT_STATUS_VOIDED;
|
|
|
|
$this->save();
|
|
|
|
|
|
|
|
return true;
|
2016-05-06 23:05:42 +02:00
|
|
|
}
|
|
|
|
|
2016-04-30 23:54:56 +02:00
|
|
|
public function markComplete()
|
|
|
|
{
|
|
|
|
$this->payment_status_id = PAYMENT_STATUS_COMPLETED;
|
|
|
|
$this->save();
|
|
|
|
Event::fire(new PaymentCompleted($this));
|
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @param string $failureMessage
|
|
|
|
*/
|
2016-06-24 17:15:51 +02:00
|
|
|
public function markFailed($failureMessage = '')
|
2016-04-30 23:54:56 +02:00
|
|
|
{
|
|
|
|
$this->payment_status_id = PAYMENT_STATUS_FAILED;
|
|
|
|
$this->gateway_error = $failureMessage;
|
|
|
|
$this->save();
|
|
|
|
Event::fire(new PaymentFailed($this));
|
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2015-03-16 22:45:25 +01:00
|
|
|
public function getEntityType()
|
|
|
|
{
|
|
|
|
return ENTITY_PAYMENT;
|
|
|
|
}
|
2016-05-11 00:46:32 +02:00
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2016-05-17 13:55:53 +02:00
|
|
|
public function getCompletedAmount()
|
|
|
|
{
|
|
|
|
return $this->amount - $this->refunded;
|
|
|
|
}
|
|
|
|
|
2017-02-09 11:46:58 +01:00
|
|
|
public function canBeRefunded()
|
|
|
|
{
|
|
|
|
return $this->getCompletedAmount() > 0 && ($this->isCompleted() || $this->isPartiallyRefunded());
|
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @return mixed|null|\stdClass|string
|
|
|
|
*/
|
2016-05-15 22:27:56 +02:00
|
|
|
public function getBankDataAttribute()
|
2016-05-11 00:46:32 +02:00
|
|
|
{
|
2017-01-30 20:40:43 +01:00
|
|
|
if (! $this->routing_number) {
|
2016-05-11 00:46:32 +02:00
|
|
|
return null;
|
|
|
|
}
|
2017-01-30 20:40:43 +01:00
|
|
|
|
2016-05-11 00:46:32 +02:00
|
|
|
return PaymentMethod::lookupBankData($this->routing_number);
|
|
|
|
}
|
2016-05-11 01:17:41 +02:00
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @param $bank_name
|
2017-01-30 20:40:43 +01:00
|
|
|
*
|
2016-07-03 18:11:58 +02:00
|
|
|
* @return null
|
|
|
|
*/
|
2016-05-24 20:16:42 +02:00
|
|
|
public function getBankNameAttribute($bank_name)
|
|
|
|
{
|
|
|
|
if ($bank_name) {
|
|
|
|
return $bank_name;
|
|
|
|
}
|
|
|
|
$bankData = $this->bank_data;
|
|
|
|
|
2017-01-30 20:40:43 +01:00
|
|
|
return $bankData ? $bankData->name : null;
|
2016-05-24 20:16:42 +02:00
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @param $value
|
2017-01-30 20:40:43 +01:00
|
|
|
*
|
2016-07-03 18:11:58 +02:00
|
|
|
* @return null|string
|
|
|
|
*/
|
2016-05-11 01:17:41 +02:00
|
|
|
public function getLast4Attribute($value)
|
|
|
|
{
|
|
|
|
return $value ? str_pad($value, 4, '0', STR_PAD_LEFT) : null;
|
|
|
|
}
|
2016-12-26 18:45:38 +01:00
|
|
|
|
|
|
|
public static function calcStatusLabel($statusId, $statusName, $amount)
|
|
|
|
{
|
|
|
|
if ($statusId == PAYMENT_STATUS_PARTIALLY_REFUNDED) {
|
|
|
|
return trans('texts.status_partially_refunded_amount', [
|
|
|
|
'amount' => $amount,
|
|
|
|
]);
|
|
|
|
} else {
|
|
|
|
return trans('texts.status_' . strtolower($statusName));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function calcStatusClass($statusId)
|
|
|
|
{
|
|
|
|
return static::$statusClasses[$statusId];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function statusClass()
|
|
|
|
{
|
|
|
|
return static::calcStatusClass($this->payment_status_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function statusLabel()
|
|
|
|
{
|
|
|
|
$amount = $this->account->formatMoney($this->refunded, $this->client);
|
2017-01-30 20:40:43 +01:00
|
|
|
|
2016-12-26 18:45:38 +01:00
|
|
|
return static::calcStatusLabel($this->payment_status_id, $this->payment_status->name, $amount);
|
|
|
|
}
|
|
|
|
|
2017-01-08 22:34:44 +01:00
|
|
|
public function invoiceJsonBackup()
|
|
|
|
{
|
|
|
|
$activity = Activity::wherePaymentId($this->id)
|
|
|
|
->whereActivityTypeId(ACTIVITY_TYPE_CREATE_PAYMENT)
|
|
|
|
->get(['json_backup'])
|
|
|
|
->first();
|
|
|
|
|
|
|
|
return $activity->json_backup;
|
|
|
|
}
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
2015-10-28 20:22:07 +01:00
|
|
|
Payment::creating(function ($payment) {
|
2015-03-16 22:45:25 +01:00
|
|
|
});
|
|
|
|
|
2015-10-28 20:22:07 +01:00
|
|
|
Payment::created(function ($payment) {
|
|
|
|
event(new PaymentWasCreated($payment));
|
2016-05-17 13:55:53 +02:00
|
|
|
});
|