-
Notifications
You must be signed in to change notification settings - Fork 1
/
ambassador_chargify.php
81 lines (60 loc) · 1.93 KB
/
ambassador_chargify.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
<?php
class Ambassador_chargify {
private
$debug = false,
$api_url = '',
$username = '', // ENTER YOUR USERNAME HERE
$api_key = '', // ENTER YOUR API_KEY HERE
$campaign_uid = ''; // ENTER YOUR CAMPAIGN UID HERE
public function __construct($debug = false, $campaign_uid = null) {
if (!function_exists('curl_init')) {
echo 'Error: CURL PHP extension is required';
}
if (!function_exists('json_decode')) {
echo 'Error: JSON PHP extension is required.';
}
$this->debug = $debug;
if (!is_null($campaign_uid)) {
$this->campaign_uid = $campaign_uid;
}
if (!empty($this->username) && !empty($this->api_key)) {
$this->api_url = "https://getambassador.com/api/v2/$this->username/$this->api_key/json/";
}
}
public function renewal($payload = null, $event = null) {
if (is_null($payload)) {
if (isset($_POST['payload'])) {
$payload = $_POST['payload'];
}
}
if (is_null($event)) {
if (isset($_POST['event'])) {
$event = $_POST['event'];
}
}
if ($event === 'renewal_success') {
$data = array(
'campaign_uid' => $this->campaign_uid,
'revenue' => $payload['subscription']['balance_in_cents']/100,
'email' => $payload['subscription']['customer']['email'],
'first_name' => $payload['subscription']['customer']['first_name'],
'last_name' => $payload['subscription']['customer']['last_name'],
'auto_create' => 0
);
$data = http_build_query($data);
$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL, $this->api_url.'event/record');
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_POST, 1);
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($curl_handle);
curl_close($curl_handle);
if ($this->debug) {
$result = json_decode($result, true);
var_dump($result);
}
}
}
}
?>