add_admin_ajax_actions();
$this->add_user_ajax_actions();
$this->add_shortcodes();
}
public static function install() {
$license = get_option('aretex_license_key');
$valid_license = false;
$license_key = get_option('aretex_license_key');
if (! empty($license_key)){
if (! class_exists('AreteX_WPI'))
{
$aretex_core_path = get_option('aretex_core_path');
if (empty($aretex_core_path))
{
// add_action( 'admin_notices', array('AreteX_paid_content','core_failure_notice') );
return;
}
if (file_exists($aretex_core_path .'AreteX_WPI.class.php'))
require_once($aretex_core_path .'AreteX_WPI.class.php');
else
{
// add_action( 'admin_notices', array('AreteX_paid_content','core_failure_notice') );
return;
}
}
if (class_exists('AreteX_WPI'))
$valid_license = AreteX_WPI::validate_license($license_key);
}
if (! $valid_license) {
// add_action( 'admin_notices', array('AreteX_paid_content','core_failure_notice') );
return;
}
else
{
// See if it already exists ...
$installed = self::checkInstallation();
if (! $installed)
return;
if ($installed['feature_installed'] == 'N' && $installed['load_feature'] == 'Y') {
global $wpdb;
$table_name = $wpdb->prefix .'aretex_features';
// self::build_db_tables();
$installed['feature_installed'] = 'Y';
$wpdb->replace( $table_name, $installed, null );
}
}
}
protected static function checkInstallation() {
global $wpdb;
$table_name = $wpdb->prefix .'aretex_features';
$feature_name = 'AreteX Shopping Cart';
$rows = $wpdb->get_results( "SELECT * FROM $table_name WHERE feature_name='$feature_name' ", ARRAY_A );
if (! empty($rows[0]['feature_name']))
{
return $rows[0];
}
return null;
}
protected function add_admin_ajax_actions() {
add_action('wp_ajax_atx_add_to_cart',array(&$this,'atx_add_to_cart'));
add_action('wp_ajax_atx_empty_cart',array(&$this,'atx_empty_cart'));
add_action('wp_ajax_atx_delete_from_cart',array(&$this,'atx_delete_from_cart'));
add_action('wp_ajax_atx_update_dlg_content',array(&$this,'atx_update_dlg_content'));
add_action('wp_ajax_atx_update_cart_items',array(&$this,'atx_update_cart_items'));
add_action('wp_ajax_atx_complete_checkout',array(&$this,'atx_complete_checkout'));
add_action('wp_ajax_atx_cart_summary',array(&$this,'atx_cart_summary'));
}
protected function add_user_ajax_actions() {
add_action('wp_ajax_nopriv_atx_add_to_cart',array(&$this,'atx_add_to_cart'));
add_action('wp_ajax_nopriv_atx_empty_cart',array(&$this,'atx_empty_cart'));
add_action('wp_ajax_nopriv_atx_delete_from_cart',array(&$this,'atx_delete_from_cart'));
add_action('wp_ajax_nopriv_atx_update_dlg_content',array(&$this,'atx_update_dlg_content'));
add_action('wp_ajax_nopriv_atx_update_cart_items',array(&$this,'atx_update_cart_items'));
add_action('wp_ajax_nopriv_atx_complete_checkout',array(&$this,'atx_complete_checkout'));
add_action('wp_ajax_nopriv_atx_cart_summary',array(&$this,'atx_cart_summary'));
}
public function atx_update_dlg_content() {
if (!session_id())
session_start();
$aretex_core_path = get_option('aretex_core_path');
if (empty($aretex_core_path)) {
echo 'Error';
die();
}
if (file_exists($aretex_core_path .'AreteXClientEngine/Checkout.class.php')) {
require_once($aretex_core_path .'AreteXClientEngine/Checkout.class.php');
require_once($aretex_core_path.'AreteXClientEngine/Crypton/Crypton.php');
require_once($aretex_core_path .'AreteX_WPI.class.php');
require_once(plugin_dir_path( __FILE__ ).'AreteXWP_Cart.php');
}
else {
echo 'Error';
die();
}
self::fixObject($_SESSION['aretex_shopping_cart']);
$line_key = $_GET['line_key'];
$options = $_SESSION['aretex_shopping_cart']->all_options[$line_key];
$item = $_SESSION['aretex_shopping_cart']->items[$line_key];
$form = '';
$form_id = self::build_updc_form($line_key,$item->code,$item->qty,$options,$item->options,$form);
echo $form;
die();
}
public function atx_delete_from_cart() {
if (!session_id())
session_start();
$aretex_core_path = get_option('aretex_core_path');
if (empty($aretex_core_path)) {
echo 'Error';
die();
}
if (file_exists($aretex_core_path .'AreteXClientEngine/Checkout.class.php')) {
require_once($aretex_core_path .'AreteXClientEngine/Checkout.class.php');
require_once($aretex_core_path.'AreteXClientEngine/Crypton/Crypton.php');
require_once($aretex_core_path .'AreteX_WPI.class.php');
require_once(plugin_dir_path( __FILE__ ).'AreteXWP_Cart.php');
}
else {
echo 'Error';
die();
}
self::fixObject($_SESSION['aretex_shopping_cart']);
$line_key = (int) $_POST['line_key'];
$item = $_SESSION['aretex_shopping_cart']->items[$line_key];
$skip_exclusivity = false;
foreach($_SESSION['aretex_shopping_cart']->items as $key=>$itm) {
if ($key != $line_key && $itm->code == $item->code) {
$skip_exclusivity = true;
}
}
if (! $skip_exclusivity) {
$del_exclu = array();
foreach($_SESSION['aretex_shopping_cart']->exclusive_item as $exclusivity=>$code) {
if ($code == $item->code)
$del_exclu[] = $exclusivity;
}
foreach($del_exclu as $exclusivity) {
unset($_SESSION['aretex_shopping_cart']->exclusive_item[$exclusivity]);
}
}
unset($_SESSION['aretex_shopping_cart']->items[$line_key]);
$_SESSION['aretex_shopping_cart']->items = array_values($_SESSION['aretex_shopping_cart']->items);
unset($_SESSION['aretex_shopping_cart']->item_options[$line_key]);
$_SESSION['aretex_shopping_cart']->item_options = array_values($_SESSION['aretex_shopping_cart']->item_options );
unset($_SESSION['aretex_shopping_cart']->all_options[$line_key]);
$_SESSION['aretex_shopping_cart']->all_options = array_values($_SESSION['aretex_shopping_cart']->all_options);
unset($_SESSION['aretex_shopping_cart']->display_options[$line_key]);
$_SESSION['aretex_shopping_cart']->display_options = array_values($_SESSION['aretex_shopping_cart']->display_options);
self::populate_session_cart(false);
if ( is_user_logged_in() ) {
global $user_login;
global $user_ID;
get_currentuserinfo(); // Populate the globals ...
update_user_meta($user_ID, 'aretex_cart', $_SESSION['aretex_shopping_cart']);
}
$str = self::build_cart_table();
echo $str;
die();
}
public function atx_empty_cart() {
if (!session_id())
session_start();
$aretex_core_path = get_option('aretex_core_path');
if (empty($aretex_core_path)) {
// error_log("Failing 1");
echo 'Error';
die();
}
if (file_exists($aretex_core_path .'AreteXClientEngine/Checkout.class.php')) {
require_once($aretex_core_path .'AreteXClientEngine/Checkout.class.php');
require_once($aretex_core_path.'AreteXClientEngine/Crypton/Crypton.php');
require_once($aretex_core_path .'AreteX_WPI.class.php');
require_once(plugin_dir_path( __FILE__ ).'AreteXWP_Cart.php');
}
else {
// error_log("Failing 2");
echo 'Error';
die();
}
$_SESSION['aretex_shopping_cart'] = self::setup_cart();
if ( is_user_logged_in() ) {
global $user_login;
global $user_ID;
get_currentuserinfo(); // Populate the globals ...
update_user_meta($user_ID, 'aretex_cart', $_SESSION['aretex_shopping_cart']);
}
$str = self::build_cart_table();
echo $str;
die();
}
protected static function build_cart_table() {
$str = self::start_cart_table();
foreach($_SESSION['aretex_shopping_cart']->items as $key=>$item) {
$str .= self::cart_item($key,$item,$_SESSION['aretex_shopping_cart']->item_options[$key]);
}
$str .= self::end_cart_table();
return $str;
}
protected function setup_cart() {
$tracking = AreteX_WPI::getCurrentTrackingCode();
$cart = new AreteXWP_Cart();
if ($tracking->valid) {
$cart->tracking_code = $tracking->standard;
$cart->tracking_code_signature = $tracking->validation;
}
else {
$cart->tracking_code = null;
$cart->tracking_code_signature = null;
}
$cart->txn_type = TxnType::sale;
return $cart;
}
public function atx_complete_checkout() {
$cart = self::checkout_data();
if ( is_user_logged_in() ) {
global $user_login;
global $user_ID;
get_currentuserinfo(); // Populate the globals ...
$customer_id = get_user_meta($user_ID, 'atx_customer_id', true);
if (! $customer_id)
{
update_user_meta( $user_ID, 'atx_customer_id', 'Pending');
// Ask AreteX for Customer Data Next Time User Logs in
}
}
$url = get_option('aretex_pcs_in_endpoint');
$url .= '/begin_cc_co.php';
$return['data'] = $cart; // Was already JSON encoded ...
$return['url'] = $url;
$return = json_encode($return);
// error_log("Button Code: ".var_export($button_code,true));
// we are ready to check out so we're done with the cart
$_SESSION['aretex_shopping_cart'] = self::setup_cart();
if ( is_user_logged_in() ) {
global $user_login;
global $user_ID;
get_currentuserinfo(); // Populate the globals ...
update_user_meta($user_ID, 'aretex_cart', $_SESSION['aretex_shopping_cart']);
}
echo $return;
die(); // this is required to return a proper result
}
public static function checkout_data() {
$license_key = get_option('aretex_license_key');
$app_key = get_option('aretex_api_key');
$crypt_keys = AreteX_WPI::GetKeys();
global $user_login;
global $user_ID;
get_currentuserinfo(); // Populate the globals ...
$customer_id = AreteX_WPI::customerSignedUp();
if (! ctype_digit($customer_id)) {
$customer_id = 0;
}
$idv = new CartIdenityValidation($license_key,$app_key,'master',$customer_id,false,$user_login);
// Yes, Idenity is misspelled ... I'll get to it.
$signed_idv = new SignedCartIdenityValidation();
$signed_idv->cartIdentity = base64_encode(json_encode($idv));
$signed_idv->signature = AreteX_API::Sign($signed_idv->cartIdentity, $crypt_keys['privatekey'] );
$aretex_core_path = get_option('aretex_core_path');
if (file_exists($aretex_core_path .'AreteXClientEngine/Checkout.class.php')) {
require_once($aretex_core_path .'AreteXClientEngine/Checkout.class.php');
require_once($aretex_core_path.'AreteXClientEngine/Crypton/Crypton.php');
require_once($aretex_core_path .'AreteX_WPI.class.php');
require_once(plugin_dir_path( __FILE__ ).'AreteXWP_Cart.php');
}
else {
// error_log("Failing 2");
echo 'Error';
die();
}
self::fixObject($_SESSION['aretex_shopping_cart']);
$items = array();
foreach($_SESSION['aretex_shopping_cart']->items as $item)
$items[] = class_cast_2('Item',$item);
$cart = class_cast_2('Cart',$_SESSION['aretex_shopping_cart']);
$cart->items = $items;
$cart->summary_page = SummaryPageType::skip_to_cc;
$cart->totalOrder();
$checkout = new Checkout();
$checkout->identity_validation = base64_encode(json_encode($signed_idv));
$checkout->cart = base64_encode(json_encode($cart));
$message = $checkout->identity_validation.$checkout->cart;
$checkout->checkout_validation = AreteX_API::Sign($message ,$crypt_keys['privatekey']);
return json_encode($checkout);
}
protected function updateCartTracking($cart) {
$tracking = AreteX_WPI::getCurrentTrackingCode();
if ($tracking->valid) {
$cart->tracking_code = $tracking->standard;
$cart->tracking_code_signature = $tracking->validation;
}
else {
$cart->tracking_code = null;
$cart->tracking_code_signature = null;
}
return $cart;
}
public function atx_cart_summary() {
$aretex_core_path = get_option('aretex_core_path');
if (!session_id()) {
session_start();
}
if (file_exists($aretex_core_path .'AreteXClientEngine/Checkout.class.php')) {
require_once($aretex_core_path .'AreteXClientEngine/Checkout.class.php');
require_once($aretex_core_path.'AreteXClientEngine/Crypton/Crypton.php');
require_once($aretex_core_path .'AreteX_WPI.class.php');
require_once(plugin_dir_path( __FILE__ ).'AreteXWP_Cart.php');
}
AreteX_shopping_cart::fixObject($_SESSION['aretex_shopping_cart']);
if (is_object($_SESSION['aretex_shopping_cart'])) {
$total_items = 0;
if (is_array($_SESSION['aretex_shopping_cart']->items)) {
foreach($_SESSION['aretex_shopping_cart']->items as $item) {
$total_items += $item->qty;
}
}
echo 'Items: '.$total_items.'';
echo 'Total: $'.number_format($_SESSION['aretex_shopping_cart']->total_due,2).'
';
}
die();
}
public function atx_update_cart_items() {
session_start();
$form_data = array();
parse_str($_POST['form_data'],$form_data);
$aretex_core_path = get_option('aretex_core_path');
if (empty($aretex_core_path)) {
// error_log("Failing 1");
echo json_encode('Error');
die();
}
if (file_exists($aretex_core_path .'AreteXClientEngine/Checkout.class.php')) {
require_once($aretex_core_path .'AreteXClientEngine/Checkout.class.php');
require_once($aretex_core_path.'AreteXClientEngine/Crypton/Crypton.php');
require_once($aretex_core_path .'AreteX_WPI.class.php');
require_once(plugin_dir_path( __FILE__ ).'AreteXWP_Cart.php');
}
else {
// error_log("Failing 2");
echo json_encode('Error');
die();
}
self::fixObject($_SESSION['aretex_shopping_cart']);
$line_key = $form_data['line_key'];
if ($form_data['quantity'] <= 0) {
unset($_SESSION['aretex_shopping_cart']->items[$line_key]);
}
else {
$_SESSION['aretex_shopping_cart']->items[$line_key]->qty = $form_data['quantity'];
$_SESSION['aretex_shopping_cart']->items[$line_key]->options = $form_data['option'];
}
$display_options = $_SESSION['aretex_shopping_cart']->display_options[$line_key];
$options = $_SESSION['aretex_shopping_cart']->items[$line_key]->options;
foreach($options as $option_name=>$value) {
$_SESSION['aretex_shopping_cart']->item_options[$line_key][$display_options[$option_name]] = $value;
}
self::populate_session_cart();
$cart = self::build_cart_table();
echo $cart;
die();
}
public function atx_add_to_cart() {
//error_log("Add to Cart:".var_export($_POST,true));
if (!session_id())
session_start();
$form_data = array();
parse_str($_POST['form_data'],$form_data);
// error_log("Form Data:".var_export($form_data,true));
$aretex_core_path = get_option('aretex_core_path');
if (empty($aretex_core_path)) {
// error_log("Failing 1");
echo json_encode('Error');
die();
}
if (file_exists($aretex_core_path .'AreteXClientEngine/Checkout.class.php')) {
require_once($aretex_core_path .'AreteXClientEngine/Checkout.class.php');
require_once($aretex_core_path.'AreteXClientEngine/Crypton/Crypton.php');
require_once($aretex_core_path .'AreteX_WPI.class.php');
require_once(plugin_dir_path( __FILE__ ).'AreteXWP_Cart.php');
}
else {
// error_log("Failing 2");
echo json_encode('Error');
die();
}
self::fixObject($_SESSION['aretex_shopping_cart']);
if (! is_object($_SESSION['aretex_shopping_cart']) ) {
$_SESSION['aretex_shopping_cart'] = self::setup_cart();
}
if ( is_user_logged_in() ) {
global $user_login;
global $user_ID;
get_currentuserinfo(); // Populate the globals ...
$user_cart = get_user_meta($user_ID, 'aretex_cart', true);
}
if (is_array($user_cart->items) && empty($_SESSION['aretex_shopping_cart']->items)) {
$_SESSION['aretex_shopping_cart'] = $user_cart;
$_SESSION['aretex_shopping_cart']->totalOrder();
}
if (is_array($form_data['option']))
$option = $form_data['option'];
else
$option = array();
$_SESSION['aretex_shopping_cart']->addItem($form_data['product_code'],$form_data['quantity'],$option,$form_data['display_option'],$form_data['all_options'],$form_data['exclusivity'],$form_data['allow_qty_change']);
// error_log("OK");
if ( is_user_logged_in() ) {
global $user_login;
global $user_ID;
get_currentuserinfo(); // Populate the globals ...
update_user_meta($user_ID, 'aretex_cart', $_SESSION['aretex_shopping_cart']);
}
echo json_encode('OK');
die();
}
protected function add_shortcodes() {
// add_shortcode('a_shortcode', array( 'AreteX_shopping_cart', 'a_shortcode' ) );
add_shortcode('aretex_add_to_cart', array( 'AreteX_shopping_cart', 'aretex_add_to_cart' ) );
add_shortcode('aretex_show_cart', array( 'AreteX_shopping_cart', 'aretex_show_cart' ) );
}
static protected function make_select($name,$option_list) {
$str = '";
return $str;
}
static protected function make_text_input($name,$detail) {
$start = strpos($detail,'(');
$end = strpos($detail,')');
$num = substr($detail,$start,$end);
if (ctype_digit($num)) {
$size = 'size="'.$num.'" maxlength="'.$num.'"';
}
else
$size = '';
$str = '';
return $str;
}
static function parse_option_input($label_name,$option_detail) {
$input = '';
$option_name = strtolower($label_name);
$option_name = str_replace(' ','_',$option_name);
$option_detail = trim($option_detail);
list($option_detail,$deliverable_code) = explode('|',$option_detail);
if (! empty($deliverable_code)) {
$input .= '';
}
$input .= '';
switch(strtoupper($option_detail[0])) {
case 'S':
$input .= self::make_select($option_name,$option_detail);
break;
case 'T':
$input .= self::make_text_input($option_name,$option_detail);
break;
}
return $input;
}
static function parse_options($content) {
if (empty($content))
return '';
$content = strip_tags($content);
$str = '';
$options = strstr($content,'options:');
$start = strstr($options,'{');
$endpos = strpos($start,'}');
$options = substr($start,0,$endpos);
$options = trim($options);
$options = trim($options,'{}');
$options = preg_split('/\r\n|\r|\n/', $options);
foreach($options as $option) {
$option = trim($option);
if (empty($option)) {
continue;
}
list($label,$detail) = explode(':',$option);
$line = '
| Quantity | Item ID | Item Name | Total Price | |
|---|---|---|---|---|
| Tax | '.number_format($cart->total_tax,2).' | |||
| Shipping | '.number_format($cart->total_shipping,2).' | |||
| Total | '.number_format($cart->total_due,2).' | |||