1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Integracao\ControlPay\API; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Exception\RequestException; |
6
|
|
|
use Integracao\ControlPay\AbstractAPI; |
7
|
|
|
use Integracao\ControlPay\Client; |
8
|
|
|
use Integracao\ControlPay\Constants\ControlPayParameterConst; |
9
|
|
|
use Integracao\ControlPay\Contracts\Pedido; |
10
|
|
|
use Integracao\ControlPay\Helpers\SerializerHelper; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class PedidoApi |
14
|
|
|
* @package Integracao\ControlPay\API |
15
|
|
|
*/ |
16
|
|
|
class PedidoApi extends AbstractAPI |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* PedidoApi constructor. |
20
|
|
|
*/ |
21
|
|
|
public function __construct(Client $client) |
22
|
|
|
{ |
23
|
|
|
parent::__construct('pedido', $client); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param Pedido\InserirRequest $inserirRequest |
28
|
|
|
* @return Pedido\InserirResponse |
29
|
|
|
* @throws \Exception |
30
|
|
|
*/ |
31
|
|
|
public function insert(Pedido\InserirRequest $inserirRequest) |
32
|
|
|
{ |
33
|
|
|
try{ |
34
|
|
|
|
35
|
|
View Code Duplication |
foreach ($inserirRequest->getProdutosPedido() as $key => $produto) |
|
|
|
|
36
|
|
|
{ |
37
|
|
|
if(empty($produto->getId())) |
38
|
|
|
$inserirRequest->getProdutosPedido()[$key]->setId( |
39
|
|
|
$this->_client->getParameter(ControlPayParameterConst::CONTROLPAY_DEFAULT_PRODUTO_ID) |
40
|
|
|
); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$this->response = $this->_httpClient->post(__FUNCTION__,[ |
44
|
|
|
'body' => json_encode($inserirRequest), |
45
|
|
|
]); |
46
|
|
|
|
47
|
|
|
return SerializerHelper::denormalize( |
48
|
|
|
$this->response->json(), |
49
|
|
|
Pedido\InserirResponse::class |
50
|
|
|
); |
51
|
|
|
}catch (RequestException $ex) { |
52
|
|
|
$this->response = $ex->getResponse(); |
53
|
|
|
$responseBody = $ex->getResponse()->json(); |
54
|
|
|
throw new \Exception($responseBody['message']); |
55
|
|
|
}catch (\Exception $ex){ |
56
|
|
|
throw new \Exception($ex->getMessage(), $ex->getCode(), $ex); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param Pedido\GetByFiltrosRequest $getByFiltrosRequest |
62
|
|
|
* @return Pedido\GetByFiltrosResponse |
63
|
|
|
* @throws \Exception |
64
|
|
|
*/ |
65
|
|
View Code Duplication |
public function getByFiltros(Pedido\GetByFiltrosRequest $getByFiltrosRequest) |
|
|
|
|
66
|
|
|
{ |
67
|
|
|
try{ |
68
|
|
|
$this->response = $this->_httpClient->post(__FUNCTION__,[ |
69
|
|
|
'body' => json_encode($getByFiltrosRequest), |
70
|
|
|
]); |
71
|
|
|
|
72
|
|
|
return SerializerHelper::denormalize( |
73
|
|
|
$this->response->json(), |
74
|
|
|
Pedido\GetByFiltrosResponse::class |
75
|
|
|
); |
76
|
|
|
}catch (RequestException $ex) { |
77
|
|
|
$this->response = $ex->getResponse(); |
78
|
|
|
$responseBody = $ex->getResponse()->json(); |
79
|
|
|
throw new \Exception($responseBody['message']); |
80
|
|
|
}catch (\Exception $ex){ |
81
|
|
|
throw new \Exception($ex->getMessage(), $ex->getCode(), $ex); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param $pessoaId |
87
|
|
|
* @param $pedidoReferencia |
88
|
|
|
* @return Pedido\GetByPessoaIdByReferenciaResponse |
89
|
|
|
* @throws \Exception |
90
|
|
|
*/ |
91
|
|
View Code Duplication |
public function getByPessoaIdByReferencia($pessoaId, $pedidoReferencia) |
|
|
|
|
92
|
|
|
{ |
93
|
|
|
try{ |
94
|
|
|
$this->response = $this->_httpClient->post(__FUNCTION__,[ |
95
|
|
|
'query' => $this->addQueryAdditionalParameters([ |
96
|
|
|
'pessoaId' => $pessoaId, |
97
|
|
|
'pedidoReferencia' => $pedidoReferencia |
98
|
|
|
]) |
99
|
|
|
]); |
100
|
|
|
|
101
|
|
|
return SerializerHelper::denormalize( |
102
|
|
|
$this->response->json(), |
103
|
|
|
Pedido\GetByPessoaIdByReferenciaResponse::class |
104
|
|
|
); |
105
|
|
|
}catch (RequestException $ex) { |
106
|
|
|
$this->response = $ex->getResponse(); |
107
|
|
|
$responseBody = $ex->getResponse()->json(); |
108
|
|
|
throw new \Exception($responseBody['message']); |
109
|
|
|
}catch (\Exception $ex){ |
110
|
|
|
throw new \Exception($ex->getMessage(), $ex->getCode(), $ex); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param $pessoaVendedorId |
116
|
|
|
* @param $pedidoId |
117
|
|
|
* @return Pedido\GetByPessoaIdByReferenciaResponse |
118
|
|
|
* @throws \Exception |
119
|
|
|
*/ |
120
|
|
View Code Duplication |
public function getPedidosByPessoaId($pessoaVendedorId, $pedidoId) |
|
|
|
|
121
|
|
|
{ |
122
|
|
|
try{ |
123
|
|
|
$this->response = $this->_httpClient->post(__FUNCTION__,[ |
124
|
|
|
'query' => $this->addQueryAdditionalParameters([ |
125
|
|
|
'pessoaVendedorId' => $pessoaVendedorId, |
126
|
|
|
'pedidoId' => $pedidoId |
127
|
|
|
]) |
128
|
|
|
]); |
129
|
|
|
|
130
|
|
|
return SerializerHelper::denormalize( |
131
|
|
|
$this->response->json(), |
132
|
|
|
Pedido\GetByPessoaIdByReferenciaResponse::class |
133
|
|
|
); |
134
|
|
|
}catch (RequestException $ex) { |
135
|
|
|
$this->response = $ex->getResponse(); |
136
|
|
|
$responseBody = $ex->getResponse()->json(); |
137
|
|
|
throw new \Exception($responseBody['message']); |
138
|
|
|
}catch (\Exception $ex){ |
139
|
|
|
throw new \Exception($ex->getMessage(), $ex->getCode(), $ex); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @param $pedidoId |
145
|
|
|
* @return Pedido\CancelarResponse |
146
|
|
|
* @throws \Exception |
147
|
|
|
*/ |
148
|
|
View Code Duplication |
public function cancelar($pedidoId) |
|
|
|
|
149
|
|
|
{ |
150
|
|
|
try{ |
151
|
|
|
$this->response = $this->_httpClient->post(__FUNCTION__,[ |
152
|
|
|
'query' => $this->addQueryAdditionalParameters([ |
153
|
|
|
'pedidoId' => $pedidoId |
154
|
|
|
]) |
155
|
|
|
]); |
156
|
|
|
|
157
|
|
|
return SerializerHelper::denormalize( |
158
|
|
|
$this->response->json(), |
159
|
|
|
Pedido\CancelarResponse::class |
160
|
|
|
); |
161
|
|
|
}catch (RequestException $ex) { |
162
|
|
|
$this->response = $ex->getResponse(); |
163
|
|
|
$responseBody = $ex->getResponse()->json(); |
164
|
|
|
throw new \Exception($responseBody['message']); |
165
|
|
|
}catch (\Exception $ex){ |
166
|
|
|
throw new \Exception($ex->getMessage(), $ex->getCode(), $ex); |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @param integer $pedidoId |
172
|
|
|
* @return Pedido\GetByIdResponse |
173
|
|
|
* @throws \Exception |
174
|
|
|
*/ |
175
|
|
View Code Duplication |
public function getById($pedidoId) |
|
|
|
|
176
|
|
|
{ |
177
|
|
|
try{ |
178
|
|
|
$this->response = $this->_httpClient->post(__FUNCTION__,[ |
179
|
|
|
'query' => $this->addQueryAdditionalParameters([ |
180
|
|
|
'pedidoId' => $pedidoId |
181
|
|
|
]) |
182
|
|
|
]); |
183
|
|
|
|
184
|
|
|
return SerializerHelper::denormalize( |
185
|
|
|
$this->response->json(), |
186
|
|
|
Pedido\GetByIdResponse::class |
187
|
|
|
); |
188
|
|
|
}catch (RequestException $ex) { |
189
|
|
|
$this->response = $ex->getResponse(); |
190
|
|
|
$responseBody = $ex->getResponse()->json(); |
191
|
|
|
throw new \Exception($responseBody['message']); |
192
|
|
|
}catch (\Exception $ex){ |
193
|
|
|
throw new \Exception($ex->getMessage(), $ex->getCode(), $ex); |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
} |