|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* MIT License |
|
5
|
|
|
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file. |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace SprykerEco\Yves\Payone\Controller; |
|
9
|
|
|
|
|
10
|
|
|
use Generated\Shared\Transfer\PayoneCancelRedirectTransfer; |
|
11
|
|
|
use Generated\Shared\Transfer\PayoneGetFileTransfer; |
|
12
|
|
|
use Generated\Shared\Transfer\PayoneGetInvoiceTransfer; |
|
13
|
|
|
use Generated\Shared\Transfer\PayoneTransactionStatusUpdateTransfer; |
|
14
|
|
|
use Spryker\Yves\Kernel\Controller\AbstractController; |
|
15
|
|
|
use SprykerEco\Yves\Payone\Plugin\Provider\PayoneControllerProvider; |
|
16
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
17
|
|
|
use Symfony\Component\HttpFoundation\StreamedResponse; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @method \SprykerEco\Client\Payone\PayoneClientInterface getClient() |
|
21
|
|
|
* @method \SprykerEco\Yves\Payone\PayoneFactory getFactory() |
|
22
|
|
|
*/ |
|
23
|
|
|
class IndexController extends AbstractController |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @param \Symfony\Component\HttpFoundation\Request $request |
|
27
|
|
|
* |
|
28
|
|
|
* @return \Symfony\Component\HttpFoundation\StreamedResponse |
|
29
|
|
|
*/ |
|
30
|
|
|
public function indexAction(Request $request) |
|
31
|
|
|
{ |
|
32
|
|
|
$statusUpdateTranfer = new PayoneTransactionStatusUpdateTransfer(); |
|
33
|
|
|
$statusUpdateTranfer->fromArray($request->request->all(), true); |
|
34
|
|
|
|
|
35
|
|
|
$response = $this->getClient()->updateStatus($statusUpdateTranfer)->getResponse(); |
|
36
|
|
|
|
|
37
|
|
|
$callback = function () use ($response) { |
|
38
|
|
|
echo $response; |
|
39
|
|
|
}; |
|
40
|
|
|
|
|
41
|
|
|
return $this->streamedResponse($callback); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param \Symfony\Component\HttpFoundation\Request $request |
|
46
|
|
|
* |
|
47
|
|
|
* @return \Symfony\Component\HttpFoundation\StreamedResponse |
|
48
|
|
|
*/ |
|
49
|
|
View Code Duplication |
public function getFileAction(Request $request) |
|
|
|
|
|
|
50
|
|
|
{ |
|
51
|
|
|
$customerClient = $this->getFactory()->createCustomerClient(); |
|
|
|
|
|
|
52
|
|
|
$customerTransfer = $customerClient->getCustomer(); |
|
53
|
|
|
|
|
54
|
|
|
if (!$customerTransfer) { |
|
55
|
|
|
return $this->redirectResponseInternal(PayoneControllerProvider::ROUTE_LOGIN); |
|
|
|
|
|
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
$getFileTransfer = new PayoneGetFileTransfer(); |
|
59
|
|
|
$getFileTransfer->setReference($request->query->get('id')); |
|
60
|
|
|
$getFileTransfer->setCustomerId($customerTransfer->getIdCustomer()); |
|
61
|
|
|
|
|
62
|
|
|
$response = $this->getClient()->getFile($getFileTransfer); |
|
63
|
|
|
|
|
64
|
|
|
if ($response->getStatus() === 'ERROR') { |
|
65
|
|
|
return $this->viewResponse(['errormessage' => $response->getCustomerErrorMessage()]); |
|
|
|
|
|
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$callback = function () use ($response) { |
|
69
|
|
|
echo base64_decode($response->getRawResponse()); |
|
70
|
|
|
}; |
|
71
|
|
|
|
|
72
|
|
|
return $this->streamedResponse($callback, 200, ["Content-type" => "application/pdf"]); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param \Symfony\Component\HttpFoundation\Request $request |
|
77
|
|
|
* |
|
78
|
|
|
* @return \Symfony\Component\HttpFoundation\StreamedResponse |
|
79
|
|
|
*/ |
|
80
|
|
View Code Duplication |
public function getInvoiceAction(Request $request) |
|
|
|
|
|
|
81
|
|
|
{ |
|
82
|
|
|
$customerClient = $this->getFactory()->getCustomerClient(); |
|
83
|
|
|
$customerTransfer = $customerClient->getCustomer(); |
|
84
|
|
|
|
|
85
|
|
|
if (!$customerTransfer) { |
|
86
|
|
|
return $this->redirectResponseInternal(PayoneControllerProvider::ROUTE_LOGIN); |
|
|
|
|
|
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
$getInvoiceTransfer = new PayoneGetInvoiceTransfer(); |
|
90
|
|
|
$getInvoiceTransfer->setReference($request->query->get('id')); |
|
91
|
|
|
$getInvoiceTransfer->setCustomerId($customerTransfer->getIdCustomer()); |
|
92
|
|
|
|
|
93
|
|
|
$response = $this->getClient()->getInvoice($getInvoiceTransfer); |
|
94
|
|
|
|
|
95
|
|
|
if ($response->getStatus() === 'ERROR') { |
|
96
|
|
|
return $this->viewResponse(['errormessage' => $response->getInternalErrorMessage()]); |
|
|
|
|
|
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
$callback = function () use ($response) { |
|
100
|
|
|
echo base64_decode($response->getRawResponse()); |
|
101
|
|
|
}; |
|
102
|
|
|
|
|
103
|
|
|
return $this->streamedResponse($callback, 200, ["Content-type" => "application/pdf"]); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @param \Symfony\Component\HttpFoundation\Request $request |
|
108
|
|
|
* |
|
109
|
|
|
* @return \Symfony\Component\HttpFoundation\StreamedResponse |
|
110
|
|
|
*/ |
|
111
|
|
View Code Duplication |
public function getSecurityInvoiceAction(Request $request) |
|
|
|
|
|
|
112
|
|
|
{ |
|
113
|
|
|
$customerClient = $this->getFactory()->getCustomerClient(); |
|
114
|
|
|
$customerTransfer = $customerClient->getCustomer(); |
|
115
|
|
|
|
|
116
|
|
|
if (!$customerTransfer) { |
|
117
|
|
|
return $this->redirectResponseInternal(PayoneControllerProvider::ROUTE_LOGIN); |
|
|
|
|
|
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
$getInvoiceTransfer = new PayoneGetInvoiceTransfer(); |
|
121
|
|
|
$getInvoiceTransfer->setReference($request->query->get('id')); |
|
122
|
|
|
$getInvoiceTransfer->setCustomerId($customerTransfer->getIdCustomer()); |
|
123
|
|
|
|
|
124
|
|
|
$response = $this->getClient()->getInvoice($getInvoiceTransfer); |
|
125
|
|
|
|
|
126
|
|
|
if ($response->getStatus() === 'ERROR') { |
|
127
|
|
|
return $this->viewResponse(['errormessage' => $response->getInternalErrorMessage()]); |
|
|
|
|
|
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
$callback = function () use ($response) { |
|
131
|
|
|
echo base64_decode($response->getRawResponse()); |
|
132
|
|
|
}; |
|
133
|
|
|
|
|
134
|
|
|
return $this->streamedResponse($callback, 200, ["Content-type" => "application/pdf"]); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* @param \Symfony\Component\HttpFoundation\Request $request |
|
139
|
|
|
* |
|
140
|
|
|
* @return \Symfony\Component\HttpFoundation\StreamedResponse |
|
141
|
|
|
*/ |
|
142
|
|
|
public function cancelRedirectAction(Request $request) |
|
143
|
|
|
{ |
|
144
|
|
|
$orderReference = $request->query->get('orderReference'); |
|
145
|
|
|
$urlHmac = $request->query->get('sig'); |
|
146
|
|
|
|
|
147
|
|
|
if ($orderReference) { |
|
148
|
|
|
$cancelRedirectTransfer = new PayoneCancelRedirectTransfer(); |
|
149
|
|
|
$cancelRedirectTransfer->setOrderReference($orderReference); |
|
150
|
|
|
$cancelRedirectTransfer->setUrlHmac($urlHmac); |
|
151
|
|
|
|
|
152
|
|
|
$response = $this->getClient()->cancelRedirect($cancelRedirectTransfer); |
|
|
|
|
|
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
return $this->redirectResponseInternal(PayoneControllerProvider::CHECKOUT_PAYMENT); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* @param callable|null $callback |
|
160
|
|
|
* @param int $status |
|
161
|
|
|
* @param array $headers |
|
162
|
|
|
* |
|
163
|
|
|
* @return \Symfony\Component\HttpFoundation\StreamedResponse |
|
164
|
|
|
*/ |
|
165
|
|
|
protected function streamedResponse($callback = null, $status = 200, $headers = []) |
|
166
|
|
|
{ |
|
167
|
|
|
$streamedResponse = new StreamedResponse($callback, $status, $headers); |
|
168
|
|
|
$streamedResponse->send(); |
|
169
|
|
|
|
|
170
|
|
|
return $streamedResponse; |
|
171
|
|
|
} |
|
172
|
|
|
} |
|
173
|
|
|
|
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.