PagseguroController::setEndereco()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
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();    
0 ignored issues
show
Unused Code introduced by
The property $client is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
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());
0 ignored issues
show
Coding Style Compatibility introduced by
The method finalizarPedido() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
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;
0 ignored issues
show
Documentation introduced by
The property endereco does not exist on object<PagseguroController>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
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);
0 ignored issues
show
Bug introduced by
The variable $paymentRequest does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
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
}