|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: a.moreira |
|
5
|
|
|
* Date: 20/10/2016 |
|
6
|
|
|
* Time: 09:51 |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Integracao\ControlPay\API; |
|
10
|
|
|
|
|
11
|
|
|
use GuzzleHttp\Exception\RequestException; |
|
12
|
|
|
use Integracao\ControlPay\AbstractAPI; |
|
13
|
|
|
use Integracao\ControlPay\Client; |
|
14
|
|
|
use Integracao\ControlPay\Constants\ControlPayParameterConst; |
|
15
|
|
|
use Integracao\ControlPay\Helpers\SerializerHelper; |
|
16
|
|
|
use Integracao\ControlPay\Contracts; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Class VenderApi |
|
20
|
|
|
* @package Integracao\ControlPay\API |
|
21
|
|
|
*/ |
|
22
|
|
|
class VendaApi extends AbstractAPI |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @var PedidoApi |
|
26
|
|
|
*/ |
|
27
|
|
|
private $pedidoApi; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* VenderApi constructor. |
|
31
|
|
|
*/ |
|
32
|
|
|
public function __construct(Client $client = null) |
|
33
|
|
|
{ |
|
34
|
|
|
parent::__construct('venda', $client); |
|
35
|
|
|
$this->pedidoApi = new PedidoApi($client); |
|
|
|
|
|
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param Contracts\Venda\VenderRequest $venderRequest |
|
40
|
|
|
* @return Contracts\Venda\VenderResponse |
|
41
|
|
|
* @throws \Exception |
|
42
|
|
|
*/ |
|
43
|
|
|
public function vender(Contracts\Venda\VenderRequest $venderRequest) |
|
44
|
|
|
{ |
|
45
|
|
|
try{ |
|
46
|
|
|
|
|
47
|
|
|
if(empty($venderRequest->getTerminalId())) |
|
48
|
|
|
$venderRequest->setTerminalId( |
|
49
|
|
|
$this->_client->getParameter(ControlPayParameterConst::CONTROLPAY_DEFAULT_TERMINAL_ID) |
|
50
|
|
|
); |
|
51
|
|
|
|
|
52
|
|
|
if(empty($venderRequest->getFormaPagamentoId())) |
|
53
|
|
|
$venderRequest->setFormaPagamentoId( |
|
54
|
|
|
$this->_client->getParameter(ControlPayParameterConst::CONTROLPAY_DEFAULT_FORMA_PAGAMENTO_ID) |
|
55
|
|
|
); |
|
56
|
|
|
|
|
57
|
|
|
if(empty($venderRequest->isAguardarTefIniciarTransacao())) |
|
58
|
|
|
$venderRequest->setAguardarTefIniciarTransacao( |
|
59
|
|
|
boolval( |
|
60
|
|
|
$this->_client->getParameter(ControlPayParameterConst::CONTROLPAY_DEFAULT_FORMA_AGUARDA_TEF) |
|
61
|
|
|
) |
|
62
|
|
|
); |
|
63
|
|
|
|
|
64
|
|
View Code Duplication |
foreach ($venderRequest->getProdutosVendidos() as $key => $produto) |
|
|
|
|
|
|
65
|
|
|
{ |
|
66
|
|
|
if(empty($produto->getId())) |
|
67
|
|
|
$venderRequest->getProdutosVendidos()[$key]->setId( |
|
68
|
|
|
$this->_client->getParameter(ControlPayParameterConst::CONTROLPAY_DEFAULT_PRODUTO_ID) |
|
69
|
|
|
); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
$this->response = $this->_httpClient->post(__FUNCTION__,[ |
|
73
|
|
|
'body' => json_encode($venderRequest), |
|
74
|
|
|
]); |
|
75
|
|
|
|
|
76
|
|
|
return SerializerHelper::denormalize( |
|
77
|
|
|
$this->response->json(), |
|
78
|
|
|
Contracts\Venda\VenderResponse::class |
|
79
|
|
|
); |
|
80
|
|
|
}catch (RequestException $ex) { |
|
81
|
|
|
$this->response = $ex->getResponse(); |
|
82
|
|
|
$responseBody = $ex->getResponse()->json(); |
|
83
|
|
|
throw new \Exception($responseBody['message']); |
|
84
|
|
|
}catch (\Exception $ex){ |
|
85
|
|
|
throw new \Exception($ex->getMessage(), $ex->getCode(), $ex); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @param Contracts\Venda\VenderComPedidoRequest $venderComPedidoRequest |
|
91
|
|
|
* @return Contracts\Venda\VenderResponse |
|
92
|
|
|
* @throws \Exception |
|
93
|
|
|
*/ |
|
94
|
|
|
public function venderComPedido(Contracts\Venda\VenderComPedidoRequest $venderComPedidoRequest) |
|
95
|
|
|
{ |
|
96
|
|
|
try{ |
|
97
|
|
|
$response = $this->pedidoApi->insert($venderComPedidoRequest->getPedidoInserirRequest()); |
|
98
|
|
|
|
|
99
|
|
|
if(empty($response->getPedido()->getId())) |
|
100
|
|
|
throw new \Exception("Falha ao inserir pedido"); |
|
101
|
|
|
|
|
102
|
|
|
$venderComPedidoRequest->getInventarioVenderRequest()->setPedidoId( |
|
103
|
|
|
$response->getPedido()->getId() |
|
104
|
|
|
); |
|
105
|
|
|
|
|
106
|
|
|
return $this->vender($venderComPedidoRequest->getInventarioVenderRequest()); |
|
107
|
|
|
}catch (RequestException $ex) { |
|
108
|
|
|
$this->response = $ex->getResponse(); |
|
109
|
|
|
$responseBody = $ex->getResponse()->json(); |
|
110
|
|
|
throw new \Exception($responseBody['message']); |
|
111
|
|
|
}catch (\Exception $ex){ |
|
112
|
|
|
throw new \Exception($ex->getMessage(), $ex->getCode(), $ex); |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
// /** |
|
|
|
|
|
|
117
|
|
|
// * API Provavelmente descontinuada |
|
118
|
|
|
// * |
|
119
|
|
|
// * @return Contracts\Venda\ConsultarVendasResponse |
|
120
|
|
|
// * @throws \Exception |
|
121
|
|
|
// */ |
|
122
|
|
|
// public function consultarVendas(array $ids = []) |
|
123
|
|
|
// { |
|
124
|
|
|
// try{ |
|
125
|
|
|
// $this->response = $this->_httpClient->post(__FUNCTION__, [ |
|
126
|
|
|
// 'body' => json_encode(array_map(function($id){ |
|
127
|
|
|
// return [ |
|
128
|
|
|
// 'Id' => (string)$id |
|
129
|
|
|
// ]; |
|
130
|
|
|
// },$ids)) |
|
131
|
|
|
// ]); |
|
132
|
|
|
// |
|
133
|
|
|
// return SerializerHelper::denormalize( |
|
134
|
|
|
// $this->response->json(), |
|
135
|
|
|
// Contracts\Venda\ConsultarVendasResponse::class |
|
136
|
|
|
// ); |
|
137
|
|
|
// }catch (RequestException $ex) { |
|
138
|
|
|
// $this->response = $ex->getResponse(); |
|
139
|
|
|
// $responseBody = $ex->getResponse()->json(); |
|
140
|
|
|
// throw new \Exception($responseBody['message']); |
|
141
|
|
|
// }catch (\Exception $ex){ |
|
142
|
|
|
// throw new \Exception($ex->getMessage(), $ex->getCode(), $ex); |
|
143
|
|
|
// } |
|
144
|
|
|
// } |
|
145
|
|
|
|
|
146
|
|
|
} |
It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.
We recommend to add an additional type check (or disallow null for the parameter):