-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhandler.php
More file actions
75 lines (61 loc) · 1.98 KB
/
handler.php
File metadata and controls
75 lines (61 loc) · 1.98 KB
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
<?php
/*
* Set path to stripe lib in webhook_config.php
*/
$config = require_once('webhook_config.php');
require_once('webhookclass.php');
require_once($config['stripe_path']);
$event_array = require_once('eventarray.php');
if($config['log_type'] === 1){
$config['db'] = new PDO("mysql:host={$config['db_host']};dbname={$config['db_name']}", $config['db_user'], $config['db_pass']);
}
\Stripe\Stripe::setApiKey($config['api_key']);
/*
* Validate the event(ensure it is coming from stripe, and not a third party event.)
*/
$input = @file_get_contents("php://input");
function handleEvent($event, $event_array, $config){
$event_type = $event->type;
/*
* replace '.' in the event type with _ to suit function naming conventions.
*/
if(in_array($event_type, $event_array)) {
$event_type = str_replace('.', '_', $event_type);
$webhook = new Webhook($config, $event);
$isNewEvent = $webhook->isNewEvent($event->id);
if($isNewEvent === false){
http_response_code(200);
exit;
}else if($isNewEvent === true){
ob_start();
$webhook->{$event_type}();
}
}else{
http_response_code(200);
exit;
}
}
if($config['verification']['enabled'] === 0){
try {
$event_json = json_decode($input);
$event = \Stripe\Event::retrieve($event_json->id);
handleEvent($event, $event_array, $config);
}catch(\Exception $e){
error_log($e);
}
}else if($config['verification']['enabled'] === 1){
$signature = $_SERVER['HTTP_STRIPE_SIGNATURE'];
try{
$event = \Stripe\Webhook::constructEvent($input,$signature,$config['verification']['code']);
handleEvent($event, $event_array, $config);
}catch(\UnexpectedValueException $e) {
http_response_code(400);
exit();
} catch(\Stripe\Error\SignatureVerification $e) {
http_response_code(400);
exit();
}
}else{
http_response_code(200);
exit();
}