Source Code Documentation


s2member\includes\classes\paypal-utilities.inc.php


  1. <?php
  2. /**
  3. * PayPal® utilities.
  4. *
  5. * Copyright: © 2009-2011
  6. * {@link http://www.websharks-inc.com/ WebSharks, Inc.}
  7. * (coded in the USA)
  8. *
  9. * Released under the terms of the GNU General Public License.
  10. * You should have received a copy of the GNU General Public License,
  11. * along with this software. In the main directory, see: /licensing/
  12. * If not, see: {@link http://www.gnu.org/licenses/}.
  13. *
  14. * @package s2Member\PayPal
  15. * @since 3.5
  16. */
  17. if(realpath(__FILE__) === realpath($_SERVER["SCRIPT_FILENAME"]))
  18. exit("Do not access this file directly.");
  19.  
  20. if(!class_exists("c_ws_plugin__s2member_paypal_utilities"))
  21. {
  22. /**
  23. * PayPal® utilities.
  24. *
  25. * @package s2Member\PayPal
  26. * @since 3.5
  27. */
  28. class c_ws_plugin__s2member_paypal_utilities
  29. {
  30. /**
  31. * Get ``$_POST`` or ``$_REQUEST`` vars from PayPal®.
  32. *
  33. * @package s2Member\PayPal
  34. * @since 3.5
  35. *
  36. * @return array|bool An array of verified ``$_POST`` or ``$_REQUEST`` variables, else false.
  37. */
  38. public static function paypal_postvars()
  39. {
  40. foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;
  41. do_action("ws_plugin__s2member_before_paypal_postvars", get_defined_vars());
  42. unset /* Unset defined __refs, __v. */($__refs, $__v);
  43. /*
  44. Custom conditionals can be applied by filters.
  45. */
  46. foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;
  47. if(!($postvars = apply_filters("ws_plugin__s2member_during_paypal_postvars_conditionals", array(), get_defined_vars())))
  48. {
  49. unset /* Unset defined __refs, __v. */($__refs, $__v);
  50.  
  51. if(!empty($_GET["tx"]) && empty($_GET["s2member_paypal_proxy"]))
  52. {
  53. $postback["tx"] = $_GET["tx"];
  54. $postback["cmd"] = "_notify-synch";
  55. $postback["at"] = $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["paypal_identity_token"];
  56.  
  57. $endpoint = ($GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["paypal_sandbox"]) ? "www.sandbox.paypal.com" : "www.paypal.com";
  58.  
  59. if(preg_match("/^SUCCESS/i", ($response = trim(c_ws_plugin__s2member_utils_urls::remote("https://".$endpoint."/cgi-bin/webscr", $postback, array("timeout" => 20))))))
  60. {
  61. foreach(preg_split("/[\r\n]+/", preg_replace("/^SUCCESS/i", "", $response)) as $varline)
  62. {
  63. list($key, $value) = preg_split("/\=/", $varline, 2);
  64. if(strlen($key = trim($key)) && strlen($value = trim($value)))
  65. $postvars[$key] = trim(stripslashes(urldecode($value)));
  66. }
  67. if(!empty($postvars["charset"]) && function_exists("mb_convert_encoding"))
  68. {
  69. foreach($postvars as &$value)
  70. $value = @mb_convert_encoding($value, "UTF-8", (($postvars["charset"] === "gb2312") ? "GBK" : $postvars["charset"]));
  71. }
  72. return apply_filters("ws_plugin__s2member_paypal_postvars", $postvars, get_defined_vars());
  73. }
  74. else return false;
  75. }
  76. else if(!empty($_REQUEST) && is_array($postvars = stripslashes_deep($_REQUEST)))
  77. {
  78. foreach($postvars as $key => $value)
  79. if(preg_match("/^s2member_/", $key))
  80. unset($postvars[$key]);
  81.  
  82. $postback = /* Copy. */ $postvars;
  83. $postback["cmd"] = "_notify-validate";
  84.  
  85. $postvars = c_ws_plugin__s2member_utils_strings::trim_deep($postvars);
  86.  
  87. if(!empty($postvars["charset"]) && function_exists("mb_convert_encoding"))
  88. {
  89. foreach($postvars as &$value)
  90. $value = @mb_convert_encoding($value, "UTF-8", (($postvars["charset"] === "gb2312") ? "GBK" : $postvars["charset"]));
  91. }
  92. $endpoint = ($GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["paypal_sandbox"]) ? "www.sandbox.paypal.com" : "www.paypal.com";
  93.  
  94. if(!empty($_REQUEST["s2member_paypal_proxy"]) && !empty($_REQUEST["s2member_paypal_proxy_verification"]) && $_REQUEST["s2member_paypal_proxy_verification"] === c_ws_plugin__s2member_paypal_utilities::paypal_proxy_key_gen())
  95. return apply_filters("ws_plugin__s2member_paypal_postvars", array_merge($postvars, array("proxy_verified" => $_REQUEST["s2member_paypal_proxy"])), get_defined_vars());
  96.  
  97. else if(empty($_POST) && !empty($_GET["s2member_paypal_proxy"]) && !empty($_GET["s2member_paypal_proxy_verification"]) && c_ws_plugin__s2member_utils_urls::s2member_sig_ok($_SERVER["REQUEST_URI"], false, false, "s2member_paypal_proxy_verification"))
  98. return apply_filters("ws_plugin__s2member_paypal_postvars", array_merge($postvars, array("proxy_verified" => $_GET["s2member_paypal_proxy"])), get_defined_vars());
  99.  
  100. else if(trim(strtolower(c_ws_plugin__s2member_utils_urls::remote("https://".$endpoint."/cgi-bin/webscr", $postback, array("timeout" => 20)))) === "verified")
  101. return apply_filters("ws_plugin__s2member_paypal_postvars", $postvars, get_defined_vars());
  102.  
  103. else return false;
  104. }
  105. else return false;
  106. }
  107. else // Else a custom conditional has been applied by Filters.
  108. {
  109. unset /* Unset defined __refs, __v. */($__refs, $__v);
  110.  
  111. return apply_filters("ws_plugin__s2member_paypal_postvars", $postvars, get_defined_vars());
  112. }
  113. }
  114. /**
  115. * Generates a PayPal® Proxy Key, for simulated IPN responses.
  116. *
  117. * @package s2Member\PayPal
  118. * @since 3.5
  119. *
  120. * @return str A Proxy Key. It's an MD5 Hash, 32 chars, URL-safe.
  121. */
  122. public static function paypal_proxy_key_gen()
  123. {
  124. global /* Multisite Networking. */ $current_site, $current_blog;
  125.  
  126. foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;
  127. do_action("ws_plugin__s2member_before_paypal_proxy_key_gen", get_defined_vars());
  128. unset /* Unset defined __refs, __v. */($__refs, $__v);
  129.  
  130. if(is_multisite() && !is_main_site())
  131. $key = md5(c_ws_plugin__s2member_utils_encryption::xencrypt($current_blog->domain.$current_blog->path, false, false));
  132.  
  133. else // Else it's a standard Proxy Key; not on a Multisite Network, or not on the Main Site anyway.
  134. $key = md5(c_ws_plugin__s2member_utils_encryption::xencrypt(preg_replace("/\:[0-9]+$/", "", $_SERVER["HTTP_HOST"]), false, false));
  135.  
  136. return apply_filters("ws_plugin__s2member_paypal_proxy_key_gen", $key, get_defined_vars());
  137. }
  138. /**
  139. * Calls upon the PayPal® API, and returns the response.
  140. *
  141. * @package s2Member\PayPal
  142. * @since 3.5
  143. *
  144. * @param array $post_vars An array of variables to send through the PayPal® API call.
  145. * @return array An array of variables returned by the PayPal® API.
  146. *
  147. * @todo Optimize this routine with ``empty()`` and ``isset()``.
  148. * @todo Possibly integrate this API: {@link http://msdn.microsoft.com/en-us/library/ff512417.aspx}.
  149. */
  150. public static function paypal_api_response($post_vars = FALSE)
  151. {
  152. global /* For Multisite support. */ $current_site, $current_blog;
  153.  
  154. foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;
  155. do_action("ws_plugin__s2member_before_paypal_api_response", get_defined_vars());
  156. unset /* Unset defined __refs, __v. */($__refs, $__v);
  157.  
  158. $url = "https://".(($GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["paypal_sandbox"]) ? "api-3t.sandbox.paypal.com" : "api-3t.paypal.com")."/nvp";
  159.  
  160. $post_vars = apply_filters("ws_plugin__s2member_paypal_api_post_vars", $post_vars, get_defined_vars());
  161. $post_vars = (is_array($post_vars)) ? $post_vars : array();
  162.  
  163. $post_vars["VERSION"] = /* Configure the PayPal® API version. */ "71.0";
  164. $post_vars["USER"] = $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["paypal_api_username"];
  165. $post_vars["PWD"] = $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["paypal_api_password"];
  166. $post_vars["SIGNATURE"] = $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["paypal_api_signature"];
  167.  
  168. foreach($post_vars as $_key => &$_value /* We need to clean these up. */)
  169. $_value = c_ws_plugin__s2member_paypal_utilities::paypal_api_nv_cleanup($_key, $_value);
  170. unset($_key, $_value);
  171.  
  172. $input_time = /* Record input/nvp for logging. */ date("D M j, Y g:i:s a T");
  173.  
  174. $nvp = trim(c_ws_plugin__s2member_utils_urls::remote($url, $post_vars, array("timeout" => 20)));
  175.  
  176. $output_time = /* Now record after output time. */ date("D M j, Y g:i:s a T");
  177.  
  178. wp_parse_str /* Parse NVP response. */($nvp, $response);
  179. $response = c_ws_plugin__s2member_utils_strings::trim_deep($response);
  180.  
  181. if(!$response["ACK"] || !preg_match("/^(Success|SuccessWithWarning)$/i", $response["ACK"]))
  182. {
  183. if(strlen($response["L_ERRORCODE0"]) || $response["L_SHORTMESSAGE0"] || $response["L_LONGMESSAGE0"])
  184. /* translators: Exclude `%2$s` and `%3$s`. These are English details returned by PayPal®. Replace `%2$s` and `%3$s` with: `Unable to process, please try again`, or something to that affect. Or, if you prefer, you could Filter ``$response["__error"]`` with `ws_plugin__s2member_paypal_api_response`. */
  185. $response["__error"] = sprintf(_x('Error #%1$s. %2$s. %3$s.', "s2member-front", "s2member"), $response["L_ERRORCODE0"], rtrim($response["L_SHORTMESSAGE0"], "."), rtrim($response["L_LONGMESSAGE0"], "."));
  186.  
  187. else // Else, generate an error messsage - so something is reported back to the Customer.
  188. $response["__error"] = _x("Error. Please contact Support for assistance.", "s2member-front", "s2member");
  189. }
  190. $logt = c_ws_plugin__s2member_utilities::time_details ();
  191. $logv = c_ws_plugin__s2member_utilities::ver_details();
  192. $logm = c_ws_plugin__s2member_utilities::mem_details();
  193. $log4 = $_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"]."\nUser-Agent: ".$_SERVER["HTTP_USER_AGENT"];
  194. $log4 = (is_multisite() && !is_main_site()) ? ($_log4 = $current_blog->domain.$current_blog->path)."\n".$log4 : $log4;
  195. $log2 = (is_multisite() && !is_main_site()) ? "paypal-api-4-".trim(preg_replace("/[^a-z0-9]/i", "-", $_log4), "-").".log" : "paypal-api.log";
  196.  
  197. if(isset($post_vars["ACCT"]) && strlen($post_vars["ACCT"]) > 4)
  198. $post_vars["ACCT"] = str_repeat("*", strlen($post_vars["ACCT"]) - 4).substr($post_vars["ACCT"], -4);
  199.  
  200. if($GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["gateway_debug_logs"])
  201. if(is_dir($logs_dir = $GLOBALS["WS_PLUGIN__"]["s2member"]["c"]["logs_dir"]))
  202. if(is_writable($logs_dir) && c_ws_plugin__s2member_utils_logs::archive_oversize_log_files())
  203. if(($log = "-------- Input vars: ( ".$input_time." ) --------\n".var_export($post_vars, true)."\n"))
  204. if(($log .= "-------- Output string/vars: ( ".$output_time." ) --------\n".$nvp."\n".var_export($response, true)))
  205. file_put_contents($logs_dir."/".$log2,
  206. "LOG ENTRY: ".$logt . "\n" . $logv."\n".$logm."\n".$log4."\n".
  207. c_ws_plugin__s2member_utils_logs::conceal_private_info($log)."\n\n",
  208. FILE_APPEND);
  209.  
  210. return apply_filters("ws_plugin__s2member_paypal_api_response", c_ws_plugin__s2member_paypal_utilities::_paypal_api_response_filters($response), get_defined_vars());
  211. }
  212. /**
  213. * A sort of callback function that Filters PayPal® responses.
  214. *
  215. * Provides alternative explanations in some cases that require special attention.
  216. *
  217. * @package s2Member\PayPal
  218. * @since 3.5
  219. *
  220. * @param array $response Expects an array of response variables returned by the PayPal® API.
  221. * @return array An array of variables returned by the PayPal® API, after ``$response["__error"]`` is Filtered.
  222. */
  223. public static function _paypal_api_response_filters($response = FALSE)
  224. {
  225. foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;
  226. do_action("_ws_plugin__s2member_before_paypal_api_response_filters", get_defined_vars());
  227. unset /* Unset defined __refs, __v. */($__refs, $__v);
  228.  
  229. if(!empty($response["__error"]) && !empty($response["L_ERRORCODE0"]))
  230. {
  231. if((int)$response["L_ERRORCODE0"] === 10422)
  232. $response["__error"] = sprintf(_x("Error #%s. Transaction declined. Please use an alternate funding source.", "s2member-front", "s2member"), $response["L_ERRORCODE0"]);
  233.  
  234. else if((int)$response["L_ERRORCODE0"] === 10435)
  235. $response["__error"] = sprintf(_x("Error #%s. Transaction declined. Express Checkout was NOT confirmed.", "s2member-front", "s2member"), $response["L_ERRORCODE0"]);
  236.  
  237. else if((int)$response["L_ERRORCODE0"] === 10417)
  238. $response["__error"] = sprintf(_x("Error #%s. Transaction declined. Please use an alternate funding source.", "s2member-front", "s2member"), $response["L_ERRORCODE0"]);
  239. }
  240. return /* Filters already applied with: ``ws_plugin__s2member_paypal_api_response``. */ $response;
  241. }
  242. /**
  243. * Cleans up values passed through PayPal® NVP strings.
  244. *
  245. * @package s2Member\PayPal
  246. * @since 121202
  247. *
  248. * @param string $key Expects a string value.
  249. * @param string $value Expects a string value.
  250. * @return string Cleaned string value.
  251. */
  252. public static function paypal_api_nv_cleanup($key = FALSE, $value = FALSE)
  253. {
  254. $value = (string)$value;
  255. $value = preg_replace('/"/', "'", $value);
  256.  
  257. if(($key === "DESC" || $key === "BA_DESC" #
  258. || preg_match("/^L_NAME[0-9]+$/", $key) || preg_match("/^PAYMENTREQUEST_[0-9]+_DESC$/", $key) || preg_match("/^PAYMENTREQUEST_[0-9]+_NAME[0-9]+$/", $key) #
  259. || preg_match("/^L_BILLINGAGREEMENTDESCRIPTION[0-9]+$/", $key)) && strlen($value) > 60)
  260. $value = substr($value, 0, 57)."...";
  261.  
  262. return apply_filters("ws_plugin__s2member_paypal_api_nv_cleanup", $value, get_defined_vars());
  263. }
  264. /**
  265. * Calls upon the PayPal® PayFlow API, and returns the response.
  266. *
  267. * @package s2Member\PayPal
  268. * @since 120514
  269. *
  270. * @param array $post_vars An array of variables to send through the PayPal® PayFlow API call.
  271. * @return array An array of variables returned by the PayPal® PayFlow API.
  272. */
  273. public static function paypal_payflow_api_response($post_vars = FALSE)
  274. {
  275. global /* For Multisite support. */ $current_site, $current_blog;
  276.  
  277. foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;
  278. do_action("ws_plugin__s2member_before_paypal_payflow_api_response", get_defined_vars());
  279. unset /* Unset defined __refs, __v. */($__refs, $__v);
  280.  
  281. $url = "https://".(($GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["paypal_sandbox"]) ? "pilot-payflowpro.paypal.com" : "payflowpro.paypal.com");
  282.  
  283. $post_vars = apply_filters("ws_plugin__s2member_paypal_payflow_api_post_vars", $post_vars, get_defined_vars());
  284. $post_vars = (is_array($post_vars)) ? $post_vars : array();
  285.  
  286. $post_vars["VERBOSITY"] = "HIGH";
  287. $post_vars["USER"] = $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["paypal_payflow_api_username"];
  288. $post_vars["PARTNER"] = $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["paypal_payflow_api_partner"];
  289. $post_vars["VENDOR"] = $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["paypal_payflow_api_vendor"];
  290. $post_vars["PWD"] = $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["paypal_payflow_api_password"];
  291.  
  292. foreach($post_vars as $_key => &$_value /* We need to clean these up. */)
  293. $_value = c_ws_plugin__s2member_paypal_utilities::paypal_payflow_api_nv_cleanup($_key, $_value);
  294. unset($_key, $_value);
  295.  
  296. $input_time = /* Record input/nvp for logging. */ date("D M j, Y g:i:s a T");
  297.  
  298. $nvp_post_vars = /* Initialize this to an empty string. */ "";
  299. foreach($post_vars as $_key => $_value /* A ridiculous `text/namevalue` format. */)
  300. $nvp_post_vars .= (($nvp_post_vars) ? "&" : "").$_key."[".strlen($_value)."]=".$_value;
  301. unset($_key, $_value);
  302.  
  303. $nvp = trim(c_ws_plugin__s2member_utils_urls::remote($url, $nvp_post_vars, array("timeout" => 20, "headers" => array("Content-Type" => "text/namevalue"))));
  304.  
  305. $output_time = /* Now record after output time. */ date("D M j, Y g:i:s a T");
  306.  
  307. wp_parse_str /* Parse NVP response. */($nvp, $response);
  308. $response = c_ws_plugin__s2member_utils_strings::trim_deep($response);
  309.  
  310. if($response["RESULT"] !== "0")
  311. {
  312. if(strlen($response["RESPMSG"]))
  313. /* translators: Exclude `%2$s`. These are English details returned by PayPal®. Replace `%2$s` with: `Unable to process, please try again`, or something to that affect. Or, if you prefer, you could Filter ``$response["__error"]`` with `ws_plugin__s2member_paypal_payflow_api_response`. */
  314. $response["__error"] = sprintf(_x('Error #%1$s. %2$s.', "s2member-front", "s2member"), $response["RESULT"], rtrim($response["RESPMSG"], "."));
  315.  
  316. else $response["__error"] = _x("Error. Please contact Support for assistance.", "s2member-front", "s2member");
  317. }
  318. else if(isset($response["TRXRESULT"]) && $response["TRXRESULT"] !== "0")
  319. {
  320. if(strlen($response["TRXRESPMSG"]))
  321. /* translators: Exclude `%2$s`. These are English details returned by PayPal®. Replace `%2$s` with: `Unable to process, please try again`, or something to that affect. Or, if you prefer, you could Filter ``$response["__error"]`` with `ws_plugin__s2member_paypal_payflow_api_response`. */
  322. $response["__error"] = sprintf(_x('Error #%1$s. %2$s.', "s2member-front", "s2member"), $response["TRXRESULT"], rtrim($response["TRXRESPMSG"], "."));
  323.  
  324. else $response["__error"] = _x("Error. Please contact Support for assistance.", "s2member-front", "s2member");
  325. }
  326.  
  327. $logt = c_ws_plugin__s2member_utilities::time_details ();
  328. $logv = c_ws_plugin__s2member_utilities::ver_details();
  329. $logm = c_ws_plugin__s2member_utilities::mem_details();
  330. $log4 = $_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"]."\nUser-Agent: ".$_SERVER["HTTP_USER_AGENT"];
  331. $log4 = (is_multisite() && !is_main_site()) ? ($_log4 = $current_blog->domain.$current_blog->path)."\n".$log4 : $log4;
  332. $log2 = (is_multisite() && !is_main_site()) ? "paypal-payflow-api-4-".trim(preg_replace("/[^a-z0-9]/i", "-", $_log4), "-").".log" : "paypal-payflow-api.log";
  333.  
  334. if(isset($post_vars["ACCT"]) && strlen($post_vars["ACCT"]) > 4)
  335. $post_vars["ACCT"] = str_repeat("*", strlen($post_vars["ACCT"]) - 4).substr($post_vars["ACCT"], -4);
  336.  
  337. if($GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["gateway_debug_logs"])
  338. if(is_dir($logs_dir = $GLOBALS["WS_PLUGIN__"]["s2member"]["c"]["logs_dir"]))
  339. if(is_writable($logs_dir) && c_ws_plugin__s2member_utils_logs::archive_oversize_log_files())
  340. if(($log = "-------- Input vars: ( ".$input_time." ) --------\n".$nvp_post_vars."\n".var_export($post_vars, true)."\n"))
  341. if(($log .= "-------- Output string/vars: ( ".$output_time." ) --------\n".$nvp."\n".var_export($response, true)))
  342. file_put_contents($logs_dir."/".$log2,
  343. "LOG ENTRY: ".$logt . "\n" . $logv."\n".$logm."\n".$log4."\n".
  344. c_ws_plugin__s2member_utils_logs::conceal_private_info($log)."\n\n",
  345. FILE_APPEND);
  346.  
  347. return apply_filters("ws_plugin__s2member_paypal_payflow_api_response", c_ws_plugin__s2member_paypal_utilities::_paypal_payflow_api_response_filters($response), get_defined_vars());
  348. }
  349. /**
  350. * A sort of callback function that Filters Payflow® responses.
  351. *
  352. * Provides alternative explanations in some cases that require special attention.
  353. *
  354. * @package s2Member\PayPal
  355. * @since 120514
  356. *
  357. * @param array $response Expects an array of response variables returned by the Payflow® API.
  358. * @return array An array of variables returned by the Payflow® API, after ``$response["__error"]`` is Filtered.
  359. */
  360. public static function _paypal_payflow_api_response_filters($response = FALSE)
  361. {
  362. foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;
  363. do_action("_ws_plugin__s2member_before_paypal_payflow_api_response_filters", get_defined_vars());
  364. unset /* Unset defined __refs, __v. */($__refs, $__v);
  365.  
  366. if(!empty($response["__error"]) && !empty($response["HOSTCODE"]))
  367. {
  368. if((int)$response["HOSTCODE"] === 11452)
  369. $response["__error"] .= _x(" Please contact PayPal Merchant Technical Support (www.paypal.com/mts) and request `Recurring Billing` service, and also ask to have `Reference Transactions` enabled for Recurring Billing via Express Checkout.", "s2member-front", "s2member");
  370. }
  371.  
  372. return /* Filters already applied with: ``ws_plugin__s2member_paypal_payflow_api_response``. */ $response;
  373. }
  374. /**
  375. * Cleans up values passed through PayPal® text/namevalue strings.
  376. *
  377. * @package s2Member\PayPal
  378. * @since 121202
  379. *
  380. * @param string $key Expects a string value.
  381. * @param string $value Expects a string value.
  382. * @return string Cleaned string value.
  383. */
  384. public static function paypal_payflow_api_nv_cleanup($key = FALSE, $value = FALSE)
  385. {
  386. $value = (string)$value;
  387. $value = preg_replace('/"/', "'", $value);
  388.  
  389. if(($key === "DESC" || $key === "ORDERDESC" || $key === "BA_DESC" || $key === "BA_CUSTOM" #
  390. || preg_match("/^L_NAME[0-9]+$/", $key) || preg_match("/^PAYMENTREQUEST_[0-9]+_DESC$/", $key) || preg_match("/^PAYMENTREQUEST_[0-9]+_NAME[0-9]+$/", $key) #
  391. || preg_match("/^L_BILLINGAGREEMENTDESCRIPTION[0-9]+$/", $key)) && strlen($value) > 60)
  392. $value = substr($value, 0, 57)."...";
  393.  
  394. return apply_filters("ws_plugin__s2member_paypal_payflow_api_nv_cleanup", $value, get_defined_vars());
  395. }
  396. /**
  397. * Converts a term `D|W|M|Y` into PayPal® Pro format.
  398. *
  399. * @package s2Member\PayPal
  400. * @since 3.5
  401. *
  402. * @param str $term Expects one of `D|W|M|Y`.
  403. * @return bool|str A full singular description of the term *( i.e. `Day|Week|Month|Year` )*, else false.
  404. */
  405. public static function paypal_pro_term($term = FALSE)
  406. {
  407. foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;
  408. do_action("ws_plugin__s2member_before_paypal_pro_term", get_defined_vars());
  409. unset /* Unset defined __refs, __v. */($__refs, $__v);
  410.  
  411. $paypal_pro_terms = array("D" => "Day", "W" => "Week", "M" => "Month", "Y" => "Year");
  412.  
  413. $pro_term = (!empty($paypal_pro_terms[strtoupper($term)])) ? $paypal_pro_terms[strtoupper($term)] : false;
  414.  
  415. return apply_filters("ws_plugin__s2member_paypal_pro_term", $pro_term, get_defined_vars());
  416. }
  417. /**
  418. * Converts a term `D|W|M|Y` into Payflow® format.
  419. *
  420. * @package s2Member\PayPal
  421. * @since 120514
  422. *
  423. * @param str $term Expects one of `D|W|M|Y`.
  424. * @param str $period Expects a numeric value.
  425. * @return bool|str A full singular description of the term *( i.e. `DAY|WEEK|BIWK|MONT|QTER|SMYR|YEAR` )*, else false.
  426. *
  427. * @note Payflow® unfortunately does NOT support daily and/or bi-monthly billing.
  428. */
  429. public static function paypal_payflow_term($term = FALSE, $period = FALSE)
  430. {
  431. foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;
  432. do_action("ws_plugin__s2member_before_paypal_payflow_term", get_defined_vars());
  433. unset /* Unset defined __refs, __v. */($__refs, $__v);
  434.  
  435. $paypal_payflow_terms = array("D" => "DAY", "W" => "WEEK", "M" => "MONT", "Y" => "YEAR");
  436.  
  437. $payflow_term = (!empty($paypal_payflow_terms[strtoupper($term)])) ? $paypal_payflow_terms[strtoupper($term)] : false;
  438.  
  439. if($payflow_term === "WEEK" && $period === "2")
  440. $payflow_term = "BIWK";
  441.  
  442. else if($payflow_term === "MONT" && $period === "3")
  443. $payflow_term = "QTER";
  444.  
  445. else if($payflow_term === "MONT" && $period === "6")
  446. $payflow_term = "SMYR";
  447.  
  448. return apply_filters("ws_plugin__s2member_paypal_payflow_term", $payflow_term, get_defined_vars());
  449. }
  450. /**
  451. * Converts a term `Day|Week|Month|Year` into PayPal® Standard format.
  452. *
  453. * @package s2Member\PayPal
  454. * @since 3.5
  455. *
  456. * @param str $term Expects one of `Day|Week|Month|Year`.
  457. * @return bool|str A term code *( i.e. `D|W|M|Y` )*, else false.
  458. */
  459. public static function paypal_std_term($term = FALSE)
  460. {
  461. foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;
  462. do_action("ws_plugin__s2member_before_paypal_std_term", get_defined_vars());
  463. unset /* Unset defined __refs, __v. */($__refs, $__v);
  464.  
  465. $paypal_std_terms = array("DAY" => "D", "WEEK" => "W", "MONTH" => "M", "YEAR" => "Y");
  466.  
  467. $std_term = (!empty($paypal_std_terms[strtoupper($term)])) ? $paypal_std_terms[strtoupper($term)] : false;
  468.  
  469. return apply_filters("ws_plugin__s2member_paypal_std_term", $std_term, get_defined_vars());
  470. }
  471. /**
  472. * Get `subscr_id` from either an array with `recurring_payment_id|subscr_id`, or use an existing string.
  473. *
  474. * @package s2Member\PayPal
  475. * @since 3.5
  476. *
  477. * @param str|array $array_or_string Either an array of PayPal® post vars, or a string.
  478. * @return str|bool A `subscr_id` string if non-empty, else false.
  479. */
  480. public static function paypal_pro_subscr_id($array_or_string = FALSE)
  481. {
  482. foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;
  483. do_action("ws_plugin__s2member_before_paypal_pro_subscr_id", get_defined_vars());
  484. unset /* Unset defined __refs, __v. */($__refs, $__v);
  485.  
  486. if(is_array($array = $array_or_string) && !empty($array["recurring_payment_id"]))
  487. $subscr_id = trim($array["recurring_payment_id"]);
  488.  
  489. else if(is_array($array = $array_or_string) && !empty($array["subscr_id"]))
  490. $subscr_id = trim($array["subscr_id"]);
  491.  
  492. else if(is_string($string = $array_or_string) && !empty($string))
  493. $subscr_id = trim($string);
  494.  
  495. return apply_filters("ws_plugin__s2member_paypal_pro_subscr_id", ((!empty($subscr_id)) ? $subscr_id : false), get_defined_vars());
  496. }
  497. /**
  498. * Get `item_number` from either an array with `PROFILEREFERENCE|rp_invoice_id|item_number1|item_number`, or use an existing string.
  499. *
  500. * @package s2Member\PayPal
  501. * @since 3.5
  502. *
  503. * @param str|array $array_or_string Either an array of PayPal® post vars, or a string.
  504. * If it's a string, we make sure it is a valid `level:ccaps:eotper` or `sp:ids:expiration` combination.
  505. * @return str|bool An `item_number` string if non-empty, else false.
  506. */
  507. public static function paypal_pro_item_number($array_or_string = FALSE)
  508. {
  509. foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;
  510. do_action("ws_plugin__s2member_before_paypal_pro_item_number", get_defined_vars());
  511. unset /* Unset defined __refs, __v. */($__refs, $__v);
  512.  
  513. if(isset($array_or_string["PROFILENAME"]) /* Payflow® API alternative. */)
  514. $array_or_string["PROFILEREFERENCE"] = $array_or_string["PROFILENAME"];
  515.  
  516. if(is_array($array = $array_or_string) && (!empty($array["PROFILEREFERENCE"]) || !empty($array["rp_invoice_id"])))
  517. list($_reference, $_domain, $_item_number) = array_map("trim", preg_split("/~/", ((!empty($array["PROFILEREFERENCE"])) ? $array["PROFILEREFERENCE"] : $array["rp_invoice_id"]), 3));
  518.  
  519. else if(is_array($array = $array_or_string) && !empty($array["item_number1"]))
  520. $_item_number = trim($array["item_number1"]);
  521.  
  522. else if(is_array($array = $array_or_string) && !empty($array["item_number"]))
  523. $_item_number = trim($array["item_number"]);
  524.  
  525. else if(is_string($string = $array_or_string) && !empty($string))
  526. $_item_number = trim($string);
  527.  
  528. if(!empty($_item_number) && preg_match($GLOBALS["WS_PLUGIN__"]["s2member"]["c"]["membership_item_number_w_or_wo_level_regex"], $_item_number))
  529. $item_number = $_item_number;
  530.  
  531. else if(!empty($_item_number) && preg_match($GLOBALS["WS_PLUGIN__"]["s2member"]["c"]["sp_access_item_number_regex"], $_item_number))
  532. $item_number = $_item_number;
  533.  
  534. return apply_filters("ws_plugin__s2member_paypal_pro_item_number", ((!empty($item_number)) ? $item_number : false), get_defined_vars());
  535. }
  536. /**
  537. * Get `item_name` from either an array with `product_name|item_name1|item_name`, or use an existing string.
  538. *
  539. * @package s2Member\PayPal
  540. * @since 3.5
  541. *
  542. * @param str|array $array_or_string Either an array of PayPal® post vars, or a string.
  543. * @return str|bool An `item_name` string if non-empty, else false.
  544. */
  545. public static function paypal_pro_item_name($array_or_string = FALSE)
  546. {
  547. foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;
  548. do_action("ws_plugin__s2member_before_paypal_pro_item_name", get_defined_vars());
  549. unset /* Unset defined __refs, __v. */($__refs, $__v);
  550.  
  551. if(is_array($array = $array_or_string) && !empty($array["product_name"]))
  552. $item_name = trim($array["product_name"]);
  553.  
  554. else if(is_array($array = $array_or_string) && !empty($array["item_name1"]))
  555. $item_name = trim($array["item_name1"]);
  556.  
  557. else if(is_array($array = $array_or_string) && !empty($array["item_name"]))
  558. $item_name = trim($array["item_name"]);
  559.  
  560. else if(is_string($string = $array_or_string) && !empty($string))
  561. $item_name = trim($string);
  562.  
  563. return apply_filters("ws_plugin__s2member_paypal_pro_item_name", ((!empty($item_name)) ? $item_name : false), get_defined_vars());
  564. }
  565. /**
  566. * Get `period1` from either an array with `PROFILEREFERENCE|rp_invoice_id|period1`, or use an existing string.
  567. *
  568. * This will also convert `1 Day`, into `1 D`, and so on.
  569. * This will also convert `1 SemiMonth`, into `2 W`, and so on.
  570. *
  571. * @package s2Member\PayPal
  572. * @since 3.5
  573. *
  574. * @param str|array $array_or_string Either an array of PayPal® post vars, or a string.
  575. * If it's a string, we make sure it is a valid `period term` combination.
  576. * @param str $default Optional. Value if unavailable. Defaults to `0 D`.
  577. * @return str A `period1` string if possible, or defaults to `0 D`.
  578. */
  579. public static function paypal_pro_period1($array_or_string = FALSE, $default = "0 D")
  580. {
  581. foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;
  582. do_action("ws_plugin__s2member_before_paypal_pro_period1", get_defined_vars());
  583. unset /* Unset defined __refs, __v. */($__refs, $__v);
  584.  
  585. if(isset($array_or_string["PROFILENAME"]) /* Payflow® API alternative. */)
  586. $array_or_string["PROFILEREFERENCE"] = $array_or_string["PROFILENAME"];
  587.  
  588. if(is_array($array = $array_or_string) && (!empty($array["PROFILEREFERENCE"]) || !empty($array["rp_invoice_id"])))
  589. {
  590. list($_reference, $_domain, $_item_number) = array_map("trim", preg_split("/~/", ((!empty($array["PROFILEREFERENCE"])) ? $array["PROFILEREFERENCE"] : $array["rp_invoice_id"]), 3));
  591. list($_start_time, $_period1, $_period3) = array_map("trim", preg_split("/\:/", $_reference, 3));
  592. }
  593. else if(is_array($array = $array_or_string) && !empty($array["period1"]))
  594. $_period1 = trim($array["period1"]);
  595.  
  596. else if(is_string($string = $array_or_string) && !empty($string))
  597. $_period1 = trim($string);
  598.  
  599. if /* Were we able to get a `period1` string? */(!empty($_period1))
  600. {
  601. list($num, $span) = array_map("trim", preg_split("/ /", $_period1, 2));
  602.  
  603. if(strtoupper($span) === "SEMIMONTH" && is_numeric($num) && $num >= 1)
  604. { $num = "2"; $span = "W"; }
  605.  
  606. if /* To Standard format. */(strlen($span) !== 1)
  607. $span = c_ws_plugin__s2member_paypal_utilities::paypal_std_term($span);
  608.  
  609. $span = (preg_match("/^[DWMY]$/i", $span)) ? $span : "";
  610. $num = ($span && is_numeric($num) && $num >= 0) ? $num : "";
  611.  
  612. $period1 = ($num && $span) ? $num." ".strtoupper($span) : $default;
  613.  
  614. return apply_filters("ws_plugin__s2member_paypal_pro_period1", $period1, get_defined_vars());
  615. }
  616. else return apply_filters("ws_plugin__s2member_paypal_pro_period1", $default, get_defined_vars());
  617. }
  618. /**
  619. * Get `period3` from either an array with `PROFILEREFERENCE|rp_invoice_id|period3`, or use an existing string.
  620. *
  621. * This will also convert `1 Day`, into `1 D`, and so on.
  622. * This will also convert `1 SemiMonth`, into `2 W`, and so on.
  623. * The Regular Period can never be less than 1 day ( `1 D` ).
  624. *
  625. * @package s2Member\PayPal
  626. * @since 3.5
  627. *
  628. * @param str|array $array_or_string Either an array of PayPal® post vars, or a string.
  629. * If it's a string, we make sure it is a valid `period term` combination.
  630. * @param str $default Optional. Value if unavailable. Defaults to `1 D`.
  631. * @return str A `period3` string if possible, or defaults to `1 D`.
  632. */
  633. public static function paypal_pro_period3($array_or_string = FALSE, $default = "1 D")
  634. {
  635. foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;
  636. do_action("ws_plugin__s2member_before_paypal_pro_period3", get_defined_vars());
  637. unset /* Unset defined __refs, __v. */($__refs, $__v);
  638.  
  639. if(isset($array_or_string["PROFILENAME"]) /* Payflow® API alternative. */)
  640. $array_or_string["PROFILEREFERENCE"] = $array_or_string["PROFILENAME"];
  641.  
  642. if(is_array($array = $array_or_string) && (!empty($array["PROFILEREFERENCE"]) || !empty($array["rp_invoice_id"])))
  643. {
  644. list($_reference, $_domain, $_item_number) = array_map("trim", preg_split("/~/", ((!empty($array["PROFILEREFERENCE"])) ? $array["PROFILEREFERENCE"] : $array["rp_invoice_id"]), 3));
  645. list($_start_time, $_period1, $_period3) = array_map("trim", preg_split("/\:/", $_reference, 3));
  646. }
  647. else if(is_array($array = $array_or_string) && !empty($array["period3"]))
  648. $_period3 = trim($array["period3"]);
  649.  
  650. else if(is_string($string = $array_or_string) && !empty($string))
  651. $_period3 = trim($string);
  652.  
  653. if /* Were we able to get a `period3` string? */(!empty($_period3))
  654. {
  655. list($num, $span) = array_map("trim", preg_split("/ /", $_period3, 2));
  656.  
  657. if(strtoupper($span) === "SEMIMONTH" && is_numeric($num) && $num >= 1)
  658. { $num = "2"; $span = "W"; }
  659.  
  660. if /* To Standard format. */(strlen($span) !== 1)
  661. $span = c_ws_plugin__s2member_paypal_utilities::paypal_std_term($span);
  662.  
  663. $span = (preg_match("/^[DWMY]$/i", $span)) ? $span : "";
  664. $num = ($span && is_numeric($num) && $num >= 0) ? $num : "";
  665.  
  666. $period3 = ($num && $span) ? $num." ".strtoupper($span) : $default;
  667.  
  668. return apply_filters("ws_plugin__s2member_paypal_pro_period3", $period3, get_defined_vars());
  669. }
  670. else return apply_filters("ws_plugin__s2member_paypal_pro_period3", $default, get_defined_vars());
  671. }
  672. }
  673. }
  674. ?>

s2member\includes\classes\paypal-utilities.inc.php

Source Code Documentation