Source Code Documentation


s2member-pro\includes\classes\gateways\paypal\paypal-checkout-pf-in.inc.php


  1. <?php
  2. /**
  3. * PayPal® Checkout Forms ( inner processing routines ).
  4. *
  5. * Copyright: © 2009-2011
  6. * {@link http://www.websharks-inc.com/ WebSharks, Inc.}
  7. * ( coded in the USA )
  8. *
  9. * This WordPress® plugin ( s2Member Pro ) is comprised of two parts:
  10. *
  11. * o (1) Its PHP code is licensed under the GPL license, as is WordPress®.
  12. * You should have received a copy of the GNU General Public License,
  13. * along with this software. In the main directory, see: /licensing/
  14. * If not, see: {@link http://www.gnu.org/licenses/}.
  15. *
  16. * o (2) All other parts of ( s2Member Pro ); including, but not limited to:
  17. * the CSS code, some JavaScript code, images, and design;
  18. * are licensed according to the license purchased.
  19. * See: {@link http://www.s2member.com/prices/}
  20. *
  21. * Unless you have our prior written consent, you must NOT directly or indirectly license,
  22. * sub-license, sell, resell, or provide for free; part (2) of the s2Member Pro Module;
  23. * or make an offer to do any of these things. All of these things are strictly
  24. * prohibited with part (2) of the s2Member Pro Module.
  25. *
  26. * Your purchase of s2Member Pro includes free lifetime upgrades via s2Member.com
  27. * ( i.e. new features, bug fixes, updates, improvements ); along with full access
  28. * to our video tutorial library: {@link http://www.s2member.com/videos/}
  29. *
  30. * @package s2Member\PayPal
  31. * @since 1.5
  32. */
  33. if(realpath(__FILE__) === realpath($_SERVER["SCRIPT_FILENAME"]))
  34. exit("Do not access this file directly.");
  35. /**/
  36. if(!class_exists("c_ws_plugin__s2member_pro_paypal_checkout_pf_in"))
  37. {
  38. /**
  39. * PayPal® Checkout Forms ( inner processing routines ).
  40. *
  41. * @package s2Member\PayPal
  42. * @since 1.5
  43. */
  44. class c_ws_plugin__s2member_pro_paypal_checkout_pf_in
  45. {
  46. /**
  47. * Handles processing of Pro Form checkouts.
  48. *
  49. * @package s2Member\PayPal
  50. * @since 1.5
  51. *
  52. * @attaches-to ``add_action("init");``
  53. *
  54. * @return null Or exits script execution after a custom URL redirection; or upon Express Checkout redirection.
  55. */
  56. public static function paypal_checkout()
  57. {
  58. if(/**/(/**/!empty($_POST["s2member_pro_paypal_checkout"]["nonce"]) && ($nonce = $_POST["s2member_pro_paypal_checkout"]["nonce"]) && wp_verify_nonce($nonce, "s2member-pro-paypal-checkout")/**/)/**/
  59. || (/**/!empty($_GET["s2member_paypal_xco"]) && $_GET["s2member_paypal_xco"] === "s2member_pro_paypal_checkout_return" /* PayPal® Express Checkout with $_GET["token"] & $_GET["PayerID"]? */
  60. && !empty($_GET["token"]) && ($_GET["token"] = esc_html($_GET["token"])) && (empty($_GET["PayerID"]) || ($_GET["PayerID"] = esc_html($_GET["PayerID"]))) /* PayerID is not required. */
  61. && ($xco_post_vars = get_transient("s2m_".md5("s2member_transient_express_checkout_".$_GET["token"])))/**/)/**/)
  62. {
  63. $GLOBALS["ws_plugin__s2member_pro_paypal_checkout_response"] = array(); /* This holds the global response details. */
  64. $global_response = &$GLOBALS["ws_plugin__s2member_pro_paypal_checkout_response"]; /* This is a shorter reference. */
  65. /**/
  66. $post_vars = ($xco_post_vars) ? $xco_post_vars : $_POST["s2member_pro_paypal_checkout"];
  67. $post_vars = c_ws_plugin__s2member_utils_strings::trim_deep(stripslashes_deep($post_vars)); /* And Filter. */
  68. $post_vars["attr"] = (!$xco_post_vars) ? unserialize(c_ws_plugin__s2member_utils_encryption::decrypt($post_vars["attr"])) : $post_vars["attr"];
  69. $post_vars["attr"] = (!$xco_post_vars) ? apply_filters("ws_plugin__s2member_pro_paypal_checkout_post_attr", $post_vars["attr"], get_defined_vars()) : $post_vars["attr"];
  70. /**/
  71. if($xco_post_vars) /* No need to re-validate this upon return from Express Checkout. */
  72. $post_vars["attr"]["captcha"] = "0";
  73. /**/
  74. $post_vars["recaptcha_challenge_field"] = (!$post_vars["recaptcha_challenge_field"]) ? trim(stripslashes($_POST["recaptcha_challenge_field"])) : $post_vars["recaptcha_challenge_field"];
  75. $post_vars["recaptcha_response_field"] = (!$post_vars["recaptcha_response_field"]) ? trim(stripslashes($_POST["recaptcha_response_field"])) : $post_vars["recaptcha_response_field"];
  76. /**/
  77. $post_vars["name"] = trim($post_vars["first_name"]." ".$post_vars["last_name"]);
  78. $post_vars["email"] = apply_filters("user_registration_email", sanitize_email($post_vars["email"]), get_defined_vars());
  79. $post_vars["username"] = preg_replace("/\s+/", "", sanitize_user($post_vars["username"], is_multisite()));
  80. /**/
  81. (!empty($_GET["token"])) ? delete_transient("s2m_".md5("s2member_transient_express_checkout_".$_GET["token"])) : null;
  82. /**/
  83. if(!c_ws_plugin__s2member_pro_paypal_responses::paypal_form_attr_validation_errors($post_vars["attr"])) /* Attr errors? */
  84. {
  85. if(!($error = c_ws_plugin__s2member_pro_paypal_responses::paypal_form_submission_validation_errors("checkout", $post_vars)))
  86. {
  87. $cp_attr = c_ws_plugin__s2member_pro_paypal_utilities::paypal_apply_coupon($post_vars["attr"], $post_vars["coupon"], "attr", array("affiliates-silent-post"));
  88. $cp_2gbp_attr = c_ws_plugin__s2member_pro_paypal_utilities::paypal_maestro_solo_2gbp( /* Now we use the new array of ``$cp_attr``. */$cp_attr, $post_vars["card_type"]);
  89. $cost_calculations = c_ws_plugin__s2member_pro_paypal_utilities::paypal_cost($cp_2gbp_attr["ta"], $cp_2gbp_attr["ra"], $post_vars["state"], $post_vars["country"], $post_vars["zip"], $cp_2gbp_attr["cc"], $cp_2gbp_attr["desc"]);
  90. /**/
  91. $use_recurring_profile = ($post_vars["attr"]["rr"] === "BN" || (!$post_vars["attr"]["tp"] && !$post_vars["attr"]["rr"])) ? false : true;
  92. $is_independent_ccaps_sale = ($post_vars["attr"]["level"] === "*") ? true : false; /* Selling Independent Custom Capabilities? */
  93. /**/
  94. if(empty($_GET["s2member_paypal_xco"]) && $post_vars["card_type"] === "PayPal")
  95. {
  96. $return_url = $cancel_url = (is_ssl()) ? "https://" : "http://";
  97. $return_url = $cancel_url = ($return_url = $cancel_url).$_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"];
  98. $return_url = $cancel_url = /* Ditch. */ remove_query_arg(array("token", "PayerID"), ($return_url = $cancel_url));
  99. $return_url = add_query_arg("s2member_paypal_xco", urlencode("s2member_pro_paypal_checkout_return"), $return_url);
  100. $cancel_url = add_query_arg("s2member_paypal_xco", urlencode("s2member_pro_paypal_checkout_cancel"), $cancel_url);
  101. /**/
  102. $user = (is_user_logged_in() && is_object($user = wp_get_current_user()) && ($user_id = $user->ID)) ? $user : false;
  103. /**/
  104. if(!($paypal_set_xco = array()) /* PayPal® Express Checkout. */)
  105. {
  106. if($use_recurring_profile /* Use Payflow® API instead? */)
  107. {
  108. $paypal_set_xco["TRXTYPE"] = "A";
  109. $paypal_set_xco["ACTION"] = "S";
  110. $paypal_set_xco["TENDER"] = "P";
  111. /**/
  112. $paypal_set_xco["RETURNURL"] = $return_url;
  113. $paypal_set_xco["CANCELURL"] = $cancel_url;
  114. /**/
  115. $paypal_set_xco["PAGESTYLE"] = $post_vars["attr"]["ps"];
  116. $paypal_set_xco["LOCALECODE"] = $post_vars["attr"]["lc"];
  117. $paypal_set_xco["NOSHIPPING"] = $post_vars["attr"]["ns"];
  118. $paypal_set_xco["ALLOWNOTE"] = "0";
  119. /**/
  120. $paypal_set_xco["AMT"] = "0.00";
  121. $paypal_set_xco["CURRENCY"] = $cost_calculations["cur"];
  122. $paypal_set_xco["PAYMENTTYPE"] = "any";
  123. /**/
  124. $paypal_set_xco["L_BILLINGTYPE0"] = "RecurringPayments";
  125. $paypal_set_xco["L_BILLINGAGREEMENTDESCRIPTION0"] = $cost_calculations["desc"];
  126. /**/
  127. $paypal_set_xco["ADDROVERRIDE"] = "1";
  128. $paypal_set_xco["SHIPTONAME"] = $post_vars["name"];
  129. $paypal_set_xco["SHIPTOSTREET"] = $post_vars["street"];
  130. $paypal_set_xco["SHIPTOCITY"] = $post_vars["city"];
  131. $paypal_set_xco["SHIPTOSTATE"] = $post_vars["state"];
  132. $paypal_set_xco["SHIPTOCOUNTRY"] = $post_vars["country"];
  133. $paypal_set_xco["SHIPTOZIP"] = $post_vars["zip"];
  134. /**/
  135. $paypal_set_xco["EMAIL"] = ($user) ? $user->user_email : $post_vars["email"];
  136. /**/
  137. if(($paypal_set_xco = c_ws_plugin__s2member_paypal_utilities::paypal_payflow_api_response($paypal_set_xco)) && empty($paypal_set_xco["__error"]))
  138. {
  139. set_transient("s2m_".md5("s2member_transient_express_checkout_".$paypal_set_xco["TOKEN"]), $post_vars, 10800);
  140. /**/
  141. $endpoint = ($GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["paypal_sandbox"]) ? "www.sandbox.paypal.com" : "www.paypal.com";
  142. /**/
  143. wp_redirect(add_query_arg("token", urlencode($paypal_set_xco["TOKEN"]), "https://".$endpoint."/cgi-bin/webscr?cmd=_express-checkout"));
  144. /**/
  145. exit /* Clean exit. */();
  146. }
  147. else /* Else, an error. */
  148. {
  149. $global_response = array("response" => $paypal_set_xco["__error"], "error" => true);
  150. }
  151. }
  152. /**/
  153. else /* We use the PayPal® Pro API, because this is NOT going to recur. */
  154. {
  155. $paypal_set_xco["METHOD"] = "SetExpressCheckout";
  156. /**/
  157. $paypal_set_xco["RETURNURL"] = $return_url;
  158. $paypal_set_xco["CANCELURL"] = $cancel_url;
  159. /**/
  160. $paypal_set_xco["PAGESTYLE"] = $post_vars["attr"]["ps"];
  161. $paypal_set_xco["LOCALECODE"] = $post_vars["attr"]["lc"];
  162. $paypal_set_xco["NOSHIPPING"] = $post_vars["attr"]["ns"];
  163. $paypal_set_xco["ALLOWNOTE"] = "0"; /* No notes. */
  164. /**/
  165. $paypal_set_xco["PAYMENTREQUEST_0_PAYMENTACTION"] = "Sale";
  166. /**/
  167. $paypal_set_xco["PAYMENTREQUEST_0_DESC"] = $cost_calculations["desc"];
  168. $paypal_set_xco["PAYMENTREQUEST_0_CUSTOM"] = $post_vars["attr"]["custom"];
  169. /**/
  170. $paypal_set_xco["PAYMENTREQUEST_0_CURRENCYCODE"] = $cost_calculations["cur"];
  171. $paypal_set_xco["PAYMENTREQUEST_0_ITEMAMT"] = $cost_calculations["sub_total"];
  172. $paypal_set_xco["PAYMENTREQUEST_0_TAXAMT"] = $cost_calculations["tax"];
  173. $paypal_set_xco["PAYMENTREQUEST_0_AMT"] = $cost_calculations["total"];
  174. /**/
  175. $paypal_set_xco["L_PAYMENTREQUEST_0_QTY0"] = "1"; /* Always (1). */
  176. $paypal_set_xco["L_PAYMENTREQUEST_0_NAME0"] = $cost_calculations["desc"];
  177. $paypal_set_xco["L_PAYMENTREQUEST_0_NUMBER0"] = $post_vars["attr"]["level_ccaps_eotper"];
  178. $paypal_set_xco["L_PAYMENTREQUEST_0_AMT0"] = $cost_calculations["sub_total"];
  179. /**/
  180. $paypal_set_xco["PAYMENTREQUEST_0_SHIPTONAME"] = $post_vars["name"];
  181. $paypal_set_xco["PAYMENTREQUEST_0_SHIPTOSTREET"] = $post_vars["street"];
  182. $paypal_set_xco["PAYMENTREQUEST_0_SHIPTOCITY"] = $post_vars["city"];
  183. $paypal_set_xco["PAYMENTREQUEST_0_SHIPTOSTATE"] = $post_vars["state"];
  184. $paypal_set_xco["PAYMENTREQUEST_0_SHIPTOCOUNTRYCODE"] = $post_vars["country"];
  185. $paypal_set_xco["PAYMENTREQUEST_0_SHIPTOZIP"] = $post_vars["zip"];
  186. /**/
  187. $paypal_set_xco["EMAIL"] = ($user) ? $user->user_email : $post_vars["email"];
  188. /**/
  189. if(($paypal_set_xco = c_ws_plugin__s2member_paypal_utilities::paypal_api_response($paypal_set_xco)) && empty($paypal_set_xco["__error"]))
  190. {
  191. set_transient("s2m_".md5("s2member_transient_express_checkout_".$paypal_set_xco["TOKEN"]), $post_vars, 10800);
  192. /**/
  193. $endpoint = ($GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["paypal_sandbox"]) ? "www.sandbox.paypal.com" : "www.paypal.com";
  194. /**/
  195. wp_redirect(add_query_arg("token", urlencode($paypal_set_xco["TOKEN"]), "https://".$endpoint."/cgi-bin/webscr?cmd=_express-checkout"));
  196. /**/
  197. exit /* Clean exit. */();
  198. }
  199. else /* Else, an error. */
  200. {
  201. $global_response = array("response" => $paypal_set_xco["__error"], "error" => true);
  202. }
  203. }
  204. }
  205. }
  206. /**/
  207. else if($use_recurring_profile && is_user_logged_in() && is_object($user = wp_get_current_user()) && ($user_id = $user->ID))
  208. {
  209. if(!($cur__subscr_id = get_user_option("s2member_subscr_id")) || !($paypal = c_ws_plugin__s2member_pro_paypal_utilities::payflow_get_profile($cur__subscr_id)) || !preg_match("/^(Pending|PendingProfile)$/i", $paypal["STATUS"]))
  210. {
  211. $period1 = c_ws_plugin__s2member_paypal_utilities::paypal_pro_period1($post_vars["attr"]["tp"]." ".$post_vars["attr"]["tt"]);
  212. $period3 = c_ws_plugin__s2member_paypal_utilities::paypal_pro_period3($post_vars["attr"]["rp"]." ".$post_vars["attr"]["rt"]);
  213. /**/
  214. $start_time = ($post_vars["attr"]["tp"]) ? /* If there's an Initial/Trial Period; start when it's over. */
  215. c_ws_plugin__s2member_pro_paypal_utilities::paypal_start_time($period1) : /* After Trial is over. */
  216. c_ws_plugin__s2member_pro_paypal_utilities::paypal_start_time($period3); /* Or next billing cycle. */
  217. /**/
  218. $reference = $start_time.":".$period1.":".$period3."~".$_SERVER["HTTP_HOST"]."~".$post_vars["attr"]["level_ccaps_eotper"];
  219. /**/
  220. update_user_meta($user_id, "first_name", $post_vars["first_name"]).update_user_meta($user_id, "last_name", $post_vars["last_name"]);
  221. /**/
  222. if(!($paypal = array()) /* Recurring Profile. */)
  223. {
  224. $paypal["TRXTYPE"] = "R";
  225. $paypal["ACTION"] = "A";
  226. /**/
  227. $paypal["EMAIL"] = $user->user_email;
  228. $paypal["FIRSTNAME"] = $post_vars["first_name"];
  229. $paypal["LASTNAME"] = $post_vars["last_name"];
  230. $paypal["CLIENTIP"] = $_SERVER["REMOTE_ADDR"];
  231. /**/
  232. $paypal["DESC"] = $cost_calculations["desc"];
  233. $paypal["BA_DESC"] = $cost_calculations["desc"];
  234. $paypal["PROFILENAME"] = $reference;
  235. /**/
  236. if(!$post_vars["attr"]["tp"] || ($post_vars["attr"]["tp"] && $cost_calculations["trial_total"] > 0))
  237. {
  238. $paypal["OPTIONALTRX"] = "S";
  239. $paypal["OPTIONALTRXAMT"] = ($post_vars["attr"]["tp"]) ? $cost_calculations["trial_total"] : $cost_calculations["total"];
  240. $paypal["FAILEDOPTIONALTRXACTION"] = "CancelOnFailure";
  241. $paypal["FAILEDINITAMTACTION"] = "CancelOnFailure";
  242. }
  243. /**/
  244. $paypal["CURRENCY"] = $cost_calculations["cur"];
  245. $paypal["AMT"] = $cost_calculations["sub_total"];
  246. $paypal["TAXAMT"] = $cost_calculations["tax"];
  247. /**/
  248. $paypal["MAXFAILPAYMENTS"] = $post_vars["attr"]["rra"];
  249. $paypal["AUTOBILLOUTSTANDINGAMT"] = apply_filters("ws_plugin__s2member_pro_paypal_auto_bill_op", "AddToNextBilling", get_defined_vars());
  250. /**/
  251. $paypal["START"] = date("mdY", $start_time);
  252. /**/
  253. $paypal["PAYPERIOD"] = c_ws_plugin__s2member_paypal_utilities::paypal_payflow_term($post_vars["attr"]["rt"], $post_vars["attr"]["rp"]);
  254. $paypal["TERM"] = ($post_vars["attr"]["rr"]) ? (($post_vars["attr"]["rrt"]) ? $post_vars["attr"]["rrt"] : "0") : "1";
  255. /**/
  256. if($_GET["s2member_paypal_xco"] === "s2member_pro_paypal_checkout_return" && !empty($_GET["token"]) && ($paypal_xco_details = array("TRXTYPE" => "S", "TENDER" => "P", "ACTION" => "G", "TOKEN" => $_GET["token"])) && ($paypal_xco_details = c_ws_plugin__s2member_paypal_utilities::paypal_payflow_api_response($paypal_xco_details)) && empty($paypal_xco_details["__error"]))
  257. {
  258. $paypal["TENDER"] = "P";
  259. $paypal["TOKEN"] = $paypal_xco_details["TOKEN"];
  260. $paypal["PAYERID"] = $paypal_xco_details["PAYERID"];
  261. $paypal["L_BILLINGAGREEMENTDESCRIPTION0"] = $cost_calculations["desc"];
  262. }
  263. else /* Otherwise, they're using a major credit card. */
  264. {
  265. $paypal["TENDER"] = "C";
  266. $paypal["ACCT"] = preg_replace("/[^0-9]/", "", $post_vars["card_number"]);
  267. if(preg_match("/^(?P<month>[0-9]{2})\/[0-9]{2}(?P<year_suffix>[0-9]{2})$/", $post_vars["card_expiration"], $_m))
  268. $paypal["EXPDATE"] = $_m["month"].$_m["year_suffix"];
  269. $paypal["CVV2"] = $post_vars["card_verification"];
  270. /**/
  271. if(in_array($post_vars["card_type"], array("Maestro", "Solo")))
  272. {
  273. if(preg_match("/^(?P<month>[0-9]{2})\/[0-9]{2}(?P<year>[0-9]{2})$/", $post_vars["card_start_date_issue_number"], $_m))
  274. $paypal["CARDSTART"] = $_m["month"].$_m["year"];
  275. else
  276. $paypal["CARDISSUE"] = $post_vars["card_start_date_issue_number"];
  277. unset /* A little housekeeping. */($_m);
  278. }
  279. /**/
  280. $paypal["STREET"] = $post_vars["street"];
  281. $paypal["CITY"] = $post_vars["city"];
  282. $paypal["STATE"] = $post_vars["state"];
  283. $paypal["COUNTRY"] = $post_vars["country"];
  284. $paypal["ZIP"] = $post_vars["zip"];
  285. }
  286. }
  287. /**/
  288. if(($paypal = c_ws_plugin__s2member_paypal_utilities::paypal_payflow_api_response($paypal)) && empty($paypal["__error"]))
  289. {
  290. $old__subscr_or_wp_id = c_ws_plugin__s2member_utils_users::get_user_subscr_or_wp_id();
  291. $old__subscr_id = get_user_option("s2member_subscr_id");
  292. $new__subscr_id = $paypal["PROFILEID"];
  293. /**/
  294. if(!($ipn = array())) /* Simulated PayPal® IPN. */
  295. {
  296. $ipn["txn_type"] = "subscr_signup";
  297. $ipn["subscr_id"] = $new__subscr_id;
  298. $ipn["custom"] = $post_vars["attr"]["custom"];
  299. /**/
  300. $ipn["txn_id"] = $new__subscr_id;
  301. /**/
  302. $ipn["period1"] = $period1;
  303. $ipn["period3"] = $period3;
  304. /**/
  305. $ipn["mc_amount1"] = $cost_calculations["trial_total"];
  306. $ipn["mc_amount3"] = $cost_calculations["total"];
  307. /**/
  308. $ipn["mc_gross"] = (preg_match("/^[1-9]/", $ipn["period1"])) ? $ipn["mc_amount1"] : $ipn["mc_amount3"];
  309. /**/
  310. $ipn["mc_currency"] = $cost_calculations["cur"];
  311. $ipn["tax"] = $cost_calculations["tax"];
  312. /**/
  313. $ipn["recurring"] = ($post_vars["attr"]["rr"]) ? "1" : "";
  314. /**/
  315. $ipn["payer_email"] = $user->user_email;
  316. $ipn["first_name"] = $post_vars["first_name"];
  317. $ipn["last_name"] = $post_vars["last_name"];
  318. /**/
  319. $ipn["option_name1"] = "Referencing Customer ID";
  320. $ipn["option_selection1"] = $old__subscr_or_wp_id;
  321. /**/
  322. $ipn["option_name2"] = "Customer IP Address";
  323. $ipn["option_selection2"] = $_SERVER["REMOTE_ADDR"];
  324. /**/
  325. $ipn["item_name"] = $cost_calculations["desc"];
  326. $ipn["item_number"] = $post_vars["attr"]["level_ccaps_eotper"];
  327. /**/
  328. $ipn_q = "&s2member_paypal_proxy=paypal&s2member_paypal_proxy_use=pro-emails";
  329. $ipn_q .= ($ipn["mc_gross"] > 0) ? ",subscr-signup-as-subscr-payment" : ""; /* Use as first payment? */
  330. $ipn_q .= "&s2member_paypal_proxy_verification=".urlencode(c_ws_plugin__s2member_paypal_utilities::paypal_proxy_key_gen());
  331. $ipn_q .= "&s2member_paypal_proxy_return_url=".rawurlencode($post_vars["attr"]["success"]);
  332. /**/
  333. $ipn["s2member_paypal_proxy_return_url"] = trim(c_ws_plugin__s2member_utils_urls::remote(site_url("/?s2member_paypal_notify=1".$ipn_q), $ipn, array("timeout" => 20)));
  334. }
  335. /**/
  336. if(!($paypal = array()) && $old__subscr_id)
  337. c_ws_plugin__s2member_pro_paypal_utilities::payflow_cancel_profile($old__subscr_id);
  338. /**/
  339. $global_response = array("response" => sprintf(_x('<strong>Thank you.</strong> Your account has been updated.<br />&mdash; Please <a href="%s" rel="nofollow">log back in</a> now.', "s2member-front", "s2member"), esc_attr(wp_login_url())));
  340. /**/
  341. if($post_vars["attr"]["success"] && substr($ipn["s2member_paypal_proxy_return_url"], 0, 2) === substr($post_vars["attr"]["success"], 0, 2) && ($custom_success_url = str_ireplace(array("%%s_response%%", /* Deprecated in v111106 ». */ "%%response%%"), array(urlencode(c_ws_plugin__s2member_utils_encryption::encrypt($global_response["response"])), urlencode($global_response["response"])), $ipn["s2member_paypal_proxy_return_url"])) && ($custom_success_url = trim(preg_replace("/%%(.+?)%%/i", "", $custom_success_url))))
  342. wp_redirect(c_ws_plugin__s2member_utils_urls::add_s2member_sig($custom_success_url, "s2p-v")).exit();
  343. }
  344. else /* Else, an error. */
  345. {
  346. $global_response = array("response" => $paypal["__error"], "error" => true);
  347. }
  348. }
  349. else /* Else, an error. The existing Billing Profile is still in a pending state. */
  350. {
  351. $global_response = array("response" => _x('<strong>Sorry.</strong> Your account is pending other changes. Please try again in 15 minutes.', "s2member-front", "s2member"), "error" => true);
  352. }
  353. }
  354. /**/
  355. else if($use_recurring_profile && !is_user_logged_in()) /* Create a new account. */
  356. {
  357. $period1 = c_ws_plugin__s2member_paypal_utilities::paypal_pro_period1($post_vars["attr"]["tp"]." ".$post_vars["attr"]["tt"]);
  358. $period3 = c_ws_plugin__s2member_paypal_utilities::paypal_pro_period3($post_vars["attr"]["rp"]." ".$post_vars["attr"]["rt"]);
  359. /**/
  360. $start_time = ($post_vars["attr"]["tp"]) ? /* If there's an Initial/Trial Period; start when it's over. */
  361. c_ws_plugin__s2member_pro_paypal_utilities::paypal_start_time($period1) : /* After Trial is over. */
  362. c_ws_plugin__s2member_pro_paypal_utilities::paypal_start_time($period3); /* Or next billing cycle. */
  363. /**/
  364. $reference = $start_time.":".$period1.":".$period3."~".$_SERVER["HTTP_HOST"]."~".$post_vars["attr"]["level_ccaps_eotper"];
  365. /**/
  366. if(!($paypal = array())) /* Recurring Profile. */
  367. {
  368. $paypal["TRXTYPE"] = "R";
  369. $paypal["ACTION"] = "A";
  370. /**/
  371. $paypal["EMAIL"] = $post_vars["email"];
  372. $paypal["FIRSTNAME"] = $post_vars["first_name"];
  373. $paypal["LASTNAME"] = $post_vars["last_name"];
  374. $paypal["CLIENTIP"] = $_SERVER["REMOTE_ADDR"];
  375. /**/
  376. $paypal["DESC"] = $cost_calculations["desc"];
  377. $paypal["BA_DESC"] = $cost_calculations["desc"];
  378. $paypal["PROFILENAME"] = $reference;
  379. /**/
  380. if(!$post_vars["attr"]["tp"] || ($post_vars["attr"]["tp"] && $cost_calculations["trial_total"] > 0))
  381. {
  382. $paypal["OPTIONALTRX"] = "S";
  383. $paypal["OPTIONALTRXAMT"] = ($post_vars["attr"]["tp"]) ? $cost_calculations["trial_total"] : $cost_calculations["total"];
  384. $paypal["FAILEDOPTIONALTRXACTION"] = "CancelOnFailure";
  385. $paypal["FAILEDINITAMTACTION"] = "CancelOnFailure";
  386. }
  387. /**/
  388. $paypal["CURRENCY"] = $cost_calculations["cur"];
  389. $paypal["AMT"] = $cost_calculations["sub_total"];
  390. $paypal["TAXAMT"] = $cost_calculations["tax"];
  391. /**/
  392. $paypal["MAXFAILPAYMENTS"] = $post_vars["attr"]["rra"];
  393. $paypal["AUTOBILLOUTSTANDINGAMT"] = apply_filters("ws_plugin__s2member_pro_paypal_auto_bill_op", "AddToNextBilling", get_defined_vars());
  394. /**/
  395. $paypal["START"] = date("mdY", $start_time);
  396. /**/
  397. $paypal["PAYPERIOD"] = c_ws_plugin__s2member_paypal_utilities::paypal_payflow_term($post_vars["attr"]["rt"], $post_vars["attr"]["rp"]);
  398. $paypal["TERM"] = ($post_vars["attr"]["rr"]) ? (($post_vars["attr"]["rrt"]) ? $post_vars["attr"]["rrt"] : "0") : "1";
  399. /**/
  400. if($_GET["s2member_paypal_xco"] === "s2member_pro_paypal_checkout_return" && !empty($_GET["token"]) && ($paypal_xco_details = array("TRXTYPE" => "S", "TENDER" => "P", "ACTION" => "G", "TOKEN" => $_GET["token"])) && ($paypal_xco_details = c_ws_plugin__s2member_paypal_utilities::paypal_payflow_api_response($paypal_xco_details)) && empty($paypal_xco_details["__error"]))
  401. {
  402. $paypal["TENDER"] = "P";
  403. $paypal["TOKEN"] = $paypal_xco_details["TOKEN"];
  404. $paypal["PAYERID"] = $paypal_xco_details["PAYERID"];
  405. $paypal["L_BILLINGAGREEMENTDESCRIPTION0"] = $cost_calculations["desc"];
  406. }
  407. else /* Otherwise, they're using a major credit card. */
  408. {
  409. $paypal["TENDER"] = "C";
  410. $paypal["ACCT"] = preg_replace("/[^0-9]/", "", $post_vars["card_number"]);
  411. if(preg_match("/^(?P<month>[0-9]{2})\/[0-9]{2}(?P<year_suffix>[0-9]{2})$/", $post_vars["card_expiration"], $_m))
  412. $paypal["EXPDATE"] = $_m["month"].$_m["year_suffix"];
  413. $paypal["CVV2"] = $post_vars["card_verification"];
  414. /**/
  415. if(in_array($post_vars["card_type"], array("Maestro", "Solo")))
  416. {
  417. if(preg_match("/^(?P<month>[0-9]{2})\/[0-9]{2}(?P<year>[0-9]{2})$/", $post_vars["card_start_date_issue_number"], $_m))
  418. $paypal["CARDSTART"] = $_m["month"].$_m["year"];
  419. else
  420. $paypal["CARDISSUE"] = $post_vars["card_start_date_issue_number"];
  421. unset /* A little housekeeping. */($_m);
  422. }
  423. /**/
  424. $paypal["STREET"] = $post_vars["street"];
  425. $paypal["CITY"] = $post_vars["city"];
  426. $paypal["STATE"] = $post_vars["state"];
  427. $paypal["COUNTRY"] = $post_vars["country"];
  428. $paypal["ZIP"] = $post_vars["zip"];
  429. }
  430. }
  431. /**/
  432. if(($paypal = c_ws_plugin__s2member_paypal_utilities::paypal_payflow_api_response($paypal)) && empty($paypal["__error"]))
  433. {
  434. $new__subscr_id = $paypal["PROFILEID"];
  435. /**/
  436. if(!($ipn = array())) /* Simulated PayPal® IPN. */
  437. {
  438. $ipn["txn_type"] = "subscr_signup";
  439. $ipn["subscr_id"] = $new__subscr_id;
  440. $ipn["custom"] = $post_vars["attr"]["custom"];
  441. /**/
  442. $ipn["txn_id"] = $new__subscr_id;
  443. /**/
  444. $ipn["period1"] = $period1;
  445. $ipn["period3"] = $period3;
  446. /**/
  447. $ipn["mc_amount1"] = $cost_calculations["trial_total"];
  448. $ipn["mc_amount3"] = $cost_calculations["total"];
  449. /**/
  450. $ipn["mc_gross"] = (preg_match("/^[1-9]/", $ipn["period1"])) ? $ipn["mc_amount1"] : $ipn["mc_amount3"];
  451. /**/
  452. $ipn["mc_currency"] = $cost_calculations["cur"];
  453. $ipn["tax"] = $cost_calculations["tax"];
  454. /**/
  455. $ipn["recurring"] = ($post_vars["attr"]["rr"]) ? "1" : "";
  456. /**/
  457. $ipn["payer_email"] = $post_vars["email"];
  458. $ipn["first_name"] = $post_vars["first_name"];
  459. $ipn["last_name"] = $post_vars["last_name"];
  460. /**/
  461. $ipn["option_name1"] = "Originating Domain";
  462. $ipn["option_selection1"] = $_SERVER["HTTP_HOST"];
  463. /**/
  464. $ipn["option_name2"] = "Customer IP Address";
  465. $ipn["option_selection2"] = $_SERVER["REMOTE_ADDR"];
  466. /**/
  467. $ipn["item_name"] = $cost_calculations["desc"];
  468. $ipn["item_number"] = $post_vars["attr"]["level_ccaps_eotper"];
  469. /**/
  470. $ipn_q = "&s2member_paypal_proxy=paypal&s2member_paypal_proxy_use=pro-emails";
  471. $ipn_q .= ($ipn["mc_gross"] > 0) ? ",subscr-signup-as-subscr-payment" : ""; /* Use as first payment? */
  472. $ipn_q .= "&s2member_paypal_proxy_verification=".urlencode(c_ws_plugin__s2member_paypal_utilities::paypal_proxy_key_gen());
  473. $ipn_q .= "&s2member_paypal_proxy_return_url=".rawurlencode($post_vars["attr"]["success"]);
  474. }
  475. /**/
  476. if(!($create_user = array())) /* Build post fields for registration configuration, and then the creation array. */
  477. {
  478. $_POST["ws_plugin__s2member_custom_reg_field_user_pass1"] = $post_vars["password1"]; /* Fake this for registration configuration. */
  479. $_POST["ws_plugin__s2member_custom_reg_field_first_name"] = $post_vars["first_name"]; /* Fake this for registration configuration. */
  480. $_POST["ws_plugin__s2member_custom_reg_field_last_name"] = $post_vars["last_name"]; /* Fake this for registration configuration. */
  481. $_POST["ws_plugin__s2member_custom_reg_field_opt_in"] = $post_vars["custom_fields"]["opt_in"]; /* Fake this too. */
  482. /**/
  483. if($GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["custom_reg_fields"])
  484. foreach(json_decode($GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["custom_reg_fields"], true) as $field)
  485. {
  486. $field_var = preg_replace("/[^a-z0-9]/i", "_", strtolower($field["id"]));
  487. $field_id_class = preg_replace("/_/", "-", $field_var);
  488. /**/
  489. if(isset($post_vars["custom_fields"][$field_var]))
  490. $_POST["ws_plugin__s2member_custom_reg_field_".$field_var] = $post_vars["custom_fields"][$field_var];
  491. }
  492. /**/
  493. $_COOKIE["s2member_subscr_gateway"] = c_ws_plugin__s2member_utils_encryption::encrypt("paypal"); /* Fake this for registration configuration. */
  494. $_COOKIE["s2member_subscr_id"] = c_ws_plugin__s2member_utils_encryption::encrypt($new__subscr_id); /* Fake this for registration configuration. */
  495. $_COOKIE["s2member_custom"] = c_ws_plugin__s2member_utils_encryption::encrypt($post_vars["attr"]["custom"]); /* Fake this for registration configuration. */
  496. $_COOKIE["s2member_item_number"] = c_ws_plugin__s2member_utils_encryption::encrypt($post_vars["attr"]["level_ccaps_eotper"]); /* Fake this too. */
  497. /**/
  498. $create_user["user_login"] = $post_vars["username"]; /* Copy this into a separate array for `wp_create_user()`. */
  499. $create_user["user_pass"] = wp_generate_password(); /* Which may fire `c_ws_plugin__s2member_registrations::generate_password()`. */
  500. $create_user["user_email"] = $post_vars["email"]; /* Copy this into a separate array for `wp_create_user()`. */
  501. }
  502. /**/
  503. if($post_vars["password1"] && $post_vars["password1"] === $create_user["user_pass"]) /* A custom Password is being used? */
  504. {
  505. if(((is_multisite() && ($new__user_id = c_ws_plugin__s2member_registrations::ms_create_existing_user($create_user["user_login"], $create_user["user_email"], $create_user["user_pass"]))) || ($new__user_id = wp_create_user($create_user["user_login"], $create_user["user_pass"], $create_user["user_email"]))) && !is_wp_error($new__user_id))
  506. {
  507. wp_new_user_notification($new__user_id, $create_user["user_pass"]);
  508. /**/
  509. $ipn["s2member_paypal_proxy_return_url"] = trim(c_ws_plugin__s2member_utils_urls::remote(site_url("/?s2member_paypal_notify=1".$ipn_q), $ipn, array("timeout" => 20)));
  510. /**/
  511. $global_response = array("response" => sprintf(_x('<strong>Thank you.</strong> Your account has been approved.<br />&mdash; Please <a href="%s" rel="nofollow">login</a>.', "s2member-front", "s2member"), esc_attr(wp_login_url())));
  512. /**/
  513. if($post_vars["attr"]["success"] && substr($ipn["s2member_paypal_proxy_return_url"], 0, 2) === substr($post_vars["attr"]["success"], 0, 2) && ($custom_success_url = str_ireplace(array("%%s_response%%", /* Deprecated in v111106 ». */ "%%response%%"), array(urlencode(c_ws_plugin__s2member_utils_encryption::encrypt($global_response["response"])), urlencode($global_response["response"])), $ipn["s2member_paypal_proxy_return_url"])) && ($custom_success_url = trim(preg_replace("/%%(.+?)%%/i", "", $custom_success_url))))
  514. wp_redirect(c_ws_plugin__s2member_utils_urls::add_s2member_sig($custom_success_url, "s2p-v")).exit();
  515. }
  516. else /* Else, an error reponse should be given. */
  517. {
  518. c_ws_plugin__s2member_utils_urls::remote(site_url("/?s2member_paypal_notify=1".$ipn_q), $ipn, array("timeout" => 20));
  519. /**/
  520. $global_response = array("response" => _x('<strong>Oops.</strong> A slight problem. Please contact Support for assistance.', "s2member-front", "s2member"), "error" => true);
  521. }
  522. }
  523. else /* Otherwise, they'll need to check their email for the auto-generated Password. */
  524. {
  525. if(((is_multisite() && ($new__user_id = c_ws_plugin__s2member_registrations::ms_create_existing_user($create_user["user_login"], $create_user["user_email"], $create_user["user_pass"]))) || ($new__user_id = wp_create_user($create_user["user_login"], $create_user["user_pass"], $create_user["user_email"]))) && !is_wp_error($new__user_id))
  526. {
  527. update_user_option($new__user_id, "default_password_nag", true, true); /* Password nag. */
  528. wp_new_user_notification($new__user_id, $create_user["user_pass"]);
  529. /**/
  530. $ipn["s2member_paypal_proxy_return_url"] = trim(c_ws_plugin__s2member_utils_urls::remote(site_url("/?s2member_paypal_notify=1".$ipn_q), $ipn, array("timeout" => 20)));
  531. /**/
  532. $global_response = array("response" => _x('<strong>Thank you.</strong> Your account has been approved.<br />&mdash; You\'ll receive an email momentarily.', "s2member-front", "s2member"));
  533. /**/
  534. if($post_vars["attr"]["success"] && substr($ipn["s2member_paypal_proxy_return_url"], 0, 2) === substr($post_vars["attr"]["success"], 0, 2) && ($custom_success_url = str_ireplace(array("%%s_response%%", /* Deprecated in v111106 ». */ "%%response%%"), array(urlencode(c_ws_plugin__s2member_utils_encryption::encrypt($global_response["response"])), urlencode($global_response["response"])), $ipn["s2member_paypal_proxy_return_url"])) && ($custom_success_url = trim(preg_replace("/%%(.+?)%%/i", "", $custom_success_url))))
  535. wp_redirect(c_ws_plugin__s2member_utils_urls::add_s2member_sig($custom_success_url, "s2p-v")).exit();
  536. }
  537. else /* Else, an error reponse should be given. */
  538. {
  539. c_ws_plugin__s2member_utils_urls::remote(site_url("/?s2member_paypal_notify=1".$ipn_q), $ipn, array("timeout" => 20));
  540. /**/
  541. $global_response = array("response" => _x('<strong>Oops.</strong> A slight problem. Please contact Support for assistance.', "s2member-front", "s2member"), "error" => true);
  542. }
  543. }
  544. }
  545. else /* Else, an error. */
  546. {
  547. $global_response = array("response" => $paypal["__error"], "error" => true);
  548. }
  549. }
  550. /**/
  551. else if(!$use_recurring_profile && is_user_logged_in() && is_object($user = wp_get_current_user()) && ($user_id = $user->ID))
  552. {
  553. if($is_independent_ccaps_sale || !($cur__subscr_id = get_user_option("s2member_subscr_id")) || !($paypal = c_ws_plugin__s2member_paypal_utilities::paypal_api_response(array("METHOD" => "GetRecurringPaymentsProfileDetails", "PROFILEID" => $cur__subscr_id))) || !empty($paypal["__error"]) || !preg_match("/^(Pending|PendingProfile)$/i", $paypal["STATUS"]))
  554. {
  555. update_user_meta($user_id, "first_name", $post_vars["first_name"]).update_user_meta($user_id, "last_name", $post_vars["last_name"]);
  556. /**/
  557. if(!($paypal = array())) /* Prepare a "Buy Now" transaction. */
  558. {
  559. if($_GET["s2member_paypal_xco"] === "s2member_pro_paypal_checkout_return" && !empty($_GET["token"]) && ($paypal_xco_details = array("METHOD" => "GetExpressCheckoutDetails", "TOKEN" => $_GET["token"])) && ($paypal_xco_details = c_ws_plugin__s2member_paypal_utilities::paypal_api_response($paypal_xco_details)) && empty($paypal_xco_details["__error"]))
  560. {
  561. $paypal["METHOD"] = "DoExpressCheckoutPayment";
  562. /**/
  563. $paypal["TOKEN"] = $paypal_xco_details["TOKEN"];
  564. $paypal["PAYERID"] = $paypal_xco_details["PAYERID"];
  565. /**/
  566. $paypal["PAYMENTREQUEST_0_PAYMENTACTION"] = "Sale";
  567. /**/
  568. $paypal["PAYMENTREQUEST_0_DESC"] = $cost_calculations["desc"];
  569. $paypal["PAYMENTREQUEST_0_CUSTOM"] = $post_vars["attr"]["custom"];
  570. /**/
  571. $paypal["PAYMENTREQUEST_0_CURRENCYCODE"] = $cost_calculations["cur"];
  572. $paypal["PAYMENTREQUEST_0_ITEMAMT"] = $cost_calculations["sub_total"];
  573. $paypal["PAYMENTREQUEST_0_TAXAMT"] = $cost_calculations["tax"];
  574. $paypal["PAYMENTREQUEST_0_AMT"] = $cost_calculations["total"];
  575. /**/
  576. $paypal["L_PAYMENTREQUEST_0_QTY0"] = "1"; /* Always (1). */
  577. $paypal["L_PAYMENTREQUEST_0_NAME0"] = $cost_calculations["desc"];
  578. $paypal["L_PAYMENTREQUEST_0_NUMBER0"] = $post_vars["attr"]["level_ccaps_eotper"];
  579. $paypal["L_PAYMENTREQUEST_0_AMT0"] = $cost_calculations["sub_total"];
  580. }
  581. else /* NOT using PayPal® Express Checkout. */
  582. {
  583. $paypal["METHOD"] = "DoDirectPayment";
  584. $paypal["PAYMENTACTION"] = "Sale";
  585. /**/
  586. $paypal["EMAIL"] = $user->user_email;
  587. $paypal["FIRSTNAME"] = $post_vars["first_name"];
  588. $paypal["LASTNAME"] = $post_vars["last_name"];
  589. $paypal["IPADDRESS"] = $_SERVER["REMOTE_ADDR"];
  590. /**/
  591. $paypal["DESC"] = $cost_calculations["desc"];
  592. $paypal["CUSTOM"] = $post_vars["attr"]["custom"];
  593. /**/
  594. $paypal["CURRENCYCODE"] = $cost_calculations["cur"];
  595. $paypal["ITEMAMT"] = $cost_calculations["sub_total"];
  596. $paypal["TAXAMT"] = $cost_calculations["tax"];
  597. $paypal["AMT"] = $cost_calculations["total"];
  598. /**/
  599. $paypal["L_QTY0"] = "1"; /* Always (1). */
  600. $paypal["L_NAME0"] = $cost_calculations["desc"];
  601. $paypal["L_NUMBER0"] = $post_vars["attr"]["level_ccaps_eotper"];
  602. $paypal["L_AMT0"] = $cost_calculations["sub_total"];
  603. /**/
  604. $paypal["CREDITCARDTYPE"] = $post_vars["card_type"];
  605. $paypal["ACCT"] = preg_replace("/[^0-9]/", "", $post_vars["card_number"]);
  606. $paypal["EXPDATE"] = preg_replace("/[^0-9]/", "", $post_vars["card_expiration"]);
  607. $paypal["CVV2"] = $post_vars["card_verification"];
  608. /**/
  609. if(in_array($post_vars["card_type"], array("Maestro", "Solo")))
  610. if(preg_match("/^[0-9]{2}\/[0-9]{4}$/", $post_vars["card_start_date_issue_number"]))
  611. $paypal["STARTDATE"] = preg_replace("/[^0-9]/", "", $post_vars["card_start_date_issue_number"]);
  612. else /* Otherwise, we assume they provided an Issue Number instead. */
  613. $paypal["ISSUENUMBER"] = $post_vars["card_start_date_issue_number"];
  614. /**/
  615. $paypal["STREET"] = $post_vars["street"];
  616. $paypal["CITY"] = $post_vars["city"];
  617. $paypal["STATE"] = $post_vars["state"];
  618. $paypal["COUNTRYCODE"] = $post_vars["country"];
  619. $paypal["ZIP"] = $post_vars["zip"];
  620. }
  621. }
  622. /**/
  623. if(($paypal = c_ws_plugin__s2member_paypal_utilities::paypal_api_response($paypal)) && empty($paypal["__error"]))
  624. {
  625. $old__subscr_id = get_user_option("s2member_subscr_id");
  626. $old__subscr_or_wp_id = c_ws_plugin__s2member_utils_users::get_user_subscr_or_wp_id();
  627. $new__subscr_id = $new__txn_id = (!empty($paypal["PAYMENTINFO_0_TRANSACTIONID"])) ? $paypal["PAYMENTINFO_0_TRANSACTIONID"] : false;
  628. $new__subscr_id = $new__txn_id = (!$new__subscr_id && !empty($paypal["TRANSACTIONID"])) ? $paypal["TRANSACTIONID"] : $new__subscr_id;
  629. /**/
  630. if(!($ipn = array())) /* Simulated PayPal® IPN. */
  631. {
  632. $ipn["txn_type"] = "web_accept";
  633. $ipn["txn_id"] = $new__subscr_id;
  634. $ipn["custom"] = $post_vars["attr"]["custom"];
  635. /**/
  636. $ipn["mc_gross"] = $cost_calculations["total"];
  637. $ipn["mc_currency"] = $cost_calculations["cur"];
  638. $ipn["tax"] = $cost_calculations["tax"];
  639. /**/
  640. $ipn["payer_email"] = $user->user_email;
  641. $ipn["first_name"] = $post_vars["first_name"];
  642. $ipn["last_name"] = $post_vars["last_name"];
  643. /**/
  644. $ipn["option_name1"] = "Referencing Customer ID";
  645. $ipn["option_selection1"] = $old__subscr_or_wp_id;
  646. /**/
  647. $ipn["option_name2"] = "Customer IP Address";
  648. $ipn["option_selection2"] = $_SERVER["REMOTE_ADDR"];
  649. /**/
  650. $ipn["item_name"] = $cost_calculations["desc"];
  651. $ipn["item_number"] = $post_vars["attr"]["level_ccaps_eotper"];
  652. /**/
  653. $ipn_q = "&s2member_paypal_proxy=paypal&s2member_paypal_proxy_use=pro-emails";
  654. $ipn_q .= "&s2member_paypal_proxy_verification=".urlencode(c_ws_plugin__s2member_paypal_utilities::paypal_proxy_key_gen());
  655. $ipn_q .= "&s2member_paypal_proxy_return_url=".rawurlencode($post_vars["attr"]["success"]);
  656. /**/
  657. $ipn["s2member_paypal_proxy_return_url"] = trim(c_ws_plugin__s2member_utils_urls::remote(site_url("/?s2member_paypal_notify=1".$ipn_q), $ipn, array("timeout" => 20)));
  658. }
  659. /**/
  660. if(!$is_independent_ccaps_sale) /* Independent? */
  661. if(!($paypal = array()) && ($paypal["PROFILEID"] = $old__subscr_id))
  662. {
  663. $paypal["METHOD"] = "ManageRecurringPaymentsProfileStatus";
  664. $paypal["ACTION"] = "Cancel";
  665. /**/
  666. c_ws_plugin__s2member_paypal_utilities::paypal_api_response($paypal);
  667. }
  668. /**/
  669. if($is_independent_ccaps_sale) /* Independent? */
  670. setcookie("s2member_tracking", ($s2member_tracking = c_ws_plugin__s2member_utils_encryption::encrypt($new__txn_id)), time() + 31556926, COOKIEPATH, COOKIE_DOMAIN).setcookie("s2member_tracking", $s2member_tracking, time() + 31556926, SITECOOKIEPATH, COOKIE_DOMAIN).($_COOKIE["s2member_tracking"] = $s2member_tracking);
  671. /**/
  672. $global_response = array("response" => sprintf(_x('<strong>Thank you.</strong> Your account has been updated.<br />&mdash; Please <a href="%s" rel="nofollow">log back in</a> now.', "s2member-front", "s2member"), esc_attr(wp_login_url())));
  673. /**/
  674. if($post_vars["attr"]["success"] && substr($ipn["s2member_paypal_proxy_return_url"], 0, 2) === substr($post_vars["attr"]["success"], 0, 2) && ($custom_success_url = str_ireplace(array("%%s_response%%", /* Deprecated in v111106 ». */ "%%response%%"), array(urlencode(c_ws_plugin__s2member_utils_encryption::encrypt($global_response["response"])), urlencode($global_response["response"])), $ipn["s2member_paypal_proxy_return_url"])) && ($custom_success_url = trim(preg_replace("/%%(.+?)%%/i", "", $custom_success_url))))
  675. wp_redirect(c_ws_plugin__s2member_utils_urls::add_s2member_sig($custom_success_url, "s2p-v")).exit();
  676. }
  677. else /* Else, an error. */
  678. {
  679. $global_response = array("response" => $paypal["__error"], "error" => true);
  680. }
  681. }
  682. else /* Else, an error. The existing Billing Profile is still in a pending state. */
  683. {
  684. $global_response = array("response" => _x('<strong>Sorry.</strong> Your account is pending other changes. Please try again in 15 minutes.', "s2member-front", "s2member"), "error" => true);
  685. }
  686. }
  687. /**/
  688. else if(!$use_recurring_profile && !is_user_logged_in())
  689. {
  690. if(!($paypal = array())) /* Prepare a "Buy Now" transaction. */
  691. {
  692. if($_GET["s2member_paypal_xco"] === "s2member_pro_paypal_checkout_return" && !empty($_GET["token"]) && ($paypal_xco_details = array("METHOD" => "GetExpressCheckoutDetails", "TOKEN" => $_GET["token"])) && ($paypal_xco_details = c_ws_plugin__s2member_paypal_utilities::paypal_api_response($paypal_xco_details)) && empty($paypal_xco_details["__error"]))
  693. {
  694. $paypal["METHOD"] = "DoExpressCheckoutPayment";
  695. /**/
  696. $paypal["TOKEN"] = $paypal_xco_details["TOKEN"];
  697. $paypal["PAYERID"] = $paypal_xco_details["PAYERID"];
  698. /**/
  699. $paypal["PAYMENTREQUEST_0_PAYMENTACTION"] = "Sale";
  700. /**/
  701. $paypal["PAYMENTREQUEST_0_DESC"] = $cost_calculations["desc"];
  702. $paypal["PAYMENTREQUEST_0_CUSTOM"] = $post_vars["attr"]["custom"];
  703. /**/
  704. $paypal["PAYMENTREQUEST_0_CURRENCYCODE"] = $cost_calculations["cur"];
  705. $paypal["PAYMENTREQUEST_0_ITEMAMT"] = $cost_calculations["sub_total"];
  706. $paypal["PAYMENTREQUEST_0_TAXAMT"] = $cost_calculations["tax"];
  707. $paypal["PAYMENTREQUEST_0_AMT"] = $cost_calculations["total"];
  708. /**/
  709. $paypal["L_PAYMENTREQUEST_0_QTY0"] = "1"; /* Always (1). */
  710. $paypal["L_PAYMENTREQUEST_0_NAME0"] = $cost_calculations["desc"];
  711. $paypal["L_PAYMENTREQUEST_0_NUMBER0"] = $post_vars["attr"]["level_ccaps_eotper"];
  712. $paypal["L_PAYMENTREQUEST_0_AMT0"] = $cost_calculations["sub_total"];
  713. }
  714. else /* NOT using PayPal® Express Checkout. */
  715. {
  716. $paypal["METHOD"] = "DoDirectPayment";
  717. $paypal["PAYMENTACTION"] = "Sale";
  718. /**/
  719. $paypal["EMAIL"] = $post_vars["email"];
  720. $paypal["FIRSTNAME"] = $post_vars["first_name"];
  721. $paypal["LASTNAME"] = $post_vars["last_name"];
  722. $paypal["IPADDRESS"] = $_SERVER["REMOTE_ADDR"];
  723. /**/
  724. $paypal["DESC"] = $cost_calculations["desc"];
  725. $paypal["CUSTOM"] = $post_vars["attr"]["custom"];
  726. /**/
  727. $paypal["CURRENCYCODE"] = $cost_calculations["cur"];
  728. $paypal["ITEMAMT"] = $cost_calculations["sub_total"];
  729. $paypal["TAXAMT"] = $cost_calculations["tax"];
  730. $paypal["AMT"] = $cost_calculations["total"];
  731. /**/
  732. $paypal["L_QTY0"] = "1"; /* Always (1). */
  733. $paypal["L_NAME0"] = $cost_calculations["desc"];
  734. $paypal["L_NUMBER0"] = $post_vars["attr"]["level_ccaps_eotper"];
  735. $paypal["L_AMT0"] = $cost_calculations["sub_total"];
  736. /**/
  737. $paypal["CREDITCARDTYPE"] = $post_vars["card_type"];
  738. $paypal["ACCT"] = preg_replace("/[^0-9]/", "", $post_vars["card_number"]);
  739. $paypal["EXPDATE"] = preg_replace("/[^0-9]/", "", $post_vars["card_expiration"]);
  740. $paypal["CVV2"] = $post_vars["card_verification"];
  741. /**/
  742. if(in_array($post_vars["card_type"], array("Maestro", "Solo")))
  743. if(preg_match("/^[0-9]{2}\/[0-9]{4}$/", $post_vars["card_start_date_issue_number"]))
  744. $paypal["STARTDATE"] = preg_replace("/[^0-9]/", "", $post_vars["card_start_date_issue_number"]);
  745. else /* Otherwise, we assume they provided an Issue Number instead. */
  746. $paypal["ISSUENUMBER"] = $post_vars["card_start_date_issue_number"];
  747. /**/
  748. $paypal["STREET"] = $post_vars["street"];
  749. $paypal["CITY"] = $post_vars["city"];
  750. $paypal["STATE"] = $post_vars["state"];
  751. $paypal["COUNTRYCODE"] = $post_vars["country"];
  752. $paypal["ZIP"] = $post_vars["zip"];
  753. }
  754. }
  755. /**/
  756. if(($paypal = c_ws_plugin__s2member_paypal_utilities::paypal_api_response($paypal)) && empty($paypal["__error"]))
  757. {
  758. $new__subscr_id = (!empty($paypal["PAYMENTINFO_0_TRANSACTIONID"])) ? $paypal["PAYMENTINFO_0_TRANSACTIONID"] : false;
  759. $new__subscr_id = (!$new__subscr_id && !empty($paypal["TRANSACTIONID"])) ? $paypal["TRANSACTIONID"] : $new__subscr_id;
  760. /**/
  761. if(!($ipn = array())) /* Simulated PayPal® IPN. */
  762. {
  763. $ipn["txn_type"] = "web_accept";
  764. $ipn["txn_id"] = $new__subscr_id;
  765. $ipn["custom"] = $post_vars["attr"]["custom"];
  766. /**/
  767. $ipn["mc_gross"] = $cost_calculations["total"];
  768. $ipn["mc_currency"] = $cost_calculations["cur"];
  769. $ipn["tax"] = $cost_calculations["tax"];
  770. /**/
  771. $ipn["payer_email"] = $post_vars["email"];
  772. $ipn["first_name"] = $post_vars["first_name"];
  773. $ipn["last_name"] = $post_vars["last_name"];
  774. /**/
  775. $ipn["option_name1"] = "Originating Domain";
  776. $ipn["option_selection1"] = $_SERVER["HTTP_HOST"];
  777. /**/
  778. $ipn["option_name2"] = "Customer IP Address";
  779. $ipn["option_selection2"] = $_SERVER["REMOTE_ADDR"];
  780. /**/
  781. $ipn["item_name"] = $cost_calculations["desc"];
  782. $ipn["item_number"] = $post_vars["attr"]["level_ccaps_eotper"];
  783. /**/
  784. $ipn_q = "&s2member_paypal_proxy=paypal&s2member_paypal_proxy_use=pro-emails";
  785. $ipn_q .= "&s2member_paypal_proxy_verification=".urlencode(c_ws_plugin__s2member_paypal_utilities::paypal_proxy_key_gen());
  786. $ipn_q .= "&s2member_paypal_proxy_return_url=".rawurlencode($post_vars["attr"]["success"]);
  787. }
  788. /**/
  789. if(!($create_user = array())) /* Build post fields for registration configuration, and then the creation array. */
  790. {
  791. $_POST["ws_plugin__s2member_custom_reg_field_user_pass1"] = $post_vars["password1"]; /* Fake this for registration configuration. */
  792. $_POST["ws_plugin__s2member_custom_reg_field_first_name"] = $post_vars["first_name"]; /* Fake this for registration configuration. */
  793. $_POST["ws_plugin__s2member_custom_reg_field_last_name"] = $post_vars["last_name"]; /* Fake this for registration configuration. */
  794. $_POST["ws_plugin__s2member_custom_reg_field_opt_in"] = $post_vars["custom_fields"]["opt_in"]; /* Fake this too. */
  795. /**/
  796. if($GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["custom_reg_fields"])
  797. foreach(json_decode($GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["custom_reg_fields"], true) as $field)
  798. {
  799. $field_var = preg_replace("/[^a-z0-9]/i", "_", strtolower($field["id"]));
  800. $field_id_class = preg_replace("/_/", "-", $field_var);
  801. /**/
  802. if(isset($post_vars["custom_fields"][$field_var]))
  803. $_POST["ws_plugin__s2member_custom_reg_field_".$field_var] = $post_vars["custom_fields"][$field_var];
  804. }
  805. /**/
  806. $_COOKIE["s2member_subscr_gateway"] = c_ws_plugin__s2member_utils_encryption::encrypt("paypal"); /* Fake this for registration configuration. */
  807. $_COOKIE["s2member_subscr_id"] = c_ws_plugin__s2member_utils_encryption::encrypt($new__subscr_id); /* Fake this for registration configuration. */
  808. $_COOKIE["s2member_custom"] = c_ws_plugin__s2member_utils_encryption::encrypt($post_vars["attr"]["custom"]); /* Fake this for registration configuration. */
  809. $_COOKIE["s2member_item_number"] = c_ws_plugin__s2member_utils_encryption::encrypt($post_vars["attr"]["level_ccaps_eotper"]); /* Fake this too. */
  810. /**/
  811. $create_user["user_login"] = $post_vars["username"]; /* Copy this into a separate array for `wp_create_user()`. */
  812. $create_user["user_pass"] = wp_generate_password(); /* Which may fire `c_ws_plugin__s2member_registrations::generate_password()`. */
  813. $create_user["user_email"] = $post_vars["email"]; /* Copy this into a separate array for `wp_create_user()`. */
  814. }
  815. /**/
  816. if($post_vars["password1"] && $post_vars["password1"] === $create_user["user_pass"]) /* A custom Password is being used? */
  817. {
  818. if(((is_multisite() && ($new__user_id = c_ws_plugin__s2member_registrations::ms_create_existing_user($create_user["user_login"], $create_user["user_email"], $create_user["user_pass"]))) || ($new__user_id = wp_create_user($create_user["user_login"], $create_user["user_pass"], $create_user["user_email"]))) && !is_wp_error($new__user_id))
  819. {
  820. wp_new_user_notification($new__user_id, $create_user["user_pass"]);
  821. /**/
  822. $ipn["s2member_paypal_proxy_return_url"] = trim(c_ws_plugin__s2member_utils_urls::remote(site_url("/?s2member_paypal_notify=1".$ipn_q), $ipn, array("timeout" => 20)));
  823. /**/
  824. $global_response = array("response" => sprintf(_x('<strong>Thank you.</strong> Your account has been approved.<br />&mdash; Please <a href="%s" rel="nofollow">login</a>.', "s2member-front", "s2member"), esc_attr(wp_login_url())));
  825. /**/
  826. if($post_vars["attr"]["success"] && substr($ipn["s2member_paypal_proxy_return_url"], 0, 2) === substr($post_vars["attr"]["success"], 0, 2) && ($custom_success_url = str_ireplace(array("%%s_response%%", /* Deprecated in v111106 ». */ "%%response%%"), array(urlencode(c_ws_plugin__s2member_utils_encryption::encrypt($global_response["response"])), urlencode($global_response["response"])), $ipn["s2member_paypal_proxy_return_url"])) && ($custom_success_url = trim(preg_replace("/%%(.+?)%%/i", "", $custom_success_url))))
  827. wp_redirect(c_ws_plugin__s2member_utils_urls::add_s2member_sig($custom_success_url, "s2p-v")).exit();
  828. }
  829. else /* Else, an error reponse should be given. */
  830. {
  831. c_ws_plugin__s2member_utils_urls::remote(site_url("/?s2member_paypal_notify=1".$ipn_q), $ipn, array("timeout" => 20));
  832. /**/
  833. $global_response = array("response" => _x('<strong>Oops.</strong> A slight problem. Please contact Support for assistance.', "s2member-front", "s2member"), "error" => true);
  834. }
  835. }
  836. else /* Otherwise, they'll need to check their email for the auto-generated Password. */
  837. {
  838. if(((is_multisite() && ($new__user_id = c_ws_plugin__s2member_registrations::ms_create_existing_user($create_user["user_login"], $create_user["user_email"], $create_user["user_pass"]))) || ($new__user_id = wp_create_user($create_user["user_login"], $create_user["user_pass"], $create_user["user_email"]))) && !is_wp_error($new__user_id))
  839. {
  840. update_user_option($new__user_id, "default_password_nag", true, true); /* Password nag. */
  841. wp_new_user_notification($new__user_id, $create_user["user_pass"]);
  842. /**/
  843. $ipn["s2member_paypal_proxy_return_url"] = trim(c_ws_plugin__s2member_utils_urls::remote(site_url("/?s2member_paypal_notify=1".$ipn_q), $ipn, array("timeout" => 20)));
  844. /**/
  845. $global_response = array("response" => _x('<strong>Thank you.</strong> Your account has been approved.<br />&mdash; You\'ll receive an email momentarily.', "s2member-front", "s2member"));
  846. /**/
  847. if($post_vars["attr"]["success"] && substr($ipn["s2member_paypal_proxy_return_url"], 0, 2) === substr($post_vars["attr"]["success"], 0, 2) && ($custom_success_url = str_ireplace(array("%%s_response%%", /* Deprecated in v111106 ». */ "%%response%%"), array(urlencode(c_ws_plugin__s2member_utils_encryption::encrypt($global_response["response"])), urlencode($global_response["response"])), $ipn["s2member_paypal_proxy_return_url"])) && ($custom_success_url = trim(preg_replace("/%%(.+?)%%/i", "", $custom_success_url))))
  848. wp_redirect(c_ws_plugin__s2member_utils_urls::add_s2member_sig($custom_success_url, "s2p-v")).exit();
  849. }
  850. else /* Else, an error reponse should be given. */
  851. {
  852. c_ws_plugin__s2member_utils_urls::remote(site_url("/?s2member_paypal_notify=1".$ipn_q), $ipn, array("timeout" => 20));
  853. /**/
  854. $global_response = array("response" => _x('<strong>Oops.</strong> A slight problem. Please contact Support for assistance.', "s2member-front", "s2member"), "error" => true);
  855. }
  856. }
  857. }
  858. else /* Else, an error. */
  859. {
  860. $global_response = array("response" => $paypal["__error"], "error" => true);
  861. }
  862. }
  863. else /* Else, we have an unknown scenario. */
  864. {
  865. $global_response = array("response" => _x('<strong>Unknown error.</strong> Please contact Support for assistance.', "s2member-front", "s2member"), "error" => true);
  866. }
  867. }
  868. else /* Else, an error. */
  869. {
  870. $global_response = $error;
  871. }
  872. }
  873. }
  874. }
  875. }
  876. }
  877. ?>

s2member-pro\includes\classes\gateways\paypal\paypal-checkout-pf-in.inc.php

Source Code Documentation