From 583001e91d1823c3e9ee324e64622ad09c0cbc7a Mon Sep 17 00:00:00 2001 From: Gabriel Pedro Date: Mon, 2 May 2016 04:12:14 -0400 Subject: [PATCH 1/5] feat: improving pagseguro payment --- buypoints.php | 21 ++++++++++++++++++++- config.php | 26 ++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/buypoints.php b/buypoints.php index eec0fe7..ba428ed 100644 --- a/buypoints.php +++ b/buypoints.php @@ -54,6 +54,25 @@ if ($paypal['enabled']) { + +

Buy points using Pagseguro:

+
+ + + + + + + + + + +
+
+ + @@ -73,5 +92,5 @@ if ($config['paygol']['enabled'] == true) { Buy Points system disabled.

Sorry, this functionality is disabled.

'; +if (!$config['paypal']['enabled'] && !$config['paygol']['enabled'] && !$config['pagseguro']['enabled']) echo '

Buy Points system disabled.

Sorry, this functionality is disabled.

'; include 'layout/overall/footer.php'; ?> \ No newline at end of file diff --git a/config.php b/config.php index 485e6eb..7a50ac0 100644 --- a/config.php +++ b/config.php @@ -707,6 +707,32 @@ 30 => 420, // +40% bonus ); + ///////////////// + /// PAGSEGURO /// + ///////////////// + // Write your pagseguro address here, and what currency you want to recieve money in. + $config['pagseguro'] = array( + 'enabled' => true, + 'sandbox' => false, + 'email' => '', // Example: pagseguro@mail.com + 'token' => '', + 'currency' => 'BRL', + 'product_name' => '', + 'price' => 100, // 1 real + 'ipn' => "http://".$_SERVER['HTTP_HOST']."/pagseguro_ipn.php", + 'urls' => array( + 'www' => 'pagseguro.uol.com.br', + 'ws' => 'ws.pagseguro.uol.com.br', + 'stc' => 'stc.pagseguro.uol.com.br' + ) + ); + + if ($config['pagseguro']['sandbox']) { + $config['pagseguro']['urls'] = array_map(function ($item) { + return str_replace('pagseguro', 'sandbox.pagseguro', $item); + }, $config['pagseguro']['urls']); + } + ////////////////// /// PAYGOL SMS /// ////////////////// From e48ee4854d9defcfafc1438585c2349ab07915e4 Mon Sep 17 00:00:00 2001 From: Gabriel Pedro Date: Mon, 2 May 2016 13:42:30 -0400 Subject: [PATCH 2/5] feat: finishing pagseguro implementation --- pagseguro_ipn.php | 112 ++++++++++++++++++++++++++++++++++++++++++ pagseguro_retorno.php | 107 ++++++++++++++++++++++++++++++++++++++++ shop.php | 4 ++ 3 files changed, 223 insertions(+) create mode 100644 pagseguro_ipn.php create mode 100644 pagseguro_retorno.php diff --git a/pagseguro_ipn.php b/pagseguro_ipn.php new file mode 100644 index 0000000..25eb8c5 --- /dev/null +++ b/pagseguro_ipn.php @@ -0,0 +1,112 @@ +status) . ' WHERE `transaction` = \'' . $payment->code . '\' '); + + // Check that the payment_status is Completed + if ($payment->status == 3) { + + // Check that transaction has not been previously processed + $transaction = mysql_select_single('SELECT `transaction`, `completed` FROM `znote_pagseguro` WHERE `transaction`= \'' . $payment->code .'\''); + $status = true; + $custom = (int) $payment->reference; + + if ($transaction['completed'] == '1') { + $status = false; + } + + if ($payment->grossAmount == 0.0) $status = false; // Wrong ammount of money + $item = $payment->items->item[0]; + if ($item->amount != ($pagseguro['price'] / 100)) $status = false; + + if ($status) { + // transaction log + mysql_update('UPDATE `znote_pagseguro` SET `completed` = 1 WHERE `transaction` = \'' . $payment->code . '\''); + + // Process payment + $data = mysql_select_single("SELECT `points` AS `old_points` FROM `znote_accounts` WHERE `account_id`='$custom';"); + + // Give points to user + $new_points = $data['old_points'] + $item->quantity; + mysql_update("UPDATE `znote_accounts` SET `points`='$new_points' WHERE `account_id`='$custom'"); + } + } else if ($payment->status == 7) { + mysql_update('UPDATE `znote_pagseguro` SET `completed` = 1 WHERE `transaction` = \'' . $payment->code . '\' '); + } +?> \ No newline at end of file diff --git a/pagseguro_retorno.php b/pagseguro_retorno.php new file mode 100644 index 0000000..8b486bb --- /dev/null +++ b/pagseguro_retorno.php @@ -0,0 +1,107 @@ +status != 7) ? 0 : 1; + + $custom = (int) $transaction->reference; + $item = $transaction->items->item[0]; + $points = $item->quantity; + $price = $item->quantity * ($pagseguro['price'] / 100); + mysql_insert('INSERT INTO `znote_pagseguro` VALUES (null, \'' . $transaction->code . '\', ' . $custom . ', \'' . $price . '\', \'' . $points . '\', ' . $transaction->status . ', ' . $completed . ')'); + + header('Location: shop.php?callback=processing'); diff --git a/shop.php b/shop.php index e002428..402f803 100644 --- a/shop.php +++ b/shop.php @@ -2,6 +2,10 @@ protect_page(); include 'layout/overall/header.php'; +if (isset($_GET['callback']) && $_GET['callback'] === 'processing') { + echo ''; +} + // Import from config: $shop = $config['shop']; $shop_list = $config['shop_offers']; From 358b16e7482073d720632688ad6732684e88e9f3 Mon Sep 17 00:00:00 2001 From: Gabriel Pedro Date: Mon, 2 May 2016 16:27:32 -0400 Subject: [PATCH 3/5] fix: removing comma --- pagseguro_retorno.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pagseguro_retorno.php b/pagseguro_retorno.php index 8b486bb..1efa958 100644 --- a/pagseguro_retorno.php +++ b/pagseguro_retorno.php @@ -38,7 +38,7 @@ `notification_code` varchar(40) NOT NULL, `details` text NOT NULL, `receive_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP, - PRIMARY KEY (`id`), + PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8; */ From 1804fe00594e8875b12a00e406fa69d80af41c1b Mon Sep 17 00:00:00 2001 From: Gabriel Pedro Date: Mon, 2 May 2016 17:30:43 -0400 Subject: [PATCH 4/5] fix: missing var, thanks @dalvorsn for testing --- buypoints.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/buypoints.php b/buypoints.php index ba428ed..6b4b275 100644 --- a/buypoints.php +++ b/buypoints.php @@ -3,6 +3,7 @@ protect_page(); include 'layout/overall/header.php'; // Import from config: +$pagseguro = $config['pagseguro']; $paypal = $config['paypal']; $prices = $config['paypal_prices']; @@ -93,4 +94,4 @@ if ($config['paygol']['enabled'] == true) { Buy Points system disabled.

Sorry, this functionality is disabled.

'; -include 'layout/overall/footer.php'; ?> \ No newline at end of file +include 'layout/overall/footer.php'; ?> From 816801880f4003b8718c74e9aef9da46f1ff49cc Mon Sep 17 00:00:00 2001 From: Gabriel Pedro Date: Wed, 4 May 2016 20:30:37 -0400 Subject: [PATCH 5/5] fix: sanitizing pagseguro --- pagseguro_ipn.php | 14 ++++++++------ pagseguro_retorno.php | 7 ++++--- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/pagseguro_ipn.php b/pagseguro_ipn.php index 25eb8c5..89af36c 100644 --- a/pagseguro_ipn.php +++ b/pagseguro_ipn.php @@ -73,17 +73,19 @@ $rawPayment = VerifyPagseguroIPN($notificationCode); $payment = simplexml_load_string($rawPayment); + $paymentStatus = (int) $paymentStatus; + $paymentCode = sanitize($paymentCode); report($notificationCode, $rawPayment); // Updating Payment Status - mysql_update('UPDATE `znote_pagseguro` SET `payment_status` = ' . ($payment->status) . ' WHERE `transaction` = \'' . $payment->code . '\' '); + mysql_update('UPDATE `znote_pagseguro` SET `payment_status` = ' . $paymentStatus . ' WHERE `transaction` = \'' . $paymentCode . '\' '); // Check that the payment_status is Completed - if ($payment->status == 3) { + if ($paymentStatus == 3) { // Check that transaction has not been previously processed - $transaction = mysql_select_single('SELECT `transaction`, `completed` FROM `znote_pagseguro` WHERE `transaction`= \'' . $payment->code .'\''); + $transaction = mysql_select_single('SELECT `transaction`, `completed` FROM `znote_pagseguro` WHERE `transaction`= \'' . $paymentCode .'\''); $status = true; $custom = (int) $payment->reference; @@ -97,7 +99,7 @@ if ($status) { // transaction log - mysql_update('UPDATE `znote_pagseguro` SET `completed` = 1 WHERE `transaction` = \'' . $payment->code . '\''); + mysql_update('UPDATE `znote_pagseguro` SET `completed` = 1 WHERE `transaction` = \'' . $paymentCode . '\''); // Process payment $data = mysql_select_single("SELECT `points` AS `old_points` FROM `znote_accounts` WHERE `account_id`='$custom';"); @@ -106,7 +108,7 @@ $new_points = $data['old_points'] + $item->quantity; mysql_update("UPDATE `znote_accounts` SET `points`='$new_points' WHERE `account_id`='$custom'"); } - } else if ($payment->status == 7) { - mysql_update('UPDATE `znote_pagseguro` SET `completed` = 1 WHERE `transaction` = \'' . $payment->code . '\' '); + } else if ($paymentStatus == 7) { + mysql_update('UPDATE `znote_pagseguro` SET `completed` = 1 WHERE `transaction` = \'' . $paymentCode . '\' '); } ?> \ No newline at end of file diff --git a/pagseguro_retorno.php b/pagseguro_retorno.php index 1efa958..a988915 100644 --- a/pagseguro_retorno.php +++ b/pagseguro_retorno.php @@ -96,12 +96,13 @@ $rawTransaction = VerifyPagseguroIPN($transactionCode); $transaction = simplexml_load_string($rawTransaction); - $completed = ($transaction->status != 7) ? 0 : 1; + $transactionStatus = (int) $transaction->status; + $completed = ($transactionStatus != 7) ? 0 : 1; $custom = (int) $transaction->reference; $item = $transaction->items->item[0]; $points = $item->quantity; - $price = $item->quantity * ($pagseguro['price'] / 100); - mysql_insert('INSERT INTO `znote_pagseguro` VALUES (null, \'' . $transaction->code . '\', ' . $custom . ', \'' . $price . '\', \'' . $points . '\', ' . $transaction->status . ', ' . $completed . ')'); + $price = $points * ($pagseguro['price'] / 100); + mysql_insert('INSERT INTO `znote_pagseguro` VALUES (null, \'' . sanitize($transaction->code) . '\', ' . $custom . ', \'' . $price . '\', \'' . $points . '\', ' . $transactionStatus . ', ' . $completed . ')'); header('Location: shop.php?callback=processing');