|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Orkhanahmadov\YandexCheckout\Models; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Container\Container; |
|
6
|
|
|
use Illuminate\Database\Eloquent\Builder; |
|
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
8
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphTo; |
|
9
|
|
|
use Illuminate\Support\Arr; |
|
10
|
|
|
use Orkhanahmadov\YandexCheckout\YandexCheckoutService; |
|
11
|
|
|
use YandexCheckout\Request\Payments\Payment\CreateCaptureRequestInterface; |
|
12
|
|
|
use YandexCheckout\Request\Refunds\CreateRefundRequestInterface; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @property int $id |
|
16
|
|
|
* @property string $payable_type |
|
17
|
|
|
* @property int $payable_id |
|
18
|
|
|
* @property string $payment_id |
|
19
|
|
|
* @property string $status |
|
20
|
|
|
* @property array $response |
|
21
|
|
|
* @property bool $succeeded |
|
22
|
|
|
* @property bool $paid |
|
23
|
|
|
* @property string|null $confirmation_url |
|
24
|
|
|
* @property string|null $cancellation_reason |
|
25
|
|
|
* @method static Builder succeeded() |
|
26
|
|
|
* @method static Builder pending() |
|
27
|
|
|
*/ |
|
28
|
|
|
class YandexCheckout extends Model |
|
29
|
|
|
{ |
|
30
|
|
|
public const STATUS_SUCCEEDED = 'succeeded'; |
|
31
|
|
|
public const STATUS_CANCELED = 'canceled'; |
|
32
|
|
|
|
|
33
|
|
|
protected $guarded = []; |
|
34
|
|
|
|
|
35
|
|
|
protected $casts = [ |
|
36
|
|
|
'response' => 'array', |
|
37
|
|
|
]; |
|
38
|
|
|
|
|
39
|
|
|
public function __construct(array $attributes = []) |
|
40
|
|
|
{ |
|
41
|
|
|
$this->setTable(config('yandex-checkout.table_name')); |
|
42
|
|
|
|
|
43
|
|
|
parent::__construct($attributes); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param CreateCaptureRequestInterface|array $captureRequest |
|
48
|
|
|
* @return YandexCheckout |
|
49
|
|
|
*/ |
|
50
|
|
|
public function capturePayment($captureRequest): YandexCheckout |
|
51
|
|
|
{ |
|
52
|
|
|
/** @var YandexCheckoutService $yandexCheckout */ |
|
53
|
|
|
$yandexCheckout = Container::getInstance()->make(YandexCheckoutService::class); |
|
54
|
|
|
|
|
55
|
|
|
$yandexCheckout->capturePayment($this, $captureRequest, $this->payable->yandexCheckoutIdempotenceKey()); |
|
56
|
|
|
|
|
57
|
|
|
return $this->refresh(); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function cancelPayment(): YandexCheckout |
|
61
|
|
|
{ |
|
62
|
|
|
/** @var YandexCheckoutService $yandexCheckout */ |
|
63
|
|
|
$yandexCheckout = Container::getInstance()->make(YandexCheckoutService::class); |
|
64
|
|
|
|
|
65
|
|
|
$yandexCheckout->cancelPayment($this, $this->payable->yandexCheckoutIdempotenceKey()); |
|
66
|
|
|
|
|
67
|
|
|
return $this->refresh(); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param CreateRefundRequestInterface|array $refundRequest |
|
72
|
|
|
* @return YandexCheckout |
|
73
|
|
|
*/ |
|
74
|
|
|
public function refundPayment($refundRequest): YandexCheckout |
|
75
|
|
|
{ |
|
76
|
|
|
/** @var YandexCheckoutService $yandexCheckout */ |
|
77
|
|
|
$yandexCheckout = Container::getInstance()->make(YandexCheckoutService::class); |
|
78
|
|
|
|
|
79
|
|
|
$yandexCheckout->refundPayment($this, $refundRequest, $this->payable->yandexCheckoutIdempotenceKey()); |
|
80
|
|
|
|
|
81
|
|
|
return $this->refresh(); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function payable(): MorphTo |
|
85
|
|
|
{ |
|
86
|
|
|
return $this->morphTo(); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public function getSucceededAttribute(): bool |
|
90
|
|
|
{ |
|
91
|
|
|
return $this->status === self::STATUS_SUCCEEDED; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
public function getPaidAttribute(): bool |
|
95
|
|
|
{ |
|
96
|
|
|
return Arr::get($this->response, 'paid', false); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
public function getRefundableAttribute(): bool |
|
100
|
|
|
{ |
|
101
|
|
|
return Arr::get($this->response, 'refundable', false); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public function getDescriptionAttribute(): bool |
|
105
|
|
|
{ |
|
106
|
|
|
return Arr::get($this->response, 'description'); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
public function getCapturedAtAttribute(): bool |
|
110
|
|
|
{ |
|
111
|
|
|
return Arr::get($this->response, 'captured_at'); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
public function getExpiresAtAttribute(): bool |
|
115
|
|
|
{ |
|
116
|
|
|
return Arr::get($this->response, 'expires_at'); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
public function getConfirmationUrlAttribute(): ?string |
|
120
|
|
|
{ |
|
121
|
|
|
return Arr::get($this->response, 'confirmation.confirmation_url'); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
public function getCancellationReasonAttribute(): ?string |
|
125
|
|
|
{ |
|
126
|
|
|
return Arr::get($this->response, 'cancellation_details.reason'); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
public function scopeSucceeded(Builder $builder): Builder |
|
130
|
|
|
{ |
|
131
|
|
|
return $builder->where('status', self::STATUS_SUCCEEDED); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
public function scopePending(Builder $builder): Builder |
|
135
|
|
|
{ |
|
136
|
|
|
return $builder->whereNotIn('status', [ |
|
137
|
|
|
self::STATUS_SUCCEEDED, |
|
138
|
|
|
self::STATUS_CANCELED, |
|
139
|
|
|
]); |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|