· 4 years ago · Jun 15, 2021, 10:08 AM
1<?php
2define("WP_ROOT", __DIR__);
3define("DS", DIRECTORY_SEPARATOR);
4require_once WP_ROOT . DS . "wp-load.php";
5
6$data = $_POST;
7$mac_provided = $data['mac'];
8$email = $data['buyer'];
9$fname = $data['buyer_name'];
10$lname = '';
11 // Get the MAC from the POST data
12unset($data['mac']); // Remove the MAC key from the data.
13
14$ver = explode('.', phpversion());
15$major = (int) $ver[0];
16$minor = (int) $ver[1];
17
18echo 'Kallada';
19
20if($major >= 5 and $minor >= 4){
21 ksort($data, SORT_STRING | SORT_FLAG_CASE);
22}
23else{
24 uksort($data, 'strcasecmp');
25}
26
27// You can get the 'salt' from Instamojo's developers page(make sure to log in first): https://www.instamojo.com/developers
28// Pass the 'salt' without the <>.
29$mac_calculated = hash_hmac("sha1", implode("|", $data), "df2dd48b97e7414c84d5a2600c83b48b");
30
31$myfile = fopen("newfile.txt", "a") or die("Unable to open file!");
32$txt = $data['buyer'];
33
34fwrite($myfile, $txt);
35fclose($myfile);
36
37if($mac_provided == $mac_calculated){
38 echo "MAC is fine";
39 // Do something here
40 if($data['status'] == "Credit"){
41 // Payment was successful, mark it as completed in your database
42 // syncMailchimp($data);
43 $permitted_chars = '0123456789abcdefghijklmnopqrstuvwxyz';
44 $data['random_pass'] = substr(str_shuffle($permitted_chars), 0, 8);
45 syncSendyList($data);
46 syncConvertKit($data);
47 syncLeadSquare($data);
48 regsiterToDemio($data);
49 enrollTeachable($data);
50 $myfile = fopen("newfile.txt", "a") or die("Unable to open file!");
51$txt = $data['buyer'];
52
53fwrite($myfile, $txt);
54fclose($myfile);
55
56 $to_email = 'santhej@kallada.com';
57 $subject = 'Kallada Academy Buyer Confirmation';
58 $message = 'This mail is sent using the PHP mail function';
59 $headers = 'From: santhej@kalladaacademy.com';
60 mail($to_email,$subject,$message,$headers);
61
62 }
63 else{
64 // Payment was unsuccessful, mark it as failed in your database
65 $myfile = fopen("newfile.txt", "a") or die("Unable to open file!");
66 $txt = 'Payment Unsuccessful';
67
68 fwrite($myfile, $txt);
69 fclose($myfile);
70 }
71}
72else{
73 echo "Invalid MAC passed";
74 $myfile = fopen("newfile.txt", "a") or die("Unable to open file!");
75 $txt = 'Invalid MAC Passed';
76
77 fwrite($myfile, $txt);
78 fclose($myfile);
79}
80
81//Adding Customer Details To MailChimp
82
83function syncMailchimp($data) {
84 $apiKey = 'b834c59870aaad929e7ad8e3fbc0c44d-us20';
85 $listId = '76e1b6d2ed';
86
87 $memberId = md5(strtolower($data['email']));
88 $dataCenter = substr($apiKey,strpos($apiKey,'-')+1);
89 $url = 'https://' . $dataCenter . '.api.mailchimp.com/3.0/lists/' . $listId . '/members/' . $memberId;
90
91 $json = json_encode([
92 'email_address' => $data['buyer'],
93 'status' => "subscribed", // "subscribed","unsubscribed","cleaned","pending"
94 'merge_fields' => [
95 'FNAME' => $data['buyer_name'],
96 'LNAME' => ''
97 ]
98 ]);
99
100 $ch = curl_init($url);
101
102 curl_setopt($ch, CURLOPT_USERPWD, 'user:' . $apiKey);
103 curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
104 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
105 curl_setopt($ch, CURLOPT_TIMEOUT, 10);
106 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
107 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
108 curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
109
110 $result = curl_exec($ch);
111 $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
112 curl_close($ch);
113
114 echo $httpCode;
115}
116
117
118
119 function syncSendyList($data)
120 {
121 $postdata = http_build_query(
122 array(
123 'name' => $data['buyer_name'],
124 'email' => $data['buyer'],
125 'list' => 'LO8J0ZBbk0Mxrkid4TNsoQ',
126 'api_key' => 'An6ziAVFTt0sxhOkkpFI',
127 'boolean' => 'true'
128 )
129 );
130 $opts = array('http' => array('method' => 'POST', 'header' => 'Content-type: application/x-www-form-urlencoded', 'content' => $postdata));
131 $context = stream_context_create($opts);
132 $result = file_get_contents('http://sendy.kalladaacademy.com/subscribe', false, $context);
133
134 return $result;
135 }
136
137function syncConvertKit($data)
138 {
139 $tagId = 2257860;
140 $postdata = json_encode(
141 array(
142 'first_name' => $data['buyer_name'],
143 'email' => $data['buyer'],
144 'api_key' => 'DUAVRNp3_OXPMeaStHmanw',
145 'fields' => array( 'user_password' => $data['random_pass'] )
146 )
147 );
148
149 $url = 'https://api.convertkit.com/v3/tags/'. $tagId .'/subscribe';
150
151 $ch = curl_init($url);
152
153 curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
154 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
155 curl_setopt($ch, CURLOPT_TIMEOUT, 10);
156 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
157 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
158 curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
159
160 $result = curl_exec($ch);
161
162 curl_close($ch);
163
164 return $result;
165 }
166function syncLeadSquare($data){
167 $url = 'https://api-in21.leadsquared.com/v2/LeadManagement.svc/Lead.Create';
168$accessKey = 'u$r0c974c4ed869702b573e59777382cc3d';
169$secretKey = '497517092a6fa8e72e55ab7eaac8cab9ffe52c3f';
170
171$lsData = array(
172 array(
173 'Attribute' => 'EmailAddress',
174 'Value' => $data['buyer']
175 ),
176 array(
177 'Attribute' => 'FirstName',
178 'Value' => $data['buyer_name']
179 ),
180 array(
181 'Attribute' => 'Phone',
182 'Value' => $data['buyer_phone']
183 ),
184
185 array(
186 'Attribute' => 'Source',
187 'Value' => 'MDM'
188 )
189
190
191);
192
193 $lsData = json_encode($lsData);
194
195
196 $ch = curl_init($url);
197
198$headers = ['Content-Type: application/json',
199 'x-LSQ-AccessKey: '.$accessKey,
200 'x-LSQ-SecretKey: '.$secretKey];
201 curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
202 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
203 curl_setopt($ch, CURLOPT_TIMEOUT, 10);
204 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
205 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
206 curl_setopt($ch, CURLOPT_POSTFIELDS, $lsData);
207
208 $result = curl_exec($ch);
209 $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
210 curl_close($ch);
211
212$textToAdd = 'Leadsquare request data:'.$lsData.PHP_EOL.'Leadsquare response: '.$httpCode.'-->'.$result;
213
214$myfile = fopen("lead_square.txt", "a") or die("Unable to open file!");
215
216
217 fwrite($myfile, $textToAdd);
218 fclose($myfile);
219}
220
221function regsiterToDemio($data){
222
223 $data['is_sent'] = 0;
224 $data['registered_at'] = date('Y-m-d H:i:s');
225 $data['sent_to_demio_at'] = '';
226
227 $myfile = fopen("demio_data.txt", "a") or die("Unable to open file!");
228
229 $fileData = file_get_contents("demio_data.txt");
230 $existingData = json_decode(trim($fileData), true);
231
232 $existingData[] = $data;
233
234 $textToAdd = json_encode($existingData);
235 fwrite($myfile, $textToAdd);
236 fclose($myfile);
237}
238
239function enrollTeachable($data){
240 $postdata = json_encode(
241 array(
242 'product_id' => 1370402,
243 'email' => $data['buyer'],
244 'name' => $data['buyer_name'],
245 'password' => $data['random_pass']
246 )
247 );
248 $username = 'santhej@kalladaacademy.com';
249 $password = 'UYRxes5@eU6q';
250
251 $headers = array(
252 'Accept: application/json',
253 'Content-Type: application/json'
254 );
255
256 $url = 'https://learn.kalladaacademy.com/api/v1/sales';
257
258 $ch = curl_init($url);
259
260 curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
261 curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
262 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
263 curl_setopt($ch, CURLOPT_TIMEOUT, 10);
264 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
265 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
266 curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
267
268 $result = curl_exec($ch);
269
270 curl_close($ch);
271
272 return $result;
273
274}
275?>