Passed
Push — master ( aee85b...676e48 )
by Orkhan
10:05
created

YandexCheckoutService::refundPayment()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
c 0
b 0
f 0
nc 2
nop 3
dl 0
loc 11
rs 10
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 YandexCheckout\Client;
9
use YandexCheckout\Request\Payments\AbstractPaymentResponse;
10
use YandexCheckout\Request\Payments\CreatePaymentRequestInterface;
11
use YandexCheckout\Request\Payments\Payment\CreateCaptureRequestInterface;
12
use YandexCheckout\Request\Refunds\CreateRefundRequest;
13
use YandexCheckout\Request\Refunds\CreateRefundRequestInterface;
14
15
class YandexCheckoutService
16
{
17
    /**
18
     * @var Client
19
     */
20
    private $client;
21
22
    public function __construct(Repository $config)
23
    {
24
        $this->client = new Client();
25
        $this->client->setAuth(
26
            $config->get('yandex-checkout.shop_id'),
27
            $config->get('yandex-checkout.secret_key')
28
        );
29
    }
30
31
    /**
32
     * @param  Model  $model
33
     * @param  CreatePaymentRequestInterface|array  $paymentRequest
34
     * @param  string|null  $idempotenceKey
35
     * @return YandexCheckout
36
     */
37
    public function createPayment(Model $model, $paymentRequest, ?string $idempotenceKey = null): YandexCheckout
38
    {
39
        $paymentResponse = $this->client->createPayment($paymentRequest, $idempotenceKey);
40
41
        $yandexCheckoutModel = new YandexCheckout();
42
        $yandexCheckoutModel->payable_type = get_class($model);
43
        $yandexCheckoutModel->payable_id = $model->getKey();
44
        $yandexCheckoutModel->payment_id = $paymentResponse->getId();
45
        $yandexCheckoutModel->status = $paymentResponse->getStatus();
46
        $yandexCheckoutModel->response = $paymentResponse->jsonSerialize();
47
        $yandexCheckoutModel->save();
48
49
        $this->dispatchEvent($yandexCheckoutModel, 'created');
50
51
        return $yandexCheckoutModel;
52
    }
53
54
    /**
55
     * @param  YandexCheckout  $model
56
     * @return YandexCheckout
57
     */
58
    public function paymentInfo(YandexCheckout $model): YandexCheckout
59
    {
60
        $paymentResponse = $this->client->getPaymentInfo($model->payment_id);
61
62
        $model = $this->updateCheckoutModel($model, $paymentResponse);
63
64
        $this->dispatchEvent($model, 'checked');
65
        $this->dispatchEvent($model);
66
67
        return $model;
68
    }
69
70
    /**
71
     * @param  YandexCheckout  $model
72
     * @param  CreateCaptureRequestInterface|array  $captureRequest
73
     * @param  string|null  $idempotenceKey
74
     * @return YandexCheckout
75
     */
76
    public function capturePayment(YandexCheckout $model, $captureRequest, ?string $idempotenceKey = null): YandexCheckout
77
    {
78
        $captureResponse = $this->client->capturePayment($captureRequest, $model->payment_id, $idempotenceKey);
79
80
        $model = $this->updateCheckoutModel($model, $captureResponse);
81
82
        $this->dispatchEvent($model);
83
84
        return $model;
85
    }
86
87
    /**
88
     * @param  YandexCheckout  $model
89
     * @param  string|null  $idempotenceKey
90
     * @return YandexCheckout
91
     */
92
    public function cancelPayment(YandexCheckout $model, ?string $idempotenceKey = null): YandexCheckout
93
    {
94
        $cancelResponse = $this->client->cancelPayment($model->payment_id, $idempotenceKey);
95
96
        $model = $this->updateCheckoutModel($model, $cancelResponse);
97
98
        $this->dispatchEvent($model);
99
100
        return $model;
101
    }
102
103
    /**
104
     * @param  YandexCheckout  $model
105
     * @param  CreateRefundRequestInterface|array  $refundRequest
106
     * @param  string|null  $idempotenceKey
107
     * @return YandexCheckout
108
     */
109
    public function refundPayment(YandexCheckout $model, $refundRequest, ?string $idempotenceKey = null)
110
    {
111
        $refundResponse = $this->client->createRefund($refundRequest, $idempotenceKey);
0 ignored issues
show
Bug introduced by
It seems like $idempotenceKey can also be of type string; however, parameter $idempotencyKey of YandexCheckout\Client::createRefund() does only seem to accept null, 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

111
        $refundResponse = $this->client->createRefund($refundRequest, /** @scrutinizer ignore-type */ $idempotenceKey);
Loading history...
112
113
        if ($refundResponse->getStatus() === YandexCheckout::STATUS_SUCCEEDED) {
114
            $this->dispatchEvent($model, 'refunded');
115
116
            $model = $this->paymentInfo($model);
117
        }
118
119
        return $model;
120
    }
121
122
    private function updateCheckoutModel(YandexCheckout $model, AbstractPaymentResponse $yandexResponse): YandexCheckout
123
    {
124
        $model->status = $yandexResponse->getStatus();
125
        $model->response = $yandexResponse->jsonSerialize();
126
        $model->save();
127
128
        return $model;
129
    }
130
131
    private function dispatchEvent(YandexCheckout $yandexCheckout, ?string $name = null): void
132
    {
133
        $name = $name ?? $yandexCheckout->status;
134
135
        if ($event = config("yandex-checkout.events.{$name}")) {
136
            $event::dispatch($yandexCheckout);
137
        }
138
    }
139
}
140