forked from valdeir2000/Moip-Checkout-Transparente
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathretorno_moip.php
185 lines (151 loc) · 6.72 KB
/
retorno_moip.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php
/**
* MoIP NASP
*
* @author Valdeir Santana <[email protected]>
* @version 1.0.0
* @license <a href="http://www.opensource.org/licenses/bsd-license.php">BSD License</a>
*/
//Inclui as variaveis globais
require_once 'config.php';
//Inclui o startup
require_once DIR_SYSTEM . 'startup.php';
//Conecta ao banco de dados
$db = new DB (DB_DRIVER, DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
//Instancia um novo objeto Language
$languageMail = new Language('portuguese-br');
//Carrega o arquivo
$languageMail->load('mail/moip');
//Captura a quantidade de dados da tabela moip_nasp
$id_transacao = $db->query('SELECT * FROM ' . DB_PREFIX . 'moip_nasp WHERE id_transacao = "' . $_POST['id_transacao'] . '"');
//Captura todos os dados do moip da tabela setting
$status_moip = $db->query('SELECT * FROM ' . DB_PREFIX . 'setting WHERE `group` = "moip" ORDER BY setting_id');
//Captura os dados da tabela setting
$config = $db->query('SELECT `key`,`value` FROM `' . DB_PREFIX . 'setting` WHERE `group` = "config" OR `group` = "moip"');
//Captura o valor da chave notifica��o, autorizado, iniciado, boleto impresso, concluido, cancelado, em analise, estornado, em revis�o e reembolsado.
$notify = $status_moip->rows[5]['value'];
$autorizado = $status_moip->rows[8]['value'];
$iniciado = $status_moip->rows[9]['value'];
$boletImpresso = $status_moip->rows[10]['value'];
$concluido = $status_moip->rows[11]['value'];
$cancelado = $status_moip->rows[12]['value'];
$emAnalise = $status_moip->rows[13]['value'];
$estornado = $status_moip->rows[14]['value'];
$emRevisao = $status_moip->rows[15]['value'];
$reembolsado = $status_moip->rows[16]['value'];
//Captura o id enviado pelo moip, compara com os valores da variaveis acima
switch ($_POST['status_pagamento']) {
case 1 :
$status = $autorizado;
break;
case 2 :
$status = $iniciado;
break;
case 3 :
$status = $boletImpresso;
break;
case 4 :
$status = $concluido;
break;
case 5 :
$status = $cancelado;
break;
case 6 :
$status = $emAnalise;
break;
}
//Caso j� exista o id recebido pelo moip na tabela moip_nasp, ele atualiza as tabelas moip_nasp e order
if (!empty($id_transacao->row)) {
$db->query('UPDATE ' . DB_PREFIX . 'moip_nasp SET status_pagamento="' . $status . '" WHERE id_transacao = "' . $_POST['id_transacao'] . '"');
}else{
//Caso n�o exista o id recebido pelo moip na tabela moip_nasp, inseri os dados recebidos do moip na tabela moip_nasp
$db->query("INSERT INTO `" . DB_PREFIX . "moip_nasp` (
`id_transacao`,
`valor`,
`status_pagamento`,
`cod_moip`,
`forma_pagamento`,
`tipo_pagamento`,
`parcelas`,
`email_consumidor`,
`cartao_bin`,
`cartao_final`,
`cartao_bandeira`,
`cofre`)
VALUES (
'" . $_POST['id_transacao'] . "',
'" . $_POST['valor'] . "',
'" . $_POST['status_pagamento'] . "',
'" . $_POST['cod_moip'] . "',
'" . $_POST['forma_pagamento'] . "',
'" . $_POST['tipo_pagamento'] . "',
'" . $_POST['parcelas'] . "',
'" . $_POST['email_consumidor'] . "',
'" . $_POST['cartao_bin'] . "',
'" . $_POST['cartao_final'] . "',
'" . $_POST['cartao_bandeira'] . "',
'" . $_POST['cofre'] . "');");
}
$db->query('UPDATE `' . DB_PREFIX . 'order` SET order_status_id = "' . $status . '" WHERE order_id = "' . $_POST['id_transacao'] . '"');
$mensagem = '';
//Verifica se a op��o notifica��o do cliente est� ativa, caso esteja:
if ($notify) {
//Captura o nome do status recebido pelo moip,
$status_order = $db->query('SELECT * FROM ' . DB_PREFIX . 'order_status WHERE order_status_id = "' . $status . '"');
//Captura as configura��es de email para o envio
for ($i = 0;$i < count($config->rows);$i++) {
if ($config->rows[$i]['key'] == 'config_mail_protocol') {
$config_mail_protocol = $config->rows[$i]['value'];
}
if ($config->rows[$i]['key'] == 'config_mail_parameter') {
$config_mail_parameter = $config->rows[$i]['value'];
}
if ($config->rows[$i]['key'] == 'config_smtp_host') {
$config_smtp_host = $config->rows[$i]['value'];
}
if ($config->rows[$i]['key'] == 'config_smtp_username') {
$config_smtp_username = $config->rows[$i]['value'];
}
if ($config->rows[$i]['key'] == 'config_smtp_password') {
$config_smtp_password = $config->rows[$i]['value'];
}
if ($config->rows[$i]['key'] == 'config_smtp_port') {
$config_smtp_port = $config->rows[$i]['value'];
}
if ($config->rows[$i]['key'] == 'config_smtp_timeout') {
$config_smtp_timeout = $config->rows[$i]['value'];
}
if ($config->rows[$i]['key'] == 'config_email') {
$config_email = $config->rows[$i]['value'];
}
if ($config->rows[$i]['key'] == 'config_name') {
$config_name = $config->rows[$i]['value'];
}
}
//Cria uma mensagem para enviar ao cliente
$assunto = sprintf($languageMail->get('text_update_subject'), $config_name, $_POST['id_transacao']);
$mensagem = $languageMail->get('text_update_order') . ' ' . $_POST['id_transacao'] . ' <br/>';
$mensagem .= $languageMail->get('text_update_date_added') . ' ' . date('d/m/Y h:m:s') . '<br/>';
$mensagem .= $languageMail->get('text_update_order_status') . '<br/><strong>'. $status_order->row['name'] .'</strong><br/><br/>';
$mensagem .= $languageMail->get('text_method_payment') . '<br/><strong>MoiP - '. $_POST['tipo_pagamento'] .'</strong><br/><br/>';
$mensagem .= $languageMail->get('text_update_link') . '<br/>';
$mensagem .= '<a href="'. HTTP_SERVER . 'index.php?route=account/order/info&order_id='. $_POST['id_transacao'] . '">'. HTTP_SERVER . 'index.php?route=account/order/info&order_id='. $_POST['id_transacao'] . '</a><br/><br/>';
$mensagem .= $languageMail->get('text_update_footer');
$mail = new Mail();
$mail->protocol = $config_mail_protocol;
$mail->parameter = $config_mail_parameter;
$mail->hostname = $config_smtp_host;
$mail->username = $config_smtp_username;
$mail->password = $config_smtp_password;
$mail->port = $config_smtp_port;
$mail->timeout = $config_smtp_timeout;
$mail->setTo($_POST['email_consumidor']);
$mail->setFrom($config_email);
$mail->setSender($config_name);
$mail->setSubject(html_entity_decode($assunto, ENT_QUOTES, 'UTF-8'));
$mail->setHtml($mensagem, ENT_NOQUOTES);
$mail->send();
}
//Adiciona a altera��o na tabela order_history (hist�rico de pedido)
$db->query("INSERT INTO `". DB_PREFIX ."order_history` (`order_id`, `order_status_id`, `notify`, `comment`, `date_added`) VALUES ('".$_POST['id_transacao']."', '".$status."', '".$notify."', '".$db->escape(strip_tags($mensagem))."', NOW())");
?>