Conditions | 8 |
Paths | 8 |
Total Lines | 66 |
Code Lines | 43 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
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::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::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::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 | preg_match('/(?P<role>\w+(\s\+\+)?)[\s](?P<addYears>\d+)/i', $checkOrder->item_description, $matches); |
||
67 | User::updateUserRole($user->id, $matches['role']); |
||
68 | User::updateUserRoleChangeDate($user->id, null, $matches['addYears']); |
||
69 | $checkOrder->update(['invoice_status' => 'Settled']); |
||
70 | Log::info('User: '.$user->username.' upgraded to '.$matches['role'].' for BTCPay webhook: '.$checkOrder->webhook_id); |
||
71 | |||
72 | return response('OK', 200); |
||
73 | } |
||
74 | |||
75 | Log::error('User not found for BTCPay webhook: '.$checkOrder->webhook_id); |
||
76 | |||
77 | return response('Not Found', 404); |
||
78 | } |
||
79 | } |
||
80 | |||
81 | return response('OK', 200); |
||
82 | } |
||
84 |