Completed
Push — master ( 3ec705...3da051 )
by Orkhan
01:08
created

Payment::scopeSuccessful()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Orkhanahmadov\LaravelGoldenpay\Models;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Database\Eloquent\Relations\MorphTo;
8
use Orkhanahmadov\Goldenpay\Response\PaymentKey;
9
10
/**
11
 * Class Payment
12
 * @package Orkhanahmadov\LaravelGoldenpay\Models
13
 *
14
 * @property int $id
15
 * @property string $payable_type
16
 * @property int $payable_id
17
 * @property string $payment_key
18
 * @property int $amount
19
 * @property string $card_type
20
 * @property string $language
21
 * @property string $description
22
 * @property int $status
23
 * @property string $message
24
 * @property string $reference_number
25
 * @property string $card_number
26
 * @property \Carbon\Carbon $payment_date
27
 * @property int $checks
28
 * @property \Carbon\Carbon $created_at
29
 * @property \Carbon\Carbon $updated_at
30
 * @property-read string $payment_url
31
 * @property-read float|int $formatted_amount
32
 * @property-read bool $successful
33
 * @method static Payment first()
34
 * @method static Builder successful()
35
 * @method static Builder pending()
36
 */
37
class Payment extends Model
38
{
39
    protected $guarded = [
40
        'status',
41
    ];
42
43
    protected $dates = [
44
        'payment_date',
45
    ];
46
47
    protected $hidden = [
48
        'card_number',
49
    ];
50
51
    protected $casts = [
52
        'amount' => 'int',
53
        'status' => 'int',
54
        'checks' => 'int',
55
    ];
56
57
    public const STATUS_SUCCESSFUL = 1;
58
59
    public const MINIMUM_REQUIRED_CHECKS = 5;
60
61
    public function __construct(array $attributes = [])
62
    {
63
        parent::__construct($attributes);
64
65
        $this->setTable(config('goldenpay.table_name'));
66
    }
67
68
    public function payable(): MorphTo
69
    {
70
        return $this->morphTo();
71
    }
72
73
    public function getPaymentUrlAttribute(): string
74
    {
75
        return PaymentKey::PAYMENT_PAGE.$this->payment_key;
76
    }
77
78
    public function getSuccessfulAttribute(): bool
79
    {
80
        return $this->status === self::STATUS_SUCCESSFUL;
81
    }
82
83
    public function getFormattedAmountAttribute()
84
    {
85
        return $this->amount / 100;
86
    }
87
88
    public function scopeSuccessful(Builder $builder): Builder
89
    {
90
        return $builder->whereStatus(self::STATUS_SUCCESSFUL);
91
    }
92
93
    public function scopePending(Builder $builder): Builder
94
    {
95
        return $builder
96
            ->where(function (Builder $query) {
97
                $query->whereNull('status')
98
                    ->orWhere('status', '<>', self::STATUS_SUCCESSFUL);
99
            })
100
            ->where(function (Builder $query) {
101
                $query->where('checks', '<', self::MINIMUM_REQUIRED_CHECKS)
102
                    ->orWhere('created_at', '>=', now()->subMinutes(30));
103
            });
104
    }
105
}
106