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/PaymentGateway.php
<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;
use App\Library\Traits\HasUid;
use App\Library\Facades\Billing;

class PaymentGateway extends Model
{
    use \Illuminate\Database\Eloquent\Factories\HasFactory;
    use HasUid;

    protected static function newFactory()
    {
        return \Database\Factories\PaymentGatewayFactory::new();
    }

    public const STATUS_ACTIVE = 'active';
    public const STATUS_INACTIVE = 'inactive';

    protected $fillable = [
        'customer_id',
        'type',
        'name',
        'description',
        'status',
        'gatewayData',
    ];

    public function paymentMethods()
    {
        return $this->hasMany('App\Model\PaymentMethod', 'payment_gateway_id');
    }

    public function customer()
    {
        return $this->belongsTo('App\Model\Customer', 'customer_id');
    }

    public static function scopeGlobal($query)
    {
        return $query->whereNull('customer_id');
    }

    public static function scopeOfCustomer($query, $customer)
    {
        $customerId = is_object($customer) ? $customer->id : $customer;
        return $query->where('customer_id', $customerId);
    }

    public static function scopeSearch($query, $keyword)
    {
        $keyword = strtolower(trim($keyword));

        // search by keyword
        if ($keyword) {
            $query = $query->whereRaw('LOWER(name) LIKE ? OR LOWER(description) LIKE ?', ['%'.$keyword.'%', '%'.$keyword.'%']);
        }
    }

    public static function newDefault($type)
    {
        $paymentGateway = new static();
        $paymentGateway->type = $type;
        $paymentGateway->status = self::STATUS_ACTIVE;

        // Default name and description
        $service = Billing::getGateways()[$paymentGateway->type];
        $paymentGateway->name = $service['name'];
        $paymentGateway->description = $service['description'];

        return $paymentGateway;
    }

    public function getGatewayDatas()
    {
        if (!$this->gatewayData) {
            return [];
        }

        return json_decode($this->gatewayData, true);
    }

    public function getGatewayData($key)
    {
        $data = $this->getGatewayDatas();

        return array_get($data, $key) ?? null;
    }

    public function savePaymentGateway($name, $description, $gatewayData)
    {
        $this->name = $name;
        $this->description = $description;
        $this->gatewayData = json_encode($gatewayData);

        // validation
        $validator = \Validator::make([
            'name' => $name,
            'description' => $description,
            'gateway_data' => $gatewayData,
        ], [
            'name' => 'required',
            'description' => 'required',
            'gateway_data' => 'required|array',
        ]);

        // errors
        if ($validator->fails()) {
            return $validator;
        }

        // save
        $this->save();

        return $validator;
    }

    public function disable()
    {
        $this->status = self::STATUS_INACTIVE;
        $this->save();
    }

    public function enable()
    {
        $this->status = self::STATUS_ACTIVE;
        $this->save();
    }

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

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

    public static function scopeActive($query)
    {
        return $query->where('status', self::STATUS_ACTIVE);
    }
}