-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.php
80 lines (67 loc) · 3.52 KB
/
index.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
<?php
// insert your patreon webhook secret here!
$secret_webhook_id = "secret";
// insert your discord webhook url here
$discord_webhook = "URL";
// post to discord snippet from https://www.reddit.com/r/discordapp/comments/58hry5/simple_php_function_for_posting_to_webhook/
function postToDiscord($message, $discord_webhook) {
$data = array("content" => $message, "username" => "Patreon Bot");
$curl = curl_init($discord_webhook);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$res = curl_exec($curl);
return $res;
}
// compat for older php versions
if(!function_exists('hash_equals')) {
function hash_equals($str1, $str2) {
if(strlen($str1) != strlen($str2)) {
return false;
} else {
$res = $str1 ^ $str2;
$ret = 0;
for($i = strlen($res) - 1; $i >= 0; $i--) $ret |= ord($res[$i]);
return !$ret;
}
}
}
// this saves the post data you get on your endpoint
$data = @file_get_contents('php://input');
// decode json post data to arrays
$event_data = json_decode($data, true);
// also get the headers patreon sends
$X_Patreon_Event = $_SERVER['HTTP_X_PATREON_EVENT'];
$X_Patreon_Signature = $_SERVER['HTTP_X_PATREON_SIGNATURE'];
// verify signature
$signature = hash_hmac('md5', $data, $secret_webhook_id);
if (!hash_equals($X_Patreon_Signature, $signature)) {
die("Patreon Signature didn't match, got: " . $X_Patreon_Signature . " expected: " . $signature);
}
// get all the user info
$pledge_amount = $event_data['data']['attributes']['amount_cents'];
$patron_id = $event_data['data']['relationships']['patron']['data']['id'];
$campaign_id = $event_data['data']['relationships']['campaign']['data']['id'];
foreach ($event_data['included'] as $included_data) {
if ($included_data['type'] == 'user' && $included_data['id'] == $patron_id) {
$user_data = $included_data;
}
if ($included_data['type'] == 'campaign' && $included_data['id'] == $campaign_id) {
$campaign_data = $included_data;
}
}
$patron_url = $user_data['attributes']['url'];
$patron_fullname = $user_data['attributes']['full_name'];
$campaign_sum = $campaign_data['attributes']['pledge_sum'];
$patron_count = $campaign_data['attributes']['patron_count'];
// send event to discord
if ($X_Patreon_Event == "pledges:create") {
postToDiscord(":star: " . $patron_fullname . " just pledged for $" . number_format(($pledge_amount /100), 2, '.', ' ') . "! <" . $patron_url . "> - New total: $" . number_format(($campaign_sum / 100), 2, '.', ' ') . " by " . $patron_count . " patreons", $discord_webhook);
} else if ($X_Patreon_Event == "pledges:delete") {
postToDiscord(":disappointed: " . $patron_fullname . " just removed their pledge! <" . $patron_url . "> - New total: $" . number_format(($campaign_sum / 100), 2, '.', ' ') . " by " . $patron_count . " patreons", $discord_webhook);
} else if ($X_Patreon_Event == "pledges:update") {
postToDiscord(":open_mouth: " . $patron_fullname . " just updated their pledge to $" . number_format(($pledge_amount /100), 2, '.', ' ') . "! <" . $patron_url . "> - New total: $" . number_format(($campaign_sum / 100), 2, '.', ' ') . " by " . $patron_count . " patreons", $discord_webhook);
} else {
postToDiscord($X_Patreon_Event . ": something happened with Patreon ¯\_(ツ)_/¯", $discord_webhook);
}