-
Notifications
You must be signed in to change notification settings - Fork 0
/
WebhookController.php
144 lines (116 loc) · 3.63 KB
/
WebhookController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
namespace App\Http\Controllers;
use App\Models\Donation;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use OpenPix\PhpSdk\Client;
class WebhookController extends Controller
{
const SIGNATURE_HEADER = "x-webhook-signature";
const TEST_WEBHOOK_EVENT = "teste_webhook";
const OPENPIX_CHARGE_COMPLETED_EVENT = "OPENPIX:CHARGE_COMPLETED";
const OPENPIX_TRANSACTION_RECEIVED_EVENT = "OPENPIX:TRANSACTION_RECEIVED";
/**
* Create a new `WebhookController` instance.
*/
public function __construct(protected Client $openpix)
{
}
/**
* Main endpoint to receive webhooks.
*/
public function receive(Request $request)
{
if ($response = $this->allowRequestOnlyFromOpenPix($request)) return $response;
return $this->handleWebhook($request);
}
/**
* Allow requests only from OpenPix.
*/
private function allowRequestOnlyFromOpenPix(Request $request)
{
$rawPayload = $request->getContent();
$signature = $request->header(self::SIGNATURE_HEADER);
$isWebhookValid = ! empty($rawPayload)
&& ! empty($signature)
&& $this->openpix->webhooks()->isWebhookValid($rawPayload, $signature);
if ($isWebhookValid) return null;
return response()->json([
"errors" => [
[
"message" => "Invalid webhook signature."
],
],
], 400);
}
/**
* Handle webhook when a charge was paid.
*/
public function handleChargePaidWebhook(Request $request)
{
$correlationID = $request->input("charge.correlationID");
$donation = Donation::where("correlationID", $correlationID)->first();
if (empty($donation)) {
return response()->json([
"errors" => [
[
"message" => "Donation not found.",
],
],
], 404);
}
$donation->status = "PAID";
$donation->save();
return response()->json(["message" => "Success."]);
}
/**
* Handles the test webhook.
*/
private function handleTestWebhook()
{
return response()->json(["message" => "Success."]);
}
/**
* Checks if the webhook is from when a charge was paid.
*/
private function isChargePaidPayload(Request $request)
{
$event = $request->input("event");
$allowedEvents = [
self::OPENPIX_CHARGE_COMPLETED_EVENT,
self::OPENPIX_TRANSACTION_RECEIVED_EVENT,
];
$isChargePaidEvent = ! empty($event) && in_array($event, $allowedEvents);
return $isChargePaidEvent
&& ! empty($request->input("charge.correlationID"));
}
/**
* Checks if the webhook is of test type.
*
* The OpenPix platform can send a test webhook to verify the webhook URL.
*/
private function isTestPayload(Request $request)
{
$event = $request->input("evento");
return ! empty($event) && $event === self::TEST_WEBHOOK_EVENT;
}
/**
* Dispatch a webhook handler by the webhook type.
*/
private function handleWebhook(Request $request)
{
if ($this->isChargePaidPayload($request)) {
return $this->handleChargePaidWebhook($request);
}
if ($this->isTestPayload($request)) {
return $this->handleTestWebhook();
}
return response()->json([
"errors" => [
[
"message" => "Invalid webhook type.",
],
]
], 400);
}
}