-
Notifications
You must be signed in to change notification settings - Fork 21
/
WebhookController.php
114 lines (97 loc) · 4 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
<?php
namespace Bgultekin\CashierFastspring\Http\Controllers;
use Bgultekin\CashierFastspring\Events;
use Bgultekin\CashierFastspring\Fastspring\Fastspring;
use Exception;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Event;
use Log;
use Symfony\Component\HttpFoundation\Response;
class WebhookController extends Controller
{
/**
* Handle a Fastspring webhook call.
*
* @param \Illuminate\Http\Request $request
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function handleWebhook(Request $request)
{
$payload = json_decode($request->getContent(), true);
// keep id of successfully managed events
$successfulEvents = [];
$hmacSecret = getenv('FASTSPRING_HMAC_SECRET') === false
? config('services.fastspring.hmac_secret')
: getenv('FASTSPRING_HMAC_SECRET');
// we try to be sure about
// message integrity and authentication of message
if ($hmacSecret) {
$signature = $request->header('X-FS-Signature');
// generate signature to check
$generatedSignature = base64_encode(hash_hmac('sha256', $request->getContent(), $hmacSecret, true));
// check if equals
if ($signature != $generatedSignature) {
// if not that means
// someone trying to fool you
// or you misconfigured your settings
throw new Exception('Message security violation, MAC is wrong!');
}
}
// iterate and trigger events
foreach ($payload['events'] as $event) {
// prepare category event class names like OrderAny
$explodedType = explode('.', $event['type']);
$category = array_shift($explodedType);
$categoryEvent = '\Bgultekin\CashierFastspring\Events\\'.studly_case($category).'Any';
// prepare category event class names like activity
$activity = str_replace('.', ' ', $event['type']);
$activityEvent = '\Bgultekin\CashierFastspring\Events\\'.studly_case($activity);
// there may be some exceptions on events
// so if anything goes bad its ID won't be added on the successfullEvents
// and these events won't be marked as processed on fastspring side
// so that will make events more manageable
try {
// check if the related event classes are exist
// there may be not handled events
if (!class_exists($categoryEvent) || !class_exists($activityEvent)) {
throw new Exception('There is no event for '.$event['type']);
}
// trigger events
Event::fire(new Events\Any(
$event['id'],
$event['type'],
$event['live'],
$event['processed'],
$event['created'],
$event['data']
));
Event::fire(new $categoryEvent(
$event['id'],
$event['type'],
$event['live'],
$event['processed'],
$event['created'],
$event['data']
));
Event::fire(new $activityEvent(
$event['id'],
$event['type'],
$event['live'],
$event['processed'],
$event['created'],
$event['data']
));
// add event id to successful events
$successfulEvents[] = $event['id'];
} catch (Exception $e) {
// log the exception
// and continue to iterate
Log::error($e);
}
}
// return successful ids of events to mark them processed
return new Response(implode("\n", $successfulEvents), 202);
}
}