PageRenderTime 33ms CodeModel.GetById 0ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/s2member/includes/classes/paypal-utilities.inc.php

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