-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathfreemius-webhook.php
110 lines (94 loc) · 3.84 KB
/
freemius-webhook.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
<?php
/**
* Super-simple webhook example that does two things:
* 1. Subscribes new plugin users to MailChimp's mailing list.
* 2. Send post uninstall custom email to users based on different uninstall reasons.
*
* @copyright Copyright (c) 2016, Freemius, Inc.
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
* @since 1.0.3
*/
// Retrieve the request's body.
$input = @file_get_contents("php://input");
// Verify the authenticity of the request.
$hash = hash_hmac('sha256', $input, 'YOUR_PLUGIN_SECRET_KEY');
$signature = $_SERVER['HTTP_X_SIGNATURE'] ?? '';
if ( ! hash_equals($hash, $signature))
{
// Invalid signature, don't expose any data to attackers.
http_response_code(200);
exit;
}
// Decode the request.
$fs_event = json_decode($input);
/**
* Freemius PHP SDK can be downloaded from GitHub:
* https://github.com/Freemius/php-sdk
*/
require_once '/path/to/freemius/sdk/Freemius.php';
$fs = new Freemius_Api(
'plugin',
'YOUR_PLUGIN_ID',
'YOUR_PLUGIN_PUBLIC_KEY',
'YOUR_PLUGIN_SECRET_KEY'
);
switch ($fs_event->type)
{
case 'install.installed':
$user = $fs_event->objects->user;
$email = $user->email;
$first = $user->first;
$last = $user->last;
/**
* MailChimp SDK for API v3 can be downloaded from here:
* https://github.com/drewm/mailchimp-api
*/
require_once '/path/to/mailchimp-api/api-v3/MailChimp.php';
// Subscribe to mailchimp list.
$blog_subscribers_list = 'LIST_ID';
$mc = new MailChimp('API_KEY');
$result = $mc->post("lists/{$blog_subscribers_list}/members", array(
'email_address' => $email,
'status' => 'subscribed',
'merge_fields' => array(
'FNAME' => $first,
'LNAME' => $last
),
));
break;
case 'install.uninstalled':
$user = $fs_event->objects->user;
$email = $user->email;
$first = $user->first;
$last = $user->last;
$fs_uninstall = $fs->Api("/installs/{$fs_event->objects->install->id}/uninstall.json");
// Send email with your favorite service (we use SendGrid).
define('FS__UNINSTALL_REASON_NO_LONGER_NEEDED', 1);
define('FS__UNINSTALL_REASON_FOUND_BETTER_PLUGIN', 2);
define('FS__UNINSTALL_REASON_USED_FOR_SHORT_PERIOD', 3);
define('FS__UNINSTALL_REASON_BROKE_WEBSITE', 4);
define('FS__UNINSTALL_REASON_STOPPED_WORKING', 5);
define('FS__UNINSTALL_REASON_CANT_CONTINUE_PAYING', 6);
define('FS__UNINSTALL_REASON_OTHER', 7);
define('FS__UNINSTALL_REASON_DID_NOT_WORK_ANONYMOUS', 8);
define('FS__UNINSTALL_REASON_DONT_LIKE_INFO_SHARE', 9);
define('FS__UNINSTALL_REASON_UNCLEAR_HOW_WORKS', 10);
define('FS__UNINSTALL_REASON_MISSING_FEATURE', 11);
define('FS__UNINSTALL_REASON_DID_NOT_WORK', 12);
define('FS__UNINSTALL_REASON_EXPECTED_SOMETHING_ELSE', 13);
define('FS__UNINSTALL_REASON_EXPECTED_TO_WORK_DIFFERENTLY', 14);
switch ($fs_uninstall->reason_id)
{
case FS__UNINSTALL_REASON_NO_LONGER_NEEDED:
// Send email.
break;
case FS__UNINSTALL_REASON_STOPPED_WORKING:
// Send email.
break;
default:
// Send email.
break;
}
break;
}
http_response_code(200);