Completed
Branch refactor (831176)
by recca
07:28
created

WebhookController::handleCapture()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Recca0120\LaravelPayum\Http\Controllers;
4
5
use Payum\Core\Payum;
6
use Illuminate\Http\Request;
7
use Payum\Core\Request\Sync;
8
use Payum\Core\Request\Cancel;
9
use Payum\Core\Request\Notify;
10
use Payum\Core\Request\Payout;
11
use Payum\Core\Request\Refund;
12
use Payum\Core\Request\Capture;
13
use Payum\Core\Request\Authorize;
14
use Illuminate\Routing\Controller;
15
use Payum\Core\Reply\ReplyInterface;
16
use Illuminate\Contracts\Routing\ResponseFactory;
17
use Payum\Core\Bridge\Symfony\ReplyToSymfonyResponseConverter;
18
19
class WebhookController extends Controller
20
{
21
    /**
22
     * $payum.
23
     *
24
     * @var \Payum\Core\Payum
25
     */
26
    protected $payum;
27
28
    /**
29
     * $responseFactory.
30
     *
31
     * @var \Illuminate\Contracts\Routing\ResponseFactory
32
     */
33
    protected $responseFactory;
34
35
    /**
36
     * $replyToSymfonyResponseConverter.
37
     *
38
     * @var \Payum\Core\Bridge\Symfony\ReplyToSymfonyResponseConverter
39
     */
40
    protected $replyToSymfonyResponseConverter;
41
42
    /**
43
     * __construct.
44
     *
45
     * @param \Payum\Core\Payum $payum
46
     * @param \Illuminate\Contracts\Routing\ResponseFactory $responseFactory
47
     * @param \Payum\Core\Bridge\Symfony\ReplyToSymfonyResponseConverter $replyToSymfonyResponseConverter
48
     */
49 10
    public function __construct(
50
        Payum $payum,
51
        ResponseFactory $responseFactory,
52
        ReplyToSymfonyResponseConverter $replyToSymfonyResponseConverter
53
    ) {
54 10
        $this->payum = $payum;
55 10
        $this->responseFactory = $responseFactory;
56 10
        $this->replyToSymfonyResponseConverter = $replyToSymfonyResponseConverter;
57 10
    }
58
59
    /**
60
     * handleAuthorize.
61
     *
62
     * @param \Illuminate\Http\Request $request
63
     * @param string $payumToken
64
     * @return \Illuminate\Http\Response
65
     */
66 1
    public function handleAuthorize(Request $request, $payumToken)
67
    {
68
        return $this->handleReceived($request, $payumToken, function ($gateway, $token, $httpRequestVerifier, $request) {
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
69 1
            $gateway->execute(new Authorize($token));
70 1
            $httpRequestVerifier->invalidate($token);
71
72 1
            return $this->responseFactory->redirectTo($token->getAfterUrl());
73 1
        });
74
    }
75
76
    /**
77
     * handleCancel.
78
     *
79
     * @param \Illuminate\Http\Request $request
80
     * @param string $payumToken
81
     * @return \Illuminate\Http\Response
82
     */
83 1 View Code Duplication
    public function handleCancel(Request $request, $payumToken)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
84
    {
85
        return $this->handleReceived($request, $payumToken, function ($gateway, $token, $httpRequestVerifier, $request) {
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
86 1
            $gateway->execute(new Cancel($token));
87 1
            $httpRequestVerifier->invalidate($token);
88 1
            $afterUrl = $token->getAfterUrl();
89 1
            if (empty($afterUrl) === false) {
90 1
                return $this->responseFactory->redirectTo($afterUrl);
91
            }
92
93
            return $this->responseFactory->make(null, 204);
94 1
        });
95
    }
96
97
    /**
98
     * handleCapture.
99
     *
100
     * @param \Illuminate\Http\Request $request
101
     * @param string $payumToken
102
     * @return \Illuminate\Http\Response
103
     */
104 2
    public function handleCapture(Request $request, $payumToken = null)
105
    {
106
        return $this->handleReceived($request, $payumToken, function ($gateway, $token, $httpRequestVerifier, $request) {
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
107 2
            $gateway->execute(new Capture($token));
108 1
            $httpRequestVerifier->invalidate($token);
109
110 1
            return $this->responseFactory->redirectTo($token->getAfterUrl());
111 2
        });
112
    }
113
114
    /**
115
     * handleNotify.
116
     *
117
     * @param \Illuminate\Http\Request $request
118
     * @param string $payumToken
119
     * @return \Illuminate\Http\Response
120
     */
121 1
    public function handleNotify(Request $request, $payumToken)
122
    {
123
        return $this->handleReceived($request, $payumToken, function ($gateway, $token, $httpRequestVerifier, $request) {
0 ignored issues
show
Unused Code introduced by
The parameter $httpRequestVerifier is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
124 1
            $gateway->execute(new Notify($token));
125
126 1
            return $this->responseFactory->make(null, 204);
127 1
        });
128
    }
129
130
    /**
131
     * handleNotifyUnsafe.
132
     *
133
     * @param string $gatewayName
134
     * @return \Illuminate\Http\Response
135
     */
136 2
    public function handleNotifyUnsafe($gatewayName)
137
    {
138
        try {
139 2
            $gateway = $this->getPayum()->getGateway($gatewayName);
140 1
            $gateway->execute(new Notify(null));
141
142 1
            return $this->responseFactory->make(null, 204);
143 1
        } catch (ReplyInterface $reply) {
144 1
            return $this->convertReply($reply);
145
        }
146
    }
147
148
    /**
149
     * handleRefund.
150
     *
151
     * @param \Illuminate\Http\Request $request
152
     * @param string $payumToken
153
     * @return \Illuminate\Http\Response
154
     */
155 1 View Code Duplication
    public function handleRefund(Request $request, $payumToken)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
156
    {
157
        return $this->handleReceived($request, $payumToken, function ($gateway, $token, $httpRequestVerifier, $request) {
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
158 1
            $gateway->execute(new Refund($token));
159 1
            $httpRequestVerifier->invalidate($token);
160 1
            $afterUrl = $token->getAfterUrl();
161
162 1
            if (empty($afterUrl) === false) {
163 1
                return $this->responseFactory->redirectTo($afterUrl);
164
            }
165
166
            return $this->responseFactory->make(null, 204);
167 1
        });
168
    }
169
170
    /**
171
     * handlePayout.
172
     *
173
     * @param \Illuminate\Http\Request $request
174
     * @param string $payumToken
175
     * @return \Illuminate\Http\Response
176
     */
177 1
    public function handlePayout(Request $request, $payumToken)
178
    {
179
        return $this->handleReceived($request, $payumToken, function ($gateway, $token, $httpRequestVerifier, $request) {
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
180 1
            $gateway->execute(new Payout($token));
181 1
            $httpRequestVerifier->invalidate($token);
182
183 1
            return $this->responseFactory->redirectTo($token->getAfterUrl());
184 1
        });
185
    }
186
187
    /**
188
     * handleSync.
189
     *
190
     * @param \Illuminate\Http\Request $request
191
     * @param string $payumToken
192
     * @return \Illuminate\Http\Response
193
     */
194
    public function handleSync(Request $request, $payumToken)
195
    {
196 1
        return $this->handleReceived($request, $payumToken, function ($gateway, $token, $httpRequestVerifier, $request) {
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
197 1
            $gateway->execute(new Sync($token));
198 1
            $httpRequestVerifier->invalidate($token);
199
200 1
            return $this->responseFactory->redirectTo($token->getAfterUrl());
201 1
        });
202
    }
203
204
    /**
205
     * handleReceived.
206
     *
207
     * @param \Illuminate\Http\Request $request
208
     * @param string $payumToken
209
     * @param callable $callback
210
     * @return \Illuminate\Http\Response
211
     */
212 8
    protected function handleReceived(Request $request, $payumToken, callable $callback)
213
    {
214 8
        if (is_null($payumToken) === true) {
215 1
            $payumToken = $request->session()->pull('payum_token');
0 ignored issues
show
Bug introduced by
The method pull() does not seem to exist on object<Symfony\Component...ssion\SessionInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
216
        }
217
218 8
        $request->merge(['payum_token' => $payumToken]);
219 8
        $httpRequestVerifier = $this->getPayum()->getHttpRequestVerifier();
220 8
        $token = $httpRequestVerifier->verify($request);
221 8
        $gateway = $this->getPayum()->getGateway($token->getGatewayName());
222
223
        try {
224 8
            return $callback($gateway, $token, $httpRequestVerifier, $request);
225 1
        } catch (ReplyInterface $reply) {
226 1
            $request->session()->put('payum_token', $payumToken);
0 ignored issues
show
Bug introduced by
The method put() does not seem to exist on object<Symfony\Component...ssion\SessionInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
227
228 1
            return $this->convertReply($reply);
229
        }
230
    }
231
232 10
    protected function getPayum()
233
    {
234 10
        return $this->payum;
235
    }
236
237 2
    protected function convertReply($reply)
238
    {
239 2
        return $this->replyToSymfonyResponseConverter->convert($reply);
240
    }
241
}
242