/**
Functions
*/
function ym_group_apply_package($pack_id, $user_id = FALSE)
{
    $vars_to_apply = array('role', 'account_type', 'hide_old_content', 'child_accounts_allowed', 'child_accounts_package_types', 'child_accounts_packages', 'hide_admin_bar');
    $pack = ym_get_pack_by_id($pack_id);
    foreach ($vars_to_apply as $value) {
        $vars_to_apply[$value] = $pack[$value];
    }
    if ($user_id) {
        $ym_user = new YourMember_User($user_id);
    } else {
        global $ym_user;
    }
    $ym_user->update($vars_to_apply);
    $ym_user->save();
    @ym_log_transaction(YM_PACKAGE_PURCHASED, $pack_id, $user_id);
    return;
}
Ejemplo n.º 2
0
function ym_wp_logout($return_url = FALSE)
{
    global $ym_sys, $ym_user;
    $redirect_to = FALSE;
    if (isset($ym_user->pack_id) && $ym_user->pack_id) {
        $pack = ym_get_pack_by_id($ym_user->pack_id);
        if ($pack['logout_redirect_url']) {
            $redirect_to = $pack['logout_redirect_url'];
        }
    }
    // what a horrible line
    $redirect_to = $redirect_to ? site_url($redirect_to) : ($ym_sys->logout_redirect_url ? site_url($ym_sys->logout_redirect_url) : (isset($_REQUEST['redirect_to']) ? $_REQUEST['redirect_to'] : FALSE));
    if ($redirect_to) {
        if ($return_url) {
            return $redirect_to;
        }
        header('Location: ' . $redirect_to);
        exit;
    }
}
Ejemplo n.º 3
0
 function ym_expiry_sub_dropdown_check()
 {
     $expired_pack_id = $this->pack_id;
     global $ym_packs;
     $packs = $ym_packs->packs;
     $pack_data = ym_get_pack_by_id($expired_pack_id);
     if ($pack_data) {
         // pack found
         if (isset($pack_data['on_expire_drop_to']) && $pack_data['on_expire_drop_to']) {
             // target package to drop to
             $new_pack = ym_get_pack_by_id($pack_data['on_expire_drop_to']);
             if ($new_pack) {
                 // it exists
                 $ypg = new ym_payment_gateway();
                 $ypg->name == 'ym_dropdown';
                 $ypg->code == 'ym_dropdown';
                 $ypg->nomore_eamil = TRUE;
                 $ypg->do_buy_subscription($pack_data['on_expire_drop_to'], $this->ID, TRUE);
                 return TRUE;
             }
         }
     }
     return FALSE;
 }
Ejemplo n.º 4
0
 public function task($offset = 0)
 {
     global $ym_sys;
     if (!$ym_sys->email_reminder_enable) {
         echo 'Not Enabled in YM SYS';
         return;
     }
     $offset = isset($_REQUEST['offset']) ? $_REQUEST['offset'] : $offset;
     if (is_null($offset)) {
         $offset = 0;
     }
     if ($offset == -1) {
         // no pagination
         $offset = 0;
         $this->limit = null;
     }
     $this->limit = isset($_REQUEST['limit']) ? $_REQUEST['limit'] : $this->limit;
     // use API Exposed Element for search
     $users = get_users(array('offset' => $offset, 'number' => $this->limit, 'meta_key' => 'ym_status', 'meta_value' => YM_STATUS_EXPIRED, 'meta_compare' => '!='));
     $current_time = time();
     // set to now + days so a future
     $limit_date = time() + $ym_sys->email_reminder_limit * 86400;
     $postarray = array();
     //Drip Feed Email
     if ($ym_sys->email_drip_reminder_enable) {
         global $wpdb;
         //Get all posts
         $args = array('meta_key' => '_ym_account_min_duration', 'post_status' => 'publish');
         $posts = get_posts($args);
         foreach ($posts as $post) {
             $drip = get_post_meta($post->ID, '_ym_account_min_duration', true);
             $new_array = array();
             if ($drip) {
                 $drip = explode(';', $drip);
                 if ($drip) {
                     foreach ($drip as $d) {
                         $array = explode('=', $d);
                         $new_array[$array[0]] = $array[1];
                     }
                 }
             }
             $postarray[$post->ID] = array_filter($new_array);
         }
         $postarray = array_filter($postarray);
     }
     $total = count($users);
     if ($total) {
         $counter = 0;
         foreach ($users as $user) {
             $counter++;
             $user = new YourMember_User($user->ID);
             $expire_date = $user->expire_date;
             // user has expire date
             // user has not been sent a reminder
             // expire_date is less that the limit date
             // expire date is in the future
             if ($user->expire_date && !$user->reminder_email_sent && $user->expire_date < $limit_date && $user->expire_date > $current_time) {
                 // lock
                 $user->update(array('reminder_email_sent' => true), true);
                 // send
                 $subject = $ym_sys->email_reminder_subject;
                 $message = $ym_sys->email_reminder_message;
                 $pack = ym_get_pack_by_id($user->pack_id);
                 if ($pack['num_cycles'] != 1) {
                     // so 0 or many ie recurring
                     $subject = $ym_sys->email_reminder_subject_recur;
                     $message = $ym_sys->email_reminder_message_recur;
                 }
                 $subject = str_replace('[site_name]', get_bloginfo(), $subject);
                 $message = ym_apply_filter_the_content($message);
                 ym_email($user->data->user_email, $subject, $message);
                 @ym_log_transaction(YM_USER_STATUS_UPDATE, __('Email Reminder Sent', 'ym'), $user->ID);
                 do_action('ym_cron_email_reminder_sent', $user->ID);
                 echo '1';
             } else {
                 echo '.';
             }
             if (substr($counter, -1, 1) == '0') {
                 echo ' ' . $counter . '/' . $total . "\n";
             }
             $reminders = array();
             foreach ($postarray as $post => $type) {
                 foreach ($type as $ac_type => $days) {
                     if ($ac_type == $act) {
                         $reminders[$post] = array('post_id' => $post, 'days' => $days);
                     }
                 }
             }
             if ($reminders) {
                 $users_reminders = unserialize(get_user_meta($user->ID, 'drip_email_reminders', true));
                 if (!$users_reminders || !is_array($users_reminders)) {
                     $users_reminders = array();
                 }
                 foreach ($reminders as $reminder) {
                     if (!in_array($reminder['post_id'], $users_reminders)) {
                         //The post ID is not marked as already sent so we may need to send it
                         //need to determine if we should send it.
                         $reg = $user->data->user_registered;
                         if ($sys->post_delay_start == 'pack_join') {
                             if ($pack_join = $user->account_type_join_date) {
                                 $reg = date('Y-m-d', $pack_join);
                             }
                         }
                         $reg = mktime(0, 0, 0, substr($reg, 5, 2), substr($reg, 8, 2), substr($reg, 0, 4));
                         $user_at = $reg + 86400 * $reminder['days'];
                         if ($user_at <= time() && $user_at >= time() - 86400 * 7) {
                             //If the time is not in the future, and no older then 10 days, we should send an email
                             //send email
                             $subject = $ym_sys->email_drip_subject;
                             $message = $ym_sys->email_drip_message;
                             $subject = str_replace('[site_name]', get_bloginfo(), $subject);
                             $message = ym_apply_filter_the_content($message);
                             ym_email($target, $subject, $message);
                             $users_reminders[] = $reminder['post_id'];
                             @ym_log_transaction(USER_STATUS_UPDATE, __('Drip Content Email for post' . $reminder['post_id'], 'ym'), $user->ID);
                             do_action('ym_cron_email_drip_sent', $user->ID, $reminder['post_id']);
                         }
                     }
                 }
                 update_user_meta($user->ID, 'drip_email_reminders', serialize($users_reminders));
             }
         }
         echo ' ' . $counter . '/' . $total . "\n";
         // loop
         echo 'Loop Complete From ' . $offset . "\n";
         if ($this->call_type == 'auto') {
             if ($this->limit != NULL) {
                 echo 'Schedule Next Step' . "\n";
                 wp_schedule_single_event(time(), 'ym_cron_email_reminder', array($offset + $this->limit));
             } else {
                 echo 'Full Call Occured' . "\n";
             }
         } else {
             // reload
             echo 'Sleeping' . "\n";
             echo '<form action="" method="post"><input type="hidden" name="run_cron_job" value="ym_cron_email_reminder" /><input type="hidden" name="offset" value="' . ($offset + $this->limit) . '" /></form>';
             echo '<script type="text/javascript">jQuery(document).ready(function() { setTimeout(\'ym_fire()\', 5000) }); function ym_fire() { jQuery(\'form\').submit(); }</script>';
         }
     } else {
         echo 'Nothing to do Job Complete' . "\n";
         do_action('ym_cron_email_reminder_complete');
     }
 }
Ejemplo n.º 5
0
/**
Non Flow Register/Upgrade
*/
function ym_available_modules($username = false, $return = false, $coupon_type = 0)
{
    //	echo 'ym_available_modules: ' . $username . ', ' . $return . ', ' . $coupon_type . '<br />';
    global $ym_active_modules, $ym_packs;
    //coupons
    $user_id = ym_get_user_id();
    $ym_custom_fields = ym_get_custom_field_array($user_id);
    $ym_home = site_url();
    $base = $ym_home . '/index.php?ym_subscribe=1&username='******'';
    if ($pack_id = ym_get('pack_id')) {
        // pack ID has been selected
        $pack = ym_get_pack_by_id($pack_id);
        // strip commas
        $cost = str_replace(',', '', $pack['cost']);
        $override = FALSE;
        $code_to_use = 'custom';
        // coupon check
        if (isset($ym_custom_fields['coupon']) && $ym_custom_fields['coupon']) {
            $cost = ym_apply_coupon($ym_custom_fields['coupon'], $coupon_type, $cost);
            if (substr($cost, 0, 4) == 'pack') {
                $pack_id = substr($cost, 5);
                // apply new pack
                $pack = ym_get_pack($pack_id);
                // import data
                $cost = $pack['cost'];
                $duration = $pack['duration'];
                $duration_type = $pack['duration_type'];
                $account_type = $pack['account_type'];
                $num_cycles = $pack['num_cycles'];
            } else {
                // makre sure formatted ok
                $cost = number_format($cost, 2);
                $override = $cost;
                $code_to_use = 'freebie_code';
            }
            ym_register_coupon_use($ym_custom_fields['coupon'], ym_get_user_id(), 'buy_subscription_' . $pack_id);
        }
        // is it free?
        if ($cost == 0) {
            // auto redirect
            $redirector = ym_get('redirector');
            // attempt to redirect to the processor.
            // if attempt fails, we show the button
            $loc = $ym_home . '/index.php?ym_process=ym_free&' . (ym_get('ym_autologin') ? 'ym_autologin=1&' : '') . $code_to_use . '=buy_subscription_' . $pack['id'] . '_' . ym_get_user_id() . '&redirector=' . urlencode($redirector);
            if (!headers_sent()) {
                header('Location: ' . $loc);
                exit;
            } else {
                echo '<script type="text/javascript">window.location = "' . $loc . '";</script>';
            }
            die;
        }
        // gateway selection BuyNow
        $shown = 0;
        $shown_name = '';
        $shown_button = '';
        foreach ($ym_active_modules as $module) {
            $get_button = FALSE;
            if ($module == 'ym_free') {
                continue;
            } else {
                // do pack gateway check
                $get_button = TRUE;
            }
            if ($get_button) {
                ${$module} = new $module();
                $this_button = ${$module}->getButton($pack_id, $override, 'ym_available_modules');
                // a button pay not be returned (pack restrict gateway)
                if ($this_button) {
                    $shown_name = $module;
                    $shown_button = $this_button;
                    $shown++;
                    $html .= $this_button;
                }
            }
        }
        if ($shown == 0) {
            $html .= __('There are no payment gateways available at this time.', 'ym');
        } else {
            if ($shown == 1) {
                if (!method_exists(${$shown_name}, 'abort_auto')) {
                    // TODO: Are we on a page where HTML has been outputted?
                    // auto fire
                    $html = '<html>
						<head>
							<title>Redirecting...</title>
							<script type="text/javascript">
								function load() {
									document.forms["' . $shown_name . '_form"].submit();
								}
							</script>
						</head>
						<body onload="load();">';
                    $html .= '<div style="display: none;">' . $shown_button . '</div>' . '<div style="color: #333333; font-size: 14px; margin: 180px 250px; font-family: tahoma; text-align: center; padding: 50px; border: 1px solid silver;" id="ym_pay_redirect">' . '<div>You are being redirected. If this page does not refresh in 5 seconds then click <a onclick="document.forms[\'' . $module . '_form\'].submit();">here</a>.</div>' . '<div style="margin-top: 10px;"><img alt="" src="' . YM_IMAGES_DIR_URL . 'loading.gif" /></div>' . '</div>' . '</body></html>';
                    echo $html;
                    die;
                } else {
                    // aborted the auto fire step
                    $html .= ${$shown_name}->abort_auto();
                }
            }
        }
    } else {
        if (!ym_get('sel', FALSE)) {
            $html .= '<table width="100%" cellpadding="3" cellspacing="0" border="0" align="center" class="like_form">' . '<tr>' . '<th><h3 class="ym_register_heading">' . __('Select Payment Gateway', 'ym') . '</h3></th>' . '</tr>';
            /**
            No Gateway Selected
            Show Gateway Selection
            */
            $shown = 0;
            $shown_name = '';
            foreach ($ym_active_modules as $module) {
                $pay = new $module();
                $packs = $ym_packs->packs;
                $packs = apply_filters('ym_packs', $packs, $pay->code);
                if (count($packs)) {
                    $html .= '<tr>' . '<td align="center" style="padding: 5px; text-align: center;">' . '<a href="' . $base . '&sel=' . $module . '">' . '<div class="ym_module_name"><strong>' . $pay->name . '</strong></div>' . '<img class="ym_module_logo" src="' . $pay->logo . '" alt="' . $pay->name . '" title="' . $pay->name . '" />' . '</a>' . '</td>' . '</tr>';
                    $shown++;
                    $no_gateway = FALSE;
                    $shown_name = $module;
                }
                unset($pay, $packs);
            }
            $html .= '</table>';
            if ($shown == 0) {
                $html .= __('There are no payment gateways available at this time.', 'ym');
            } else {
                if ($shown == 1) {
                    // we only have one to show....
                    // auto fire
                    $loc = $base . '&sel=' . $shown_name;
                    if (!headers_sent()) {
                        header('Location: ' . $loc);
                    } else {
                        echo '<script type="text/javascript">window.location="' . $loc . '";</script>';
                    }
                    exit;
                }
            }
        } else {
            if ($selected = ym_get('sel')) {
                /**
                Gateway selected
                Show Buy Now Buttons for this gateway
                */
                // user has selected a gateway
                if (!class_exists($selected)) {
                    wp_die(sprintf(__('Unknown Module: %s', 'ym'), $selected));
                }
                $pay = new $selected();
                $html .= $pay->generateButtons(true);
            } else {
                /**
                Should not get here
                */
                wp_die(__('An error Occured (Code: YM_AVAILABLE_MODULES'));
            }
        }
    }
    /**
    Return
    */
    if ($return) {
        return $html;
    } else {
        echo $html;
        return;
    }
}
Ejemplo n.º 6
0
/**
Tax Controller
Returns Tax Percentage
*/
function ym_get_pack_tax($pack)
{
    if (is_numeric($pack)) {
        // got pack ID
        $pack = ym_get_pack_by_id($pack);
    }
    $tax = FALSE;
    global $ym_sys;
    if (isset($pack['vat_applicable']) && $pack['vat_applicable'] || $ym_sys->global_vat_applicable) {
        if ($ym_sys->vat_rate) {
            $tax = $ym_sys->vat_rate;
        }
    }
    if ($vat_rate = apply_filters('ym_vat_override', false, $user_id)) {
        $tax = $vat_rate;
    }
    return $tax;
}
             $id = $pack['id'] > $id ? $pack['id'] : $id;
         }
         $id++;
         $data['id'] = $id;
         // id
         $obj->packs[] = $data;
     }
     echo '<pre>';
     update_option('ym_packs', $obj);
     echo '<meta http-equiv="refresh" content="1;' . $link . '&message=ok" />';
     echo '</div>';
     return;
 }
 wp_enqueue_script('ym_admin_js_members', YM_JS_DIR_URL . 'ym_admin_membership_packages.js', array('jquery'), YM_PLUGIN_VERSION);
 if ($data['edit_id']) {
     $predata = ym_get_pack_by_id($data['edit_id']);
     $data['id'] = $data['edit_id'];
     unset($data['edit_id']);
     foreach ($data as $key => $current) {
         $data[$key] = isset($predata[$key]) ? $predata[$key] : $data[$key];
     }
     // invert gateway_disable
     // current stores gateways to hide
     $gateways_disabled = array();
     global $ym_active_modules;
     foreach ((array) $ym_active_modules as $key => $module) {
         if (FALSE === array_search($module, $data['gateway_disable'])) {
             $gateways_disabled[] = $module;
         }
     }
     $data['gateway_disable'] = $gateways_disabled;
	<input type="hidden" name="action" value="create_child_account" />
<table class="form-table">
';
    $ym_formgen->render_form_table_email_row(__('Email Address', 'ym'), 'email_address');
    $ym_formgen->render_form_table_text_row(__('Username', 'ym'), 'username', '', __('Leave blank to use the email address', 'ym'));
    $ym_formgen->render_form_table_password_row(__('Password', 'ym'), 'password', '', __('Leave blank to auto generate', 'ym'));
    $ym_formgen->render_form_table_password_row(__('Confirm Password', 'ym'), 'c_password');
    if (count($package_type_options)) {
        array_unshift($package_type_options, __('Select', 'ym'));
        $ym_formgen->render_combo_from_array_row(__('Package Type', 'ym'), 'package_type', $package_type_options);
    }
    $options = array();
    // package
    if (count($ym_user->child_accounts_packages)) {
        foreach ($ym_user->child_accounts_packages as $id) {
            $pack = ym_get_pack_by_id($id);
            if ($current_counts[$pack['account_type']] < $allowed_counts[$pack['account_type']]) {
                $options[$id] = ym_get_pack_label($id);
            }
        }
    }
    if (count($options)) {
        $options[0] = __('Select', 'ym');
        ksort($options);
        $label = __('Apply A Package', 'ym');
        if (count($ym_user->child_accounts_packages)) {
            $label = __('Or', 'ym') . ' ' . $label;
        }
        $ym_formgen->render_combo_from_array_row($label, 'package', $options);
    }
    echo '<tr><td colspan="2"><p class="submit"><input type="submit" class="button-primary alignright" value="' . __('Create', 'ym') . '" /></p></td></tr>';
 function redirectlogic($pack, $go = FALSE)
 {
     get_current_user();
     global $current_user;
     $post = FALSE;
     // redirect logic
     if (!is_array($pack)) {
         // assume packid
         $pack = preg_replace('/[^\\d\\.]/', '', $pack);
         $pack = ym_get_pack_by_id($pack);
     }
     // pack redirect?
     $red = '';
     $additional = '';
     if (isset($pack['success_redirect']) && $pack['success_redirect']) {
         $red = site_url($pack['success_redirect']);
     }
     if (isset($pack['ppp'])) {
         $post = TRUE;
         // lifted and merged
         if (isset($pack['ppp_pack_id'])) {
             // bundle
             $additional = 'bundle_id=' . $pack['ppp_pack_id'];
             $item = 'bundle_' . $pack['ppp_pack_id'];
             $word = 'bundle';
         } else {
             // post
             $additional = 'post_id=' . $pack['post_id'];
             $item = 'post_' . $pack['post_id'];
             $word = 'post';
         }
         $red = get_permalink(isset($pack['post_id']) ? $pack['post_id'] : get_the_ID());
         if (!$current_user->ID) {
             $red = get_option('siteurl') . '/wp-login.php?checkemail=registered&redirect_to=' . $red;
         }
     } else {
         $item = 'pack_id_' . $pack['id'];
         $word = 'subscribed';
     }
     $red = $red ? $red : (isset($this->thanks_url) && $this->thanks_url ? $this->thanks_url : '');
     $red = $red ? $red : (isset($this->return_url) && $this->return_url ? $this->return_url : '');
     $red = $red ? $red : (isset($this->return) && $this->return ? $this->return : '');
     $red = $red ? $red : site_url('/wp-login.php?checkemail=' . $word);
     // cord for callback script to fire
     if (strpos($red, '?')) {
         $red .= '&';
     } else {
         $red .= '?';
     }
     $red .= 'from_gateway=' . $this->code . '&';
     if (!$post) {
         $red .= 'pack_id=' . $pack['id'];
     } else {
         $red .= $additional;
     }
     $red .= '&item=' . $item;
     $red = apply_filters('ym_payment_gateway_redirectlogic', $red, $pack, $go);
     if ($go) {
         $this->redirect($red);
     } else {
         return $red;
     }
 }
Ejemplo n.º 10
0
 function expire_interrupt($data, $ymuser)
 {
     // interrupt and send to grace?
     // this is fired the moment they expire (ish)
     global $ym_sys;
     if (!$ym_sys->grace_enable) {
         // grace not on
         return $data;
     }
     // instantiate
     $invoice = new ym_invoice();
     if ($ymuser->gateway_used != $invoice->code) {
         return $data;
     }
     // check package
     $package = ym_get_pack_by_id($ymuser->pack_id);
     if ($package->num_cycles == 1) {
         // single occurance
         return $data;
     }
     // lets put them into grace
     $data = array('status' => YM_STATUS_GRACE, 'status_str' => __('User is entering Invoice Grace', 'ym'), 'expire_date' => time() + 86400 * $invoice->invoice_limit);
     @ym_log_transaction(YM_ACCESS_EXPIRY, $data['expire_date'], $ymuser->userId);
     @ym_log_transaction(YM_USER_STATUS_UPDATE, YM_STATUS_GRACE, $ymuser->userId);
     // trigger invoice email
     $this->generate_invoice($ymuser, $invoice);
     // notify admin
     if ($invoice->notify_admin_on_grace) {
         $email = get_bloginfo('admin_email');
         $subject = '[' . get_bloginfo() . '] ' . __('Invoice notification', 'ym');
         $message = __('The user ' . $ymuser->data->user_login . ' is entering Invoice Grace and has been sent a invoice', 'ym');
         ym_email($email, $subject, $message);
     }
     return $data;
 }
function ym_register_flow($flow_id, $pack_id = false, $widget = false)
{
    global $current_page, $next_page, $the_flow_id, $wpdb, $ym_res, $ym_sys;
    global $post_data, $pack_data;
    $html = $form_top = '';
    $payment_gateway_detected = false;
    if (!is_singular() && !$widget) {
        return __('A Register Flow Error Occurred (Type 0) Not on a Flow Page', 'ym');
    }
    if (!$flow_id) {
        return __('A Register Flow Error Occurred (Type 1) No Flow Selected', 'ym');
    }
    if (ym_post('flowcomplete')) {
        // complete
        $query = 'SELECT complete_text FROM ' . $wpdb->prefix . 'ym_register_flows WHERE flow_id = ' . $flow_id;
        $complete_text = $wpdb->get_var($query);
        if ($complete_text) {
            return '<p>' . $complete_text . '</p>';
        } else {
            return '<p>' . __('Registration/Upgrade is complete', 'ym') . '</p>';
        }
    }
    $flow_pages = 'SELECT flow_pages, complete_button FROM ' . $wpdb->prefix . 'ym_register_flows WHERE flow_id = ' . $flow_id;
    $flow_pages = $wpdb->get_row($flow_pages);
    if (!$flow_pages) {
        return __('A Register Flow Error Occurred (Type 2) Flow Not Found', 'ym');
    }
    $complete_button = $flow_pages->complete_button;
    $the_flow_id = $flow_id;
    // have a flow
    $flow_pages = unserialize($flow_pages->flow_pages);
    $last_page = ym_post('ym_register_flow_page', 0);
    $current_page = ym_post('ym_register_flow_next_page', 0);
    $flowcomplete = ym_post('flowcomplete', 0);
    if (!$current_page) {
        $copy = $flow_pages;
        $current_page = array_shift($copy);
    }
    $next_page = 0;
    while ($next_page == 0 && count($flow_pages)) {
        $page = array_shift($flow_pages);
        if ($page == $current_page) {
            $next_page = array_shift($flow_pages);
        }
    }
    //$permalink = get_permalink();
    $permalink = '';
    if (ym_superuser()) {
        echo '<div class="ym_message"><p class="ym_message_liner">' . __('Warning, entering this flow may change your WordPress role', 'ym') . '</p></div>';
    }
    echo '
<style type="text/css">
	label {
		display: block;
	}
</style>
';
    $form = '
<form action="' . $permalink . '" method="post" enctype="multipart/form-data" id="ym_register_flow_form">
';
    $html .= $form;
    $form_top .= $form;
    $custom_data = get_option('ym_custom_fields');
    $custom_data = $custom_data->entries;
    // required?
    $required_data = isset($_POST['required']) ? $_POST['required'] : array();
    $ok = true;
    $email = true;
    $useremail = true;
    $username = true;
    $coupon = true;
    $dupepassword = true;
    $dont_hidden = array('email_address', 'username', 'signed_request');
    // maintaint
    $post_data = array();
    foreach ($_POST as $field => $entry) {
        if ($field != 'ym_register_flow_page' && $field != 'ym_register_flow_next_page' && $field != 'required' && $field != 'flowcomplete') {
            if (isset($required_data[$field]) && $required_data[$field] == 1 && !$entry) {
                $ok = false;
            }
            if ($field == 'email_address' && !is_email($entry)) {
                $email = false;
                $entry = '';
            } else {
                if ($field == 'email_address') {
                    // verify unique
                    if (email_exists($entry)) {
                        $useremail = false;
                    }
                }
            }
            if ($field == 'username') {
                if (username_exists($entry)) {
                    $username = false;
                }
            }
            if ($field == 'coupon' && $entry) {
                $type = ym_post('coupon_type');
                if ($type == 'coupon_register') {
                    $type = array(0);
                } else {
                    if ($type == 'coupon_upgrade') {
                        $type = array(1);
                    } else {
                        if (!is_int($type)) {
                            // both
                            $type = array(0, 1);
                        }
                    }
                }
                $value = false;
                $coupon_type = '';
                foreach ($type as $t) {
                    $value = ym_validate_coupon($entry, $t);
                    if ($value) {
                        $coupon_type = $t;
                        // TODO: register coupon use
                        break;
                    }
                }
                if ($value) {
                    //valid
                    $post_data['coupon_value'] = $value;
                    $form = '<input type="hidden" name="coupon_value" value="' . $value . '" />';
                    $post_data['coupon_type'] = $coupon_type;
                    $form = '<input type="hidden" name="coupon_type" value="' . $coupon_type . '" />';
                    $coupon = true;
                } else {
                    // not valid
                    $coupon = false;
                }
            }
            //YM duplicate password check
            if ($field == 'ym_password') {
                if (ym_post('ym_password_check') || ym_post('ym_password_dupe')) {
                    $dupepassword = false;
                    if (ym_post('ym_password') == ym_post('ym_password_check')) {
                        $dupepassword = true;
                    }
                }
            }
            if (!isset($post_data[$field])) {
                $post_data[$field] = ym_post($field);
                //$entry;
                if (!in_array($field, $dont_hidden)) {
                    $form = '<input type="hidden" name="' . $field . '" value="' . $entry . '" />
';
                    $html .= $form;
                    $form_top .= $form;
                }
            }
        }
    }
    $call_login = 0;
    if ($_POST) {
        if (!$ok || !$email || !$useremail || !$username || !$coupon || !$dupepassword) {
            $next_page = $current_page;
            $current_page = $last_page;
            if (!$email) {
                $html .= '<div class="ym_message"><p class="ym_message_liner">' . $ym_res->registration_flow_email_invalid . '</p></div>';
            }
            if (!$useremail) {
                $html .= '<div class="ym_message"><p class="ym_message_liner">' . $ym_res->registration_flow_email_inuse . '</p></div>';
            }
            if (!$username) {
                $html .= '<div class="ym_message"><p class="ym_message_liner">' . $ym_res->registration_flow_username_inuse . '</p></div>';
            }
            if (!$ok) {
                $html .= '<div class="ym_message"><p class="ym_message_liner">' . $ym_res->registration_flow_required_fields . '</p></div>';
            }
            if (!$coupon) {
                $html .= '<div class="ym_message"><p class="ym_message_liner">' . $ym_res->registration_flow_invalid_coupon . '</p></div>';
            }
            if (!$dupepassword) {
                $html .= '<div class="ym_message"><p class="ym_message_liner">' . $ym_res->registration_flow_invalid_password . '</p></div>';
            }
            $ok = false;
        }
        global $current_user;
        get_currentuserinfo();
        $username = $password = $fb_widget_ok = false;
        // check registation
        if ($ok) {
            if (!$current_user->ID) {
                $email = isset($post_data['email_address']) ? $post_data['email_address'] : '';
                $username = isset($post_data['username']) ? $post_data['username'] : '';
                $password = isset($post_data['password']) ? $post_data['password'] : '';
                if ($email) {
                    // minimum for registeration
                    if (!$username) {
                        $username = $email;
                    }
                    if (username_exists($username)) {
                        // register failed
                        $html .= '<div class="ym_message"><p class="ym_message_liner">' . $ym_res->registration_flow_username_inuse . '</p></div>';
                    } else {
                        // able to registers
                        $ym_user = new YourMember_User();
                        $user_id = $ym_user->create($email, false, true, $username, $password);
                        wp_set_current_user($user_id);
                        $call_login = 1;
                    }
                } else {
                    if (ym_post('signed_request')) {
                        $data = ym_facebook_uncode(ym_post('signed_request'));
                        if ($data) {
                            if ($data->registration) {
                                // register!
                                if (email_exists($data->registration->email) || username_exists($data->registration->email)) {
                                    $html .= '<div class="ym_message"><p class="ym_message_liner">' . $ym_res->registration_flow_email_inuse . '</p></div>';
                                } else {
                                    $ym_user = new YourMember_User();
                                    $user_id = $ym_user->create($data->registration->email, false, true, $data->registration->email, $data->registration->password, array('first_name' => $data->registration->first_name, 'last_name' => $data->registration->last_name));
                                    wp_set_current_user($user_id);
                                    $call_login = 1;
                                    $fb_widget_ok = true;
                                }
                            } else {
                                $html .= '<div class="ym_message"><p class="ym_message_liner">' . __('Faecbook Registration Error (2)', 'ym') . '</p></div>';
                            }
                        } else {
                            $html .= '<div class="ym_message"><p class="ym_message_liner">' . __('Faecbook Registration Error (1)', 'ym') . '</p></div>';
                        }
                    }
                }
            } else {
                // update key user entries
                if (isset($post_data['username'])) {
                    if ($username = $post_data['username']) {
                        $query = 'UPDATE ' . $wpdb->users . ' SET user_login = \'' . $username . '\' WHERE ID = ' . $current_user->ID;
                        $wpdb->query($query);
                    }
                }
                if (isset($post_data['password'])) {
                    if ($password = $post_data['password']) {
                        $pw_hash = wp_hash_password($password);
                        $query = 'UPDATE ' . $wpdb->users . ' SET user_pass = \'' . $pw_hash . '\' WHERE ID = ' . $current_user->ID;
                        $wpdb->query($query);
                        $call_login = 1;
                    }
                }
            }
            // customs
            ym_update_custom_fields();
        }
    }
    $gateway_return = ym_request('gateway_return', false);
    if ($gateway_return) {
        // return from gateway into flow
        // all details dropped :-(
        $to_remove = array('gateway_return', 'item', 'ym_register_flow_page', 'ym_register_flow_next_page', 'user_id');
        $query = $_SERVER['QUERY_STRING'];
        foreach ($to_remove as $remove) {
            $query = preg_replace('/' . $remove . '\\=' . "([a-zA-Z0-9_]+)/", '', $query);
        }
        while (substr($query, -1, 1) == '&') {
            $query = substr($query, 0, -1);
        }
        $html = str_replace('<form action=""', '<form action="?' . $query . '"', $html);
        $user_id = ym_request('user_id', false);
        if ($user_id) {
            $call_login = 1;
        }
    }
    $ym_register_user_id = ym_request('ym_register_user_id', false);
    if ($ym_register_user_id) {
        $call_login = 1;
        $user_id = $ym_register_user_id;
    }
    if ($call_login) {
        // temp login
        wp_set_current_user($user_id);
        $html .= '<input type="hidden" name="ym_register_user_id" value="' . $user_id . '" />';
    }
    unset($username);
    unset($password);
    $form = '
	<input type="hidden" name="ym_register_flow_page" value="' . $current_page . '" />
	<input type="hidden" name="ym_register_flow_next_page" value="' . $next_page . '" />
	';
    $html .= $form;
    $form_top .= $form;
    // data maintain whats left
    foreach ($post_data as $key => $item) {
        if (!in_array($key, $dont_hidden)) {
            $form = '
	<input type="hidden" name="' . $key . '" value="' . $item . '" />
	';
            $html .= $form;
            $form_top .= $form;
        }
    }
    // load
    $page = 'SELECT page_fields, button_text FROM ' . $wpdb->prefix . 'ym_register_pages WHERE page_id = ' . $current_page;
    $page = $wpdb->get_row($page);
    if (!$page) {
        return __('A Register Flow Error Occurred (Type 3) Page Not Found', 'ym');
    }
    $page_data = $page->page_fields;
    $next_button = $page->button_text;
    $page_data = unserialize($page_data);
    foreach ($page_data as $item => $field) {
        foreach ($field as $i => $f) {
            $page_data[$item][$i] = stripslashes(urldecode($f));
        }
    }
    $block_logic = array();
    // parse pack data
    $pack_data = false;
    if (isset($post_data['pack_id'])) {
        // load from form
        $pack_id = $post_data['pack_id'];
        // pass thru....
    }
    if ($pack_id) {
        $pack_data = ym_get_pack_by_id($pack_id);
    } else {
        // no pack id :-(
        // default
        $pack_order = ym_get_packs();
        $pack_data = array_shift($pack_order);
    }
    $first_button = true;
    foreach ($page_data as $index => $field_data) {
        $display = true;
        if ($field_data['iflogic']) {
            // block has logic
            $display = false;
            // evaulate the block logic result
            // is it a then or a else?
            switch ($field_data['iflogic']) {
                case 'loggedin':
                    $match = $field_data['iflogic_quantity_loggedin'];
                    $logged_in = is_user_logged_in();
                    if ($logged_in && $match) {
                        // user is logged on and the match is for logged in
                        $this_logic = 'then';
                    } else {
                        if (!$logged_in && !$match) {
                            $this_logic = 'then';
                        } else {
                            $this_logic = 'else';
                        }
                    }
                    break;
                case 'buying':
                    $match = $field_data['iflogic_quantity_pack'];
                    if (isset($post_data['pack_id']) && $post_data['pack_id'] == $match) {
                        $this_logic = 'then';
                    } else {
                        $this_logic = 'else';
                    }
                    break;
                case 'currentlyon':
                    $match = $field_data['iflogic_quantity_pack'];
                    if (is_user_logged_in()) {
                        global $ym_user;
                        $pack_id = $ym_user->pack_id ? $ym_user->pack_id : 0;
                        if ($pack_id == $match) {
                            $this_logic = 'then';
                        } else {
                            $this_logic = 'else';
                        }
                    } else {
                        $this_logic = 'else';
                    }
                    break;
                case 'accounttype':
                    $match = $field_data['iflogic_quantity_pack'];
                    $match = strtolower($match);
                    if (is_user_logged_in()) {
                        global $ym_user;
                        $account_type = $ym_user->account_type ? $ym_user->account_type : '';
                        $account_type = strtolower($account_type);
                        if ($account_type == $match) {
                            $this_logic = 'then';
                        } else {
                            $this_logic = 'else';
                        }
                    } else {
                        $this_logic = 'else';
                    }
                    break;
                case 'filledin':
                    // custom field
                    $field = $field_data['iflogic_quantity_custom'];
                    $value = $field_data['iflogic_quantity_custom_compare'];
                    if (is_user_logged_in()) {
                        $customs = get_user_meta($current_user->ID, 'ym_custom_fields', true);
                        $test = $customs->{$field};
                        if ($test == $value) {
                            $this_logic = 'then';
                        } else {
                            $this_logic = 'else';
                        }
                    } else {
                        $this_logic = 'else';
                    }
                    break;
                case 'servervar':
                case 'getvar':
                case 'postvar':
                case 'cookievar':
                    $source = '_' . substr($field_data['iflogic'], 0, -3);
                    $match_name = $field_data['iflogic_quantity_field'];
                    $match_value = $field_data['iflogic_quantity_entry'];
                    $current_value = $source[$match_name];
                    if ($current_value == $match_value) {
                        $this_logic = 'then';
                    } else {
                        $this_logic = 'else';
                    }
                    break;
                case 'registeredfor':
                    //				case 'memberfor':
                //				case 'memberfor':
                case 'expiresin':
                    $match_value = $field_data['iflogic_quantity_memberfor_value'];
                    $match_unit = $field_data['iflogic_quantity_memberfor_unit'];
                    if (is_user_logged_in()) {
                        global $ym_user;
                        if ($field_data['iflogic'] == 'registeredfor') {
                            $math_date = strtotime($current_user->user_registered);
                        } else {
                            if ($field_data['iflogic'] == 'expiresin') {
                                $math_data = $ym_user->expire_date;
                            } else {
                                $math_date = '';
                            }
                        }
                        $seconds = ym_register_flow_date_math($match_value, $match_unit);
                        $diff = time() - $math_date;
                        if ($diff > $seconds) {
                            $this_logic = 'then';
                        } else {
                            $this_logic = 'else';
                        }
                    } else {
                        $this_logic = 'else';
                    }
                    break;
                default:
                    $this_logic = 'else';
            }
            if ($this_logic == 'then' && $field_data['iflogic_showhide'] == 'show' || $this_logic == 'else' && $field_data['iflogic_showhide'] == 'hide') {
                $display = true;
            } else {
                $display = false;
            }
        }
        if ($field_data['label'] == 'page_logic' && ($this_logic == 'then' && $field_data['iflogic_showhide'] == 'hide' || $this_logic == 'else' && $field_data['iflogic_showhide'] == 'hide')) {
            $html .= '
<script type="text/javascript">
	jQuery(document).ready(function() {
		jQuery(\'#ym_register_flow_form\').submit();
	});
</script>
';
            $html .= '</form>';
            return $html;
        } else {
            if ($field_data['label'] == 'page_logic') {
                // skip the IF and just skip the whole loop if should?
                continue;
            }
        }
        /**
         output render
        */
        $html .= '<span class="' . $field_data['classes'] . '">';
        //open class span
        if ($field_data['types'] == 'freetext' && $display) {
            $html .= '<p>' . nl2br($field_data['names']) . '</p>';
            //names o.0 lol
            /**
            Customs Processor
            */
        } else {
            if ($field_data['types'] == 'custom' && $display) {
                // LIFTED FROM ym.php line 642
                // modded tooooo!
                if ($field_data['names'] == 'terms_and_conditions' && !empty($ym_res->tos)) {
                    $html .= '<p>
						<textarea name="tos" cols="29" rows="5" readonly="readonly">' . $ym_res->tos . '</textarea>';
                    $html .= '</p>';
                    $html .= '<p>
						<label class="ym_label" for="terms_and_conditions">
							<div><input type="checkbox" class="checkbox" name="terms_and_conditions" id="terms_and_conditions" value="1" />
							' . __('I agree to the Terms and Conditions.', 'ym') . '</div>
						</label>
					</p>' . "\n";
                } else {
                    if ($field_data['names'] == 'ym_password') {
                        $html .= '<label for="ym_password">' . $field_data['label'];
                        $html .= '<input type="password" name="' . $field_data['names'] . '" value="" />';
                        $html .= '<input type="hidden" name="required[' . $field_data['names'] . ']" value="' . $field_data['required'] . '" />';
                        if ($field_data['required']) {
                            $html .= $ym_sys->required_custom_field_symbol;
                        }
                        $html .= '</label>';
                        if (!empty($field_data['options'])) {
                            $html .= '<label for="ym_password_check">' . __('Confirm Password', 'ym');
                            $html .= '<input type="password" name="ym_password_check" value="" />';
                            $html .= '<input type="hidden" name="ym_password_dupe" value="1" />';
                            if ($field_data['required']) {
                                $html .= $ym_sys->required_custom_field_symbol;
                            }
                            $html .= '</label>';
                        }
                    } else {
                        if ($field_data['names'] == 'subscription_introduction' && !empty($ym_res->subs_intro)) {
                            $html .= '<div class="ym_subs_intro">' . $ym_res->subs_intro . '</div>';
                        } else {
                            if ($field_data['names'] == 'subscription_options') {
                                global $ym_packs;
                                $upsell_from = ym_request('gateway_return', false) ? $pack_id : false;
                                // TO DO
                                $pack_data = false;
                                if ($pack_id && !ym_request('gateway_return', false)) {
                                    $pack_data = ym_get_pack_by_id($pack_id);
                                    if ($pack_data) {
                                        $label = ym_get_pack_label($pack_id);
                                        $html .= '<p>' . sprintf(__('You are subscribing to <b>%s</b>', 'ym'), $label) . '</p>';
                                        $html .= '<input type="hidden" name="pack_id" value="' . $pack_id . '" />';
                                    } else {
                                        return __('A Register Flow Error Occurred (Type 4) Specified Pack Not Found', 'ym');
                                    }
                                } else {
                                    $ym_packs->packs = apply_filters('ym_packs', $ym_packs->packs);
                                    foreach ($ym_packs->packs as $pack) {
                                        if ($upsell_from == $pack['id']) {
                                            continue;
                                        }
                                        $label = ym_get_pack_label($pack['id']);
                                        $html .= '<label for="pack_id_' . $pack['id'] . '">';
                                        $html .= '<input type="radio" name="pack_id" id="pack_id_' . $pack['id'] . '" value="' . $pack['id'] . '" />';
                                        $html .= ' ' . $label . ' ';
                                        $html .= '</label>';
                                    }
                                }
                            } else {
                                if ($field_data['names'] == 'birthdate') {
                                    $html .= '<label for="ym_birthdate_month">' . $field_data['label'];
                                    $birthdate_fields = ym_birthdate_fields('ym_birthdate', ym_post('ym_birthdate_month', ''), ym_post('ym_birthdate_day', ''), ym_post('ym_birthdate_year', ''));
                                    $html .= $birthdate_fields;
                                    $html .= '<input type="hidden" name="required[ym_birthdate_month]" value="' . $field_data['required'] . '" />';
                                    $html .= '<input type="hidden" name="required[ym_birthdate_day]" value="' . $field_data['required'] . '" />';
                                    $html .= '<input type="hidden" name="required[ym_birthdate_year]" value="' . $field_data['required'] . '" />';
                                    if ($field_data['required']) {
                                        $html .= $ym_sys->required_custom_field_symbol;
                                    }
                                    $html .= '</label>';
                                } else {
                                    if ($field_data['names'] == 'country') {
                                        $html .= '<label for="ym_country">' . $field_data['label'];
                                        $countries_sel = ym_countries_list('ym_country', ym_post('ym_country', false));
                                        $html .= $countries_sel;
                                        $html .= '<input type="hidden" name="required[ym_country]" value="' . $field_data['required'] . '" />';
                                        if ($field_data['required']) {
                                            $html .= $ym_sys->required_custom_field_symbol;
                                        }
                                        $html .= '</label>';
                                    } else {
                                        // HERE
                                        $this_custom = '';
                                        foreach ($custom_data as $custom) {
                                            $label = $custom['label'];
                                            if (!$label) {
                                                $label = strtolower(str_replace(' ', '_', $custom['name']));
                                            }
                                            if ($label == $field_data['label']) {
                                                // found
                                                $this_custom = $custom;
                                                break;
                                            }
                                        }
                                        if ($this_custom) {
                                            $ro = $this_custom['readonly'] ? 'readonly="readonly"' : '';
                                            // check for special
                                            $value = $this_custom['value'];
                                            if (strpos($value, ':') !== false) {
                                                $array = explode(':', $value);
                                                if (count($array)) {
                                                    switch ($array[0]) {
                                                        case 'cookie':
                                                            $value = ym_cookie($array[1]);
                                                            break;
                                                        case 'session':
                                                            $value = ym_session($array[1]);
                                                            break;
                                                        case 'get':
                                                            $value = ym_get($array[1]);
                                                            break;
                                                        case 'post':
                                                            $value = ym_post($array[1]);
                                                            break;
                                                        case 'request':
                                                        case 'qs':
                                                            $value = ym_request($array[1]);
                                                            break;
                                                        default:
                                                            $value = '';
                                                            break;
                                                    }
                                                    $this_custom['value'] = ym_post($this_custom['name'], $value);
                                                }
                                            } else {
                                                if (is_user_logged_in()) {
                                                    $this_custom['value'] = ym_custom_value($this_custom['id']);
                                                } else {
                                                    $this_custom['value'] = ym_post($this_custom['name'], $this_custom['value']);
                                                }
                                            }
                                            // ro adjust for fields that should not be changed
                                            switch ($this_custom['type']) {
                                                case 'password':
                                                case 'text':
                                                    $html .= '<label for="' . $this_custom['name'] . '">' . $this_custom['label'];
                                                    $html .= '<input type="' . $this_custom['type'] . '" name="' . $this_custom['name'] . '" value="' . $this_custom['value'] . '" ' . $ro . ' />';
                                                    $html .= '<input type="hidden" name="required[' . $this_custom['name'] . ']" value="' . $field_data['required'] . '" />';
                                                    if ($field_data['required'] && !$ro) {
                                                        $html .= $ym_sys->required_custom_field_symbol;
                                                    }
                                                    $html .= '</label>';
                                                    break;
                                                case 'hidden':
                                                    $html .= '<input type="hidden" name="' . $this_custom['name'] . '" value="' . $this_custom['value'] . '" ' . $ro . ' />';
                                                    break;
                                                case 'yesnocheckbox':
                                                    $html .= '<label for="' . $this_custom['name'] . '">' . $this_custom['label'];
                                                    $html .= '<input type="checkbox" name="' . $this_custom['name'] . '" value="1" ' . ($this_custom['value'] ? 'checked="checked"' : '') . ' ' . $ro . ' />';
                                                    $html .= '<input type="hidden" name="required[' . $this_custom['name'] . ']" value="' . $field_data['required'] . '" />';
                                                    if ($field_data['required'] && !$ro) {
                                                        $html .= $ym_sys->required_custom_field_symbol;
                                                    }
                                                    $html .= '</label>';
                                                    break;
                                                case 'yesno':
                                                case 'select':
                                                case 'multiselect':
                                                    $html .= '<label for="' . $this_custom['name'] . '">' . $this_custom['label'];
                                                    if ($this_custom['type'] == 'multiselect') {
                                                        $html .= '<select name="' . $this_custom['name'] . '[]" multiple="multiple"';
                                                    } else {
                                                        $html .= '<select name="' . $this_custom['name'] . '" ';
                                                    }
                                                    $html .= '>';
                                                    if ($this_custom['type'] == 'select' || $this_custom['type'] == 'multiselect') {
                                                        $options = explode(';', $this_custom['available_values']);
                                                    } else {
                                                        $options = array(__('Yes', 'ym'), __('No', 'ym'));
                                                    }
                                                    foreach ($options as $option) {
                                                        if (strpos($option, ':')) {
                                                            list($option, $val) = explode(':', $option);
                                                            $html .= '<option value="' . $option . '" ' . ($option == $this_custom['value'] ? 'selected="selected"' : '') . '>' . $val . '</option>';
                                                        } else {
                                                            $html .= '<option value="' . $option . '" ' . ($option == $this_custom['value'] ? 'selected="selected"' : '') . '>' . $option . '</option>';
                                                        }
                                                    }
                                                    $html .= '
</select>
';
                                                    $html .= '<input type="hidden" name="required[' . $this_custom['name'] . ']" value="' . $field_data['required'] . '" />';
                                                    if ($field_data['required'] && !$ro) {
                                                        $html .= $ym_sys->required_custom_field_symbol;
                                                    }
                                                    $html .= '</label>';
                                                    break;
                                                case 'textarea':
                                                    $html .= '<label for="' . $this_custom['name'] . '">' . $this_custom['label'];
                                                    $html .= '<textarea name="' . $this_custom['name'] . '" cols="29" rows="5" ' . $ro . '>' . $this_custom['value'] . '</textarea>';
                                                    $html .= '<input type="hidden" name="required[' . $this_custom['name'] . ']" value="' . $field_data['required'] . '" />';
                                                    if ($field_data['required'] && !$ro) {
                                                        $html .= $ym_sys->required_custom_field_symbol;
                                                    }
                                                    $html .= '</label>';
                                                    break;
                                                case 'file':
                                                    $html .= '<label for="' . $this_custom['name'] . '">' . $this_custom['label'];
                                                    $html .= '<input type="file" name="' . $this_custom['name'] . '" />';
                                                    $html .= '</label>';
                                                    break;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                // END LIFT
                /**
                Buttons
                */
            } else {
                if (($field_data['types'] == 'payment_button' || $field_data['types'] == 'payment_action') && $display) {
                    $payment_gateway_detected = true;
                    add_filter('ym_additional_code', 'ym_register_flow_override_return', 10, 3);
                    $enabled = get_option('ym_modules');
                    // use the ym user id function
                    if (ym_get_user_id()) {
                        if (in_array($field_data['names'], $enabled)) {
                            // register flow
                            $class = $field_data['names'];
                            $pay = new $class();
                            if ($first_button) {
                                $html .= '</form>';
                                $first_button = false;
                            }
                            $this_pack = $pack_data;
                            // coupon check
                            if (isset($post_data['coupon_value']) && $post_data['coupon_value']) {
                                // stop
                                // stash
                                $value = ym_apply_coupon($post_data['coupon'], $post_data['coupon_type'], $this_pack['cost']);
                                $type = ym_get_coupon_type($value);
                                if ($type == 'percent') {
                                    // percent cost change
                                    $this_pack['cost'] = $this_pack['cost'] / 100 * $value;
                                } else {
                                    if ($type == 'sub_pack') {
                                        // diff pack
                                        $this_pack = ym_get_pack_by_id($value);
                                    } else {
                                        // other
                                        // new cost
                                        $this_pack['cost'] = $value;
                                    }
                                }
                                ym_register_coupon_use($post_data['coupon'], ym_get_user_id(), 'buy_subscription_' . $pack_data['id']);
                                if (!$this_pack['cost']) {
                                    // change to free
                                    //							$field_data['names'] = 'ym_free';
                                    // lifted from 135 of ym-register.include.php
                                    $code_to_use = 'freebie_code';
                                    // attempt to redirect to the processor.
                                    $loc = $ym_home . '/index.php?ym_process=ym_free&' . $code_to_use . '=buy_subscription_' . $this_pack['id'] . '_' . ym_get_user_id();
                                    if (!headers_sent()) {
                                        header('Location: ' . $loc);
                                        exit;
                                    } else {
                                        echo '<script type="text/javascript">window.location = "' . $loc . '";</script>';
                                    }
                                    die;
                                }
                            }
                            // there will always be pack data becuase I picked the default one earlier
                            // but it will default to the default pack anyway
                            if ($this_pack['cost']) {
                                // && $field_data['names'] != 'ym_free') {
                                //						$gw_button_form = $pay->getButton($this_pack['id'], (isset($post_data['coupon_value']) ? $this_pack['cost'] : false));
                                //						$html .= $gw_button_form;
                                $gw_button_form = $pay->getButton($this_pack['id'], isset($post_data['coupon_value']) ? $this_pack['cost'] : false);
                                if ($field_data['types'] == 'payment_action') {
                                    if (method_exists($pay, 'register_auto_payment_action')) {
                                        $html .= $pay->register_auto_payment_action($this_pack['id'], isset($post_data['coupon_value']) ? $this_pack['cost'] : false, true);
                                    } else {
                                        if ($gw_button_form) {
                                            $html .= $gw_button_form . '
<script type="text/javascript">
	jQuery(document).ready(function() {
		jQuery(\'.' . $field_data['names'] . '_form\').submit();
	});
</script>
';
                                        }
                                    }
                                } else {
                                    if (method_exists($pay, 'register_payment_action')) {
                                        $html .= $pay->register_payment_action(true);
                                    } else {
                                        $html .= $gw_button_form;
                                    }
                                }
                            } else {
                                if (!$this_pack['cost'] && $field_data['names'] == 'ym_free') {
                                    // free
                                    $gw_button_form = $pay->getButton($this_pack['id'], false);
                                    $html .= $gw_button_form;
                                    if ($field_data['types'] == 'payment_action') {
                                        if (method_exists($pay, 'register_auto_payment_action')) {
                                            $html .= $pay->register_auto_payment_action($this_pack['id'], false, true);
                                        } else {
                                            if ($gw_button_form) {
                                                $html .= '
<script type="text/javascript">
	jQuery(document).ready(function() {
		jQuery(\'.ym_free_form\').submit();
	});
</script>
';
                                            }
                                        }
                                    } else {
                                        if (method_exists($pay, 'register_payment_action')) {
                                            $html .= $pay->register_payment_action(true);
                                        }
                                    }
                                } else {
                                    if (isset($post_data['coupon_value']) && $post_data['coupon_value']) {
                                        return __('A Register Flow Error Occurred (Type 5) Pack/Coupon Error', 'ym');
                                    }
                                }
                            }
                        }
                    } else {
                        // user not logged in // No User ID Determined
                        return __('In order to continue you need to Register or Login', 'ym');
                    }
                    /**
                    Widgets
                    */
                } else {
                    if ($field_data['types'] == 'widget' && $display) {
                        if ($field_data['names'] == 'login') {
                            // login form
                            $html .= '
	<input type="hidden" name="ym_register_flow_page" value="' . $current_page . '" />
	<input type="hidden" name="ym_register_flow_next_page" value="' . $current_page . '" />
	';
                            //	<input type="hidden" name="ym_register_flow_do_login" value="1" />';
                            $html .= ym_login_form();
                        } else {
                            if ($field_data['names'] == 'register_facebook') {
                                // check for and handle a signed request
                                if ($fb_widget_ok) {
                                    // skippy
                                    $html .= '
					<script type="text/javascript">
						jQuery(document).ready(function() {
							jQuery(\'#ym_register_flow_form\').submit();
						});
					</script>
					';
                                } else {
                                    $html .= '
<iframe src="https://www.facebook.com/plugins/registration?
             client_id=' . get_option('ym_register_flow_fb_app_id') . '&
             redirect_uri=' . get_permalink() . '&
             fields=name,email,first_name,last_name,password"
        scrolling="auto"
        frameborder="no"
        style="border:none"
        allowTransparency="true"
        width="100%"
        height="550">
</iframe>
';
                                    // no next/complete please
                                }
                                $payment_gateway_detected = true;
                            } else {
                                $html .= 'Undefined Widget: (' . $field_data['names'] . ')';
                            }
                        }
                        /**
                        Coupon
                        */
                    } else {
                        if ($field_data['names'] == 'coupon' && $display) {
                            $value = isset($post_data[$field_data['names']]) ? $post_data[$field_data['names']] : '';
                            $html .= '<label for="' . $field_data['names'] . '">' . $field_data['label'];
                            $html .= '<input type="text" name="' . $field_data['names'] . '" id="' . $field_data['names'] . '" value="' . $value . '" />';
                            $html .= '<input type="hidden" name="coupon_type" value="' . $field_data['types'] . '" />';
                            $html .= '<input type="hidden" name="required[' . $field_data['names'] . ']" value="' . $field_data['required'] . '" />';
                            if ($field_data['required']) {
                                $html .= $ym_sys->required_custom_field_symbol;
                            }
                            $html .= '</label>';
                            /**
                            Display everything else
                            */
                        } else {
                            if ($display) {
                                $value = isset($post_data[$field_data['names']]) ? $post_data[$field_data['names']] : '';
                                $html .= '<label for="' . $field_data['names'] . '">' . $field_data['label'];
                                $html .= '<input type="' . $field_data['types'] . '" name="' . $field_data['names'] . '" id="' . $field_data['names'] . '" value="' . $value . '" />';
                                $html .= '<input type="hidden" name="required[' . $field_data['names'] . ']" value="' . $field_data['required'] . '" />';
                                if ($field_data['required']) {
                                    $html .= $ym_sys->required_custom_field_symbol;
                                }
                                $html .= '</label>';
                            }
                        }
                    }
                }
            }
        }
        $html .= '</span>';
        //closes class span
    }
    if (!$first_button) {
        // kill id
        $html = str_replace('id="ym_register_flow_form"', '', $html);
        // complete?
        if (!$next_page) {
            $query = 'SELECT complete_url FROM ' . $wpdb->prefix . 'ym_register_flows WHERE flow_id = ' . $flow_id;
            if ($url = $wpdb->get_var($query)) {
                $url = site_url($url);
                $form_top = str_replace('<form action=""', '<form action="' . $url . '"', $form_top);
            }
        }
        // append the form top
        $html .= $form_top;
        // end it
    }
    // payment gateway?
    if (!$payment_gateway_detected) {
        $html .= '<p>';
        if ($next_page) {
            $html .= '<input type="submit" value="' . $next_button . '" />';
        } else {
            $html .= '
<input type="hidden" name="flowcomplete" value="1" />
<input type="submit" value="' . $complete_button . '" />';
        }
        $html .= '</p>';
    }
    $html .= '</form>';
    return $html;
}
Ejemplo n.º 12
0
function ym_upgrade_buttons($return = false, $pack_id = false, $user_id = false)
{
    global $wpdb, $duration_str, $current_user, $ym_res, $ym_sys, $ym_packs;
    get_currentuserinfo();
    if (!$user_id) {
        $user_id = $current_user->ID;
    }
    if ($pack_id == 'all') {
        global $ym_packs;
        $html = '';
        foreach ($ym_packs->packs as $pack) {
            if (!$pack['hide_subscription']) {
                $html .= ym_upgrade_buttons(TRUE, $pack['id']);
            }
        }
        if ($return) {
            return $html;
        } else {
            echo $html;
            return;
        }
    }
    $html = '';
    $ym_home = get_option('siteurl');
    if (!$user_id) {
        $html = $ym_res->msg_header . __('Sorry but you must be logged in to upgrade your account', 'ym') . $ym_res->msg_footer;
    } else {
        $user_data = new YourMember_User($user_id);
        $account_type = $user_data->account_type;
        $packs = $ym_packs->packs;
        //		$trial_taken = get_user_meta($user_id, 'ym_trial_taken', TRUE);
        // UP TO HERE
        global $ym_active_modules;
        $base = add_query_arg(array('ym_subscribe' => 1, 'ud' => 1, 'username' => $current_user->user_login), $ym_home);
        if ((!isset($_POST['submit']) || !isset($_POST['subs_opt'])) && !$pack_id) {
            // TODO: Does this code even run?
            $html = '<p class="message register">' . __('Choose an Account Type', 'ym') . '</p>';
            $html .= '<form action="" method="post" class="ym"><div style="clear: both; overflow: auto; padding-bottom: 10px;">';
            // RENDER2
            $packs_shown = 0;
            if ($existing_data = ym_request('ym_subscription')) {
                $default = $existing_data;
            } else {
                //				$default = ym_get_default_pack();
                $default = $user_data->pack_id;
            }
            $did_checked = FALSE;
            foreach ($packs as $pack) {
                if (!$pack['hide_subscription']) {
                    $html .= '<div class="ym_register_form_subs_row">
								<div class="ym_reg_form_pack_radio">
									<input type="radio" ';
                    if ($pack['id'] == $default && !$did_checked) {
                        $html .= 'checked="checked"';
                        $did_checked = TRUE;
                    }
                    $packs_shown++;
                    $html .= ' class="checkbox" id="ym_subscription_' . $pack['id'] . '" name="ym_subscription" value="' . $pack['id'] . '" />
							</div>
							<label for="ym_subscription_' . $pack['id'] . '" class="ym_subs_opt_label ym_reg_form_pack_name">' . ym_get_pack_label($pack['id']) . '</label>
						</div>';
                }
            }
            if (!$packs_shown) {
                $hide_label = true;
            } else {
                if (isset($entry['caption']) && $entry['caption']) {
                    $html .= '<div class="ym_clear">&nbsp;</div><div class="ym_register_form_caption">' . $entry['caption'] . '</div>' . $row;
                }
            }
            // END RENDER2
            if ($packs_shown) {
                //				$html .= '</div><input type="hidden" name="ref" value="'. md5($user_data->amount .'_'. $user_data->duration .'_'. $user_data->duration_type .'_'. $user_data->account_type) .'" />';
                $html .= '<p class="submit"><input type="submit" name="submit" value="' . __('Next &raquo;', 'ym') . '" /></p>';
            } else {
                $html .= '<p>' . __('Sorry there are currently no upgrade/downgrade options available to you.', 'ym') . '</p>';
            }
            $html .= '</form>';
        } else {
            if (!ym_post('subs_opt') && $pack_id != ym_post('ym_subscription')) {
                global $ym_res;
                $html = '<form action="" method="post" class="ym_upgrade_shortcode">';
                $html .= '<input type="hidden" name="ym_subscription" value="' . $pack_id . '" />';
                //				$html .= '<input type="hidden" name="ref" value="'. md5($user_data->amount .'_'. $user_data->duration .'_'. $user_data->duration_type .'_'. $user_data->account_type) .'" />';
                $html .= ym_get_pack_label($pack_id);
                $html .= '&nbsp;<a href="#nowhere" onClick="jQuery(this).parents(\'form\').submit();">Upgrade</a>';
                $html .= '</form>';
                return $html;
            } else {
                $pack = ym_get_pack_by_id($pack_id);
                $cost = $pack['cost'];
                if (!$pack_id) {
                    $html .= '<br /><table width="100%" cellpadding="3" cellspacing="0" border="0" align="center" class="form-table">';
                    if ($cost == 0 || $account_type == 'free') {
                        $html .= '<tr><th>' . __('Create a free account: ', 'ym') . ucwords($account_type) . '</th></tr>';
                    } else {
                        $html .= '<tr><th>' . __('Select Payment Gateway', 'ym') . '</th></tr>';
                        $html .= '<tr><th>' . ym_get_pack_label($pack['id']) . '</th></tr>';
                    }
                }
                if (count($ym_active_modules)) {
                    $buttons_shown = array();
                    foreach ($ym_active_modules as $module) {
                        if ($module == 'ym_free' && $pack['cost'] > 0) {
                            continue;
                        }
                        $obj = new $module();
                        $string = $obj->getButton($pack['id']);
                        if ($string) {
                            $buttons_shown[] = $module;
                            $html .= $string;
                        }
                        $string = false;
                        $obj = null;
                    }
                    $html .= '</table>';
                    if (count($buttons_shown) == 1) {
                        $module = array_pop($buttons_shown);
                        // check that I'm allowed to auto fire
                        $check = new $module();
                        if (method_exists($check, 'abort_auto')) {
                            continue;
                        }
                        $form_code = '<div style="display:none;">' . $html . '</div>';
                        $js = 'document.forms["' . $module . '_form"].submit();';
                        $html = '  <html>
													<head>
														<title>Redirecting...</title>
														<script type="text/javascript">
															function load() {
																' . $js . '
															}
														</script>
													</head>
													<body onload="load();">';
                        $html .= '  <div style="color: #333333; font-size: 14px; margin: 30px 10px; font-family: tahoma; text-align: center; padding: 50px; border: 1px solid silver;">';
                        $html .= '  <div>' . __('You are being redirected. If this page does not refresh in 5 seconds then click', 'ym') . ' <a onclick="document.forms[\'' . $module . '_form\'].submit();">here</a>.</div>
											   <div style="margin-top: 10px;"><img alt="" src="' . YM_IMAGES_DIR_URL . 'loading.gif" /></div>';
                        $html .= '  </div>';
                        $html .= $form_code;
                        $html .= '	  </body>
												</html>';
                        echo $html;
                        die;
                    }
                } else {
                    $html .= '</table>';
                    $html .= __('There are no gateways available at this time.', 'ym');
                }
            }
        }
    }
    if ($return) {
        return $html;
    } else {
        echo $html;
    }
}
function ym_group_membership_create_child($email_address, $username, $password, $c_password, $sub_id, $package_type = false, $message = FALSE, $parent_id = FALSE)
{
    if ($parent_id) {
        $ym_user = new YourMember_User($parent_id);
    } else {
        global $ym_user;
    }
    $current_counts = ym_group_membership_get_counts($ym_user);
    if (count($ym_user->child_ids) >= $ym_user->child_accounts_allowed) {
        if ($message) {
            ym_display_message(__('You are out of Child Accounts', 'ym'), 'error');
        }
        return FALSE;
    } else {
        if ($email_address && is_email($email_address)) {
            if (!empty($password) && $password != $c_password) {
                ym_display_message(__('Passwords do not match', 'ym'), 'error');
            }
            if ($sub_id) {
                if (!in_array($sub_id, $ym_user->child_accounts_packages)) {
                    if ($message) {
                        ym_display_message(__('You do not have access to this pacakge', 'ym'), 'error');
                    }
                    return FALSE;
                }
                $pack = ym_get_pack_by_id($sub_id);
            } else {
                if ($package_type) {
                    $pack = array();
                    $pack['account_type'] = $package_type;
                } else {
                    // inherit mode
                    $pack = array();
                    $pack['account_type'] = $ym_user->account_type;
                }
            }
            $inherit = true;
            foreach ($ym_user->child_accounts_package_types as $type => $type_count) {
                if ($type_count) {
                    $inherit = false;
                }
            }
            //			if ($inherit) {
            //				$pack['account_type'] = '';
            //			}
            if ($pack['account_type'] && $ym_user->child_accounts_package_types[$pack['account_type']] > $current_counts[$pack['account_type']] || $inherit && $ym_user->child_accounts_allowed > count($ym_user->child_ids)) {
                $new_user = new YourMember_User();
                $result = $new_user->create($email_address, $sub_id, FALSE, $username, $password);
                if (is_wp_error($result)) {
                    ym_display_message($result->get_error_message(), 'error');
                } else {
                    // apply child
                    $data = array('parent_id' => $ym_user->ID, 'account_type' => $pack['account_type'], 'status_str' => __('Child Account', 'ym'));
                    if (!$sub_id) {
                        // the child has inherited they won't have a role!
                        $new_user->updaterole('subscriber');
                    }
                    $new_user->update($data);
                    $new_user->save();
                    unset($new_user);
                    //garbage collect
                    $child_ids = $ym_user->child_ids;
                    $child_ids[] = $result;
                    $ym_user->update(array('child_ids' => $child_ids));
                    $ym_user->save();
                    @ym_log_transaction(YM_ACCOUNT_TYPE_ASSIGNATION, __('Child', 'ym') . ' ' . $data['account_type'], $result);
                    @ym_log_transaction(YM_USER_STATUS_UPDATE, YM_STATUS_ACTIVE . ' - ' . $data['status_str'], $result);
                    // all done
                    if ($message) {
                        ym_display_message(__('Child User was created successfully', 'ym'));
                    }
                    return TRUE;
                }
            } else {
                if ($message) {
                    ym_display_message(__('Total for this package type has been reached', 'ym'), 'error');
                }
                return FALSE;
            }
        } else {
            if ($message) {
                ym_display_message(__('The Email Address was Blank or Invalid', 'ym'), 'error');
            }
            return FALSE;
        }
    }
}
Ejemplo n.º 14
0
 private function sync_packages($plan_id = FALSE)
 {
     if ($plan_id) {
         list($buy, $sub, $pack_id) = explode('_', $plan_id);
         $packages = array(ym_get_pack_by_id($pack_id));
     } else {
         global $ym_packs;
         $packages = $this->pack_filter($ym_packs->packs);
     }
     $r = TRUE;
     foreach ($packages as $pack) {
         $plan_id = 'buy_subscription_' . $pack['id'];
         if (strtolower($pack['duration_type']) == 'm') {
             $dt = 'month';
         } else {
             $dt = 'year';
         }
         list($r_code, $response) = $this->stripe_api_request('plans/' . $plan_id);
         $plan = array('id' => $plan_id, 'amount' => $pack['cost'] * 100, 'currency' => 'usd', 'interval' => $dt, 'name' => ym_get_pack_label($pack['id'], false, false));
         if ($pack['trial_on']) {
             // add free trial
             // not free causes additional charge on plan sub
             $days = 0;
             if ($pack['trial_duration_type'] == 'd') {
                 $days = $pack['trial_duration'];
             } else {
                 if ($pack['trial_duration_type'] == 'm') {
                     $days = $pack['trial_duration'] * 28;
                 } else {
                     $days = $pack['trial_duration'] * 365;
                 }
             }
             $plan['trial_period_days'] = $days;
         }
         if ($r_code == 404) {
             // create
             list($r_code, $response) = $this->stripe_api_request('plans', 'POST', $plan);
             if ($r_code != 200) {
                 $r = FALSE;
             }
         } else {
             if ($r_code == 200) {
                 // update?
                 $call_update = FALSE;
                 foreach ($plan as $key => $value) {
                     if ($response->{$key} != $value) {
                         $call_update = TRUE;
                     }
                 }
                 if ($call_update) {
                     // delete recreate
                     list($r_code, $response) = $this->stripe_api_request('plans/' . $plan_id, 'DELETE');
                     if ($r_code != 200) {
                         $r = FALSE;
                     } else {
                         list($r_code, $response) = $this->stripe_api_request('plans', 'POST', $plan);
                         if ($r_code != 200) {
                             $r = FALSE;
                         }
                     }
                 }
             } else {
                 $r = FALSE;
             }
         }
     }
     return $r;
 }
Ejemplo n.º 15
0
    function do_process()
    {
        $action = ym_request('action');
        if ($action == 'js') {
            header('Content-Type: text/javascript');
            ?>
jQuery(document).ready(function() {
	jQuery('.ym_braintree_button').click(function(event) {
		event.preventDefault();

		jQuery('.ym_form').slideUp();
		jQuery('#<?php 
            echo $this->code;
            ?>
_cc_form_unique_' + jQuery(this).attr('data-unique')).slideDown();
	});

	var braintree = Braintree.create("<?php 
            echo $this->encryptionkey;
            ?>
");
	jQuery('.<?php 
            echo $this->code;
            ?>
_cc_form').submit(function(e) {
		e.preventDefault();

		jQuery('.ym_braintree_icon').addClass('ym_ajax_loading_image');

		var target = jQuery(this);
		target.find('.error').remove();

		var data = jQuery(this).clone();
		data.find('#braintree_credit_card_number').val(braintree.encrypt(jQuery(this).find('#braintree_credit_card_number').val()));
		data.find('#braintree_credit_card_ccv').val(braintree.encrypt(jQuery(this).find('#braintree_credit_card_ccv').val()));
		data.find('#braintree_credit_card_exp').val(braintree.encrypt(jQuery(this).find('#braintree_credit_card_exp').val()));

		target.find('input').attr('disabled', 'disabled');

		jQuery.post('<?php 
            echo $this->action_url;
            ?>
&action=ajax', data.serialize(), function(resp) {
			jQuery('.ym_braintree_icon').removeClass('ym_ajax_loading_image');

			resp = jQuery.parseJSON(resp);
			if (resp['ok']) {
				target.find('#braintree_credit_card_number').val('');
				target.find('#braintree_credit_card_ccv').val('');
				target.find('#braintree_credit_card_exp').val('');

				jQuery('<div class="success"><p>' + resp['message'] + '</p></div>').prependTo(target);

				document.location = resp['url'];
			} else {
				target.find('input').removeAttr('disabled');
				jQuery('<div class="error"><p>' + resp['message'] + '</p></div>').prependTo(target);
			}
		});
	});
});
<?php 
            exit;
        } else {
            if ($action == 'ajax') {
                ob_start();
                $this->_braintree();
                // issue sale or subscribe
                $code = $_POST['code'];
                list($buy, $what, $pack_id, $user_id) = explode('_', $code);
                // credit card update
                $result = Braintree_Customer::update('ym_' . $user_id, array('creditCard' => array('number' => $_POST['customer']['credit_card']['number'], 'cvv' => $_POST['customer']['credit_card']['cvv'], 'expirationDate' => $_POST['customer']['credit_card']['expiration_date'])));
                if ($result->success) {
                    // grab token and subscribe
                    //				if ($pack['num_cycles'] == 1 || $planId) {
                    if ($what == 'subscription') {
                        // above catches both kinds of package/subscription
                        $pack = ym_get_pack_by_id($pack_id);
                        $planId = isset($pack['braintree_plan_id']) ? $pack['braintree_plan_id'] : false;
                        // initiate charge against just added credit card
                        if ($planId) {
                            $result = Braintree_Subscription::create(array('planId' => $planId, 'paymentMethodToken' => $result->customer->creditCards[0]->token));
                            $amount = $result->subscription->transactions[0]->amount;
                        } else {
                            $result = Braintree_Transaction::sale(array('amount' => $pack['cost'], 'options' => array('submitForSettlement' => true), 'customerId' => $result->customer->id, 'paymentMethodToken' => $result->customer->creditCards[0]->token));
                            $amount = $result->transaction->amount;
                        }
                        if ($result->success) {
                            // common
                            $this->common_process($code, $amount, true, false);
                            // thanks
                            $url = $this->redirectlogic($pack);
                            $r = array('ok' => true, 'url' => $url, 'message' => __('Payment Complete', 'ym'));
                        } else {
                            $r = $this->_failedBraintree($result, true);
                        }
                    } else {
                        if ($what == 'bundle' || $what == 'post') {
                            // post or bundle purchase
                            if ($what == 'post') {
                                $cost = get_post_meta($pack_id, '_ym_post_purchasable_cost', true);
                            } else {
                                $bundle = ym_get_bundle($pack_id);
                                if (!$bundle) {
                                    $r = array('ok' => false, 'message' => __('Bundle Error', 'ym'));
                                } else {
                                    $cost = $bundle->cost;
                                }
                            }
                            if ($cost) {
                                $result = Braintree_Transaction::sale(array('amount' => $cost, 'options' => array('submitForSettlement' => true), 'customerId' => $result->customer->id, 'paymentMethodToken' => $result->customer->creditCards[0]->token));
                                $amount = $result->transaction->amount;
                                if ($result->success) {
                                    // common
                                    $this->common_process($code, $amount, true, false);
                                    // thanks
                                    if ($what == 'subscription') {
                                        $url = $this->redirectlogic($pack);
                                    } else {
                                        if ($what == 'post') {
                                            $url = $this->redirectlogic(array('ppp' => true, 'post_id' => $pack_id));
                                        } else {
                                            $url = $this->redirectlogic(array('ppp' => true, 'ppp_pack_id' => $pack_id));
                                        }
                                    }
                                    $r = array('ok' => true, 'url' => $url, 'message' => __('Payment Complete', 'ym'));
                                } else {
                                    $r = $this->_failedBraintree($result, true);
                                }
                            }
                        } else {
                            // unhandled purchase
                            $r = $this->_failedBraintree($result, true);
                        }
                    }
                } else {
                    $r = $this->_failedBraintree($result, true);
                }
                ob_clean();
                echo json_encode($r);
                // bugger
                exit;
                // non ajax/primary js failed
                // transparent redirect handlers
            } else {
                if ($action == 'process') {
                    $this->_braintree();
                    $queryString = $_SERVER['QUERY_STRING'];
                    try {
                        $result = Braintree_TransparentRedirect::confirm($queryString);
                    } catch (Exception $e) {
                        if (get_class($e) == 'Braintree_Exception_NotFound') {
                            echo 'not found';
                        } else {
                            echo '<pre>';
                            print_r($e);
                            echo $e->getMessage();
                        }
                        exit;
                    }
                    if ($result->success) {
                        $code = ym_request('code');
                        // grab token and subscribe
                        list($buy, $what, $pack_id, $user_id) = explode('_', $code);
                        $pack = ym_get_pack_by_id($pack_id);
                        $planId = isset($pack['braintree_plan_id']) ? $pack['braintree_plan_id'] : false;
                        if ($pack['num_cycles'] == 1 || $planId) {
                            // initiate charge against just added credit card
                            if ($planId) {
                                $result = Braintree_Subscription::create(array('planId' => $planId, 'paymentMethodToken' => $result->customer->creditCards[0]->token));
                                $amount = $result->subscription->transactions[0]->amount;
                            } else {
                                $result = Braintree_Transaction::sale(array('amount' => $pack['cost'], 'options' => array('submitForSettlement' => true), 'customerId' => $result->customer->id, 'paymentMethodToken' => $result->customer->creditCards[0]->token));
                                $amount = $result->transaction->amount;
                            }
                            if ($result->success) {
                                // common
                                $this->common_process($code, $amount, true, false);
                                // thanks
                                $this->redirectlogic($pack, true);
                                exit;
                            } else {
                                $this->_failedBraintree($result);
                            }
                        } else {
                            $this->_failedBraintree($result);
                        }
                        exit;
                    }
                    $this->_failedBraintree($result);
                } else {
                    $this->_failedBraintree();
                }
            }
        }
    }