Source Code Documentation


pro\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"] = (is_multisite()) ? strtolower($post_vars["username"]) : $post_vars["username"]; // Force lowercase.
  80. $post_vars["username"] = preg_replace("/\s+/", "", sanitize_user(($post_vars["_o_username"] = $post_vars["username"]), is_multisite()));
  81.  
  82. (!empty($_GET["token"])) ? delete_transient("s2m_".md5("s2member_transient_express_checkout_".$_GET["token"])) : null;
  83.  
  84. if(!c_ws_plugin__s2member_pro_paypal_responses::paypal_form_attr_validation_errors($post_vars["attr"])) // Attr errors?
  85. {
  86. if(!($error = c_ws_plugin__s2member_pro_paypal_responses::paypal_form_submission_validation_errors("checkout", $post_vars)))
  87. {
  88. $cp_attr = c_ws_plugin__s2member_pro_paypal_utilities::paypal_apply_coupon($post_vars["attr"], $post_vars["coupon"], "attr", array("affiliates-silent-post"));
  89. $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"]);
  90. $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"]);
  91.  
  92. $use_recurring_profile = ($post_vars["attr"]["rr"] === "BN" || (!$post_vars["attr"]["tp"] && !$post_vars["attr"]["rr"])) ? false : true;
  93. $is_independent_ccaps_sale = ($post_vars["attr"]["level"] === "*") ? true : false; // Selling Independent Custom Capabilities?
  94.  
  95. if(empty($_GET["s2member_paypal_xco"]) && $post_vars["card_type"] === "PayPal")
  96. {
  97. $return_url = $cancel_url = (is_ssl()) ? "https://" : "http://";
  98. $return_url = $cancel_url = ($return_url = $cancel_url).$_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"];
  99. $return_url = $cancel_url = /* Ditch. */ remove_query_arg(array("token", "PayerID"), ($return_url = $cancel_url));
  100. $return_url = add_query_arg("s2member_paypal_xco", urlencode("s2member_pro_paypal_checkout_return"), $return_url);
  101. $cancel_url = add_query_arg("s2member_paypal_xco", urlencode("s2member_pro_paypal_checkout_cancel"), $cancel_url);
  102.  
  103. $user = (is_user_logged_in() && is_object($user = wp_get_current_user()) && ($user_id = $user->ID)) ? $user : false;
  104.  
  105. if(!($paypal_set_xco = array()) /* PayPal® Express Checkout. */)
  106. {
  107. if($use_recurring_profile /* Use Payflow® API instead? */)
  108. {
  109. $paypal_set_xco["TRXTYPE"] = "A";
  110. $paypal_set_xco["ACTION"] = "S";
  111. $paypal_set_xco["TENDER"] = "P";
  112.  
  113. $paypal_set_xco["RETURNURL"] = $return_url;
  114. $paypal_set_xco["CANCELURL"] = $cancel_url;
  115.  
  116. $paypal_set_xco["PAGESTYLE"] = $post_vars["attr"]["ps"];
  117. $paypal_set_xco["LOCALECODE"] = $post_vars["attr"]["lc"];
  118. $paypal_set_xco["NOSHIPPING"] = $post_vars["attr"]["ns"];
  119. $paypal_set_xco["ALLOWNOTE"] = "0";
  120.  
  121. $paypal_set_xco["AMT"] = "0.00";
  122. $paypal_set_xco["CURRENCY"] = $cost_calculations["cur"];
  123. $paypal_set_xco["PAYMENTTYPE"] = "any";
  124.  
  125. $paypal_set_xco["ORDERDESC"] = $cost_calculations["desc"];
  126.  
  127. $paypal_set_xco["BILLINGTYPE"] = "RecurringBilling";
  128. $paypal_set_xco["BA_DESC"] = $cost_calculations["desc"];
  129. $paypal_set_xco["BA_CUSTOM"] = $_SERVER["HTTP_HOST"];
  130.  
  131. $paypal_set_xco["ADDROVERRIDE"] = "1";
  132. $paypal_set_xco["SHIPTONAME"] = $post_vars["name"];
  133. $paypal_set_xco["SHIPTOSTREET"] = $post_vars["street"];
  134. $paypal_set_xco["SHIPTOCITY"] = $post_vars["city"];
  135. $paypal_set_xco["SHIPTOSTATE"] = $post_vars["state"];
  136. $paypal_set_xco["SHIPTOCOUNTRY"] = $post_vars["country"];
  137. $paypal_set_xco["SHIPTOZIP"] = $post_vars["zip"];
  138.  
  139. $paypal_set_xco["EMAIL"] = ($user) ? $user->user_email : $post_vars["email"];
  140.  
  141. if(($paypal_set_xco = c_ws_plugin__s2member_paypal_utilities::paypal_payflow_api_response($paypal_set_xco)) && empty($paypal_set_xco["__error"]))
  142. {
  143. set_transient("s2m_".md5("s2member_transient_express_checkout_".$paypal_set_xco["TOKEN"]), $post_vars, 10800);
  144.  
  145. $endpoint = ($GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["paypal_sandbox"]) ? "www.sandbox.paypal.com" : "www.paypal.com";
  146.  
  147. wp_redirect(add_query_arg("token", urlencode($paypal_set_xco["TOKEN"]), "https://".$endpoint."/cgi-bin/webscr?cmd=_express-checkout"));
  148.  
  149. exit /* Clean exit. */();
  150. }
  151. else // Else, an error.
  152. {
  153. $global_response = array("response" => $paypal_set_xco["__error"], "error" => true);
  154. }
  155. }
  156.  
  157. else // We use the PayPal® Pro API, because this is NOT going to recur.
  158. {
  159. $paypal_set_xco["METHOD"] = "SetExpressCheckout";
  160.  
  161. $paypal_set_xco["RETURNURL"] = $return_url;
  162. $paypal_set_xco["CANCELURL"] = $cancel_url;
  163.  
  164. $paypal_set_xco["PAGESTYLE"] = $post_vars["attr"]["ps"];
  165. $paypal_set_xco["LOCALECODE"] = $post_vars["attr"]["lc"];
  166. $paypal_set_xco["NOSHIPPING"] = $post_vars["attr"]["ns"];
  167. $paypal_set_xco["ALLOWNOTE"] = "0"; // No notes.
  168.  
  169. $paypal_set_xco["PAYMENTREQUEST_0_PAYMENTACTION"] = "Sale";
  170.  
  171. $paypal_set_xco["PAYMENTREQUEST_0_DESC"] = $cost_calculations["desc"];
  172. $paypal_set_xco["PAYMENTREQUEST_0_CUSTOM"] = $post_vars["attr"]["custom"];
  173.  
  174. $paypal_set_xco["PAYMENTREQUEST_0_CURRENCYCODE"] = $cost_calculations["cur"];
  175. $paypal_set_xco["PAYMENTREQUEST_0_ITEMAMT"] = $cost_calculations["sub_total"];
  176. $paypal_set_xco["PAYMENTREQUEST_0_TAXAMT"] = $cost_calculations["tax"];
  177. $paypal_set_xco["PAYMENTREQUEST_0_AMT"] = $cost_calculations["total"];
  178.  
  179. $paypal_set_xco["L_PAYMENTREQUEST_0_QTY0"] = "1"; // Always (1).
  180. $paypal_set_xco["L_PAYMENTREQUEST_0_NAME0"] = $cost_calculations["desc"];
  181. $paypal_set_xco["L_PAYMENTREQUEST_0_NUMBER0"] = $post_vars["attr"]["level_ccaps_eotper"];
  182. $paypal_set_xco["L_PAYMENTREQUEST_0_AMT0"] = $cost_calculations["sub_total"];
  183.  
  184. $paypal_set_xco["PAYMENTREQUEST_0_SHIPTONAME"] = $post_vars["name"];
  185. $paypal_set_xco["PAYMENTREQUEST_0_SHIPTOSTREET"] = $post_vars["street"];
  186. $paypal_set_xco["PAYMENTREQUEST_0_SHIPTOCITY"] = $post_vars["city"];
  187. $paypal_set_xco["PAYMENTREQUEST_0_SHIPTOSTATE"] = $post_vars["state"];
  188. $paypal_set_xco["PAYMENTREQUEST_0_SHIPTOCOUNTRYCODE"] = $post_vars["country"];
  189. $paypal_set_xco["PAYMENTREQUEST_0_SHIPTOZIP"] = $post_vars["zip"];
  190.  
  191. $paypal_set_xco["EMAIL"] = ($user) ? $user->user_email : $post_vars["email"];
  192.  
  193. if(($paypal_set_xco = c_ws_plugin__s2member_paypal_utilities::paypal_api_response($paypal_set_xco)) && empty($paypal_set_xco["__error"]))
  194. {
  195. set_transient("s2m_".md5("s2member_transient_express_checkout_".$paypal_set_xco["TOKEN"]), $post_vars, 10800);
  196.  
  197. $endpoint = ($GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["paypal_sandbox"]) ? "www.sandbox.paypal.com" : "www.paypal.com";
  198.  
  199. wp_redirect(add_query_arg("token", urlencode($paypal_set_xco["TOKEN"]), "https://".$endpoint."/cgi-bin/webscr?cmd=_express-checkout"));
  200.  
  201. exit /* Clean exit. */();
  202. }
  203. else // Else, an error.
  204. {
  205. $global_response = array("response" => $paypal_set_xco["__error"], "error" => true);
  206. }
  207. }
  208. }
  209. }
  210.  
  211. else if($use_recurring_profile && is_user_logged_in() && is_object($user = wp_get_current_user()) && ($user_id = $user->ID))
  212. {
  213. if(($old__subscr_id = get_user_option("s2member_subscr_id")))
  214. $paypal = c_ws_plugin__s2member_pro_paypal_utilities::payflow_get_profile($old__subscr_id);
  215. $old__baid = (!empty($paypal) && !empty($paypal["BAID"])) ? $paypal["BAID"] : "";
  216. $old__subscr_or_wp_id = c_ws_plugin__s2member_utils_users::get_user_subscr_or_wp_id();
  217.  
  218. $period1 = c_ws_plugin__s2member_paypal_utilities::paypal_pro_period1($post_vars["attr"]["tp"]." ".$post_vars["attr"]["tt"]);
  219. $period3 = c_ws_plugin__s2member_paypal_utilities::paypal_pro_period3($post_vars["attr"]["rp"]." ".$post_vars["attr"]["rt"]);
  220.  
  221. $start_time = ($post_vars["attr"]["tp"]) ? // If there's an Initial/Trial Period; start when it's over.
  222. c_ws_plugin__s2member_pro_paypal_utilities::paypal_start_time($period1) : // After Trial is over.
  223. c_ws_plugin__s2member_pro_paypal_utilities::paypal_start_time($period3); // Or next billing cycle.
  224.  
  225. $reference = $start_time.":".$period1.":".$period3."~".$_SERVER["HTTP_HOST"]."~".$post_vars["attr"]["level_ccaps_eotper"];
  226.  
  227. update_user_meta($user_id, "first_name", $post_vars["first_name"]).update_user_meta($user_id, "last_name", $post_vars["last_name"]);
  228.  
  229. if(!($paypal = array()) /* Recurring Profile. */)
  230. {
  231. $paypal["TRXTYPE"] = "R";
  232. $paypal["ACTION"] = "A";
  233.  
  234. $paypal["EMAIL"] = $user->user_email;
  235. $paypal["FIRSTNAME"] = $post_vars["first_name"];
  236. $paypal["LASTNAME"] = $post_vars["last_name"];
  237. $paypal["CLIENTIP"] = $_SERVER["REMOTE_ADDR"];
  238.  
  239. $paypal["PROFILENAME"] = $reference;
  240. $paypal["DESC"] = $cost_calculations["desc"];
  241.  
  242. if(!$post_vars["attr"]["tp"] || ($post_vars["attr"]["tp"] && $cost_calculations["trial_total"] > 0))
  243. {
  244. $paypal["OPTIONALTRX"] = "S";
  245. $paypal["OPTIONALTRXAMT"] = ($post_vars["attr"]["tp"]) ? $cost_calculations["trial_total"] : $cost_calculations["total"];
  246. $paypal["FAILEDOPTIONALTRXACTION"] = "CancelOnFailure";
  247. $paypal["FAILEDINITAMTACTION"] = "CancelOnFailure";
  248. }
  249.  
  250. $paypal["CURRENCY"] = $cost_calculations["cur"];
  251. $paypal["AMT"] = $cost_calculations["sub_total"];
  252. $paypal["TAXAMT"] = $cost_calculations["tax"];
  253.  
  254. $paypal["MAXFAILPAYMENTS"] = $post_vars["attr"]["rra"];
  255. $paypal["AUTOBILLOUTSTANDINGAMT"] = apply_filters("ws_plugin__s2member_pro_paypal_auto_bill_op", "AddToNextBilling", get_defined_vars());
  256.  
  257. $paypal["START"] = date("mdY", $start_time);
  258.  
  259. $paypal["PAYPERIOD"] = c_ws_plugin__s2member_paypal_utilities::paypal_payflow_term($post_vars["attr"]["rt"], $post_vars["attr"]["rp"]);
  260. $paypal["TERM"] = ($post_vars["attr"]["rr"]) ? (($post_vars["attr"]["rrt"]) ? $post_vars["attr"]["rrt"] : "0") : "1";
  261.  
  262. if($_GET["s2member_paypal_xco"] === "s2member_pro_paypal_checkout_return" && !empty($_GET["token"])
  263.  
  264. && ($paypal_xco_details = array("TRXTYPE" => "A", "ACTION" => "G", "TENDER" => "P", "TOKEN" => $_GET["token"]))
  265. && ($paypal_xco_details = c_ws_plugin__s2member_paypal_utilities::paypal_payflow_api_response($paypal_xco_details))
  266. && empty($paypal_xco_details["__error"])
  267.  
  268. && ($paypal_xco_bagree = array("TRXTYPE" => "A", "ACTION" => "X", "TENDER" => "P", "TOKEN" => $paypal_xco_details["TOKEN"]))
  269. && ($paypal_xco_bagree = c_ws_plugin__s2member_paypal_utilities::paypal_payflow_api_response($paypal_xco_bagree))
  270. && empty($paypal_xco_bagree["__error"]))
  271. {
  272. $paypal["TENDER"] = "P";
  273. $paypal["PAYERID"] = $paypal_xco_details["PAYERID"];
  274. $paypal["BAID"] = $paypal_xco_bagree["BAID"];
  275. }
  276. else if($_GET["s2member_paypal_xco"] !== "s2member_pro_paypal_checkout_return")
  277. {
  278. $paypal["TENDER"] = "C";
  279. $paypal["ACCT"] = preg_replace("/[^0-9]/", "", $post_vars["card_number"]);
  280. if(preg_match("/^(?P<month>[0-9]{2})\/[0-9]{2}(?P<year_suffix>[0-9]{2})$/", $post_vars["card_expiration"], $_m))
  281. $paypal["EXPDATE"] = $_m["month"].$_m["year_suffix"];
  282. $paypal["CVV2"] = $post_vars["card_verification"];
  283.  
  284. if(in_array($post_vars["card_type"], array("Maestro", "Solo")))
  285. {
  286. if(preg_match("/^(?P<month>[0-9]{2})\/[0-9]{2}(?P<year>[0-9]{2})$/", $post_vars["card_start_date_issue_number"], $_m))
  287. $paypal["CARDSTART"] = $_m["month"].$_m["year"];
  288. else
  289. $paypal["CARDISSUE"] = $post_vars["card_start_date_issue_number"];
  290. unset /* A little housekeeping. */($_m);
  291. }
  292.  
  293. $paypal["STREET"] = $post_vars["street"];
  294. $paypal["CITY"] = $post_vars["city"];
  295. $paypal["STATE"] = $post_vars["state"];
  296. $paypal["COUNTRY"] = $post_vars["country"];
  297. $paypal["ZIP"] = $post_vars["zip"];
  298. }
  299. }
  300.  
  301. if(($paypal = c_ws_plugin__s2member_paypal_utilities::paypal_payflow_api_response($paypal)) && empty($paypal["__error"]))
  302. {
  303. $new__subscr_id = $paypal["PROFILEID"];
  304.  
  305. if(!($ipn = array())) // Simulated PayPal® IPN.
  306. {
  307. $ipn["txn_type"] = "subscr_signup";
  308. $ipn["subscr_id"] = $new__subscr_id;
  309. $ipn["custom"] = $post_vars["attr"]["custom"];
  310.  
  311. $ipn["txn_id"] = $new__subscr_id;
  312.  
  313. $ipn["period1"] = $period1;
  314. $ipn["period3"] = $period3;
  315.  
  316. $ipn["mc_amount1"] = $cost_calculations["trial_total"];
  317. $ipn["mc_amount3"] = $cost_calculations["total"];
  318.  
  319. $ipn["mc_gross"] = (preg_match("/^[1-9]/", $ipn["period1"])) ? $ipn["mc_amount1"] : $ipn["mc_amount3"];
  320.  
  321. $ipn["mc_currency"] = $cost_calculations["cur"];
  322. $ipn["tax"] = $cost_calculations["tax"];
  323.  
  324. $ipn["recurring"] = ($post_vars["attr"]["rr"]) ? "1" : "";
  325.  
  326. $ipn["payer_email"] = $user->user_email;
  327. $ipn["first_name"] = $post_vars["first_name"];
  328. $ipn["last_name"] = $post_vars["last_name"];
  329.  
  330. $ipn["option_name1"] = "Referencing Customer ID";
  331. $ipn["option_selection1"] = $old__subscr_or_wp_id;
  332.  
  333. $ipn["option_name2"] = "Customer IP Address";
  334. $ipn["option_selection2"] = $_SERVER["REMOTE_ADDR"];
  335.  
  336. $ipn["item_name"] = $cost_calculations["desc"];
  337. $ipn["item_number"] = $post_vars["attr"]["level_ccaps_eotper"];
  338.  
  339. $ipn["s2member_paypal_proxy"] = "paypal";
  340. $ipn["s2member_paypal_proxy_use"] = "pro-emails";
  341. $ipn["s2member_paypal_proxy_use"] .= ($ipn["mc_gross"] > 0) ? ",subscr-signup-as-subscr-payment" : "";
  342. $ipn["s2member_paypal_proxy_coupon"] = array("coupon_code" => $cp_attr["_coupon_code"], "full_coupon_code" => $cp_attr["_full_coupon_code"], "affiliate_id" => $cp_attr["_coupon_affiliate_id"]);
  343. $ipn["s2member_paypal_proxy_verification"] = c_ws_plugin__s2member_paypal_utilities::paypal_proxy_key_gen();
  344. $ipn["s2member_paypal_proxy_return_url"] = $post_vars["attr"]["success"];
  345.  
  346. $ipn["s2member_paypal_proxy_return_url"] = trim(c_ws_plugin__s2member_utils_urls::remote(site_url("/?s2member_paypal_notify=1"), $ipn, array("timeout" => 20)));
  347. }
  348.  
  349. if($old__subscr_id) // There is an old Recurring Profile?
  350. c_ws_plugin__s2member_pro_paypal_utilities::payflow_cancel_profile($old__subscr_id, $old__baid);
  351.  
  352. setcookie("s2member_tracking", ($s2member_tracking = c_ws_plugin__s2member_utils_encryption::encrypt($new__subscr_id)), time() + 31556926, COOKIEPATH, COOKIE_DOMAIN).setcookie("s2member_tracking", $s2member_tracking, time() + 31556926, SITECOOKIEPATH, COOKIE_DOMAIN).($_COOKIE["s2member_tracking"] = $s2member_tracking);
  353.  
  354. $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())));
  355.  
  356. 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))))
  357. wp_redirect(c_ws_plugin__s2member_utils_urls::add_s2member_sig($custom_success_url, "s2p-v")).exit();
  358. }
  359. else // Else, an error.
  360. {
  361. $global_response = array("response" => $paypal["__error"], "error" => true);
  362. }
  363. }
  364.  
  365. else if($use_recurring_profile && !is_user_logged_in()) // Create a new account.
  366. {
  367. $period1 = c_ws_plugin__s2member_paypal_utilities::paypal_pro_period1($post_vars["attr"]["tp"]." ".$post_vars["attr"]["tt"]);
  368. $period3 = c_ws_plugin__s2member_paypal_utilities::paypal_pro_period3($post_vars["attr"]["rp"]." ".$post_vars["attr"]["rt"]);
  369.  
  370. $start_time = ($post_vars["attr"]["tp"]) ? // If there's an Initial/Trial Period; start when it's over.
  371. c_ws_plugin__s2member_pro_paypal_utilities::paypal_start_time($period1) : // After Trial is over.
  372. c_ws_plugin__s2member_pro_paypal_utilities::paypal_start_time($period3); // Or next billing cycle.
  373.  
  374. $reference = $start_time.":".$period1.":".$period3."~".$_SERVER["HTTP_HOST"]."~".$post_vars["attr"]["level_ccaps_eotper"];
  375.  
  376. if(!($paypal = array())) // Recurring Profile.
  377. {
  378. $paypal["TRXTYPE"] = "R";
  379. $paypal["ACTION"] = "A";
  380.  
  381. $paypal["EMAIL"] = $post_vars["email"];
  382. $paypal["FIRSTNAME"] = $post_vars["first_name"];
  383. $paypal["LASTNAME"] = $post_vars["last_name"];
  384. $paypal["CLIENTIP"] = $_SERVER["REMOTE_ADDR"];
  385.  
  386. $paypal["PROFILENAME"] = $reference;
  387. $paypal["DESC"] = $cost_calculations["desc"];
  388.  
  389. if(!$post_vars["attr"]["tp"] || ($post_vars["attr"]["tp"] && $cost_calculations["trial_total"] > 0))
  390. {
  391. $paypal["OPTIONALTRX"] = "S";
  392. $paypal["OPTIONALTRXAMT"] = ($post_vars["attr"]["tp"]) ? $cost_calculations["trial_total"] : $cost_calculations["total"];
  393. $paypal["FAILEDOPTIONALTRXACTION"] = "CancelOnFailure";
  394. $paypal["FAILEDINITAMTACTION"] = "CancelOnFailure";
  395. }
  396.  
  397. $paypal["CURRENCY"] = $cost_calculations["cur"];
  398. $paypal["AMT"] = $cost_calculations["sub_total"];
  399. $paypal["TAXAMT"] = $cost_calculations["tax"];
  400.  
  401. $paypal["MAXFAILPAYMENTS"] = $post_vars["attr"]["rra"];
  402. $paypal["AUTOBILLOUTSTANDINGAMT"] = apply_filters("ws_plugin__s2member_pro_paypal_auto_bill_op", "AddToNextBilling", get_defined_vars());
  403.  
  404. $paypal["START"] = date("mdY", $start_time);
  405.  
  406. $paypal["PAYPERIOD"] = c_ws_plugin__s2member_paypal_utilities::paypal_payflow_term($post_vars["attr"]["rt"], $post_vars["attr"]["rp"]);
  407. $paypal["TERM"] = ($post_vars["attr"]["rr"]) ? (($post_vars["attr"]["rrt"]) ? $post_vars["attr"]["rrt"] : "0") : "1";
  408.  
  409. if($_GET["s2member_paypal_xco"] === "s2member_pro_paypal_checkout_return" && !empty($_GET["token"])
  410.  
  411. && ($paypal_xco_details = array("TRXTYPE" => "A", "ACTION" => "G", "TENDER" => "P", "TOKEN" => $_GET["token"]))
  412. && ($paypal_xco_details = c_ws_plugin__s2member_paypal_utilities::paypal_payflow_api_response($paypal_xco_details))
  413. && empty($paypal_xco_details["__error"])
  414.  
  415. && ($paypal_xco_bagree = array("TRXTYPE" => "A", "ACTION" => "X", "TENDER" => "P", "TOKEN" => $paypal_xco_details["TOKEN"]))
  416. && ($paypal_xco_bagree = c_ws_plugin__s2member_paypal_utilities::paypal_payflow_api_response($paypal_xco_bagree))
  417. && empty($paypal_xco_bagree["__error"]))
  418. {
  419. $paypal["TENDER"] = "P";
  420. $paypal["PAYERID"] = $paypal_xco_details["PAYERID"];
  421. $paypal["BAID"] = $paypal_xco_bagree["BAID"];
  422. }
  423. else if($_GET["s2member_paypal_xco"] !== "s2member_pro_paypal_checkout_return")
  424. {
  425. $paypal["TENDER"] = "C";
  426. $paypal["ACCT"] = preg_replace("/[^0-9]/", "", $post_vars["card_number"]);
  427. if(preg_match("/^(?P<month>[0-9]{2})\/[0-9]{2}(?P<year_suffix>[0-9]{2})$/", $post_vars["card_expiration"], $_m))
  428. $paypal["EXPDATE"] = $_m["month"].$_m["year_suffix"];
  429. $paypal["CVV2"] = $post_vars["card_verification"];
  430.  
  431. if(in_array($post_vars["card_type"], array("Maestro", "Solo")))
  432. {
  433. if(preg_match("/^(?P<month>[0-9]{2})\/[0-9]{2}(?P<year>[0-9]{2})$/", $post_vars["card_start_date_issue_number"], $_m))
  434. $paypal["CARDSTART"] = $_m["month"].$_m["year"];
  435. else
  436. $paypal["CARDISSUE"] = $post_vars["card_start_date_issue_number"];
  437. unset /* A little housekeeping. */($_m);
  438. }
  439.  
  440. $paypal["STREET"] = $post_vars["street"];
  441. $paypal["CITY"] = $post_vars["city"];
  442. $paypal["STATE"] = $post_vars["state"];
  443. $paypal["COUNTRY"] = $post_vars["country"];
  444. $paypal["ZIP"] = $post_vars["zip"];
  445. }
  446. }
  447.  
  448. if(($paypal = c_ws_plugin__s2member_paypal_utilities::paypal_payflow_api_response($paypal)) && empty($paypal["__error"]))
  449. {
  450. $new__subscr_id = $paypal["PROFILEID"];
  451.  
  452. if(!($ipn = array())) // Simulated PayPal® IPN.
  453. {
  454. $ipn["txn_type"] = "subscr_signup";
  455. $ipn["subscr_id"] = $new__subscr_id;
  456. $ipn["custom"] = $post_vars["attr"]["custom"];
  457.  
  458. $ipn["txn_id"] = $new__subscr_id;
  459.  
  460. $ipn["period1"] = $period1;
  461. $ipn["period3"] = $period3;
  462.  
  463. $ipn["mc_amount1"] = $cost_calculations["trial_total"];
  464. $ipn["mc_amount3"] = $cost_calculations["total"];
  465.  
  466. $ipn["mc_gross"] = (preg_match("/^[1-9]/", $ipn["period1"])) ? $ipn["mc_amount1"] : $ipn["mc_amount3"];
  467.  
  468. $ipn["mc_currency"] = $cost_calculations["cur"];
  469. $ipn["tax"] = $cost_calculations["tax"];
  470.  
  471. $ipn["recurring"] = ($post_vars["attr"]["rr"]) ? "1" : "";
  472.  
  473. $ipn["payer_email"] = $post_vars["email"];
  474. $ipn["first_name"] = $post_vars["first_name"];
  475. $ipn["last_name"] = $post_vars["last_name"];
  476.  
  477. $ipn["option_name1"] = "Originating Domain";
  478. $ipn["option_selection1"] = $_SERVER["HTTP_HOST"];
  479.  
  480. $ipn["option_name2"] = "Customer IP Address";
  481. $ipn["option_selection2"] = $_SERVER["REMOTE_ADDR"];
  482.  
  483. $ipn["item_name"] = $cost_calculations["desc"];
  484. $ipn["item_number"] = $post_vars["attr"]["level_ccaps_eotper"];
  485.  
  486. $ipn["s2member_paypal_proxy"] = "paypal";
  487. $ipn["s2member_paypal_proxy_use"] = "pro-emails";
  488. $ipn["s2member_paypal_proxy_use"] .= ($ipn["mc_gross"] > 0) ? ",subscr-signup-as-subscr-payment" : "";
  489. $ipn["s2member_paypal_proxy_coupon"] = array("coupon_code" => $cp_attr["_coupon_code"], "full_coupon_code" => $cp_attr["_full_coupon_code"], "affiliate_id" => $cp_attr["_coupon_affiliate_id"]);
  490. $ipn["s2member_paypal_proxy_verification"] = c_ws_plugin__s2member_paypal_utilities::paypal_proxy_key_gen();
  491. $ipn["s2member_paypal_proxy_return_url"] = $post_vars["attr"]["success"];
  492. }
  493.  
  494. if(!($create_user = array())) // Build post fields for registration configuration, and then the creation array.
  495. {
  496. $_POST["ws_plugin__s2member_custom_reg_field_user_pass1"] = $post_vars["password1"]; // Fake this for registration configuration.
  497. $_POST["ws_plugin__s2member_custom_reg_field_first_name"] = $post_vars["first_name"]; // Fake this for registration configuration.
  498. $_POST["ws_plugin__s2member_custom_reg_field_last_name"] = $post_vars["last_name"]; // Fake this for registration configuration.
  499. $_POST["ws_plugin__s2member_custom_reg_field_opt_in"] = $post_vars["custom_fields"]["opt_in"]; // Fake this too.
  500.  
  501. if($GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["custom_reg_fields"])
  502. foreach(json_decode($GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["custom_reg_fields"], true) as $field)
  503. {
  504. $field_var = preg_replace("/[^a-z0-9]/i", "_", strtolower($field["id"]));
  505. $field_id_class = preg_replace("/_/", "-", $field_var);
  506.  
  507. if(isset($post_vars["custom_fields"][$field_var]))
  508. $_POST["ws_plugin__s2member_custom_reg_field_".$field_var] = $post_vars["custom_fields"][$field_var];
  509. }
  510.  
  511. $_COOKIE["s2member_subscr_gateway"] = c_ws_plugin__s2member_utils_encryption::encrypt("paypal"); // Fake this for registration configuration.
  512. $_COOKIE["s2member_subscr_id"] = c_ws_plugin__s2member_utils_encryption::encrypt($new__subscr_id); // Fake this for registration configuration.
  513. $_COOKIE["s2member_custom"] = c_ws_plugin__s2member_utils_encryption::encrypt($post_vars["attr"]["custom"]); // Fake this for registration configuration.
  514. $_COOKIE["s2member_item_number"] = c_ws_plugin__s2member_utils_encryption::encrypt($post_vars["attr"]["level_ccaps_eotper"]); // Fake this too.
  515.  
  516. $create_user["user_login"] = $post_vars["username"]; // Copy this into a separate array for `wp_create_user()`.
  517. $create_user["user_pass"] = wp_generate_password(); // Which may fire `c_ws_plugin__s2member_registrations::generate_password()`.
  518. $create_user["user_email"] = $post_vars["email"]; // Copy this into a separate array for `wp_create_user()`.
  519. }
  520.  
  521. if($post_vars["password1"] && $post_vars["password1"] === $create_user["user_pass"]) // A custom Password is being used?
  522. {
  523. 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))
  524. {
  525. wp_new_user_notification($new__user_id, $create_user["user_pass"]);
  526.  
  527. $ipn["s2member_paypal_proxy_return_url"] = trim(c_ws_plugin__s2member_utils_urls::remote(site_url("/?s2member_paypal_notify=1"), $ipn, array("timeout" => 20)));
  528.  
  529. $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())));
  530.  
  531. 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))))
  532. wp_redirect(c_ws_plugin__s2member_utils_urls::add_s2member_sig($custom_success_url, "s2p-v")).exit();
  533. }
  534. else // Else, an error reponse should be given.
  535. {
  536. c_ws_plugin__s2member_utils_urls::remote(site_url("/?s2member_paypal_notify=1"), $ipn, array("timeout" => 20));
  537.  
  538. $global_response = array("response" => _x('<strong>Oops.</strong> A slight problem. Please contact Support for assistance.', "s2member-front", "s2member"), "error" => true);
  539. }
  540. }
  541. else // Otherwise, they'll need to check their email for the auto-generated Password.
  542. {
  543. 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))
  544. {
  545. update_user_option($new__user_id, "default_password_nag", true, true); // Password nag.
  546. wp_new_user_notification($new__user_id, $create_user["user_pass"]);
  547.  
  548. $ipn["s2member_paypal_proxy_return_url"] = trim(c_ws_plugin__s2member_utils_urls::remote(site_url("/?s2member_paypal_notify=1"), $ipn, array("timeout" => 20)));
  549.  
  550. $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"));
  551.  
  552. 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))))
  553. wp_redirect(c_ws_plugin__s2member_utils_urls::add_s2member_sig($custom_success_url, "s2p-v")).exit();
  554. }
  555. else // Else, an error reponse should be given.
  556. {
  557. c_ws_plugin__s2member_utils_urls::remote(site_url("/?s2member_paypal_notify=1"), $ipn, array("timeout" => 20));
  558.  
  559. $global_response = array("response" => _x('<strong>Oops.</strong> A slight problem. Please contact Support for assistance.', "s2member-front", "s2member"), "error" => true);
  560. }
  561. }
  562. }
  563. else // Else, an error.
  564. {
  565. $global_response = array("response" => $paypal["__error"], "error" => true);
  566. }
  567. }
  568.  
  569. else if(!$use_recurring_profile && is_user_logged_in() && is_object($user = wp_get_current_user()) && ($user_id = $user->ID))
  570. {
  571. if(($old__subscr_id = get_user_option("s2member_subscr_id")))
  572. $paypal = c_ws_plugin__s2member_pro_paypal_utilities::payflow_get_profile($old__subscr_id);
  573. $old__baid = (!empty($paypal) && !empty($paypal["BAID"])) ? $paypal["BAID"] : "";
  574. $old__subscr_or_wp_id = c_ws_plugin__s2member_utils_users::get_user_subscr_or_wp_id();
  575.  
  576. update_user_meta($user_id, "first_name", $post_vars["first_name"]).update_user_meta($user_id, "last_name", $post_vars["last_name"]);
  577.  
  578. if(!($paypal = array())) // Prepare a "Buy Now" transaction.
  579. {
  580. if($_GET["s2member_paypal_xco"] === "s2member_pro_paypal_checkout_return" && !empty($_GET["token"])
  581. && ($paypal_xco_details = array("METHOD" => "GetExpressCheckoutDetails", "TOKEN" => $_GET["token"]))
  582. && ($paypal_xco_details = c_ws_plugin__s2member_paypal_utilities::paypal_api_response($paypal_xco_details))
  583. && empty($paypal_xco_details["__error"]))
  584. {
  585. $paypal["METHOD"] = "DoExpressCheckoutPayment";
  586.  
  587. $paypal["TOKEN"] = $paypal_xco_details["TOKEN"];
  588. $paypal["PAYERID"] = $paypal_xco_details["PAYERID"];
  589.  
  590. $paypal["PAYMENTREQUEST_0_PAYMENTACTION"] = "Sale";
  591.  
  592. $paypal["PAYMENTREQUEST_0_DESC"] = $cost_calculations["desc"];
  593. $paypal["PAYMENTREQUEST_0_CUSTOM"] = $post_vars["attr"]["custom"];
  594.  
  595. $paypal["PAYMENTREQUEST_0_CURRENCYCODE"] = $cost_calculations["cur"];
  596. $paypal["PAYMENTREQUEST_0_ITEMAMT"] = $cost_calculations["sub_total"];
  597. $paypal["PAYMENTREQUEST_0_TAXAMT"] = $cost_calculations["tax"];
  598. $paypal["PAYMENTREQUEST_0_AMT"] = $cost_calculations["total"];
  599.  
  600. $paypal["L_PAYMENTREQUEST_0_QTY0"] = "1"; // Always (1).
  601. $paypal["L_PAYMENTREQUEST_0_NAME0"] = $cost_calculations["desc"];
  602. $paypal["L_PAYMENTREQUEST_0_NUMBER0"] = $post_vars["attr"]["level_ccaps_eotper"];
  603. $paypal["L_PAYMENTREQUEST_0_AMT0"] = $cost_calculations["sub_total"];
  604. }
  605. else // NOT using PayPal® Express Checkout.
  606. {
  607. $paypal["METHOD"] = "DoDirectPayment";
  608. $paypal["PAYMENTACTION"] = "Sale";
  609.  
  610. $paypal["EMAIL"] = $user->user_email;
  611. $paypal["FIRSTNAME"] = $post_vars["first_name"];
  612. $paypal["LASTNAME"] = $post_vars["last_name"];
  613. $paypal["IPADDRESS"] = $_SERVER["REMOTE_ADDR"];
  614.  
  615. $paypal["DESC"] = $cost_calculations["desc"];
  616. $paypal["CUSTOM"] = $post_vars["attr"]["custom"];
  617.  
  618. $paypal["CURRENCYCODE"] = $cost_calculations["cur"];
  619. $paypal["ITEMAMT"] = $cost_calculations["sub_total"];
  620. $paypal["TAXAMT"] = $cost_calculations["tax"];
  621. $paypal["AMT"] = $cost_calculations["total"];
  622.  
  623. $paypal["L_QTY0"] = "1"; // Always (1).
  624. $paypal["L_NAME0"] = $cost_calculations["desc"];
  625. $paypal["L_NUMBER0"] = $post_vars["attr"]["level_ccaps_eotper"];
  626. $paypal["L_AMT0"] = $cost_calculations["sub_total"];
  627.  
  628. $paypal["CREDITCARDTYPE"] = $post_vars["card_type"];
  629. $paypal["ACCT"] = preg_replace("/[^0-9]/", "", $post_vars["card_number"]);
  630. $paypal["EXPDATE"] = preg_replace("/[^0-9]/", "", $post_vars["card_expiration"]);
  631. $paypal["CVV2"] = $post_vars["card_verification"];
  632.  
  633. if(in_array($post_vars["card_type"], array("Maestro", "Solo")))
  634. if(preg_match("/^[0-9]{2}\/[0-9]{4}$/", $post_vars["card_start_date_issue_number"]))
  635. $paypal["STARTDATE"] = preg_replace("/[^0-9]/", "", $post_vars["card_start_date_issue_number"]);
  636. else // Otherwise, we assume they provided an Issue Number instead.
  637. $paypal["ISSUENUMBER"] = $post_vars["card_start_date_issue_number"];
  638.  
  639. $paypal["STREET"] = $post_vars["street"];
  640. $paypal["CITY"] = $post_vars["city"];
  641. $paypal["STATE"] = $post_vars["state"];
  642. $paypal["COUNTRYCODE"] = $post_vars["country"];
  643. $paypal["ZIP"] = $post_vars["zip"];
  644. }
  645. }
  646.  
  647. if(($paypal = c_ws_plugin__s2member_paypal_utilities::paypal_api_response($paypal)) && empty($paypal["__error"]))
  648. {
  649. $new__subscr_id = (!empty($paypal["PAYMENTINFO_0_TRANSACTIONID"])) ? $paypal["PAYMENTINFO_0_TRANSACTIONID"] : false;
  650. $new__subscr_id = (!$new__subscr_id && !empty($paypal["TRANSACTIONID"])) ? $paypal["TRANSACTIONID"] : $new__subscr_id;
  651.  
  652. if(!($ipn = array())) // Simulated PayPal® IPN.
  653. {
  654. $ipn["txn_type"] = "web_accept";
  655. $ipn["txn_id"] = $new__subscr_id;
  656. $ipn["custom"] = $post_vars["attr"]["custom"];
  657.  
  658. $ipn["mc_gross"] = $cost_calculations["total"];
  659. $ipn["mc_currency"] = $cost_calculations["cur"];
  660. $ipn["tax"] = $cost_calculations["tax"];
  661.  
  662. $ipn["payer_email"] = $user->user_email;
  663. $ipn["first_name"] = $post_vars["first_name"];
  664. $ipn["last_name"] = $post_vars["last_name"];
  665.  
  666. $ipn["option_name1"] = "Referencing Customer ID";
  667. $ipn["option_selection1"] = $old__subscr_or_wp_id;
  668.  
  669. $ipn["option_name2"] = "Customer IP Address";
  670. $ipn["option_selection2"] = $_SERVER["REMOTE_ADDR"];
  671.  
  672. $ipn["item_name"] = $cost_calculations["desc"];
  673. $ipn["item_number"] = $post_vars["attr"]["level_ccaps_eotper"];
  674.  
  675. $ipn["s2member_paypal_proxy"] = "paypal";
  676. $ipn["s2member_paypal_proxy_use"] = "pro-emails";
  677. $ipn["s2member_paypal_proxy_coupon"] = array("coupon_code" => $cp_attr["_coupon_code"], "full_coupon_code" => $cp_attr["_full_coupon_code"], "affiliate_id" => $cp_attr["_coupon_affiliate_id"]);
  678. $ipn["s2member_paypal_proxy_verification"] = c_ws_plugin__s2member_paypal_utilities::paypal_proxy_key_gen();
  679. $ipn["s2member_paypal_proxy_return_url"] = $post_vars["attr"]["success"];
  680.  
  681. $ipn["s2member_paypal_proxy_return_url"] = trim(c_ws_plugin__s2member_utils_urls::remote(site_url("/?s2member_paypal_notify=1"), $ipn, array("timeout" => 20)));
  682. }
  683.  
  684. if(!$is_independent_ccaps_sale && $old__subscr_id) // There is an old Recurring Profile?
  685. c_ws_plugin__s2member_pro_paypal_utilities::payflow_cancel_profile($old__subscr_id, $old__baid);
  686.  
  687. setcookie("s2member_tracking", ($s2member_tracking = c_ws_plugin__s2member_utils_encryption::encrypt($new__subscr_id)), time() + 31556926, COOKIEPATH, COOKIE_DOMAIN).setcookie("s2member_tracking", $s2member_tracking, time() + 31556926, SITECOOKIEPATH, COOKIE_DOMAIN).($_COOKIE["s2member_tracking"] = $s2member_tracking);
  688.  
  689. $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())));
  690.  
  691. 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))))
  692. wp_redirect(c_ws_plugin__s2member_utils_urls::add_s2member_sig($custom_success_url, "s2p-v")).exit();
  693. }
  694. else // Else, an error.
  695. {
  696. $global_response = array("response" => $paypal["__error"], "error" => true);
  697. }
  698. }
  699.  
  700. else if(!$use_recurring_profile && !is_user_logged_in())
  701. {
  702. if(!($paypal = array())) // Prepare a "Buy Now" transaction.
  703. {
  704. if($_GET["s2member_paypal_xco"] === "s2member_pro_paypal_checkout_return" && !empty($_GET["token"])
  705. && ($paypal_xco_details = array("METHOD" => "GetExpressCheckoutDetails", "TOKEN" => $_GET["token"]))
  706. && ($paypal_xco_details = c_ws_plugin__s2member_paypal_utilities::paypal_api_response($paypal_xco_details))
  707. && empty($paypal_xco_details["__error"]))
  708. {
  709. $paypal["METHOD"] = "DoExpressCheckoutPayment";
  710.  
  711. $paypal["TOKEN"] = $paypal_xco_details["TOKEN"];
  712. $paypal["PAYERID"] = $paypal_xco_details["PAYERID"];
  713.  
  714. $paypal["PAYMENTREQUEST_0_PAYMENTACTION"] = "Sale";
  715.  
  716. $paypal["PAYMENTREQUEST_0_DESC"] = $cost_calculations["desc"];
  717. $paypal["PAYMENTREQUEST_0_CUSTOM"] = $post_vars["attr"]["custom"];
  718.  
  719. $paypal["PAYMENTREQUEST_0_CURRENCYCODE"] = $cost_calculations["cur"];
  720. $paypal["PAYMENTREQUEST_0_ITEMAMT"] = $cost_calculations["sub_total"];
  721. $paypal["PAYMENTREQUEST_0_TAXAMT"] = $cost_calculations["tax"];
  722. $paypal["PAYMENTREQUEST_0_AMT"] = $cost_calculations["total"];
  723.  
  724. $paypal["L_PAYMENTREQUEST_0_QTY0"] = "1"; // Always (1).
  725. $paypal["L_PAYMENTREQUEST_0_NAME0"] = $cost_calculations["desc"];
  726. $paypal["L_PAYMENTREQUEST_0_NUMBER0"] = $post_vars["attr"]["level_ccaps_eotper"];
  727. $paypal["L_PAYMENTREQUEST_0_AMT0"] = $cost_calculations["sub_total"];
  728. }
  729. else // NOT using PayPal® Express Checkout.
  730. {
  731. $paypal["METHOD"] = "DoDirectPayment";
  732. $paypal["PAYMENTACTION"] = "Sale";
  733.  
  734. $paypal["EMAIL"] = $post_vars["email"];
  735. $paypal["FIRSTNAME"] = $post_vars["first_name"];
  736. $paypal["LASTNAME"] = $post_vars["last_name"];
  737. $paypal["IPADDRESS"] = $_SERVER["REMOTE_ADDR"];
  738.  
  739. $paypal["DESC"] = $cost_calculations["desc"];
  740. $paypal["CUSTOM"] = $post_vars["attr"]["custom"];
  741.  
  742. $paypal["CURRENCYCODE"] = $cost_calculations["cur"];
  743. $paypal["ITEMAMT"] = $cost_calculations["sub_total"];
  744. $paypal["TAXAMT"] = $cost_calculations["tax"];
  745. $paypal["AMT"] = $cost_calculations["total"];
  746.  
  747. $paypal["L_QTY0"] = "1"; // Always (1).
  748. $paypal["L_NAME0"] = $cost_calculations["desc"];
  749. $paypal["L_NUMBER0"] = $post_vars["attr"]["level_ccaps_eotper"];
  750. $paypal["L_AMT0"] = $cost_calculations["sub_total"];
  751.  
  752. $paypal["CREDITCARDTYPE"] = $post_vars["card_type"];
  753. $paypal["ACCT"] = preg_replace("/[^0-9]/", "", $post_vars["card_number"]);
  754. $paypal["EXPDATE"] = preg_replace("/[^0-9]/", "", $post_vars["card_expiration"]);
  755. $paypal["CVV2"] = $post_vars["card_verification"];
  756.  
  757. if(in_array($post_vars["card_type"], array("Maestro", "Solo")))
  758. if(preg_match("/^[0-9]{2}\/[0-9]{4}$/", $post_vars["card_start_date_issue_number"]))
  759. $paypal["STARTDATE"] = preg_replace("/[^0-9]/", "", $post_vars["card_start_date_issue_number"]);
  760. else // Otherwise, we assume they provided an Issue Number instead.
  761. $paypal["ISSUENUMBER"] = $post_vars["card_start_date_issue_number"];
  762.  
  763. $paypal["STREET"] = $post_vars["street"];
  764. $paypal["CITY"] = $post_vars["city"];
  765. $paypal["STATE"] = $post_vars["state"];
  766. $paypal["COUNTRYCODE"] = $post_vars["country"];
  767. $paypal["ZIP"] = $post_vars["zip"];
  768. }
  769. }
  770.  
  771. if(($paypal = c_ws_plugin__s2member_paypal_utilities::paypal_api_response($paypal)) && empty($paypal["__error"]))
  772. {
  773. $new__subscr_id = (!empty($paypal["PAYMENTINFO_0_TRANSACTIONID"])) ? $paypal["PAYMENTINFO_0_TRANSACTIONID"] : false;
  774. $new__subscr_id = (!$new__subscr_id && !empty($paypal["TRANSACTIONID"])) ? $paypal["TRANSACTIONID"] : $new__subscr_id;
  775.  
  776. if(!($ipn = array())) // Simulated PayPal® IPN.
  777. {
  778. $ipn["txn_type"] = "web_accept";
  779. $ipn["txn_id"] = $new__subscr_id;
  780. $ipn["custom"] = $post_vars["attr"]["custom"];
  781.  
  782. $ipn["mc_gross"] = $cost_calculations["total"];
  783. $ipn["mc_currency"] = $cost_calculations["cur"];
  784. $ipn["tax"] = $cost_calculations["tax"];
  785.  
  786. $ipn["payer_email"] = $post_vars["email"];
  787. $ipn["first_name"] = $post_vars["first_name"];
  788. $ipn["last_name"] = $post_vars["last_name"];
  789.  
  790. $ipn["option_name1"] = "Originating Domain";
  791. $ipn["option_selection1"] = $_SERVER["HTTP_HOST"];
  792.  
  793. $ipn["option_name2"] = "Customer IP Address";
  794. $ipn["option_selection2"] = $_SERVER["REMOTE_ADDR"];
  795.  
  796. $ipn["item_name"] = $cost_calculations["desc"];
  797. $ipn["item_number"] = $post_vars["attr"]["level_ccaps_eotper"];
  798.  
  799. $ipn["s2member_paypal_proxy"] = "paypal";
  800. $ipn["s2member_paypal_proxy_use"] = "pro-emails";
  801. $ipn["s2member_paypal_proxy_coupon"] = array("coupon_code" => $cp_attr["_coupon_code"], "full_coupon_code" => $cp_attr["_full_coupon_code"], "affiliate_id" => $cp_attr["_coupon_affiliate_id"]);
  802. $ipn["s2member_paypal_proxy_verification"] = c_ws_plugin__s2member_paypal_utilities::paypal_proxy_key_gen();
  803. $ipn["s2member_paypal_proxy_return_url"] = $post_vars["attr"]["success"];
  804. }
  805.  
  806. if(!($create_user = array())) // Build post fields for registration configuration, and then the creation array.
  807. {
  808. $_POST["ws_plugin__s2member_custom_reg_field_user_pass1"] = $post_vars["password1"]; // Fake this for registration configuration.
  809. $_POST["ws_plugin__s2member_custom_reg_field_first_name"] = $post_vars["first_name"]; // Fake this for registration configuration.
  810. $_POST["ws_plugin__s2member_custom_reg_field_last_name"] = $post_vars["last_name"]; // Fake this for registration configuration.
  811. $_POST["ws_plugin__s2member_custom_reg_field_opt_in"] = $post_vars["custom_fields"]["opt_in"]; // Fake this too.
  812.  
  813. if($GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["custom_reg_fields"])
  814. foreach(json_decode($GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["custom_reg_fields"], true) as $field)
  815. {
  816. $field_var = preg_replace("/[^a-z0-9]/i", "_", strtolower($field["id"]));
  817. $field_id_class = preg_replace("/_/", "-", $field_var);
  818.  
  819. if(isset($post_vars["custom_fields"][$field_var]))
  820. $_POST["ws_plugin__s2member_custom_reg_field_".$field_var] = $post_vars["custom_fields"][$field_var];
  821. }
  822.  
  823. $_COOKIE["s2member_subscr_gateway"] = c_ws_plugin__s2member_utils_encryption::encrypt("paypal"); // Fake this for registration configuration.
  824. $_COOKIE["s2member_subscr_id"] = c_ws_plugin__s2member_utils_encryption::encrypt($new__subscr_id); // Fake this for registration configuration.
  825. $_COOKIE["s2member_custom"] = c_ws_plugin__s2member_utils_encryption::encrypt($post_vars["attr"]["custom"]); // Fake this for registration configuration.
  826. $_COOKIE["s2member_item_number"] = c_ws_plugin__s2member_utils_encryption::encrypt($post_vars["attr"]["level_ccaps_eotper"]); // Fake this too.
  827.  
  828. $create_user["user_login"] = $post_vars["username"]; // Copy this into a separate array for `wp_create_user()`.
  829. $create_user["user_pass"] = wp_generate_password(); // Which may fire `c_ws_plugin__s2member_registrations::generate_password()`.
  830. $create_user["user_email"] = $post_vars["email"]; // Copy this into a separate array for `wp_create_user()`.
  831. }
  832.  
  833. if($post_vars["password1"] && $post_vars["password1"] === $create_user["user_pass"]) // A custom Password is being used?
  834. {
  835. 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))
  836. {
  837. wp_new_user_notification($new__user_id, $create_user["user_pass"]);
  838.  
  839. $ipn["s2member_paypal_proxy_return_url"] = trim(c_ws_plugin__s2member_utils_urls::remote(site_url("/?s2member_paypal_notify=1"), $ipn, array("timeout" => 20)));
  840.  
  841. $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())));
  842.  
  843. 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))))
  844. wp_redirect(c_ws_plugin__s2member_utils_urls::add_s2member_sig($custom_success_url, "s2p-v")).exit();
  845. }
  846. else // Else, an error reponse should be given.
  847. {
  848. c_ws_plugin__s2member_utils_urls::remote(site_url("/?s2member_paypal_notify=1"), $ipn, array("timeout" => 20));
  849.  
  850. $global_response = array("response" => _x('<strong>Oops.</strong> A slight problem. Please contact Support for assistance.', "s2member-front", "s2member"), "error" => true);
  851. }
  852. }
  853. else // Otherwise, they'll need to check their email for the auto-generated Password.
  854. {
  855. 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))
  856. {
  857. update_user_option($new__user_id, "default_password_nag", true, true); // Password nag.
  858. wp_new_user_notification($new__user_id, $create_user["user_pass"]);
  859.  
  860. $ipn["s2member_paypal_proxy_return_url"] = trim(c_ws_plugin__s2member_utils_urls::remote(site_url("/?s2member_paypal_notify=1"), $ipn, array("timeout" => 20)));
  861.  
  862. $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"));
  863.  
  864. 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))))
  865. wp_redirect(c_ws_plugin__s2member_utils_urls::add_s2member_sig($custom_success_url, "s2p-v")).exit();
  866. }
  867. else // Else, an error reponse should be given.
  868. {
  869. c_ws_plugin__s2member_utils_urls::remote(site_url("/?s2member_paypal_notify=1"), $ipn, array("timeout" => 20));
  870.  
  871. $global_response = array("response" => _x('<strong>Oops.</strong> A slight problem. Please contact Support for assistance.', "s2member-front", "s2member"), "error" => true);
  872. }
  873. }
  874. }
  875. else // Else, an error.
  876. {
  877. $global_response = array("response" => $paypal["__error"], "error" => true);
  878. }
  879. }
  880. else // Else, we have an unknown scenario.
  881. {
  882. $global_response = array("response" => _x('<strong>Unknown error.</strong> Please contact Support for assistance.', "s2member-front", "s2member"), "error" => true);
  883. }
  884. }
  885. else // Else, an error.
  886. {
  887. $global_response = $error;
  888. }
  889. }
  890. }
  891. }
  892. }
  893. }
  894. ?>

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

Source Code Documentation