function ym_debug($data = false)
{
    if (ym_debug_mode_check()) {
        $html = '<div style="padding: 2px; margin: 2px; border: 1px solid black; background-color: white;">
					<!-- <strong>Backtrace: </strong>
						<pre>' . print_r(debug_backtrace(), true) . '</pre> -->
					<strong>Data: </strong>
						<pre>' . (is_array($data) || is_object($data) ? print_r($data, true) : $data) . '</pre>
				</div>';
        if ($_SESSION['ym_debug_mode_method'] == 'echo') {
            echo $html;
        } else {
            ym_email(YM_DEBUG_EMAIL, 'Debug Email' . ($where_from ? ' from ' . $where_from : ''), $html);
        }
    }
}
Example #2
0
 function verify_callback()
 {
     @set_time_limit(60);
     $admin = get_userdata(1);
     $admin_email = $admin->user_email;
     if (!$this->bypass_paypal_ipn_verfication) {
         $req = 'cmd=_notify-validate';
         $headers = array();
         $domain = 'https://www.sandbox.paypal.com';
         if ($this->status == 'live') {
             $domain = 'https://www.paypal.com';
         }
         //          $domain = str_replace('https://', '', $domain);
         foreach ($_POST as $key => $value) {
             if (get_magic_quotes_gpc()) {
                 $value = stripslashes($value);
             }
             $req .= '&' . $key . '=' . $value;
         }
         $headers['Content-Type:'] = 'application/x-www-form-urlencoded\\r\\n';
         $headers['Content-length:'] = 'Content-length: ' . strlen($req) . '\\r\\n';
         $request = new WP_Http();
         $response = $request->request($domain, array('method' => 'POST', 'body' => $req, 'headers' => $headers));
         //          if (is_wp_error($response)) {
         //              echo $response->get_error_message();
         //          }
         if (is_wp_error($response) || FALSE === strpos('VERIFIED', $response['body'])) {
             //falls back to IP method. This will cut down the number of cases whereby turning on the override is necessary
             if (!$this->verify_paypal_ipn_ip($_SERVER['REMOTE_ADDR'])) {
                 ym_email($admin_email, 'callback failed', "sent a request to host: '" . $domain . "'. \n\n <br />response was: \n\n <br />" . print_r($response, TRUE) . "\n\n <br />post vars: <br /><pre>\n" . print_r($_POST, true)) . '</pre>';
                 header('HTTP/1.1 400 Bad Request');
                 die;
             }
         }
     } else {
         if (!$this->verify_paypal_ipn_ip($_SERVER['REMOTE_ADDR'])) {
             ym_email($admin_email, 'callback failed, could not verify request IP', "Client at " . $_SERVER['REMOTE_ADDR'] . " attempted to send an IPN. IP was not in known Paypal addresses and therefore the request was blocked.<br />< br/>post vars: <br /><pre>\n" . print_r($_POST, true)) . '</pre>';
             header('HTTP/1.1 400 Bad Request');
             die;
         }
     }
 }
 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');
     }
 }
Example #4
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;
 }
<?php

global $ym_formgen, $ym_res, $ym_sys, $wpdb, $allowed_extensions;
global $current_user;
get_currentuserinfo();
$target = $current_user->user_email;
if (isset($_POST['send_test']) && !empty($_POST['send_test'])) {
    $ym_sys->email_reminder_subject = str_replace('[site_name]', get_bloginfo(), $ym_sys->email_reminder_subject);
    $message = ym_apply_filter_the_content($ym_sys->email_reminder_message);
    ym_email($target, $ym_sys->email_reminder_subject, $message);
    ym_display_message(__('Sent test Email Reminder test email to you', 'ym'));
}
if (isset($_POST['send_test_recur']) && !empty($_POST['send_test_recur'])) {
    $ym_sys->email_reminder_subject_recur = str_replace('[site_name]', get_bloginfo(), $ym_sys->email_reminder_subject_recur);
    $message = ym_apply_filter_the_content($ym_sys->email_reminder_message_recur);
    ym_email($target, $ym_sys->email_reminder_subject_recur, $message);
    ym_display_message(__('Sent test Email Reminder test email to you', 'ym'));
}
if (isset($_POST['settings_update']) && !empty($_POST['settings_update'])) {
    $ym_sys->update_from_post();
    update_option('ym_sys', $ym_sys);
    ym_display_message(__('System Updated', 'ym'));
}
echo '<div class="wrap" id="poststuff">';
echo ym_box_top(__('Email Configuration', 'ym'));
echo '<p>' . sprintf(__('These from settings will also be used for all YM initiated emails, instead of the WordPress default, normally wordpress@%s', 'ym'), $_SERVER['HTTP_HOST']) . '</p>';
echo '<form action="" method="post">
<table class="form-table">';
$ym_formgen->render_form_table_text_row(__('From Name', 'ym'), 'from_name', $ym_sys->from_name, __('Name of the sender (you or your site&#39;s name)', 'ym'));
$ym_formgen->render_form_table_text_row(__('From Email', 'ym'), 'from_email', $ym_sys->from_email, __('The email address where site emails will come from', 'ym'));
$ym_formgen->render_form_table_radio_row(__('All Emails', 'ym'), 'filter_all_emails', $ym_sys->filter_all_emails, __('Apply these filters to <strong>ALL</strong> Emails WordPress Sends', 'ym'));
 function notify_user($packet, $nomore = false)
 {
     if (isset($this->nomore_email) && $this->nomore_email) {
         return;
     }
     $this->nomore_email = $nomore;
     global $ym_res;
     $user = get_userdata($packet['user_id']);
     $message_id = '';
     // make sure its an int
     if ($packet['status']) {
         $packet['status'] = 1;
     } else {
         $packet['status'] = 0;
     }
     if (isset($packet['post_id']) && $packet['post_id']) {
         $message_id = 'post_' . $packet['status'];
     } else {
         if (isset($packet['ppack_id']) && $packet['ppack_id']) {
             $message_id = 'ppack_' . $packet['status'];
         } else {
             $message_id = 'pack_' . $packet['status'];
         }
     }
     $target = $subject = $message = $target_scan = '';
     $do = FALSE;
     if (method_exists($this, 'messages')) {
         // if data returned then send email here
         // otherwise no send or payment gateway handles
         // data returns message to send
         $data = $this->messages($message_id, $user, $packet);
         if ($data) {
             $target = $data['to'];
             $subject = $data['subject'];
             $message = $data['message'];
             $target_scan = $data['target_scan'];
             $do = TRUE;
         }
     } else {
         $target = $user->display_name . ' <' . $user->user_email . '>';
         $display_name = $user->display_name;
         // use default messages
         switch ($message_id) {
             case 'post_1':
                 if ($ym_res->payment_gateway_enable_post_success) {
                     $do = TRUE;
                 }
                 $target_scan = 'payment_gateway_email_post_success';
                 $posttitle = get_the_title($packet['post_id']);
                 $posttitle = strip_tags($posttitle);
                 $postlink = get_permalink($packet['post_id']);
                 $subject = $ym_res->payment_gateway_subject_post_success;
                 $subject = str_replace('[blogname]', get_option('blogname'), $subject);
                 $subject = str_replace('[post_title]', $posttitle, $subject);
                 $message = $ym_res->payment_gateway_message_post_success;
                 $message = str_replace('[display_name]', $display_name, $message);
                 $message = str_replace('[post_title]', $posttitle, $message);
                 $message = str_replace('[post_link]', $postlink, $message);
                 $message = str_replace('[blogname]', get_option('blogname'), $message);
                 break;
             case 'post_0':
                 if ($ym_res->payment_gateway_enable_post_failed) {
                     $do = TRUE;
                 }
                 $target_scan = 'payment_gateway_email_post_failed';
                 $posttitle = get_the_title($packet['post_id']);
                 $posttitle = strip_tags($posttitle);
                 $subject = $ym_res->payment_gateway_subject_post_failed;
                 $subject = str_replace('[blogname]', get_option('blogname'), $subject);
                 $subject = str_replace('[post_title]', $posttitle, $subject);
                 $message = $ym_res->payment_gateway_message_post_failed;
                 $message = str_replace('[display_name]', $display_name, $message);
                 $message = str_replace('[post_title]', $posttitle, $message);
                 $message = str_replace('[blogname]', get_option('blogname'), $message);
                 break;
             case 'ppack_1':
                 if ($ym_res->payment_gateway_enable_ppack_success) {
                     $do = TRUE;
                 }
                 $target_scan = 'payment_gateway_email_ppack_success';
                 $pack_data = ym_get_bundle($packet['ppack_id']);
                 $posts = ym_get_bundle_posts($packet['ppack_id']);
                 $post_urls = '';
                 foreach ($posts as $post) {
                     $post_urls .= '<a href="' . get_permalink($post->post_id) . '">' . get_the_title($post->post_id) . '</a><br />';
                 }
                 $subject = $ym_res->payment_gateway_subject_ppack_success;
                 $subject = str_replace('[blogname]', get_option('blogname'), $subject);
                 $subject = str_replace('[pack_title]', $pack_data->name, $subject);
                 $message = $ym_res->payment_gateway_message_ppack_success;
                 $message = str_replace('[display_name]', $display_name, $message);
                 $message = str_replace('[pack_name]', $pack_data->name, $message);
                 $message = str_replace('[posts_in_pack]', $post_urls, $message);
                 $message = str_replace('[blogname]', get_option('blogname'), $message);
                 break;
             case 'ppack_0':
                 if ($ym_res->payment_gateway_enable_ppack_failed) {
                     $do = TRUE;
                 }
                 $target_scan = 'payment_gateway_email_ppack_failed';
                 $pack_data = ym_get_bundle($packet['ppack_id']);
                 $subject = $ym_res->payment_gateway_subject_ppack_failed;
                 $subject = str_replace('[blogname]', get_option('blogname'), $subject);
                 $subject = str_replace('[pack_title]', $pack_data->name, $subject);
                 $message = $ym_res->payment_gateway_message_ppack_failed;
                 $message = str_replace('[display_name]', $display_name, $message);
                 $message = str_replace('[pack_name]', $pack_data->name, $message);
                 $message = str_replace('[blogname]', get_option('blogname'), $message);
                 break;
             case 'pack_1':
                 if ($ym_res->payment_gateway_enable_subscription_success) {
                     $do = TRUE;
                 }
                 $target_scan = 'payment_gateway_email_subscription_success';
                 $label = ym_get_pack_label($packet['pack_id']);
                 $label = strip_tags($label);
                 $user = new YourMember_User($packet['user_id']);
                 $expire = $user->expire_date;
                 $f = YM_DATE;
                 $expire = date($f, $expire);
                 $subject = $ym_res->payment_gateway_subject_subscription_success;
                 $subject = str_replace('[blogname]', get_option('blogname'), $subject);
                 $subject = str_replace('[pack_label]', $label, $subject);
                 $message = $ym_res->payment_gateway_message_subscription_success;
                 $message = str_replace('[display_name]', $display_name, $message);
                 $message = str_replace('[pack_label]', $label, $message);
                 $message = str_replace('[pack_expire]', $expire, $message);
                 $message = str_replace('[blogname]', get_option('blogname'), $message);
                 break;
             case 'pack_0':
                 if ($ym_res->payment_gateway_enable_subscription_failed) {
                     $do = TRUE;
                 }
                 $target_scan = 'payment_gateway_email_subscription_failed';
                 $label = ym_get_pack_label($packet['pack_id']);
                 $label = strip_tags($label);
                 $subject = $ym_res->payment_gateway_subject_subscription_failed;
                 $subject = str_replace('[blogname]', get_option('blogname'), $subject);
                 $subject = str_replace('[pack_label]', $label, $subject);
                 $message = $ym_res->payment_gateway_message_subscription_failed;
                 $message = str_replace('[display_name]', $display_name, $message);
                 $message = str_replace('[pack_label]', $label, $message);
                 $message = str_replace('[blogname]', get_option('blogname'), $message);
                 break;
         }
     }
     remove_all_shortcodes();
     // login user to alow shortcodes to behave
     wp_set_current_user($packet['user_id']);
     // apply ym_user_custom shortcode
     add_shortcode('ym_user_is', 'ym_user_is');
     add_shortcode('ym_user_is_not', 'ym_user_is_not');
     add_shortcode('ym_user_custom', 'ym_shortcode_user');
     $message = do_shortcode($message);
     // transaction log ID
     global $ym_this_transaction_id;
     $log_id = 'YMTRANS_' . $ym_this_transaction_id;
     $message = str_replace('[ym_log_id]', $log_id, $message);
     $message_prepend = __('This is a copy of a Message not sent', 'ym');
     if ($do) {
         ym_email($target, $subject, $message);
         $message_prepend = __('This is a copy of a Message sent', 'ym');
     }
     // additional targets?
     if (is_array($ym_res->{$target_scan})) {
         $subject = 'CC: ' . $subject;
         $message = $message_prepend . '<br /><br />' . $message;
         foreach ($ym_res->{$target_scan} as $target) {
             if ($target) {
                 ym_email($target, $subject, $message);
             }
         }
     }
 }
Example #7
0
 function do_process()
 {
     // IPN Handler
     echo __('One Moment... Processing', 'ym');
     if (!ym_get('msgType') || !ym_get('installation') || !ym_post('M_item_number')) {
         header('HTTP/1.1 400 Bad Request');
         echo 'Error in IPN. Missing msgType, installation or item_number.';
         exit;
     }
     if (ym_get('installation') != ym_post('installation') || ym_post('installation') != $this->inst_id) {
         header('HTTP/1.1 401 Unauthorized');
         echo 'Error in IPN. Missing installation ID or Invalid';
         exit;
     }
     if ($this->callbackPW && ym_post('callbackPW') != $this->callbackPW) {
         header('HTTP/1.1 401 Unauthorized');
         echo 'Error in IPN. Missing callbackPW or invalid.';
         exit;
     }
     $array = array('Merchant Cancelled', 'Customer Cancelled');
     if (ym_post('futurePayStatusChange') && in_array(ym_post('futurePayStatusChange'), $array)) {
         // expired!
         $this->common_process(ym_post('M_item_number'), ym_post('cost'), FALSE, FALSE);
     }
     if (ym_post('rawAuthCode') == 'A') {
         // && ym_post('rawAuthMessage') == 'authorised') {
         // pre 11 catch
         if (isset($_POST['M_custom'])) {
             // is pre
             list($duration, $amount, $currency, $user_id, $account_type, $duration_type) = explode('_', $_POST['M_custom']);
             global $ym_packs;
             foreach ($ym_packs->packs as $pack) {
                 if ((md5($pack['account_type']) == strtolower($account_type) || md5(strtolower($pack['account_type'])) == strtolower($account_type)) && $pack['cost'] == $amount && $pack['duration'] == $duration && strtolower($pack['duration_type']) == strtolower($duration_type)) {
                     $pack_id = $pack['id'];
                     break;
                 }
             }
             if ($pack_id) {
                 $item = 'buy_subscription_' . $pack_id . '_' . $test[3];
             } else {
                 $admin = get_userdata(1);
                 $admin_email = $admin->user_email;
                 ym_email($admin_email, 'YM 10 Packet failed', 'Could not determine what the user is buying after looping thru all packets Debug: <pre>' . print_r($_POST, TRUE)) . "\n\n\n" . print_r($ym_packs, TRUE) . '</pre>';
                 header('HTTP/1.1 400 Bad Request');
                 exit;
             }
         } else {
             $item = ym_post('M_item_number');
         }
         // success
         $this->common_process($item, ym_post('cost'), TRUE, FALSE);
         $url = ym_post('M_return');
         if (ym_post('futurePayId')) {
             list($buy, $what, $id, $user_id) = explode('_', $item);
             update_user_meta($user_id, 'ym_worldpay_futurepay_id', ym_post('futurePayId'));
         }
     } else {
         // must be C - cancelled payment
         // where go?
         if (isset($this->cancel_url) && $this->cancel_url) {
             $url = site_url($this->cancel_url);
         } else {
             $url = site_url('/');
         }
     }
     echo '<meta http-equiv="refresh" content="0;' . $url . '" />';
     exit;
 }
Example #8
0
 function do_process()
 {
     global $ym_sys;
     $mode = $this->status == 'test' ? TRUE : FALSE;
     $gc = new GoCardless($this->merchant_id, $this->application_id, $this->application_secret, $this->access_token, $mode, $this->magical_word);
     if (!$gc->valid) {
         echo 'An Error Occured. Please contact Site Admin: Invalid Keys';
         exit;
     }
     $action = ym_request('action');
     if ($action == 'go') {
         // redirecting to gocardless
         $pack = $_POST;
         if (isset($pack['num_cycles']) && $pack['num_cycles'] != 1) {
             // subscription
             // convert pack values to something gocardless can understand
             switch ($pack['duration_type']) {
                 case 'y':
                     // convert to month
                     $interval_unit = 'month';
                     $interval_length = $pack['duration'] * 12;
                     $expire_length = $interval_length * $pack['num_cycles'];
                     $expire = mktime(23, 59, 59, date('n', time()) + $expire_length, date('j', time()), date('Y', time()));
                     break;
                 case 'm':
                     $interval_unit = 'month';
                     $interval_length = $pack['duration'];
                     $expire_length = $interval_length * $pack['num_cycles'];
                     $expire = mktime(23, 59, 59, date('n', time()) + $expire_length, date('j', time()), date('Y', time()));
                     break;
                 case 'd':
                     $interval_unit = 'day';
                     $interval_length = $pack['duration'];
                     $expire_length = $interval_length * $pack['num_cycles'];
                     $expire = mktime(23, 59, 59, date('n', time()), date('j', time()) + $expire_length, date('Y', time()));
             }
             $subscription = array('amount' => number_format($pack['cost'], 2), 'interval_length' => $interval_length, 'interval_unit' => $interval_unit, 'merchant_id' => $this->merchant_id, 'name' => get_bloginfo() . ' ' . __('Subscription', 'ym'), 'description' => isset($pack['item_name']) && $pack['item_name'] ? $pack['item_name'] : $ym_sys->item_name);
             if ($subscription['name'] == $subscription['description']) {
                 unset($subscription['description']);
             }
             if ($pack['num_cycles'] > 1) {
                 $subscription['expire'] = date('c', $expire);
             }
         } else {
             // single purchase
             // bill
             $bill = array('amount' => number_format($pack['cost'], 2), 'merchant_id' => $this->merchant_id, 'name' => get_bloginfo() . ' ' . __('Purchase', 'ym'), 'description' => isset($pack['item_name']) && $pack['item_name'] ? $pack['item_name'] : $ym_sys->item_name);
         }
         $data = array();
         // common fields
         $data['redirect_uri'] = site_url('?ym_process=' . $this->code . '&action=confirm');
         $data['cancel_uri'] = site_url($this->cancel_url);
         // state AKA custom
         if (isset($pack['ppp_pack_id'])) {
             $data['state'] = 'buy_bundle_' . $pack['ppp_pack_id'] . '_' . $pack['user_id'];
         } else {
             if (isset($pack['post_id'])) {
                 $data['state'] = 'buy_post_' . ($pack['post_id'] ? $pack['post_id'] : get_the_ID()) . '_' . $pack['user_id'];
             } else {
                 $data['state'] = 'buy_subscription_' . $pack['id'] . '_' . $pack['user_id'];
             }
         }
         // user fields
         $user = array();
         if ($first = get_user_meta($pack['user_id'], 'first_name', true)) {
             $user['first_name'] = $first;
         }
         if ($last = get_user_meta($pack['user_id'], 'last_name', true)) {
             $user['last_name'] = $last;
         }
         $user['email'] = get_user_by('id', $pack['user_id']);
         $user['email'] = $user['email']->user_email;
         // generate and go to URL
         if (isset($bill)) {
             $gc->NewPayment($bill, $data, $user);
         } else {
             echo 'sub';
             $gc->NewSubscription($subscription, $data, $user);
         }
         exit;
     }
     if ($action == 'confirm') {
         // perform confirm and redirect
         $state = ym_get('state');
         if (!$state) {
             header('HTTP/1.1 400 Bad Request');
             echo 'Missing State';
             exit;
         }
         $r = $gc->catchReturn();
         if ($r) {
             // update the user and set then to pending or grace
             // cost is 0 as no money yet
             // deny receipt email
             $this->nomore_email = TRUE;
             // process
             $this->common_process($state, '0', FALSE, FALSE);
             // technically true and Don't exit
             list($buy, $what, $id, $user_id) = explode('_', $state);
             // we need to store the bill/subscription ID in order to track the user
             // state is not returned with webhooks
             $key = ym_get('resource_id');
             $data = array('state' => $state, 'user_id' => $user_id, 'amount' => ym_get('amount'));
             update_option('ym_gocardless_states_' . $key, $data);
             if ($what == 'post') {
                 $pack = array('ppp' => 1, 'post_id' => $id);
             } else {
                 if ($what == 'bundle') {
                     $pack = array('ppp' => 1, 'ppp_pack_id' => $id);
                 } else {
                     // subscriptiom
                     update_user_meta('ym_gocardless_active_subscription', $key, $user_id);
                     $pack = $id;
                 }
             }
             $this->redirectlogic($pack, TRUE);
         } else {
             echo 'An Error Occured, you should contact the Site Admin';
             exit;
         }
     }
     // assume webhook
     $data = $gc->catchWebHook();
     if (!$data) {
         header('HTTP/1.1 403 Unauthorised');
         echo 'Signature Invalid';
         exit;
     } else {
         // post or sub?
         // status
         // created failed paid cancelled expired withdrawn
         // abort cases
         // widthdrawn jsut means money has moved from the GC account to the merchant account.
         $aborts = array('created', 'withdrawn');
         if (in_array($data['action'], $aborts)) {
             // ignore created packets
             header('HTTP/1.1 200 OK');
             echo 'ok';
             exit;
         }
         $success_states = array('paid');
         $failed_states = array('failed', 'cancelled', 'expired');
         foreach ($data['resources'] as $packet) {
             $id = $packet->id;
             $status = $packet->status;
             $uri = $packet->uri;
             $source_type = isset($packet->source_type) ? $packet->source_type : '';
             if ($source_type == 'subscription') {
                 $id = $packet->source_id;
             }
             $state_data = get_option('ym_gocardless_states_' . $id, FALSE);
             if ($state_data) {
                 // packet found
                 $state = $state_data['state'];
                 $user_id = $state_data['user_id'];
                 $amount = $state_data['amount'];
                 // store for trans log
                 $_POST = $state_data;
                 $complete = FALSE;
                 if (in_array($status, $success_states)) {
                     $complete = TRUE;
                 }
                 $this->common_process($state, $amount, $complete, FALSE);
             } else {
                 $admin = get_userdata(1);
                 $admin_email = $admin->user_email;
                 ym_email($admin_email, 'GC PAYLOAD STATE FAIL', print_r($packet, TRUE));
             }
         }
         exit;
     }
 }