Update packages and btcpayment support

This commit is contained in:
DariusIII
2024-01-08 18:59:30 +01:00
parent bedca3e0bc
commit 59c59d049a
5 changed files with 131 additions and 86 deletions
+29 -3
View File
@@ -2,6 +2,7 @@
namespace App\Http\Controllers;
use App\Models\Payment;
use App\Models\User;
use Blacklight\libraries\Geary;
use Illuminate\Http\Request;
@@ -87,6 +88,9 @@ class BtcPaymentController extends BasePageController
}
}
/**
* BtcPay callback.
*/
public function btcPayCallback(Request $request): Response
{
$hashCheck = 'sha256='.hash_hmac('sha256', $request->getContent(), config('nntmux.btcpay_webhook_secret'));
@@ -110,9 +114,31 @@ class BtcPaymentController extends BasePageController
}
$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']);
$checkOrder = Payment::query()->where('invoice_id', '=', $payload['invoiceId'])->where('payment_status', '=', 'Settled')->first();
if (! empty($checkOrder)) {
Log::error('Duplicate BTCPay webhook: '.$payload['webhookId']);
return response('Not Found', 404);
} else {
Payment::create([
'email' => $payload['metadata']['buyerEmail'],
'username' => $user->username,
'item_description' => $payload['metadata']['itemDesc'],
'order_id' => $payload['metadata']['orderId'],
'payment_id' => $payload['payment']['id'],
'payment_status' => $payload['payment']['status'],
'invoice_amount' => $payload['metadata']['invoice_amount'],
'payment_method' => $payload['paymentMethod'],
'payment_value' => $payload['payment']['value'],
'webhook_id' => $payload['webhookId'],
'invoice_id' => $payload['invoiceId'],
]);
User::updateUserRole($user->id, $matches['role']);
User::updateUserRoleChangeDate($user->id, null, $matches['addYears']);
Log::info('User: '.$user->username.' upgraded to '.$matches['role'].' for BTCPay webhook: '.$payload['metadata']['buyerEmail']);
return response('OK', 200);
}
} else {
Log::error('User not found for BTCPay webhook: '.$payload['metadata']['buyerEmail']);
+13
View File
@@ -0,0 +1,13 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Payment extends Model
{
use HasFactory;
protected $guarded = [];
}
Generated
+50 -49
View File
@@ -3092,20 +3092,20 @@
},
{
"name": "intervention/gif",
"version": "3.0.0",
"version": "4.0.0",
"source": {
"type": "git",
"url": "https://github.com/Intervention/gif.git",
"reference": "cfececc760862f075a52acf747031bad08c8301b"
"reference": "521010e0cf1f5cde9df54b8adcd7496ab5f1504f"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/Intervention/gif/zipball/cfececc760862f075a52acf747031bad08c8301b",
"reference": "cfececc760862f075a52acf747031bad08c8301b",
"url": "https://api.github.com/repos/Intervention/gif/zipball/521010e0cf1f5cde9df54b8adcd7496ab5f1504f",
"reference": "521010e0cf1f5cde9df54b8adcd7496ab5f1504f",
"shasum": ""
},
"require": {
"php": "^8.0"
"php": "^8.1"
},
"require-dev": {
"phpstan/phpstan": "^1",
@@ -3138,27 +3138,27 @@
],
"support": {
"issues": "https://github.com/Intervention/gif/issues",
"source": "https://github.com/Intervention/gif/tree/3.0.0"
"source": "https://github.com/Intervention/gif/tree/4.0.0"
},
"time": "2023-11-27T18:54:30+00:00"
"time": "2024-01-06T16:35:57+00:00"
},
{
"name": "intervention/image",
"version": "3.2.1",
"version": "3.2.3",
"source": {
"type": "git",
"url": "https://github.com/Intervention/image.git",
"reference": "3f96a8f752f4db17cde0a152891bc332ea9aa28d"
"reference": "72680e81388bba766cef9db42943ff7d152a6d84"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/Intervention/image/zipball/3f96a8f752f4db17cde0a152891bc332ea9aa28d",
"reference": "3f96a8f752f4db17cde0a152891bc332ea9aa28d",
"url": "https://api.github.com/repos/Intervention/image/zipball/72680e81388bba766cef9db42943ff7d152a6d84",
"reference": "72680e81388bba766cef9db42943ff7d152a6d84",
"shasum": ""
},
"require": {
"ext-mbstring": "*",
"intervention/gif": "^3",
"intervention/gif": "^4",
"php": "^8.1"
},
"require-dev": {
@@ -3195,7 +3195,7 @@
],
"support": {
"issues": "https://github.com/Intervention/image/issues",
"source": "https://github.com/Intervention/image/tree/3.2.1"
"source": "https://github.com/Intervention/image/tree/3.2.3"
},
"funding": [
{
@@ -3207,7 +3207,7 @@
"type": "github"
}
],
"time": "2023-12-30T20:10:28+00:00"
"time": "2024-01-08T09:53:13+00:00"
},
{
"name": "james-heinrich/getid3",
@@ -7334,16 +7334,16 @@
},
{
"name": "php-http/promise",
"version": "1.2.1",
"version": "1.3.0",
"source": {
"type": "git",
"url": "https://github.com/php-http/promise.git",
"reference": "44a67cb59f708f826f3bec35f22030b3edb90119"
"reference": "2916a606d3b390f4e9e8e2b8dd68581508be0f07"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/php-http/promise/zipball/44a67cb59f708f826f3bec35f22030b3edb90119",
"reference": "44a67cb59f708f826f3bec35f22030b3edb90119",
"url": "https://api.github.com/repos/php-http/promise/zipball/2916a606d3b390f4e9e8e2b8dd68581508be0f07",
"reference": "2916a606d3b390f4e9e8e2b8dd68581508be0f07",
"shasum": ""
},
"require": {
@@ -7380,9 +7380,9 @@
],
"support": {
"issues": "https://github.com/php-http/promise/issues",
"source": "https://github.com/php-http/promise/tree/1.2.1"
"source": "https://github.com/php-http/promise/tree/1.3.0"
},
"time": "2023-11-08T12:57:08+00:00"
"time": "2024-01-04T18:49:48+00:00"
},
{
"name": "php-tmdb/api",
@@ -7736,16 +7736,16 @@
},
{
"name": "phpstan/phpdoc-parser",
"version": "1.24.5",
"version": "1.25.0",
"source": {
"type": "git",
"url": "https://github.com/phpstan/phpdoc-parser.git",
"reference": "fedf211ff14ec8381c9bf5714e33a7a552dd1acc"
"reference": "bd84b629c8de41aa2ae82c067c955e06f1b00240"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/phpstan/phpdoc-parser/zipball/fedf211ff14ec8381c9bf5714e33a7a552dd1acc",
"reference": "fedf211ff14ec8381c9bf5714e33a7a552dd1acc",
"url": "https://api.github.com/repos/phpstan/phpdoc-parser/zipball/bd84b629c8de41aa2ae82c067c955e06f1b00240",
"reference": "bd84b629c8de41aa2ae82c067c955e06f1b00240",
"shasum": ""
},
"require": {
@@ -7777,9 +7777,9 @@
"description": "PHPDoc parser with support for nullable, intersection and generic types",
"support": {
"issues": "https://github.com/phpstan/phpdoc-parser/issues",
"source": "https://github.com/phpstan/phpdoc-parser/tree/1.24.5"
"source": "https://github.com/phpstan/phpdoc-parser/tree/1.25.0"
},
"time": "2023-12-16T09:33:33+00:00"
"time": "2024-01-04T17:06:16+00:00"
},
{
"name": "pragmarx/google2fa",
@@ -10037,35 +10037,35 @@
},
{
"name": "spatie/laravel-ignition",
"version": "2.3.3",
"version": "2.4.0",
"source": {
"type": "git",
"url": "https://github.com/spatie/laravel-ignition.git",
"reference": "66499cd3c858642ded56dafb8fa0352057ca20dd"
"reference": "b9395ba48d3f30d42092cf6ceff75ed7256cd604"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/spatie/laravel-ignition/zipball/66499cd3c858642ded56dafb8fa0352057ca20dd",
"reference": "66499cd3c858642ded56dafb8fa0352057ca20dd",
"url": "https://api.github.com/repos/spatie/laravel-ignition/zipball/b9395ba48d3f30d42092cf6ceff75ed7256cd604",
"reference": "b9395ba48d3f30d42092cf6ceff75ed7256cd604",
"shasum": ""
},
"require": {
"ext-curl": "*",
"ext-json": "*",
"ext-mbstring": "*",
"illuminate/support": "^10.0",
"illuminate/support": "^10.0|^11.0",
"php": "^8.1",
"spatie/flare-client-php": "^1.3.5",
"spatie/ignition": "^1.9",
"symfony/console": "^6.2.3",
"symfony/var-dumper": "^6.2.3"
"symfony/console": "^6.2.3|^7.0",
"symfony/var-dumper": "^6.2.3|^7.0"
},
"require-dev": {
"livewire/livewire": "^2.11",
"livewire/livewire": "^2.11|^3.3.5",
"mockery/mockery": "^1.5.1",
"openai-php/client": "^0.3.4",
"orchestra/testbench": "^8.0",
"pestphp/pest": "^1.22.3",
"openai-php/client": "^0.8.1",
"orchestra/testbench": "^8.0|^9.0",
"pestphp/pest": "^2.30",
"phpstan/extension-installer": "^1.2",
"phpstan/phpstan-deprecation-rules": "^1.1.1",
"phpstan/phpstan-phpunit": "^1.3.3",
@@ -10125,7 +10125,7 @@
"type": "github"
}
],
"time": "2023-12-21T09:43:05+00:00"
"time": "2024-01-04T14:51:24+00:00"
},
{
"name": "spatie/laravel-image-optimizer",
@@ -15602,21 +15602,22 @@
},
{
"name": "friendsofphp/php-cs-fixer",
"version": "v3.45.0",
"version": "v3.46.0",
"source": {
"type": "git",
"url": "https://github.com/PHP-CS-Fixer/PHP-CS-Fixer.git",
"reference": "c0daa33cb2533cd73f48dde1c70c2afa3e7953b5"
"reference": "be6831c9af1740470d2a773119b9273f8ac1c3d2"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/PHP-CS-Fixer/PHP-CS-Fixer/zipball/c0daa33cb2533cd73f48dde1c70c2afa3e7953b5",
"reference": "c0daa33cb2533cd73f48dde1c70c2afa3e7953b5",
"url": "https://api.github.com/repos/PHP-CS-Fixer/PHP-CS-Fixer/zipball/be6831c9af1740470d2a773119b9273f8ac1c3d2",
"reference": "be6831c9af1740470d2a773119b9273f8ac1c3d2",
"shasum": ""
},
"require": {
"composer/semver": "^3.4",
"composer/xdebug-handler": "^3.0.3",
"ext-filter": "*",
"ext-json": "*",
"ext-tokenizer": "*",
"php": "^7.4 || ^8.0",
@@ -15680,7 +15681,7 @@
],
"support": {
"issues": "https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/issues",
"source": "https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/tree/v3.45.0"
"source": "https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/tree/v3.46.0"
},
"funding": [
{
@@ -15688,7 +15689,7 @@
"type": "github"
}
],
"time": "2023-12-30T02:07:07+00:00"
"time": "2024-01-03T21:38:46+00:00"
},
{
"name": "hamcrest/hamcrest-php",
@@ -16690,16 +16691,16 @@
},
{
"name": "phpstan/phpstan",
"version": "1.10.50",
"version": "1.10.55",
"source": {
"type": "git",
"url": "https://github.com/phpstan/phpstan.git",
"reference": "06a98513ac72c03e8366b5a0cb00750b487032e4"
"reference": "9a88f9d18ddf4cf54c922fbeac16c4cb164c5949"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/phpstan/phpstan/zipball/06a98513ac72c03e8366b5a0cb00750b487032e4",
"reference": "06a98513ac72c03e8366b5a0cb00750b487032e4",
"url": "https://api.github.com/repos/phpstan/phpstan/zipball/9a88f9d18ddf4cf54c922fbeac16c4cb164c5949",
"reference": "9a88f9d18ddf4cf54c922fbeac16c4cb164c5949",
"shasum": ""
},
"require": {
@@ -16748,7 +16749,7 @@
"type": "tidelift"
}
],
"time": "2023-12-13T10:59:42+00:00"
"time": "2024-01-08T12:32:40+00:00"
},
{
"name": "phpunit/php-code-coverage",
@@ -0,0 +1,38 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('payments', function (Blueprint $table) {
$table->id();
$table->timestamps();
$table->string('email');
$table->string('username');
$table->string('item_description');
$table->string('order_id');
$table->string('payment_id');
$table->string('payment_status');
$table->string('invoice_amount');
$table->string('payment_method');
$table->string('payment_value');
$table->string('webhook_id');
$table->string('invoice_id');
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('payments');
}
};
+1 -34
View File
@@ -376,37 +376,4 @@ Route::post('2faVerify', function () {
return redirect()->to(URL()->previous());
})->name('2faVerify')->middleware('2fa');
Route::post('btcpay/webhook', function (Illuminate\Http\Request $request) {
$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'] === '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);
});
Route::post('btcpay/webhook', [BtcPaymentController::class, 'btcPayCallback'])->name('btcpay.webhook');