-
Notifications
You must be signed in to change notification settings - Fork 8
/
foxyshop-datafeed-endpoint.php
387 lines (273 loc) · 15.7 KB
/
foxyshop-datafeed-endpoint.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
<?php /*
------------ ATTENTION ------------
If you need to edit this template, do not edit the version in the plugin directory. Place a copy in your template folder and edit it there.
This will allow you to upgrade FoxyShop without breaking your customizations. More details here: http://www.foxy-shop.com/documentation/theme-customization/
-----------------------------------
*/
//Set Globals and Get Settings
global $wpdb, $foxyshop_settings;
require(FOXYSHOP_PATH.'/datafeedfunctions.php');
//EXTERNAL DATAFEEDS
//-----------------------------------------------------
//If you need to use more than one datafeed with FoxyCart, you can enter as many as you like in the $external_datafeeds array
//If you are using more than one integration and one of them fails, the entire process will fail and that failure will be sent to FoxyCart.
//If you have more than one additional datafeed and one is more unreliable than another, put the most unreliable one first so that any failures will result in a full retry from FoxyCart.
//This function will send the WordPress admin (this is filterable) an email if any datafeeds fail.
//There are reports that this won't work on GoDaddy's hosting, which begs the question: why are you hosting on GoDaddy?
$external_datafeeds = array();
foxyshop_run_external_datafeeds($external_datafeeds);
//-----------------------------------------------------
// TRANSACTION DATAFEED
//-----------------------------------------------------
if (isset($_POST["FoxyData"])) {
if ($_POST['FoxyData'] == "") die('No Content Received From Datafeed');
//DECRYPT (required)
//-----------------------------------------------------
$FoxyData_decrypted = foxyshop_decrypt($_POST["FoxyData"]);
$xml = simplexml_load_string($FoxyData_decrypted, NULL, LIBXML_NOCDATA);
//TROUBLESHOOTING
//-----------------------------------------------------
//For testing, write datafeed to file in theme folder
//$file = STYLESHEETPATH.'/datafeed.xml';
//$fh = fopen($file, 'a') or die("Couldn't open $file for writing!");
//fwrite($fh, $FoxyData_decrypted);
//fclose($fh);
//Uncomment These If You Need Help Troubleshooting
//error_reporting(E_ALL);
//ini_set('display_errors','On');
//SKIP IF HOSTED GATEWAY AND NOT YET APPROVED
//-----------------------------------------------------
$status = isset($xml->transactions->transaction->status) ? (string)$xml->transactions->transaction->status : '';
if ($status == "pending") {
die("foxy");
}
//BUILT-IN FEATURES
//-----------------------------------------------------
//Update Inventory
//Comment Out if Using QB or other External Inventory Management
if ($foxyshop_settings['manage_inventory_levels']) foxyshop_datafeed_inventory_update($xml);
//Set Subscription Features If Using SSO
if ($foxyshop_settings['enable_subscriptions'] && $foxyshop_settings['enable_sso']) foxyshop_datafeed_sso_update($xml);
//Add/Update WordPress User
if ($foxyshop_settings['checkout_customer_create']) foxyshop_datafeed_user_update($xml);
//MANUAL PROCESSES GO HERE
//-----------------------------------------------------
//For Each Transaction
foreach($xml->transactions->transaction as $transaction) {
//This variable will tell us whether this is a multi-ship store or not
$is_multiship = 0;
//Get FoxyCart Transaction Information
//Simply setting lots of helpful data to PHP variables so you can access it easily
//If you need to access more variables, you can see some sample XML here: http://wiki.foxycart.com/v/0.7.2/transaction_xml_datafeed
$transaction_id = (string)$transaction->id;
$transaction_date = (string)$transaction->transaction_date;
$customer_ip = (string)$transaction->customer_ip;
$customer_id = (string)$transaction->customer_id;
$customer_first_name = (string)$transaction->customer_first_name;
$customer_last_name = (string)$transaction->customer_last_name;
$customer_company = (string)$transaction->customer_company;
$customer_email = (string)$transaction->customer_email;
$customer_password = (string)$transaction->customer_password;
$customer_address1 = (string)$transaction->customer_address1;
$customer_address2 = (string)$transaction->customer_address2;
$customer_city = (string)$transaction->customer_city;
$customer_state = (string)$transaction->customer_state;
$customer_postal_code = (string)$transaction->customer_postal_code;
$customer_country = (string)$transaction->customer_country;
$customer_phone = (string)$transaction->customer_phone;
//This is for a multi-ship store. The shipping addresses will go in a $shipto array with the address name as the key
$shipto = array();
foreach($transaction->shipto_addresses->shipto_address as $shipto_address) {
$is_multiship = 1;
$shipto_name = (string)$shipto_address->address_name;
$shipto[$shipto_name] = array(
'first_name' => (string)$shipto_address->shipto_first_name,
'last_name' => (string)$shipto_address->shipto_last_name,
'company' => (string)$shipto_address->shipto_company,
'address1' => (string)$shipto_address->shipto_address1,
'address2' => (string)$shipto_address->shipto_address2,
'city' => (string)$shipto_address->shipto_city,
'state' => (string)$shipto_address->shipto_state,
'postal_code' => (string)$shipto_address->shipto_postal_code,
'country' => (string)$shipto_address->shipto_country,
'shipping_service_description' => (string)$shipto_address->shipto_shipping_service_description,
'subtotal' => (string)$shipto_address->shipto_subtotal,
'tax_total' => (string)$shipto_address->shipto_tax_total,
'shipping_total' => (string)$shipto_address->shipto_shipping_total,
'total' => (string)$shipto_address->shipto_,
'custom_fields' => array()
);
//Putting the Custom Fields in an array if they are there
if (!empty($shipto_address->custom_fields)) {
foreach($shipto_address->custom_fields->custom_field as $custom_field) {
$shipto[$shipto_name]['custom_fields'][(string)$custom_field->custom_field_name] = (string)$custom_field->custom_field_value;
}
}
}
//This is setup for a single ship store
if (!$is_multiship) {
$shipping_first_name = (string)$transaction->shipping_first_name ? (string)$transaction->shipping_first_name : $customer_first_name;
$shipping_last_name = (string)$transaction->shipping_last_name ? (string)$transaction->shipping_last_name : $customer_last_name;
$shipping_company = (string)$transaction->shipping_company ? (string)$transaction->shipping_company : $customer_company;
$shipping_address1 = (string)$transaction->shipping_address1 ? (string)$transaction->shipping_address1 : $customer_address1;
$shipping_address2 = (string)$transaction->shipping_address1 ? (string)$transaction->shipping_address2 : $customer_address2; //shipping_address1 is intended here
$shipping_city = (string)$transaction->shipping_city ? (string)$transaction->shipping_city : $customer_city;
$shipping_state = (string)$transaction->shipping_state ? (string)$transaction->shipping_state : $customer_state;
$shipping_postal_code = (string)$transaction->shipping_postal_code ? (string)$transaction->shipping_postal_code : $customer_postal_code;
$shipping_country = (string)$transaction->shipping_country ? (string)$transaction->shipping_country : $customer_country;
$shipping_phone = (string)$transaction->shipping_phone ? (string)$transaction->shipping_phone : $customer_phone;
$shipto_shipping_service_description = (string)$transaction->shipto_shipping_service_description;
}
//Putting the Custom Fields in an array if they are there. These are on the top level and could be there for both single ship and multiship stores
$custom_fields = array();
if (!empty($transaction->custom_fields)) {
foreach($transaction->custom_fields->custom_field as $custom_field) {
$custom_fields[(string)$custom_field->custom_field_name] = (string)$custom_field->custom_field_value;
}
}
//For Each Transaction Detail
foreach($transaction->transaction_details->transaction_detail as $transaction_detail) {
$product_name = (string)$transaction_detail->product_name;
$product_code = (string)$transaction_detail->product_code;
$product_quantity = (int)$transaction_detail->product_quantity;
$product_price = (double)$transaction_detail->product_price;
$product_shipto = (string)$transaction_detail->shipto;
$category_code = (string)$transaction_detail->category_code;
$product_delivery_type = (string)$transaction_detail->product_delivery_type;
$sub_token_url = (string)$transaction_detail->sub_token_url;
$subscription_frequency = (string)$transaction_detail->subscription_frequency;
$subscription_startdate = (string)$transaction_detail->subscription_startdate;
$subscription_nextdate = (string)$transaction_detail->subscription_nextdate;
$subscription_enddate = (string)$transaction_detail->subscription_enddate;
//These are the options for the product
$transaction_detail_options = array();
foreach($transaction_detail->transaction_detail_options->transaction_detail_option as $transaction_detail_option) {
$product_option_name = (string)$transaction_detail_option->product_option_name;
$product_option_value = (string)$transaction_detail_option->product_option_value;
$price_mod = (double)$transaction_detail_option->price_mod;
$weight_mod = (double)$transaction_detail_option->weight_mod;
$transaction_detail_options[$product_name] = $product_option_value;
}
//If you have custom code to run for each product, put it here:
//Runs a WordPress action for each transaction detail if you prefer to customize that way
do_action('foxyshop_datafeed_transaction_detail', $transaction, $transaction_detail);
}
//If you have custom code to run for each order, put it here:
//Runs a WordPress action at the end if you prefer to customize that way
do_action('foxyshop_datafeed_order', $transaction);
}
//All Done!
die("foxy");
//-----------------------------------------------------
// SUBSCRIPTION DATAFEED
//-----------------------------------------------------
} elseif (isset($_POST["FoxySubscriptionData"])) {
if ($_POST['FoxySubscriptionData'] == "") die('No Content Received From Subscription Datafeed');
//If you don't want to use any of the subscription datafeed reminder features, uncomment the next line
//die("Not In Use");
//Setup Defaults
$failed_days_before_cancel = 7;
$billing_reminder_frequency_in_days = 3;
$update_payment_method_reminder_days_of_month = array(1, 15, 28);
//Decrypt
$FoxyData_decrypted = foxyshop_decrypt($_POST["FoxySubscriptionData"]);
$xml = simplexml_load_string($FoxyData_decrypted, NULL, LIBXML_NOCDATA);
//For Each Subscription
foreach($xml->subscriptions->subscription AS $subscription) {
//Get Variables
$customer_id = (string)$subscription->customer_id;
$customer_first_name = (string)$subscription->customer_first_name;
$customer_last_name = (string)$subscription->customer_last_name;
$customer_email = (string)$subscription->customer_email;
$transaction_date = (string)$subscription->transaction_date;
$sub_token_url = (string)$subscription->sub_token_url;
$past_due_amount = (double)$subscription->past_due_amount;
$end_date = (string)$subscription->end_date;
//Get Product Code
foreach($subscription->transaction_details->transaction_detail AS $transaction_detail) {
$product_code = (string)$transaction_detail->product_code;
}
$canceled = 0;
$sendReminder = 0;
//This Entry Was Canceled Today
if (date("Y-m-d",strtotime("now")) == date("Y-m-d", strtotime($end_date))) {
$canceled = 1;
}
if (!$canceled && $past_due_amount > 0) {
$failedDays = floor((strtotime("now") - strtotime($transaction_date)) / (60 * 60 * 24));
if ($failedDays > $failed_days_before_cancel) {
$canceled = 1;
} else {
if (($failedDays % $billing_reminder_frequency_in_days) == 0) {
$sendReminder = 1;
}
}
}
//Set Subscription Inactive
if ($canceled) {
//Get WordPress User ID
$user_id = $wpdb->get_var("SELECT user_id FROM $wpdb->usermeta WHERE meta_key = 'foxycart_customer_id' AND meta_value = '$customer_id'");
if ($user_id) {
//Get User's Subscription Array
$foxyshop_subscription = get_user_meta($user_id, 'foxyshop_subscription', true);
if (!is_array($foxyshop_subscription)) $foxyshop_subscription = array();
//Set To NON-ACTIVE
$foxyshop_subscription[$product_code] = array(
"is_active" => 0,
"sub_token_url" => $sub_token_url
);
//Write Serialized Array Back to DB
update_user_meta($user_id, 'foxyshop_subscription', $foxyshop_subscription);
}
}
//Send reminder email
if ($sendReminder && $foxyshop_settings['expiring_cards_reminder']) {
$subject_line = "Please Update Payment Information";
$to_email = $customer_email;
$message = "Dear $customer_first_name,\n\n";
$message .= "This is a reminder that your recent subscription payment failed. Please click the link below to update the credit card you have on file with us. Thank you!\n\n";
$message .= $sub_token_url . "&empty=true&cart=checkout\n\n";
$message .= "";
$headers = 'From: ' . get_bloginfo('name') . ' <' . get_bloginfo('admin_email') . '>' . "\r\n";
//$headers .= 'Bcc: ' . get_bloginfo('admin_email') . "\r\n";
wp_mail($to_email, $subject_line, $message, $headers);
}
}
// send emails to customers with soon to expire credit cards. Ignore already expired cards, since they should have already been
// sent an email when their payment failed.
if (in_array(date("j"),$update_payment_method_reminder_days_of_month) && $foxyshop_settings['expiring_cards_reminder']) {
foreach($xml->payment_methods_soon_to_expire->customer AS $customer) {
$customer_id = (string)$customer->customer_id;
$customer_first_name = (string)$customer->customer_first_name;
$customer_last_name = (string)$customer->customer_last_name;
$customer_email = (string)$customer->customer_email;
$cc_exp_month = (int)$customer->cc_exp_month;
$cc_exp_year = (int)$customer->cc_exp_year;
if (strtolower($customer_first_name) === $customer_first_name) $customer_first_name = ucwords($customer_first_name);
if (strtolower($customer_last_name) === $customer_last_name) $customer_last_name = ucwords($customer_last_name);
if (mktime(0,0,0,$cc_exp_month+1, 1, $cc_exp_year) > strtotime("now")) {
$subject_line = "Reminder to Update Your Credit Card";
$to_email = $customer_email;
$message = "Dear $customer_first_name,\n\n";
$message .= "This is a reminder that the credit card you have on file with us is about to expire. Please login to your account by clicking the link below to update your card. Thank you!\n\n";
$message .= "https://" . $foxyshop_settings['domain'] . "/cart?empty=true&cart=updateinfo&customer_email=" . urlencode($customer_email) . "\n\n";
$message .= "";
$headers = 'From: ' . get_bloginfo('name') . ' <' . get_bloginfo('admin_email') . '>' . "\r\n";
//$headers .= 'Bcc: ' . get_bloginfo('admin_email') . "\r\n";
wp_mail($to_email, $subject_line, $message, $headers);
}
}
}
//All Done!
die("foxysub");
//-----------------------------------------------------
// CONSOLIBYTE QUICKBOOKS UPDATE
//-----------------------------------------------------
} elseif (isset($_POST["FoxyInventory"])) {
foxyshop_consolibyte_inventory_process();
//-----------------------------------------------------
// NO POST CONTENT SENT
//-----------------------------------------------------
} else {
die('No Content Received From Datafeed');
}