| 1 |
<?php |
| 2 |
// @codingStandardsIgnoreFile |
| 3 |
/** |
| 4 |
* s2Member's PayPal Auto-Return/PDT handler (inner processing routine). |
| 5 |
* |
| 6 |
* Copyright: © 2009-2011 |
| 7 |
* {@link http://websharks-inc.com/ WebSharks, Inc.} |
| 8 |
* (coded in the USA) |
| 9 |
* |
| 10 |
* Released under the terms of the GNU General Public License. |
| 11 |
* You should have received a copy of the GNU General Public License, |
| 12 |
* along with this software. In the main directory, see: /licensing/ |
| 13 |
* If not, see: {@link http://www.gnu.org/licenses/}. |
| 14 |
* |
| 15 |
* @package s2Member\PayPal |
| 16 |
* @since 110720 |
| 17 |
*/ |
| 18 |
if(!defined('WPINC')) // MUST have WordPress. |
| 19 |
exit('Do not access this file directly.'); |
| 20 |
|
| 21 |
if(!class_exists('c_ws_plugin__s2member_paypal_return_in_subscr_modify_w_level')) |
| 22 |
{ |
| 23 |
/** |
| 24 |
* s2Member's PayPal Auto-Return/PDT handler (inner processing routine). |
| 25 |
* |
| 26 |
* @package s2Member\PayPal |
| 27 |
* @since 110720 |
| 28 |
*/ |
| 29 |
class c_ws_plugin__s2member_paypal_return_in_subscr_modify_w_level |
| 30 |
{ |
| 31 |
/** |
| 32 |
* s2Member's PayPal Auto-Return/PDT handler (inner processing routine). |
| 33 |
* |
| 34 |
* @package s2Member\PayPal |
| 35 |
* @since 110720 |
| 36 |
* |
| 37 |
* @param array $vars Required. An array of defined variables passed by {@link s2Member\PayPal\c_ws_plugin__s2member_paypal_return_in::paypal_return()}. |
| 38 |
* |
| 39 |
* @return array|bool The original ``$paypal`` array passed in (extracted) from ``$vars``, or false when conditions do NOT apply. |
| 40 |
*/ |
| 41 |
public static function cp($vars = array() /* Conditional phase for ``c_ws_plugin__s2member_paypal_notify_in::paypal_notify()``. */) |
| 42 |
{ |
| 43 |
extract($vars, EXTR_OVERWRITE | EXTR_REFS /* Extract all vars passed in from: ``c_ws_plugin__s2member_paypal_notify_in::paypal_notify()``. */); |
| 44 |
|
| 45 |
if((!empty($paypal['txn_type']) && preg_match('/^subscr_modify$/i', $paypal['txn_type'])) |
| 46 |
&& (!empty($paypal['item_number']) && preg_match($GLOBALS['WS_PLUGIN__']['s2member']['c']['membership_item_number_w_level_regex'], $paypal['item_number'])) |
| 47 |
&& (!empty($paypal['subscr_id'])) && (!empty($paypal['subscr_baid']) || ($paypal['subscr_baid'] = $paypal['subscr_id'])) |
| 48 |
&& (!empty($paypal['subscr_cid']) || ($paypal['subscr_cid'] = $paypal['subscr_id'])) |
| 49 |
) |
| 50 |
{ |
| 51 |
foreach(array_keys(get_defined_vars()) as $__v) $__refs[$__v] =& $$__v; |
| 52 |
do_action('ws_plugin__s2member_during_paypal_return_before_subscr_modify', get_defined_vars()); |
| 53 |
unset($__refs, $__v); |
| 54 |
|
| 55 |
if(!get_transient($transient_rtn = 's2m_rtn_'.md5('s2member_transient_'.$_paypal_s)) && set_transient($transient_rtn, time(), 31556926 * 10)) |
| 56 |
{ |
| 57 |
$paypal['s2member_log'][] = 's2Member `txn_type` identified as ( `subscr_modify` ), a Subscription Modification.'; |
| 58 |
|
| 59 |
//260814 Split against the full three-part membership grammar so an EOT period cannot be folded into CCaps. |
| 60 |
list($paypal['level'], $paypal['ccaps']/*, $paypal['eotper'] */) = array_pad(explode(':', $paypal['item_number'], 3), 2, ''); |
| 61 |
//260814 Set optional gateway elements used below so sparse/null payload fields do not trigger PHP diagnostics. |
| 62 |
$paypal = c_ws_plugin__s2member_utils_arrays::set_unset_elements($paypal, array('option_name2', 'option_selection2', 'invoice', 'first_name', 'last_name')); |
| 63 |
|
| 64 |
$paypal['ip'] = (preg_match('/ip address/i', $paypal['option_name2']) && $paypal['option_selection2']) ? $paypal['option_selection2'] : ''; |
| 65 |
$paypal['ip'] = (!$paypal['ip'] && preg_match('/^[a-z0-9]+~[0-9\.]+$/i', $paypal['invoice'])) ? preg_replace('/^[a-z0-9]+~/i', '', $paypal['invoice']) : $paypal['ip']; |
| 66 |
$paypal['ip'] = (!$paypal['ip'] && c_ws_plugin__s2member_utils_ip::current()) ? c_ws_plugin__s2member_utils_ip::current() : $paypal['ip']; |
| 67 |
|
| 68 |
$paypal['period1'] = (preg_match('/^[1-9]/', $paypal['period1'])) ? $paypal['period1'] : '0 D'; // Defaults to '0 D' (zero days). |
| 69 |
$paypal['mc_amount1'] = (strlen($paypal['mc_amount1']) && $paypal['mc_amount1'] > 0) ? $paypal['mc_amount1'] : '0.00'; |
| 70 |
|
| 71 |
if(preg_match('/^web_accept$/i', $paypal['txn_type']) /* Conversions for Lifetime & Fixed-Term sales. */) |
| 72 |
{ |
| 73 |
$paypal['period3'] = !empty($paypal['eotper']) ? $paypal['eotper'] : '1 L'; // 1 Lifetime. |
| 74 |
$paypal['mc_amount3'] = $paypal['mc_gross']; // The 'Buy Now' amount is the full gross. |
| 75 |
} |
| 76 |
$paypal['initial_term'] = (preg_match('/^[1-9]/', $paypal['period1'])) ? $paypal['period1'] : '0 D'; // Defaults to '0 D' (zero days). |
| 77 |
$paypal['initial'] = (strlen($paypal['mc_amount1']) && preg_match('/^[1-9]/', $paypal['period1'])) ? $paypal['mc_amount1'] : $paypal['mc_amount3']; |
| 78 |
$paypal['regular'] = $paypal['mc_amount3']; // This is the Regular Payment Amount that is charged to the Customer. Always required by PayPal. |
| 79 |
$paypal['regular_term'] = $paypal['period3']; // This is just set to keep a standard; this way both initial_term & regular_term are available. |
| 80 |
$paypal['recurring'] = ($paypal['recurring']) ? $paypal['mc_amount3'] : '0'; // If non-recurring, this should be zero, otherwise Regular. |
| 81 |
$paypal['currency'] = strtoupper($paypal['mc_currency']); // Normalize input currency. |
| 82 |
$paypal['currency_symbol'] = c_ws_plugin__s2member_utils_cur::symbol($paypal['currency']); |
| 83 |
|
| 84 |
$ipn_signup_vars = $paypal; // Copy of PayPal vars; used as IPN signup vars. |
| 85 |
unset($ipn_signup_vars['s2member_log']); // Create array of wouldbe IPN signup vars w/o s2member_log. |
| 86 |
|
| 87 |
if(($user_id = c_ws_plugin__s2member_utils_users::get_user_id_with($paypal['subscr_id'])) && is_object($user = new WP_User($user_id)) && $user->ID) |
| 88 |
{ |
| 89 |
if(!$user->has_cap('administrator') /* Do NOT process this routine on Administrators. */) |
| 90 |
{ |
| 91 |
$processing = $modifying = $during = TRUE; // Yes, we ARE processing this. |
| 92 |
|
| 93 |
foreach(array_keys(get_defined_vars()) as $__v) $__refs[$__v] =& $$__v; |
| 94 |
do_action('ws_plugin__s2member_during_paypal_return_during_before_subscr_modify', get_defined_vars()); |
| 95 |
do_action('ws_plugin__s2member_during_collective_mods', $user_id, get_defined_vars(), 'rtn-upgrade-downgrade', 'modification', 's2member_level'.$paypal['level']); |
| 96 |
unset($__refs, $__v); |
| 97 |
|
| 98 |
$fields = get_user_option('s2member_custom_fields', $user_id); // These will be needed in the routines below. |
| 99 |
$user_reg_ip = get_user_option('s2member_registration_ip', $user_id); // Original IP during Registration. |
| 100 |
$user_reg_ip = $paypal['ip'] = ($user_reg_ip) ? $user_reg_ip : $paypal['ip']; // Now merge conditionally. |
| 101 |
|
| 102 |
if(is_multisite() && !is_user_member_of_blog($user_id) /* Must have a Role on this Blog. */) |
| 103 |
{ |
| 104 |
add_existing_user_to_blog(array('user_id' => $user_id, 'role' => 's2member_level'.$paypal['level'])); |
| 105 |
$user = new WP_User($user_id); // Now update the $user object we're using. |
| 106 |
} |
| 107 |
$current_role = c_ws_plugin__s2member_user_access::user_access_role($user); |
| 108 |
|
| 109 |
if($current_role !== 's2member_level'.$paypal['level'] /* Only if we need to. */) |
| 110 |
$user->set_role('s2member_level'.$paypal['level']); // Upgrade/downgrade. |
| 111 |
|
| 112 |
if($paypal['ccaps'] && preg_match('/^-all/', str_replace('+', '', $paypal['ccaps']))) |
| 113 |
foreach($user->allcaps as $cap => $cap_enabled) |
| 114 |
if(preg_match('/^access_s2member_ccap_/', $cap)) |
| 115 |
$user->remove_cap($ccap = $cap); |
| 116 |
|
| 117 |
if($paypal['ccaps'] && preg_replace('/^-all['."\r\n\t".'\s;,]*/', '', str_replace('+', '', $paypal['ccaps']))) |
| 118 |
foreach(preg_split('/['."\r\n\t".'\s;,]+/', preg_replace('/^-all['."\r\n\t".'\s;,]*/', '', str_replace('+', '', $paypal['ccaps']))) as $ccap) |
| 119 |
if(strlen($ccap = trim(strtolower(preg_replace('/[^a-z_0-9]/i', '', $ccap))))) |
| 120 |
$user->add_cap('access_s2member_ccap_'.$ccap); |
| 121 |
|
| 122 |
update_user_option($user_id, 's2member_subscr_gateway', $paypal['subscr_gateway']); |
| 123 |
update_user_option($user_id, 's2member_subscr_id', $paypal['subscr_id']); |
| 124 |
update_user_option($user_id, 's2member_subscr_baid', $paypal['subscr_baid']); |
| 125 |
update_user_option($user_id, 's2member_subscr_cid', $paypal['subscr_cid']); |
| 126 |
|
| 127 |
update_user_option($user_id, 's2member_custom', $paypal['custom']); |
| 128 |
|
| 129 |
if(!get_user_option('s2member_registration_ip', $user_id)) |
| 130 |
update_user_option($user_id, 's2member_registration_ip', $paypal['ip']); |
| 131 |
|
| 132 |
update_user_option($user_id, 's2member_ipn_signup_vars', $ipn_signup_vars); |
| 133 |
|
| 134 |
delete_user_option($user_id, 's2member_file_download_access_log'); |
| 135 |
|
| 136 |
delete_user_option($user_id, 's2member_auto_eot_time'); |
| 137 |
|
| 138 |
$pr_times = get_user_option('s2member_paid_registration_times', $user_id); |
| 139 |
$pr_times['level'] = (!$pr_times['level']) ? time() : $pr_times['level']; // Preserve existing. |
| 140 |
$pr_times['level'.$paypal['level']] = (!$pr_times['level'.$paypal['level']]) ? time() : $pr_times['level'.$paypal['level']]; |
| 141 |
update_user_option($user_id, 's2member_paid_registration_times', $pr_times); |
| 142 |
|
| 143 |
c_ws_plugin__s2member_user_notes::clear_user_note_lines($user_id, '/^Demoted by s2Member\:/'); |
| 144 |
c_ws_plugin__s2member_user_notes::clear_user_note_lines($user_id, '/^Paid Subscr\. ID @ time of demotion\:/'); |
| 145 |
|
| 146 |
$paypal['s2member_log'][] = 's2Member Level/Capabilities updated on ( `subscr_modify` ), a Subscription Modification.'; |
| 147 |
|
| 148 |
setcookie('s2member_tracking', ($s2member_tracking = c_ws_plugin__s2member_utils_encryption::encrypt($paypal['subscr_id'])), time() + 31556926, COOKIEPATH, COOKIE_DOMAIN).setcookie('s2member_tracking', $s2member_tracking, time() + 31556926, SITECOOKIEPATH, COOKIE_DOMAIN).($_COOKIE['s2member_tracking'] = $s2member_tracking); |
| 149 |
|
| 150 |
$paypal['s2member_log'][] = 'Transient Tracking Cookie set on ( `subscr_modify` ), a Subscription Modification.'; |
| 151 |
|
| 152 |
if($processing && ($code = $GLOBALS['WS_PLUGIN__']['s2member']['o']['modification_tracking_codes'])) |
| 153 |
{ |
| 154 |
if(($code = c_ws_plugin__s2member_utils_strings::fill_cvs($code, $paypal['custom'])) && ($code = preg_replace('/%%subscr_id%%/i', c_ws_plugin__s2member_utils_strings::esc_refs($paypal['subscr_id']), $code))) |
| 155 |
if(($code = preg_replace('/%%subscr_baid%%/i', c_ws_plugin__s2member_utils_strings::esc_refs($paypal['subscr_baid']), $code)) && ($code = preg_replace('/%%subscr_cid%%/i', c_ws_plugin__s2member_utils_strings::esc_refs($paypal['subscr_cid']), $code))) |
| 156 |
if(($code = preg_replace('/%%currency%%/i', c_ws_plugin__s2member_utils_strings::esc_refs($paypal['currency']), $code)) && ($code = preg_replace('/%%currency_symbol%%/i', c_ws_plugin__s2member_utils_strings::esc_refs($paypal['currency_symbol']), $code))) |
| 157 |
if(($code = preg_replace('/%%initial%%/i', c_ws_plugin__s2member_utils_strings::esc_refs($paypal['initial']), $code)) && ($code = preg_replace('/%%regular%%/i', c_ws_plugin__s2member_utils_strings::esc_refs($paypal['regular']), $code)) && ($code = preg_replace('/%%recurring%%/i', c_ws_plugin__s2member_utils_strings::esc_refs($paypal['recurring']), $code))) |
| 158 |
if(($code = preg_replace('/%%initial_term%%/i', c_ws_plugin__s2member_utils_strings::esc_refs($paypal['initial_term']), $code)) && ($code = preg_replace('/%%regular_term%%/i', c_ws_plugin__s2member_utils_strings::esc_refs($paypal['regular_term']), $code))) |
| 159 |
if(($code = preg_replace('/%%item_number%%/i', c_ws_plugin__s2member_utils_strings::esc_refs($paypal['item_number']), $code)) && ($code = preg_replace('/%%item_name%%/i', c_ws_plugin__s2member_utils_strings::esc_refs($paypal['item_name']), $code))) |
| 160 |
if(($code = preg_replace('/%%first_name%%/i', c_ws_plugin__s2member_utils_strings::esc_refs($paypal['first_name']), $code)) && ($code = preg_replace('/%%last_name%%/i', c_ws_plugin__s2member_utils_strings::esc_refs($paypal['last_name']), $code))) |
| 161 |
if(($code = preg_replace('/%%full_name%%/i', c_ws_plugin__s2member_utils_strings::esc_refs(trim($paypal['first_name'].' '.$paypal['last_name'])), $code))) |
| 162 |
if(($code = preg_replace('/%%payer_email%%/i', c_ws_plugin__s2member_utils_strings::esc_refs($paypal['payer_email']), $code))) |
| 163 |
{ |
| 164 |
if(($code = preg_replace('/%%user_first_name%%/i', c_ws_plugin__s2member_utils_strings::esc_refs($user->first_name), $code)) && ($code = preg_replace('/%%user_last_name%%/i', c_ws_plugin__s2member_utils_strings::esc_refs($user->last_name), $code))) |
| 165 |
if(($code = preg_replace('/%%user_full_name%%/i', c_ws_plugin__s2member_utils_strings::esc_refs(trim($user->first_name.' '.$user->last_name)), $code))) |
| 166 |
if(($code = preg_replace('/%%user_email%%/i', c_ws_plugin__s2member_utils_strings::esc_refs($user->user_email), $code))) |
| 167 |
if(($code = preg_replace('/%%user_login%%/i', c_ws_plugin__s2member_utils_strings::esc_refs($user->user_login), $code))) |
| 168 |
if(($code = preg_replace('/%%user_ip%%/i', c_ws_plugin__s2member_utils_strings::esc_refs($user_reg_ip), $code))) |
| 169 |
if(($code = preg_replace('/%%user_id%%/i', c_ws_plugin__s2member_utils_strings::esc_refs($user_id), $code))) |
| 170 |
{ |
| 171 |
if(is_array($fields) && !empty($fields)) |
| 172 |
foreach($fields as $var => $val /* Custom Registration/Profile Fields. */) |
| 173 |
if(!($code = preg_replace('/%%'.preg_quote($var, '/').'%%/i', c_ws_plugin__s2member_utils_strings::esc_refs(maybe_serialize($val)), $code))) |
| 174 |
break; |
| 175 |
|
| 176 |
if(($code = trim(preg_replace('/%%(.+?)%%/i', '', $code))) /* This gets stored into a Transient Queue. */) |
| 177 |
{ |
| 178 |
$paypal['s2member_log'][] = 'Storing Modification Tracking Codes into a Transient Queue. These will be processed on-site.'; |
| 179 |
set_transient('s2m_'.md5('s2member_transient_modification_tracking_codes_'.$paypal['subscr_id']), $code, 43200); |
| 180 |
} |
| 181 |
} |
| 182 |
} |
| 183 |
} |
| 184 |
foreach(array_keys(get_defined_vars()) as $__v) $__refs[$__v] =& $$__v; |
| 185 |
do_action('ws_plugin__s2member_during_paypal_return_during_subscr_modify', get_defined_vars()); |
| 186 |
unset($__refs, $__v); |
| 187 |
|
| 188 |
if(($redirection_url_after_modification = apply_filters('ws_plugin__s2member_redirection_url_after_modification', FALSE, get_defined_vars()))) |
| 189 |
{ |
| 190 |
$paypal['s2member_log'][] = 'Redirecting this Member to a custom URL after modification: '.$redirection_url_after_modification; |
| 191 |
|
| 192 |
wp_redirect($redirection_url_after_modification); |
| 193 |
} |
| 194 |
else // Else, use standard/default handling in this scenario. Have the Customer log in again. |
| 195 |
{ |
| 196 |
$paypal['s2member_log'][] = 'Redirecting Customer to the Login Page (after displaying a quick thank-you message). They need to log back in.'; |
| 197 |
|
| 198 |
echo c_ws_plugin__s2member_return_templates::return_template($paypal['subscr_gateway'], |
| 199 |
'<strong>'._x('Thank you! You\'ve been updated to:', 's2member-front', 's2member').'<br /><em>'.esc_html($paypal['item_name']).'</em></strong>', |
| 200 |
_x('Please Log Back In (Click Here)', 's2member-front', 's2member'), wp_login_url()); |
| 201 |
} |
| 202 |
} |
| 203 |
else // Else, unable to modify Subscription. The existing User ID is associated with an Administrator. Stopping here. |
| 204 |
{ |
| 205 |
$paypal['s2member_log'][] = 'Unable to modify Subscription. The existing User ID is associated with an Administrator. Stopping here. Otherwise, an Administrator could lose access. Please make sure that you are NOT logged in as an Administrator while testing.'; |
| 206 |
|
| 207 |
$paypal['s2member_log'][] = 'Redirecting Customer to the Home Page (after displaying an error message).'; |
| 208 |
|
| 209 |
echo c_ws_plugin__s2member_return_templates::return_template($paypal['subscr_gateway'], |
| 210 |
_x('<strong>ERROR:</strong> Unable to modify Subscription.<br />Please contact Support for assistance.<br /><br />The existing User ID is associated with an Administrator. Stopping here. Otherwise, an Administrator could lose access. Please make sure that you are NOT logged in as an Administrator while testing.', 's2member-front', 's2member'), |
| 211 |
_x('Back To Home Page', 's2member-front', 's2member'), home_url('/')); |
| 212 |
} |
| 213 |
} |
| 214 |
else // Unable to modify Subscription. Could not get the existing User ID from the DB. |
| 215 |
{ |
| 216 |
$paypal['s2member_log'][] = 'Unable to modify Subscription. Could not get the existing User ID from the DB.'; |
| 217 |
|
| 218 |
$paypal['s2member_log'][] = 'Redirecting Customer to the Home Page (after displaying an error message).'; |
| 219 |
|
| 220 |
echo c_ws_plugin__s2member_return_templates::return_template($paypal['subscr_gateway'], |
| 221 |
_x('<strong>ERROR:</strong> Unable to modify Subscription.<br />Please contact Support for assistance.<br /><br />Could not get the existing User ID from the DB.', 's2member-front', 's2member'), |
| 222 |
_x('Back To Home Page', 's2member-front', 's2member'), home_url('/')); |
| 223 |
} |
| 224 |
} |
| 225 |
else // Page Expired. Duplicate Return-Data. |
| 226 |
{ |
| 227 |
$paypal['s2member_log'][] = 'Page Expired. Duplicate Return-Data.'; |
| 228 |
$paypal['s2member_log'][] = 's2Member `txn_type` identified as `subscr_modify`.'; |
| 229 |
$paypal['s2member_log'][] = 'Page Expired. Instructing customer to check their email for further details about how to obtain access to what they purchased.'; |
| 230 |
|
| 231 |
echo c_ws_plugin__s2member_return_templates::return_template($paypal['subscr_gateway'], |
| 232 |
'<strong>'._x('Thank you! Please check your email for further details regarding your purchase.', 's2member-front', 's2member').'</strong>', |
| 233 |
_x('Return to Home Page', 's2member-front', 's2member'), home_url('/')); |
| 234 |
} |
| 235 |
foreach(array_keys(get_defined_vars()) as $__v) $__refs[$__v] =& $$__v; |
| 236 |
do_action('ws_plugin__s2member_during_paypal_return_after_subscr_modify', get_defined_vars()); |
| 237 |
unset($__refs, $__v); |
| 238 |
|
| 239 |
return apply_filters('c_ws_plugin__s2member_paypal_return_in_subscr_modify_w_level', $paypal, get_defined_vars()); |
| 240 |
} |
| 241 |
else return apply_filters('c_ws_plugin__s2member_paypal_return_in_subscr_modify_w_level', FALSE, get_defined_vars()); |
| 242 |
} |
| 243 |
} |
| 244 |
} |
| 245 |
|