HEX
Server: LiteSpeed
System: Linux s1049.use1.mysecurecloudhost.com 4.18.0-477.27.2.lve.el8.x86_64 #1 SMP Wed Oct 11 12:32:56 UTC 2023 x86_64
User: xedaptot (3356)
PHP: 8.3.31
Disabled: NONE
Upload Files
File: /home/xedaptot/ai.naniguide.com/app/Model/Transaction.php
<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;
use App\Library\Traits\HasUid;
use Twilio\TwiML\Voice\Pay;

class Transaction extends Model
{
    use HasUid;
    protected $connection = 'mysql';

    // wait status
    public const STATUS_PENDING = 'pending';
    public const STATUS_FAILED = 'failed';
    public const STATUS_SUCCESS = 'success';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'status', 'payment_method_id', 'error', 'allow_manual_review'
    ];

    /**
     * Invoice.
     */
    public function invoice()
    {
        return $this->belongsTo('App\Model\Invoice');
    }

    public function paymentMethod()
    {
        return $this->belongsTo(PaymentMethod::class, 'payment_method_id');
    }

    /**
     * Is failed.
     */
    public function isFailed()
    {
        return $this->status == self::STATUS_FAILED;
    }

    public function isPending()
    {
        return $this->status == self::STATUS_PENDING;
    }

    /**
     * Set as success.
     */
    public function setSuccess()
    {
        $this->status = self::STATUS_SUCCESS;
        $this->save();
    }

    // Transaction that needs admin review
    public function allowManualReview()
    {
        return $this->allow_manual_review;
    }

    public static function scopePending($query)
    {
        $query = $query->where('status', Transaction::STATUS_PENDING);
    }

    public function approve()
    {
        // for only new invoice
        if (!$this->invoice->isNew()) {
            throw new \Exception("Trying to approve an transaction that its invoice is not NEW (Invoice ID: {$this->id}, status: {$this->status}");
        }

        // fulfill invoice
        $this->invoice->paySuccess();
    }
}