mirror of
https://github.com/NNTmux/newznab-tmux.git
synced 2026-08-29 17:28:55 +00:00
126 lines
5.0 KiB
PHP
126 lines
5.0 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\User;
|
|
use Blacklight\libraries\Geary;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Http\Response;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Spatie\Permission\Models\Role;
|
|
|
|
class BtcPaymentController extends BasePageController
|
|
{
|
|
/**
|
|
* @return \Illuminate\Http\RedirectResponse|void
|
|
*
|
|
* @throws \Exception
|
|
*/
|
|
public function show(Request $request)
|
|
{
|
|
$this->setPreferences();
|
|
$gateway_id = config('settings.mycelium_gateway_id');
|
|
$gateway_secret = config('settings.mycelium_gateway_secret');
|
|
|
|
$action = $request->input('action') ?? 'view';
|
|
$donation = Role::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' => $this->userdata->id, 'username' => $this->userdata->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;
|
|
|
|
return redirect()->to($url);
|
|
}
|
|
break;
|
|
case 'view':
|
|
default:
|
|
$userId = $this->userdata->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(compact('content', 'meta_title', 'title', 'meta_description'));
|
|
$this->pagerender();
|
|
}
|
|
|
|
/**
|
|
* Callback data from Mycelium Gear.
|
|
*/
|
|
public function callback(): void
|
|
{
|
|
$gateway_id = config('settings.mycelium_gateway_id');
|
|
$gateway_secret = config('settings.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'], null, $addYear);
|
|
}
|
|
}
|
|
}
|
|
|
|
public function btcPayCallback(Request $request): Response
|
|
{
|
|
$hashCheck = 'sha256='.hash_hmac('sha256', $request->getContent(), config('nntmux.btcpay_webhook_secret'));
|
|
if ($hashCheck !== $request->header('btcpay-sig')) {
|
|
Log::error('BTCPay webhook hash check failed: '.$request->header('btcpay-sig'));
|
|
|
|
return response('Not Found', 404);
|
|
}
|
|
$payload = json_decode($request->getContent(), true);
|
|
// We have received a payment for an invoice and user should be upgraded to a paid plan based on order
|
|
if ($payload['type'] === 'InvoiceReceivedPayment' || $payload['type'] === 'InvoicePaymentSettled') {
|
|
preg_match('/(?P<role>\w+(\s\+\+)?)[\s](?P<addYears>\d+)/i', $payload['metadata']['itemDesc'], $matches);
|
|
if (empty($matches)) {
|
|
Log::error('Could not parse BTCPay webhook: '.$payload['metadata']['itemDesc']);
|
|
preg_match('/(?P<role>\w+(\s\+\+)?)[\s](?P<addYears>\d+)/i', $payload['metadata']['itemCode'], $matches);
|
|
if (empty($matches)) {
|
|
Log::error('Could not parse BTCPay webhook: '.$payload['metadata']['itemDesc']);
|
|
|
|
return response('Not Found', 404);
|
|
}
|
|
}
|
|
$user = User::query()->where('email', '=', $payload['metadata']['buyerEmail'])->first();
|
|
if ($user) {
|
|
User::updateUserRole($user->id, $matches['role']);
|
|
User::updateUserRoleChangeDate($user->id, null, $matches['addYears']);
|
|
Log::info('User upgraded to '.$matches['role'].' for BTCPay webhook: '.$payload['metadata']['buyerEmail']);
|
|
} else {
|
|
Log::error('User not found for BTCPay webhook: '.$payload['metadata']['buyerEmail']);
|
|
|
|
return response('Not Found', 404);
|
|
}
|
|
}
|
|
|
|
return response('OK', 200);
|
|
}
|
|
}
|