pagseguro_retorno.php

main 116 lines · 4 KB Raw
Alex Alex Commit Initial commit 01/10/2026 09:20
1<?php
2 /*
3 Instalação
4 - Configurando Pagseguro (Acessar https://pagseguro.uol.com.br/preferencias/integracoes.jhtml)
5 - Notificação de Transação:
6 - http://you-site/pagseguro_ipn.php
7 - Página de redirecionamento:
8 - A. Página fixa de redirecionamento
9 - http://you-site/pagseguro_retorno.php
10 - B. Redirecionamento com o código da transação
11 - transaction
12 - Gerar o Token e copiar para a próxima etapa
13
14 - Configurando ZnoteACC
15 - config.php
16 - $config['pagseguro']['email']
17 - Seu email da conta do pagseguro que irá receber o pagamento
18 - $config['pagseguro']['token']
19 - Preencher com o Token que pedi pra copiar na primeira etapa
20 - $config['pagseguro']['product_name']
21 - Nome do Produto
22
23 - Instalando Tabelas
24 CREATE TABLE IF NOT EXISTS `znote_pagseguro` (
25 `id` int(11) NOT NULL AUTO_INCREMENT,
26 `transaction` varchar(36) NOT NULL,
27 `account` int(11) NOT NULL,
28 `price` decimal(11,2) NOT NULL,
29 `points` int(11) NOT NULL,
30 `payment_status` tinyint(1) NOT NULL,
31 `completed` tinyint(4) NOT NULL,
32 PRIMARY KEY (`id`),
33 FOREIGN KEY (account) REFERENCES accounts(id)
34 ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
35
36 CREATE TABLE IF NOT EXISTS `znote_pagseguro_notifications` (
37 `id` int(11) NOT NULL AUTO_INCREMENT,
38 `notification_code` varchar(40) NOT NULL,
39 `details` text NOT NULL,
40 `receive_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
41 PRIMARY KEY (`id`)
42 ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
43 */
44
45 // Require the functions to fetch config values
46 require 'config.php';
47
48 // Require the functions to connect to database
49 require 'engine/database/connect.php';
50
51 $pagseguro = $config['pagseguro'];
52
53 // Fetch and sanitize POST and GET values
54 function getValue($value) {
55 return (!empty($value)) ? sanitize($value) : false;
56 }
57 function sanitize($data) {
58 return htmlentities(strip_tags(mysql_znote_escape_string($data)));
59 }
60
61 // Util function to insert log
62 function report($code, $details = '') {
63 $connectedIp = $_SERVER['REMOTE_ADDR'];
64 $details = getValue($details);
65 $details .= '\nConnection from IP: '. $connectedIp;
66 db()->execute('INSERT INTO `znote_pagseguro_notifications` VALUES (null, ?, ?, CURRENT_TIMESTAMP)', [getValue($code), $details]);
67 }
68
69 function VerifyPagseguroIPN($code) {
70 global $pagseguro;
71 $url = $pagseguro['urls']['ws'];
72
73 $cURL = curl_init();
74 curl_setopt($cURL, CURLOPT_SSL_VERIFYPEER, false);
75 curl_setopt($cURL, CURLOPT_SSL_VERIFYHOST, false);
76 curl_setopt($cURL, CURLOPT_URL, 'https://' . $url . '/v3/transactions/' . $code . '?email=' . $pagseguro['email'] . '&token=' . $pagseguro['token']);
77 curl_setopt($cURL, CURLOPT_HEADER, false);
78 curl_setopt($cURL, CURLOPT_RETURNTRANSFER, true);
79 curl_setopt($cURL, CURLOPT_FORBID_REUSE, true);
80 curl_setopt($cURL, CURLOPT_FRESH_CONNECT, true);
81 curl_setopt($cURL, CURLOPT_CONNECTTIMEOUT, 30);
82 curl_setopt($cURL, CURLOPT_TIMEOUT, 60);
83 curl_setopt($cURL, CURLINFO_HEADER_OUT, true);
84 curl_setopt($cURL, CURLOPT_HTTPHEADER, array(
85 'Connection: close',
86 'Expect: ',
87 ));
88 $Response = curl_exec($cURL);
89 $Status = (int)curl_getinfo($cURL, CURLINFO_HTTP_CODE);
90 curl_close($cURL);
91
92 return trim($Response);
93 }
94
95 $transactionCode = getValue($_GET['transaction'] ?? null);
96 $rawTransaction = VerifyPagseguroIPN($transactionCode);
97 $transaction = simplexml_load_string((string)$rawTransaction);
98 if ($transaction === false || !isset($transaction->items->item[0])) {
99 header('Location: shop.php');
100 exit;
101 }
102
103 $transactionStatus = (int) $transaction->status;
104 $completed = ($transactionStatus != 7) ? 0 : 1;
105
106 $custom = (int) $transaction->reference;
107 $item = $transaction->items->item[0];
108 $points = $item->quantity;
109 $price = $points * ($pagseguro['price'] / 100);
110 db()->execute(
111 'INSERT INTO `znote_pagseguro` VALUES (null, ?, ?, ?, ?, ?, ?)',
112 [sanitize((string)$transaction->code), $custom, $price, $points, $transactionStatus, $completed]
113 );
114
115 header('Location: shop.php?callback=processing');
116
Top