Issues (7)

src/YandexCheckoutService.php (2 issues)

Labels
Severity
1
<?php
2
3
namespace Orkhanahmadov\YandexCheckout;
4
5
use Illuminate\Contracts\Config\Repository;
6
use Illuminate\Database\Eloquent\Model;
7
use Orkhanahmadov\YandexCheckout\Models\YandexCheckout;
8
use YooKassa\Client;
9
use YooKassa\Request\Payments\AbstractPaymentResponse;
10
use YooKassa\Request\Payments\CreatePaymentRequestInterface;
11
use YooKassa\Request\Payments\Payment\CreateCaptureRequestInterface;
12
use YooKassa\Request\Refunds\CreateRefundRequestInterface;
13
14
class YandexCheckoutService
15
{
16
    /**
17
     * @var Client
18
     */
19
    private $client;
20
21
    public function __construct(Repository $config)
22
    {
23
        $this->client = new Client();
24
        $this->client->setAuth(
25
            $config->get('yandex-checkout.shop_id'),
26
            $config->get('yandex-checkout.secret_key')
27
        );
28
    }
29
30
    /**
31
     * @param  Model  $model
32
     * @param  CreatePaymentRequestInterface|array  $paymentRequest
33
     * @param  string|null  $idempotenceKey
34
     * @return YandexCheckout
35
     */
36
    public function createPayment(Model $model, $paymentRequest, ?string $idempotenceKey = null): YandexCheckout
37
    {
38
        $paymentResponse = $this->client->createPayment($paymentRequest, $idempotenceKey);
39
40
        $yandexCheckoutModel = new YandexCheckout();
41
        $yandexCheckoutModel->payable_type = get_class($model);
42
        $yandexCheckoutModel->payable_id = $model->getKey();
43
        $yandexCheckoutModel->payment_id = $paymentResponse->getId();
44
        $yandexCheckoutModel->status = $paymentResponse->getStatus();
45
        $yandexCheckoutModel->response = $paymentResponse->jsonSerialize();
46
        $yandexCheckoutModel->save();
47
48
        $this->dispatchEvent($yandexCheckoutModel, 'created');
49
50
        return $yandexCheckoutModel;
51
    }
52
53
    /**
54
     * @param  YandexCheckout  $model
55
     * @return YandexCheckout
56
     */
57
    public function paymentInfo(YandexCheckout $model): YandexCheckout
58
    {
59
        $paymentResponse = $this->client->getPaymentInfo($model->payment_id);
60
61
        $model = $this->updateCheckoutModel($model, $paymentResponse);
62
63
        $this->dispatchEvent($model, 'checked');
64
        $this->dispatchEvent($model);
65
66
        return $model;
67
    }
68
69
    /**
70
     * @param  YandexCheckout  $model
71
     * @param  CreateCaptureRequestInterface|array  $captureRequest
72
     * @param  string|null  $idempotenceKey
73
     * @return YandexCheckout
74
     */
75
    public function capturePayment(YandexCheckout $model, $captureRequest, ?string $idempotenceKey = null): YandexCheckout
76
    {
77
        $captureResponse = $this->client->capturePayment($captureRequest, $model->payment_id, $idempotenceKey);
78
79
        $model = $this->updateCheckoutModel($model, $captureResponse);
0 ignored issues
show
It seems like $captureResponse can also be of type null; however, parameter $yandexResponse of Orkhanahmadov\YandexChec...::updateCheckoutModel() does only seem to accept YooKassa\Request\Payments\AbstractPaymentResponse, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

79
        $model = $this->updateCheckoutModel($model, /** @scrutinizer ignore-type */ $captureResponse);
Loading history...
80
81
        $this->dispatchEvent($model);
82
83
        return $model;
84
    }
85
86
    /**
87
     * @param  YandexCheckout  $model
88
     * @param  string|null  $idempotenceKey
89
     * @return YandexCheckout
90
     */
91
    public function cancelPayment(YandexCheckout $model, ?string $idempotenceKey = null): YandexCheckout
92
    {
93
        $cancelResponse = $this->client->cancelPayment($model->payment_id, $idempotenceKey);
94
95
        $model = $this->updateCheckoutModel($model, $cancelResponse);
0 ignored issues
show
It seems like $cancelResponse can also be of type null; however, parameter $yandexResponse of Orkhanahmadov\YandexChec...::updateCheckoutModel() does only seem to accept YooKassa\Request\Payments\AbstractPaymentResponse, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

95
        $model = $this->updateCheckoutModel($model, /** @scrutinizer ignore-type */ $cancelResponse);
Loading history...
96
97
        $this->dispatchEvent($model);
98
99
        return $model;
100
    }
101
102
    /**
103
     * @param  YandexCheckout  $model
104
     * @param  CreateRefundRequestInterface|array  $refundRequest
105
     * @param  string|null  $idempotenceKey
106
     * @return YandexCheckout
107
     */
108
    public function refundPayment(YandexCheckout $model, $refundRequest, ?string $idempotenceKey = null)
109
    {
110
        $refundResponse = $this->client->createRefund($refundRequest, $idempotenceKey);
111
112
        if ($refundResponse->getStatus() === YandexCheckout::STATUS_SUCCEEDED) {
113
            $this->dispatchEvent($model, 'refunded');
114
115
            $model = $this->paymentInfo($model);
116
        }
117
118
        return $model;
119
    }
120
121
    private function updateCheckoutModel(YandexCheckout $model, AbstractPaymentResponse $yandexResponse): YandexCheckout
122
    {
123
        $model->status = $yandexResponse->getStatus();
124
        $model->response = $yandexResponse->jsonSerialize();
125
        $model->save();
126
127
        return $model;
128
    }
129
130
    private function dispatchEvent(YandexCheckout $yandexCheckout, ?string $name = null): void
131
    {
132
        $name = $name ?? $yandexCheckout->status;
133
134
        if ($event = config("yandex-checkout.events.{$name}")) {
135
            $event::dispatch($yandexCheckout);
136
        }
137
    }
138
}
139