|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
|
4
|
|
|
|
|
5
|
|
|
use App\Models\Payment; |
|
6
|
|
|
use App\Models\User; |
|
7
|
|
|
use Illuminate\Http\Request; |
|
8
|
|
|
use Illuminate\Http\Response; |
|
9
|
|
|
use Illuminate\Support\Facades\Log; |
|
10
|
|
|
|
|
11
|
|
|
class BtcPaymentController extends BasePageController |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* BtcPay callback. |
|
15
|
|
|
*/ |
|
16
|
|
|
public function btcPayCallback(Request $request): Response |
|
17
|
|
|
{ |
|
18
|
|
|
$hashCheck = 'sha256='.hash_hmac('sha256', $request->getContent(), config('nntmux.btcpay_webhook_secret')); |
|
19
|
|
|
if ($hashCheck !== $request->header('btcpay-sig')) { |
|
20
|
|
|
Log::channel('btc_payment')->error('BTCPay webhook hash check failed: '.$request->header('btcpay-sig')); |
|
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
return response('Not Found', 404); |
|
23
|
|
|
} |
|
24
|
|
|
$payload = json_decode($request->getContent(), true); |
|
|
|
|
|
|
25
|
|
|
// We have received a payment for an invoice and user should be upgraded to a paid plan based on order |
|
26
|
|
|
if ($payload['type'] === 'InvoicePaymentSettled') { |
|
27
|
|
|
$user = User::query()->where('email', '=', $payload['metadata']['buyerEmail'])->first(); |
|
28
|
|
|
if ($user) { |
|
29
|
|
|
$checkOrder = Payment::query()->where('invoice_id', '=', $payload['invoiceId'])->where('payment_status', '=', 'Settled')->first(); |
|
30
|
|
|
if ($checkOrder !== null) { |
|
31
|
|
|
Log::channel('btc_payment')->error('Duplicate BTCPay webhook: '.$payload['webhookId']); |
|
32
|
|
|
|
|
33
|
|
|
return response('OK', 200); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
Payment::create([ |
|
37
|
|
|
'email' => $payload['metadata']['buyerEmail'], |
|
38
|
|
|
'username' => $user->username, |
|
39
|
|
|
'item_description' => $payload['metadata']['itemDesc'], |
|
40
|
|
|
'order_id' => $payload['metadata']['orderId'], |
|
41
|
|
|
'payment_id' => $payload['payment']['id'], |
|
42
|
|
|
'payment_status' => $payload['payment']['status'], |
|
43
|
|
|
'invoice_amount' => $payload['metadata']['invoice_amount'], |
|
44
|
|
|
'payment_method' => $payload['paymentMethodId'], |
|
45
|
|
|
'payment_value' => $payload['payment']['value'], |
|
46
|
|
|
'webhook_id' => $payload['webhookId'], |
|
47
|
|
|
'invoice_id' => $payload['invoiceId'], |
|
48
|
|
|
]); |
|
49
|
|
|
|
|
50
|
|
|
return response('OK', 200); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
Log::channel('btc_payment')->error('User not found for BTCPay webhook: '.$payload['metadata']['buyerEmail']); |
|
54
|
|
|
|
|
55
|
|
|
return response('Not Found', 404); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
if ($payload['type'] === 'InvoiceSettled') { |
|
59
|
|
|
// Check if we have the invoice_id in payments table and if we do, update the user account |
|
60
|
|
|
$checkOrder = Payment::query()->where('invoice_id', '=', $payload['invoiceId'])->where('payment_status', '=', 'Settled')->where(function ($query) { |
|
61
|
|
|
return $query->where('invoice_status', 'Pending')->orWhereNull('invoice_status'); |
|
62
|
|
|
})->first(); |
|
63
|
|
|
if ($checkOrder !== null) { |
|
64
|
|
|
$user = User::query()->where('email', '=', $checkOrder->email)->first(); |
|
|
|
|
|
|
65
|
|
|
if ($user) { |
|
66
|
|
|
// Extract role name and addYears from item_description using regex |
|
67
|
|
|
// Matches patterns like "User 1", "Admin ++ 2", "Friend 3" |
|
68
|
|
|
if (preg_match('/(?P<role>\w+(\s\+\+)?)[\s]+(?P<addYears>\d+)/i', $checkOrder->item_description, $matches)) { |
|
|
|
|
|
|
69
|
|
|
$roleName = $matches['role']; |
|
70
|
|
|
$addYears = (int) $matches['addYears']; |
|
71
|
|
|
} else { |
|
72
|
|
|
$roleName = $checkOrder->item_description; |
|
73
|
|
|
$addYears = null; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
User::updateUserRole($user->id, $roleName, addYears: $addYears); |
|
77
|
|
|
$checkOrder->update(['invoice_status' => 'Settled']); |
|
78
|
|
|
Log::channel('btc_payment')->info('User: '.$user->username.' upgraded to '.$roleName.' (+'.$addYears.' years) for BTCPay webhook: '.$checkOrder->webhook_id); |
|
|
|
|
|
|
79
|
|
|
|
|
80
|
|
|
return response('OK', 200); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
Log::channel('btc_payment')->error('User not found for BTCPay webhook: '.$checkOrder->webhook_id); |
|
84
|
|
|
|
|
85
|
|
|
return response('Not Found', 404); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
return response('OK', 200); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|