diff --git a/Changelog b/Changelog index 35d0b7e04..266eeccb6 100755 --- a/Changelog +++ b/Changelog @@ -1,4 +1,5 @@ 2018-04-07 DariusIII + * Chg: Add BtcPaymentController * Chg: Update libraries to their latest versions 2018-04-06 DariusIII * Fix: Fix pager on browse pages diff --git a/app/Http/Controllers/BasePageController.php b/app/Http/Controllers/BasePageController.php index e59b5a16d..9155a6a63 100644 --- a/app/Http/Controllers/BasePageController.php +++ b/app/Http/Controllers/BasePageController.php @@ -92,7 +92,7 @@ class BasePageController extends Controller */ public function __construct(Request $request) { - $this->middleware('auth')->except('api', 'rss', 'contact', 'showContactForm'); + $this->middleware('auth')->except('api', 'rss', 'contact', 'showContactForm', 'callback'); // Buffer settings/DB connection. $this->settings = new Settings(); $this->pdo = new DB(); diff --git a/app/Http/Controllers/BtcPaymentController.php b/app/Http/Controllers/BtcPaymentController.php new file mode 100644 index 000000000..3a09f0d01 --- /dev/null +++ b/app/Http/Controllers/BtcPaymentController.php @@ -0,0 +1,97 @@ +setPrefs(); + $gateway_id = env('MYCELIUM_GATEWAY_ID'); + $gateway_secret = env('MYCELIUM_GATEWAY_SECRET'); + + $userId = Auth::id(); + $user = User::find($userId); + $action = $request->input('action') ?? 'view'; + $donation = UserRole::query()->where('donation', '>', 0)->get(['id', 'name', 'donation', 'addyears']); + $this->smarty->assign('donation', $donation); + + switch ($action) { + case 'submit': + $price = $request->input('price'); + $role = $request->input('role'); + $roleName = $request->input('rolename'); + $addYears = $request->input('addyears'); + $data = ['user_id' => $userId, 'username' => $user->username, 'price' => $price, 'role' => $role, 'rolename' => $roleName, '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 = Auth::id(); + break; + } + + $title = 'Become a supporter'; + $meta_title = 'Become a supporter'; + $meta_description = 'Become a supporter'; + + $content = $this->smarty->fetch('btc_payment.tpl'); + + $this->smarty->assign( + [ + 'content' => $content, + 'meta_title' => $meta_title, + 'title' => $title, + 'meta_description' => $meta_description, + ] + ); + $this->pagerender(); + } + + /** + * Callback data from Mycelium Gear + */ + public function callback() + { + $gateway_id = env('MYCELIUM_GATEWAY_ID'); + $gateway_secret = env('MYCELIUM_GATEWAY_SECRET'); + + $geary = new Geary($gateway_id, $gateway_secret); + $order = $geary->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) { + User::updateUserRole($callback_data['user_id'], $newRole); + User::updateUserRoleChangeDate($callback_data['user_id'], Carbon::now()->addYears($addYear)); + } + } + } +} diff --git a/public/pages/btc_payment.php b/public/pages/btc_payment.php deleted file mode 100644 index 79e369c9f..000000000 --- a/public/pages/btc_payment.php +++ /dev/null @@ -1,55 +0,0 @@ -show403(); -} - -$page = new Page(); - -$gateway_id = env('MYCELIUM_GATEWAY_ID'); -$gateway_secret = env('MYCELIUM_GATEWAY_SECRET'); - -$userId = Auth::id(); -$user = User::find($userId); -$action = request()->input('action') ?? 'view'; -$donation = UserRole::query()->where('donation', '>', 0)->get(['id', 'name', 'donation', 'addyears']); -$page->smarty->assign('donation', $donation); - -switch ($action) { - case 'submit': - $price = request()->input('price'); - $role = request()->input('role'); - $roleName = request()->input('rolename'); - $addYears = request()->input('addyears'); - $data = ['user_id' => $userId, 'username' => $user->username, 'price' => $price, 'role' => $role, 'rolename' => $roleName, '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 = Auth::id(); - 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->pagerender(); diff --git a/public/pages/btc_payment_callback.php b/public/pages/btc_payment_callback.php deleted file mode 100644 index 925d8e54a..000000000 --- a/public/pages/btc_payment_callback.php +++ /dev/null @@ -1,23 +0,0 @@ -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) { - User::updateUserRole($callback_data['user_id'], $newRole); - User::updateUserRoleChangeDate($callback_data['user_id'], \Carbon\Carbon::now()->addYears($addYear)); - } -} diff --git a/routes/web.php b/routes/web.php index 943988fe2..917f5739d 100644 --- a/routes/web.php +++ b/routes/web.php @@ -139,18 +139,10 @@ Route::get('/filelist/{id}', function () { redirect('/filelist'); })->middleware('auth'); -Route::get('/btc_payment', function () { - redirect('/btc_payment'); -})->middleware('auth'); +Route::get('btc_payment', 'BtcPaymentController@show'); -Route::post('/btc_payment', function () { - redirect('/btc_payment'); -})->middleware('auth'); +Route::post('btc_payment', 'BtcPaymentController@show'); -Route::get('/btc_payment_callback', function () { - redirect('/btc_payment_callback'); -}); +Route::get('btc_payment_callback', 'BtcPaymentController@callback'); -Route::post('/btc_payment_callback', function () { - redirect('/btc_payment_callback'); -}); +Route::post('btc_payment_callback', 'BtcPaymentController@callback');