Source Code Documentation


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

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

Source Code Documentation