From 030eb44caae9d433a4d2fbfba3bacd90839c3844 Mon Sep 17 00:00:00 2001 From: DariusIII Date: Tue, 5 Dec 2017 23:47:29 +0100 Subject: [PATCH] Add mycelium gear btc payment support --- nntmux/Users.php | 8 +++- public/admin/role-edit.php | 6 ++- public/index.php | 2 + public/pages/btc_payment.php | 53 ++++++++++++++++++++++++++ public/pages/btc_payment_callback.php | 24 ++++++++++++ public/themes/Charisma/btc_payment.tpl | 29 ++++++++++++++ public/themes/Gamma/btc_payment.tpl | 29 ++++++++++++++ public/themes/Gentele/btc_payment.tpl | 29 ++++++++++++++ public/themes/Omicron/btc_payment.tpl | 29 ++++++++++++++ public/themes/admin/role-edit.tpl | 12 ++++++ public/themes/admin/role-list.tpl | 4 ++ resources/db/schema/mysql-ddl.sql | 2 + 12 files changed, 223 insertions(+), 4 deletions(-) create mode 100644 public/pages/btc_payment.php create mode 100644 public/pages/btc_payment_callback.php create mode 100644 public/themes/Charisma/btc_payment.tpl create mode 100644 public/themes/Gamma/btc_payment.tpl create mode 100644 public/themes/Gentele/btc_payment.tpl create mode 100644 public/themes/Omicron/btc_payment.tpl diff --git a/nntmux/Users.php b/nntmux/Users.php index 3ac83a130..d6c5b57e0 100755 --- a/nntmux/Users.php +++ b/nntmux/Users.php @@ -1153,7 +1153,7 @@ class Users * * @return false|int|string */ - public function addRole($name, $apirequests, $downloadrequests, $defaultinvites, $canpreview, $hideads) + public function addRole($name, $apirequests, $downloadrequests, $defaultinvites, $canpreview, $hideads, $donation, $addYear) { return UserRole::query()->insertGetId( [ @@ -1163,6 +1163,8 @@ class Users 'defaultinvites' => $defaultinvites, 'canpreview' => $canpreview, 'hideads' => $hideads, + 'donation' => $donation, + 'addyears' => $addYear, ] ); } @@ -1179,7 +1181,7 @@ class Users * * @return int */ - public function updateRole($id, $name, $apirequests, $downloadrequests, $defaultinvites, $isdefault, $canpreview, $hideads) + public function updateRole($id, $name, $apirequests, $downloadrequests, $defaultinvites, $isdefault, $canpreview, $hideads, $donation, $addYear) { if ((int) $isdefault === 1) { UserRole::query()->update(['isdefault' => 0]); @@ -1194,6 +1196,8 @@ class Users 'isdefault' => $isdefault, 'canpreview' => $canpreview, 'hideads' => $hideads, + 'donation' => $donation, + 'addyears' => $addYear, ] ); } diff --git a/public/admin/role-edit.php b/public/admin/role-edit.php index 1ceafaa44..433b33b25 100644 --- a/public/admin/role-edit.php +++ b/public/admin/role-edit.php @@ -25,6 +25,8 @@ switch ($_REQUEST['action'] ?? 'view') { 'defaultinvites' => '', 'canpreview' => 0, 'hideads' => 0, + 'donation' => 0, + 'addyears' => 0, ]; $page->smarty->assign('role', $role); break; @@ -32,12 +34,12 @@ switch ($_REQUEST['action'] ?? 'view') { case 'submit': if ($_POST['id'] === '') { $role = $page->users->addRole($_POST['name'], $_POST['apirequests'], $_POST['downloadrequests'], - $_POST['defaultinvites'], $_POST['canpreview'], $_POST['hideads'] + $_POST['defaultinvites'], $_POST['canpreview'], $_POST['hideads'], $_POST['donation'], $_POST['addyears'] ); header('Location:'.WWW_TOP.'/role-list.php'); } else { $role = $page->users->updateRole($_POST['id'], $_POST['name'], $_POST['apirequests'], - $_POST['downloadrequests'], $_POST['defaultinvites'], $_POST['isdefault'], $_POST['canpreview'], $_POST['hideads'] + $_POST['downloadrequests'], $_POST['defaultinvites'], $_POST['isdefault'], $_POST['canpreview'], $_POST['hideads'], $_POST['donation'], $_POST['addyears'] ); header('Location:'.WWW_TOP.'/role-list.php'); diff --git a/public/index.php b/public/index.php index f13242a64..d639b180a 100644 --- a/public/index.php +++ b/public/index.php @@ -26,6 +26,8 @@ switch ($page->page) { case 'books': case 'browse': case 'browsegroup': + case 'btc_payment': + case 'btc_payment_callback': case 'cart': case 'console': case 'consolemodal': diff --git a/public/pages/btc_payment.php b/public/pages/btc_payment.php new file mode 100644 index 000000000..cc749e5d0 --- /dev/null +++ b/public/pages/btc_payment.php @@ -0,0 +1,53 @@ +users->isLoggedIn()) { + $page->show403(); +} + + +$gateway_id = env('MYCELIUM_GATEWAY_ID'); +$gateway_secret = env('MYCELIUM_GATEWAY_SECRET'); + +$userId = $page->users->currentUserId(); +$user = (new \nntmux\Users())->getById($userId); +$action = $_REQUEST['action'] ?? 'view'; +$donation = UserRole::query()->where('donation', '>', 0)->get(['id', 'name', 'donation', 'addyears']); +$page->smarty->assign('donation', $donation); + +switch ($action) { + case 'submit': + $price = $_POST['price']; + $role = $_POST['role']; + $addYears = $_POST['addyears']; + $data = ['user_id' => $userId, 'username' => $user->username,'price' => $price, 'role' => $role, 'addyears' => $addYears]; + $keychain_id = random_int(0, 19); + $callback_data = json_encode($data); + + $geary = new Geary($gateway_id, $gateway_secret); + $order = $geary->create_order($price, $keychain_id, $callback_data); + + if ($order->payment_id) { + // Redirect to a payment gateway + $url = 'https://gateway.gear.mycelium.com/pay/'. $order->payment_id; + header('Location: ' . $url); + die(); + } + break; + case 'view': + default: + $userId = $page->users->currentUserId(); + break; +} + + +$page->title = 'Become a supporter'; +$page->meta_title = 'Become a supporter'; +$page->meta_description = 'Become a supporter'; + +$page->content = $page->smarty->fetch('btc_payment.tpl'); +$page->render(); diff --git a/public/pages/btc_payment_callback.php b/public/pages/btc_payment_callback.php new file mode 100644 index 000000000..e6887b7dc --- /dev/null +++ b/public/pages/btc_payment_callback.php @@ -0,0 +1,24 @@ +check_order_callback(); + +// Order status was received +if ($order !== false) { + $callback_data = json_decode($order['callback_data'], true); + $newRole = $callback_data['role']; + $amount = $callback_data['price']; + $addYear = $callback_data['addyears']; + // If order was paid in full (2) or overpaid (4) + if ((int) $order['status'] === 2 || (int) $order['status'] === 4) { + $users->updateUserRole($callback_data['user_id'], $newRole); + $users->updateUserRoleChangeDate($callback_data['user_id'], \Carbon\Carbon::now()->addYears($addYear)); + } +} diff --git a/public/themes/Charisma/btc_payment.tpl b/public/themes/Charisma/btc_payment.tpl new file mode 100644 index 000000000..695027e7b --- /dev/null +++ b/public/themes/Charisma/btc_payment.tpl @@ -0,0 +1,29 @@ +
+
+
+
+ This page will redirect you to site outside of {$site->title} to make your payment +
+ If, for some reason, your account isn't updated automaticaly, please send us an email or use our contact form to inform us so we can fix the issue.
+
+
+
+ + {foreach $donation as $donate} + + + + + + + + + {/foreach} + +
{$donate->name} ({$donate->donation}$)
+ + + + +
+
\ No newline at end of file diff --git a/public/themes/Gamma/btc_payment.tpl b/public/themes/Gamma/btc_payment.tpl new file mode 100644 index 000000000..695027e7b --- /dev/null +++ b/public/themes/Gamma/btc_payment.tpl @@ -0,0 +1,29 @@ +
+
+
+
+ This page will redirect you to site outside of {$site->title} to make your payment +
+ If, for some reason, your account isn't updated automaticaly, please send us an email or use our contact form to inform us so we can fix the issue.
+
+
+
+ + {foreach $donation as $donate} + + + + + + + + + {/foreach} + +
{$donate->name} ({$donate->donation}$)
+ + + + +
+
\ No newline at end of file diff --git a/public/themes/Gentele/btc_payment.tpl b/public/themes/Gentele/btc_payment.tpl new file mode 100644 index 000000000..695027e7b --- /dev/null +++ b/public/themes/Gentele/btc_payment.tpl @@ -0,0 +1,29 @@ +
+
+
+
+ This page will redirect you to site outside of {$site->title} to make your payment +
+ If, for some reason, your account isn't updated automaticaly, please send us an email or use our contact form to inform us so we can fix the issue.
+
+
+
+ + {foreach $donation as $donate} + + + + + + + + + {/foreach} + +
{$donate->name} ({$donate->donation}$)
+ + + + +
+
\ No newline at end of file diff --git a/public/themes/Omicron/btc_payment.tpl b/public/themes/Omicron/btc_payment.tpl new file mode 100644 index 000000000..695027e7b --- /dev/null +++ b/public/themes/Omicron/btc_payment.tpl @@ -0,0 +1,29 @@ +
+
+
+
+ This page will redirect you to site outside of {$site->title} to make your payment +
+ If, for some reason, your account isn't updated automaticaly, please send us an email or use our contact form to inform us so we can fix the issue.
+
+
+
+ + {foreach $donation as $donate} + + + + + + + + + {/foreach} + +
{$donate->name} ({$donate->donation}$)
+ + + + +
+
\ No newline at end of file diff --git a/public/themes/admin/role-edit.tpl b/public/themes/admin/role-edit.tpl index 932ebb862..5d98d6cf0 100644 --- a/public/themes/admin/role-edit.tpl +++ b/public/themes/admin/role-edit.tpl @@ -53,6 +53,18 @@
Whether ad's are hidden
+ + Donation amount: + + + + + + Years Added: + + + + {if $role.id != ''} diff --git a/public/themes/admin/role-list.tpl b/public/themes/admin/role-list.tpl index ee983f994..d19e0acf2 100644 --- a/public/themes/admin/role-list.tpl +++ b/public/themes/admin/role-list.tpl @@ -10,6 +10,8 @@ invites can preview hide ads + donation + add years default roles options @@ -23,6 +25,8 @@ {$role.defaultinvites} {if $role.canpreview == 1}Yes{else}No{/if} {if $role.hideads == 1}Yes{else}No{/if} + {$role.donation} + {$role.addyears} {if $role.isdefault=="1"}Yes{else}No{/if} edit {if $role.id>"3"}delete{/if} diff --git a/resources/db/schema/mysql-ddl.sql b/resources/db/schema/mysql-ddl.sql index 69d880859..48c9fca4b 100755 --- a/resources/db/schema/mysql-ddl.sql +++ b/resources/db/schema/mysql-ddl.sql @@ -1312,6 +1312,8 @@ CREATE TABLE user_roles ( isdefault TINYINT(1) UNSIGNED NOT NULL DEFAULT '0', canpreview TINYINT(1) UNSIGNED NOT NULL DEFAULT '0', hideads TINYINT(1) UNSIGNED NOT NULL DEFAULT '0', + donation INT(10) NOT NULL DEFAULT '0', + addyears INT(2) NOT NULL DEFAULT '0', PRIMARY KEY (id) ) ENGINE = InnoDB