Payment::getPaymentUrlAttribute()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
nc 2
nop 0
dl 0
loc 7
c 0
b 0
f 0
cc 2
rs 10
1
<?php
2
3
namespace Orkhanahmadov\LaravelGoldenpay\Models;
4
5
use Illuminate\Database\Query\Builder;
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Database\Eloquent\Relations\MorphTo;
8
use Illuminate\Support\Facades\Config;
9
use Illuminate\Support\Facades\Crypt;
10
use Orkhanahmadov\Goldenpay\Response\PaymentKey;
11
use Orkhanahmadov\LaravelGoldenpay\QueryBuilders\PaymentQueryBuilder;
12
13
/**
14
 * Class Payment.
15
 *
16
 * @property int $id
17
 * @property string $payable_type
18
 * @property int $payable_id
19
 * @property string $payment_key
20
 * @property int $amount
21
 * @property string $card_type
22
 * @property string $language
23
 * @property string $description
24
 * @property int $status
25
 * @property string $message
26
 * @property string $reference_number
27
 * @property string $card_number
28
 * @property \Carbon\Carbon $payment_date
29
 * @property int $checks
30
 * @property \Carbon\Carbon $created_at
31
 * @property \Carbon\Carbon $updated_at
32
 * @property-read string|null $payment_url
33
 * @property-read float|int $formatted_amount
34
 * @property-read string|null $card_number_decrypted
35
 * @property-read bool $successful
36
 * @method static Payment first()
37
 * @method static Builder whereSuccessful()
38
 * @method static Builder wherePending()
39
 */
40
class Payment extends Model
41
{
42
    protected $guarded = [
43
        'status',
44
    ];
45
46
    protected $dates = [
47
        'payment_date',
48
    ];
49
50
    protected $hidden = [
51
        'card_number',
52
    ];
53
54
    protected $casts = [
55
        'amount' => 'int',
56
        'status' => 'int',
57
        'checks' => 'int',
58
    ];
59
60
    public const STATUS_SUCCESSFUL = 1;
61
62
    public const MINIMUM_REQUIRED_CHECKS = 3;
63
64
    /**
65
     * Payment constructor.
66
     *
67
     * @param array $attributes
68
     */
69
    public function __construct(array $attributes = [])
70
    {
71
        $this->setTable(Config::get('goldenpay.table_name'));
72
73
        parent::__construct($attributes);
74
    }
75
76
    /**
77
     * @param Builder $query
78
     *
79
     * @return PaymentQueryBuilder
80
     */
81
    public function newEloquentBuilder($query): PaymentQueryBuilder
82
    {
83
        return new PaymentQueryBuilder($query);
84
    }
85
86
    /**
87
     * Returns payment's related model.
88
     *
89
     * @return MorphTo
90
     */
91
    public function payable(): MorphTo
92
    {
93
        return $this->morphTo();
94
    }
95
96
    /**
97
     * "payment_url" accessor.
98
     * Used to get "Goldenpay payment page url" from Payment model instance.
99
     * Returns "null" if payment is considered successful.
100
     *
101
     * @return string|null
102
     */
103
    public function getPaymentUrlAttribute(): ?string
104
    {
105
        if ($this->successful) {
106
            return null;
107
        }
108
109
        return (new PaymentKey($this->payment_key))->paymentUrl();
110
    }
111
112
    /**
113
     * "successful" accessor.
114
     * Used on Payment model instances to get if payment considered successful.
115
     *
116
     * @return bool
117
     */
118
    public function getSuccessfulAttribute(): bool
119
    {
120
        return $this->status === self::STATUS_SUCCESSFUL;
121
    }
122
123
    /**
124
     * "formatted_amount" accessor.
125
     * Because all amount related values stored as integer,
126
     * this accessor is used to return values as decimal.
127
     *
128
     * @return float|int
129
     */
130
    public function getFormattedAmountAttribute()
131
    {
132
        return $this->amount / 100;
133
    }
134
135
    /**
136
     * "card_number_decrypted" accessor.
137
     * Returns decrypted value for "card_number".
138
     * Returns null if "encrypt_card_numbers" setting turned off or "card_number" is not available.
139
     *
140
     * @return string|null
141
     */
142
    public function getCardNumberDecryptedAttribute(): ?string
143
    {
144
        if (Config::get('goldenpay.encrypt_card_numbers') && $this->card_number) {
145
            return Crypt::decrypt($this->card_number);
146
        }
147
148
        return null;
149
    }
150
151
    /**
152
     * Mutator for encrypting "card_number" data.
153
     *
154
     * @param string|null $value
155
     */
156
    public function setCardNumberAttribute(?string $value): void
157
    {
158
        if (Config::get('goldenpay.encrypt_card_numbers') && $value) {
159
            $this->attributes['card_number'] = Crypt::encrypt($value);
160
        } else {
161
            $this->attributes['card_number'] = $value;
162
        }
163
    }
164
}
165