You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
xshop/app/Models/Invoice.php

151 lines
4.1 KiB
PHTML

8 months ago
<?php
namespace App\Models;
use App\Events\InvoiceFailed;
use App\Events\InvoiceSucceed;
8 months ago
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
8 months ago
class Invoice extends Model
{
use HasFactory,SoftDeletes;
const PENDING = 'PENDING';
const PROCESSING = 'PROCESSING';
const COMPLETED = 'COMPLETED';
const CANCELED = 'CANCELED';
const FAILED = 'FAILED';
protected $casts = [
'meta' => 'array',
];
public static $invoiceStatus = ['PENDING', 'CANCELED', 'FAILED', 'PAID', 'PROCESSING', 'COMPLETED'];
public function getRouteKey()
{
return 'hash';
}
public function customer()
{
return $this->belongsTo(Customer::class);
}
public function payments()
{
return $this->hasMany(Payment::class);
}
public function successPayments()
{
return $this->hasMany(Payment::class)->where('status', 'COMPLETED');
}
public function payByBankUrl($gateway)
{
return route('redirect.bank', ['invoice' => $this->id, 'gateway' => $gateway]);
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function products()
{
return $this->belongsToMany(Product::class, 'invoice_product')
->withPivot(
'count',
'price_total',
'data',
'quantity_id'
);
}
public function isCompleted()
{
return $this->status == 'COMPLETED' or $this->status == 'PROCESSING';
}
public function orders()
{
return $this->hasMany(Order::class);
}
protected static function boot()
{
parent::boot();
static::creating(function ($model) {
$model->hash = generateUniqueID((strlen(Invoice::count()) + 2));
});
}
public function storePaymentRequest($orderId,$amount, $token = null, $type = 'ONLINE', $bank = null): \App\Models\Payment
{
$payment = new Payment();
$payment->order_id = $orderId;
$payment->type = $type?$type:'ONLINE';
$payment->amount=$amount;
$payment->meta = [
'fingerprint' => \Request::fingerprint(),
'bank' => $bank,
'token' => $token,
'ip' => \Request::ip(),
'auth_user' => \Auth::id(),
'user_agent' => \Request::userAgent(),
];
/** @var \App\Models\Invoice $this */
$this->payments()->save($payment);
// $payment->save();
return $payment;
}
public function storeSuccessPayment($paymentId, $referenceId, $cardNumber = null): \App\Models\Payment
{
/** @var Payment $payment */
$payment = Payment::findOrFail($paymentId);
$payment->reference_id = $referenceId;
$payment->meta = array_merge($payment->meta, ['card_number' => $cardNumber]);
$payment->status = "SUCCESS";
$payment->save();
/** @var \App\Models\Invoice $this */
$this->status = "PAID";
$this->save();
try {
event(new InvoiceSucceed($this, $payment));
}catch (\Throwable $exception){
\Log::debug('Error In Event OrderSucceed. But Process Continued!',compact('payment'));
\Log::warning($exception->getMessage(),[$exception->getTraceAsString()]);
}
return $payment;
}
public function storeFailPayment($paymentId, $message = null): \App\Models\Payment
{
try {
/** @var Payment $payment */
$payment = Payment::findOrFail($paymentId);
if ($payment->status === Payment::SUCCESS) {
return $payment;
}
$payment->status = Payment::FAIL;
$payment->comment = $message;
$payment->save();
} catch (\Throwable $exception) {
$payment = new Payment();
}
$this->status = "FAILED";
/** @var \App\Models\Invoice $this */
$this->save();
event(new InvoiceFailed($this, $payment));
return $payment;
}
8 months ago
}