PayuTransaction::invalid()   A
last analyzed

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 0
1
<?php
2
3
namespace Tzsk\Payu\Models;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Database\Eloquent\Relations\MorphTo;
8
use Illuminate\Database\Eloquent\SoftDeletes;
9
use Illuminate\Support\Carbon;
10
use Tzsk\Payu\Concerns\Transaction;
11
use Tzsk\Payu\Gateway\Gateway;
12
use Tzsk\Payu\Gateway\PayuBiz;
13
use Tzsk\Payu\Gateway\PayuMoney;
14
use Tzsk\Payu\Jobs\VerifyTransaction;
15
use Tzsk\Payu\Models\Casts\Serialized;
16
17
/**
18
 * Class PayuPayments
19
 * @package Tzsk\Payu\Models
20
 *
21
 * @property string $transaction_id;
22
 * @property PayuMoney|PayuBiz $gateway
23
 * @property Transaction $body
24
 * @property string $destination
25
 * @property string $hash
26
 * @property string $status
27
 * @property Carbon $verified_at
28
 * @method static Builder verifiable()
29
 * @method static self locate(string $transaction_id)
30
 * @method string response(string $key)
31
 */
32
class PayuTransaction extends Model
33
{
34
    use SoftDeletes;
35
36
    const STATUS_PENDING = 'pending';
37
    const STATUS_FAILED = 'failed';
38
    const STATUS_SUCCESSFUL = 'successful';
39
    const STATUS_INVALID = 'invalid';
40
41
    protected $fillable = [
42
        'transaction_id', 'paid_for_id', 'paid_for_type', 'gateway', 'body',
43
        'destination', 'hash', 'response', 'status', 'verified_at',
44
    ];
45
46
    protected $casts = [
47
        'gateway' => Serialized::class,
48
        'body' => Serialized::class,
49
        'response' => 'array',
50
    ];
51
52
    protected $dates = [
53
        'verified_at',
54
    ];
55
56
    public function paidFor(): MorphTo
57
    {
58
        return $this->morphTo('paid_for');
59
    }
60
61
    public function scopePending(Builder $builder): Builder
62
    {
63
        return $builder->where('status', self::STATUS_PENDING);
64
    }
65
66
    public function scopeFailed(Builder $builder): Builder
67
    {
68
        return $builder->where('status', self::STATUS_FAILED);
69
    }
70
71
    public function scopeSuccessful(Builder $builder): Builder
72
    {
73
        return $builder->where('status', self::STATUS_SUCCESSFUL);
74
    }
75
76
    public function scopeVerifiable(Builder $builder): Builder
77
    {
78
        return $builder->whereIn('status', config('payu.verify', []))->whereNull('verified_at');
79
    }
80
81
    public function scopeLocate(Builder $builder, string $id): ?self
82
    {
83
        /** @var self $row */
84
        $row = $builder->where('transaction_id', $id)->firstOrFail();
85
86
        return $row;
87
    }
88
89
    public function successful()
90
    {
91
        return $this->status == self::STATUS_SUCCESSFUL;
92
    }
93
94
    public function failed()
95
    {
96
        return $this->status == self::STATUS_FAILED;
97
    }
98
99
    public function pending()
100
    {
101
        return $this->status == self::STATUS_PENDING;
102
    }
103
104
    public function invalid()
105
    {
106
        return $this->status == self::STATUS_INVALID;
107
    }
108
109
    public function verified()
110
    {
111
        return ! empty($this->verified_at);
112
    }
113
114
    public function shouldVerify()
115
    {
116
        $allowedStatus = in_array($this->status, config('payu.verify', []));
117
        $notChecked = empty($this->verified_at);
118
119
        return $allowedStatus && $notChecked;
120
    }
121
122
    public function verify(): ?self
123
    {
124
        /** @var Gateway $gateway */
125
        $gateway = $this->getAttribute('gateway');
126
        $gateway->verifier()->handle($this);
127
128
        return $this->fresh();
129
    }
130
131
    public function verifyAsync()
132
    {
133
        VerifyTransaction::dispatch($this);
134
    }
135
136
    public function response($key)
137
    {
138
        return data_get($this->getAttribute('response'), $key);
139
    }
140
}
141