|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
require 'GatewayInterface.php'; |
|
4
|
|
|
include(APP . 'Vendor/PagSeguro/source/PagSeguroLibrary/PagSeguroLibrary.php'); |
|
5
|
|
|
|
|
6
|
|
|
class PagseguroController extends AppController implements GatewayInterface |
|
7
|
|
|
{ |
|
8
|
|
|
|
|
9
|
|
|
private $paymentRequest; |
|
10
|
|
|
private $email; |
|
11
|
|
|
private $token; |
|
12
|
|
|
private $produtos = array(); |
|
13
|
|
|
private $client = array(); |
|
|
|
|
|
|
14
|
|
|
private $reference; |
|
15
|
|
|
private $valor_frete; |
|
16
|
|
|
|
|
17
|
|
|
public function __construct() |
|
18
|
|
|
{ |
|
19
|
|
|
$this->paymentRequest = new PagSeguroPaymentRequest(); |
|
20
|
|
|
// Set the currency |
|
21
|
|
|
$this->paymentRequest->setCurrency("BRL"); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
// $products, $andress, $client, $total, $valor_frete, $id_venda |
|
25
|
|
|
public function finalizarPedido() |
|
26
|
|
|
{ |
|
27
|
|
|
$this->paymentRequest->setReference($this->reference); |
|
28
|
|
|
$this->paymentRequest->setShippingCost($this->valor_frete); |
|
29
|
|
|
|
|
30
|
|
|
|
|
31
|
|
|
// Set the url used by PagSeguro to redirect user after checkout process ends |
|
32
|
|
|
$this->paymentRequest->setRedirectUrl("http://www.lojamodelo.com.br"); |
|
33
|
|
|
|
|
34
|
|
|
try { |
|
35
|
|
|
|
|
36
|
|
|
/* |
|
37
|
|
|
* #### Credentials ##### |
|
38
|
|
|
* Replace the parameters below with your credentials |
|
39
|
|
|
* You can also get your credentials from a config file. See an example: |
|
40
|
|
|
* $credentials = PagSeguroConfig::getAccountCredentials(); |
|
41
|
|
|
// */ |
|
42
|
|
|
|
|
43
|
|
|
// seller authentication |
|
44
|
|
|
$credentials = new PagSeguroAccountCredentials($this->email, $this->token); |
|
45
|
|
|
|
|
46
|
|
|
// application authentication |
|
47
|
|
|
//$credentials = PagSeguroConfig::getApplicationCredentials(); |
|
48
|
|
|
|
|
49
|
|
|
//$credentials->setAuthorizationCode("E231B2C9BCC8474DA2E260B6C8CF60D3"); |
|
50
|
|
|
|
|
51
|
|
|
// Register this payment request in PagSeguro to obtain the payment URL to redirect your customer. |
|
52
|
|
|
$url = $this->paymentRequest->register($credentials); |
|
53
|
|
|
|
|
54
|
|
|
return $url; |
|
55
|
|
|
|
|
56
|
|
|
} catch (PagSeguroServiceException $e) { |
|
57
|
|
|
|
|
58
|
|
|
die($e->getMessage()); |
|
|
|
|
|
|
59
|
|
|
|
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @return void |
|
65
|
|
|
* @param String $token |
|
66
|
|
|
**/ |
|
67
|
|
|
public function setToken($token) |
|
68
|
|
|
{ |
|
69
|
|
|
$this->token = $token; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @return String $token |
|
74
|
|
|
**/ |
|
75
|
|
|
public function getToken() |
|
76
|
|
|
{ |
|
77
|
|
|
return $this->token; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @return void |
|
82
|
|
|
* @param String $email |
|
83
|
|
|
**/ |
|
84
|
|
|
public function setEmail($email) |
|
85
|
|
|
{ |
|
86
|
|
|
$this->email = $email; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @return String $email |
|
91
|
|
|
**/ |
|
92
|
|
|
public function getEmail() |
|
93
|
|
|
{ |
|
94
|
|
|
return $this->email; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @return void |
|
99
|
|
|
* @param Array $produtos |
|
100
|
|
|
**/ |
|
101
|
|
|
public function setProdutos($produtos) |
|
102
|
|
|
{ |
|
103
|
|
|
$this->produtos = $produtos; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @return Array $produtos |
|
108
|
|
|
**/ |
|
109
|
|
|
public function getProdutos() |
|
110
|
|
|
{ |
|
111
|
|
|
return $this->produtos; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @param Array Produtos |
|
116
|
|
|
* @return Array Produtos |
|
117
|
|
|
**/ |
|
118
|
|
|
public function adicionarProdutosGateway() |
|
119
|
|
|
{ |
|
120
|
|
|
if (empty($this->getProdutos())) |
|
121
|
|
|
{ |
|
122
|
|
|
throw new Exception("Você precisa usar a função setar os dados do produto!", 1); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
foreach ($this->getProdutos() as $i => $item) { |
|
126
|
|
|
$this->paymentRequest->addItem( |
|
127
|
|
|
'000' . $item['Produto']['id'], |
|
128
|
|
|
$item['Produto']['nome'] . ' Tamanho: '. $item['Produto']['variacao'], |
|
129
|
|
|
$item['Produto']['quantidade'], |
|
130
|
|
|
number_format($item['Produto']['preco'], 2, '.', '') |
|
131
|
|
|
); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
return $this->getProdutos(); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
public function setEndereco($endereco) |
|
138
|
|
|
{ |
|
139
|
|
|
$this->endereco = $endereco; |
|
|
|
|
|
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
public function getEndereco() |
|
143
|
|
|
{ |
|
144
|
|
|
return $this->endereco; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
public function setEnderecoClienteGateway() |
|
148
|
|
|
{ |
|
149
|
|
|
if (empty($this->endereco)) |
|
150
|
|
|
{ |
|
151
|
|
|
throw new Exception("Você precisa usar a função setar os dados do cliente!", 1); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
$sedexCode = PagSeguroShippingType::getCodeByType('PAC'); |
|
155
|
|
|
$paymentRequest->setShippingType($sedexCode); |
|
|
|
|
|
|
156
|
|
|
|
|
157
|
|
|
$paymentRequest->setShippingAddress( |
|
158
|
|
|
$this->endereco['cep'], |
|
159
|
|
|
$this->endereco['endereco'], |
|
160
|
|
|
$this->endereco['numero'], |
|
161
|
|
|
$this->endereco['complemento'], |
|
162
|
|
|
$this->endereco['bairro'], |
|
163
|
|
|
$this->endereco['cidade'], |
|
164
|
|
|
$this->endereco['estado'], |
|
165
|
|
|
'BRA' |
|
166
|
|
|
); |
|
167
|
|
|
|
|
168
|
|
|
return $this->getEndereco(); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
public function setReference($reference) |
|
172
|
|
|
{ |
|
173
|
|
|
$this->reference = $reference; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
public function getReference() |
|
177
|
|
|
{ |
|
178
|
|
|
return $this->reference; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
public function setValorFrete($valor_frete) |
|
182
|
|
|
{ |
|
183
|
|
|
$this->valor_frete = $valor_frete; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
public function getValorFrete() |
|
187
|
|
|
{ |
|
188
|
|
|
return $this->valor_frete; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
public function setClienteGateway() |
|
192
|
|
|
{ |
|
193
|
|
|
if (empty($this->cliente)) |
|
194
|
|
|
{ |
|
195
|
|
|
throw new Exception("Você precisa usar a função setar os dados do cliente!", 1); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
// Set your customer information. |
|
199
|
|
|
$this->paymentRequest->setSender( |
|
200
|
|
|
$this->cliente['nome'], |
|
201
|
|
|
$this->cliente['email'], |
|
202
|
|
|
$this->cliente['ddd'], |
|
203
|
|
|
$this->cliente['telefone'], |
|
204
|
|
|
'CPF', |
|
205
|
|
|
$this->cliente['cpf'] |
|
206
|
|
|
); |
|
207
|
|
|
|
|
208
|
|
|
return $this->getCliente(); |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
public function setCliente($cliente) |
|
212
|
|
|
{ |
|
213
|
|
|
$this->cliente = $cliente; |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
public function getCliente() |
|
217
|
|
|
{ |
|
218
|
|
|
return $this->cliente; |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
} |
This check marks private properties in classes that are never used. Those properties can be removed.