Completed
Push — master ( e89996...cf322c )
by Reginaldo
43:27 queued 25:36
created

AsaasController   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 122
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 3

6 Methods

Rating   Name   Duplication   Size   Complexity  
A configuracoes() 0 13 1
B save() 0 29 3
A criar_cobranca() 0 4 1
A criar_cliente() 0 4 1
A request() 0 23 2
A loadToken() 0 16 2
1
<?php
2
3
/**
4
* 
5
*/
6
class AsaasController extends AppController
7
{
8
9
	protected $endpoint = 'https://www.asaas.com/api/v3';
10
11
	public function configuracoes()
12
	{
13
		$this->loadModel('Asaas');
14
		
15
		$asaas = $this->Asaas->find('first', array(
16
				'Asaas.usuario_id' => $this->instancia
17
			)
18
		);
19
20
		$this->set('asaas', $asaas);
21
22
		$this->layout = 'wadmin';
23
	}
24
25
	public function save()
26
	{
27
		$key_api = $this->request->data('key_api');
28
29
		$data = [
30
			'api_key' => $key_api,
31
			'usuario_id' => $this->instancia
32
		];
33
34
		$this->loadModel('Asaas');
35
		
36
		$exist = $this->Asaas->find('first', array(
37
				'Asaas.key_api' => $key_api,
38
				'Asaas.usuario_id' => $this->instancia
39
			)
40
		);
41
42
		if (!empty($exist)) {
43
			$this->Asaas->id = $exist['Asaas']['id'];
44
		}
45
46
		if (!$this->Asaas->save($data)) {
47
			$this->Session->setFlash('Erro ao atualizar dados!');
48
            return $this->redirect('/asaas/configuracoes');
49
		}
50
51
		$this->Session->setFlash('Dados atualizados com sucesso!');
52
        return $this->redirect('/asaas/configuracoes');
53
	}
54
55
	// \"customer\": \"{CUSTOMER_ID}\",
56
	// \"billingType\": \"BOLETO\",
57
	// \"dueDate\": \"2017-06-10\",
58
	// \"value\": 100,
59
	// \"description\": \"Pedido 056984\",
60
	// \"externalReference\": \"056984\"
61
	public function criar_cobranca($data)
62
	{
63
		return $this->request($data, '/payments', 'POST');
64
	}
65
66
	// {
67
	// 	  \"name\": \"Marcelo Almeida\",
68
	// 	  \"email\": \"[email protected]\",
69
	// 	  \"phone\": \"4738010919\",
70
	// 	  \"mobilePhone\": \"4799376637\",
71
	// 	  \"cpfCnpj\": \"24971563792\",
72
	// 	  \"postalCode\": \"01310-000\",
73
	// 	  \"address\": \"Av. Paulista\",
74
	// 	  \"addressNumber\": \"150\",
75
	// 	  \"complement\": \"Sala 201\",
76
	// 	  \"province\": \"Centro\",
77
	// 	  \"externalReference\": \"12987382\",
78
	// 	  \"notificationDisabled\": false,
79
	// 	  \"additionalEmails\": \"[email protected],[email protected]\"
80
	// 	}
81
	public function criar_cliente($data)
82
	{
83
		return $this->request($data, '/customers', 'POST');
84
	}
85
86
	public function request($data, $method, $type='GET')
87
	{
88
		$ch = curl_init();
89
90
		curl_setopt($ch, CURLOPT_URL, $this->endpoint . $method);
91
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
92
		curl_setopt($ch, CURLOPT_HEADER, FALSE);
93
94
		if (strtoupper($type) == "POST") {
95
			curl_setopt($ch, CURLOPT_POST, TRUE);
96
			curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
97
		}
98
99
		curl_setopt($ch, CURLOPT_HTTPHEADER, array(
100
		  "Content-Type: application/json",
101
		  "access_token: " . $this->loadToken()
102
		));
103
104
		$response = curl_exec($ch);
105
		curl_close($ch);
106
107
		return json_decode($response);
108
	}
109
110
	public function loadToken()
111
	{
112
		$this->loadModel('Asaas');
113
114
		$response = $this->Asaas->find('first', array(
115
				array('conditions' => array(
116
						'Asaas.usuario_id' => $this->instancia
117
					)
118
				)
119
			)
120
		);
121
122
		if (!empty($response)) {
123
			return $response['Asaas']['api_key'];
124
		}
125
	}
126
	
127
}