Source Code Documentation


s2member-pro\includes\classes\gateways\authnet\authnet-utilities.inc.php


  1. <?php
  2. /**
  3. * Authorize.Net® utilities.
  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\AuthNet
  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_authnet_utilities"))
  37. {
  38. /**
  39. * Authorize.Net® utilities.
  40. *
  41. * @package s2Member\AuthNet
  42. * @since 1.5
  43. */
  44. class c_ws_plugin__s2member_pro_authnet_utilities
  45. {
  46. /**
  47. * Calls upon Authorize.Net® AIM, and returns the response.
  48. *
  49. * @package s2Member\AuthNet
  50. * @since 1.5
  51. *
  52. * @param array $post_vars An array of variables to send through the Authorize.Net® API call.
  53. * @return array An array of variables returned from the API call.
  54. *
  55. * @todo Continue optimizing this routine with ``empty()`` and ``isset()``.
  56. */
  57. public static function authnet_aim_response($post_vars = FALSE)
  58. {
  59. global $current_site, $current_blog; /* For Multisite support. */
  60. /**/
  61. $url = "https://".(($GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["pro_authnet_sandbox"]) ? "test.authorize.net" : "secure.authorize.net")."/gateway/transact.dll";
  62. /**/
  63. $post_vars = (is_array($post_vars)) ? $post_vars : array(); /* Must be in array format. */
  64. /**/
  65. $post_vars["x_version"] = "3.1"; /* Configure the Authorize.Net® transaction version. */
  66. $post_vars["x_login"] = $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["pro_authnet_api_login_id"];
  67. $post_vars["x_tran_key"] = $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["pro_authnet_api_trans_key"];
  68. /**/
  69. $post_vars["x_delim_data"] = "true"; /* Yes, return a delimited string. */
  70. $post_vars["x_delim_char"] = ","; /* Fields delimitation character. */
  71. $post_vars["x_encap_char"] = '"'; /* Field encapsulation character. */
  72. $post_vars["x_relay_response"] = "false"; /* Always off for AIM. */
  73. /**/
  74. $post_vars["x_invoice_num"] = (!empty($post_vars["x_invoice_num"])) ? substr($post_vars["x_invoice_num"], 0, 20) : "";
  75. $post_vars["x_description"] = (!empty($post_vars["x_description"])) ? substr($post_vars["x_description"], 0, 255) : "";
  76. $post_vars["x_description"] = c_ws_plugin__s2member_utils_strings::strip_2_kb_chars($post_vars["x_description"]);
  77. /**/
  78. $input_time = date("D M j, Y g:i:s a T"); /* Record input time for logging. */
  79. /**/
  80. $csv = trim(c_ws_plugin__s2member_utils_urls::remote($url, $post_vars, array("timeout" => 20)));
  81. /**/
  82. $output_time = date("D M j, Y g:i:s a T"); /* Now record after output time. */
  83. /**/
  84. $response = ($csv) ? c_ws_plugin__s2member_utils_strings::trim_dq_deep(preg_split("/\",\"/", $csv)) : array();
  85. $response = c_ws_plugin__s2member_utils_strings::trim_deep(stripslashes_deep($response));
  86. /**/
  87. foreach(array("response_code", "response_subcode", "response_reason_code", "response_reason_text", "authorization_code", "avs_response", "transaction_id", "invoice_number", "description", "amount", "method", "transaction_type", "customer_id", "first_name", "last_name", "company", "address", "city", "state", "zipcode", "country", "phone", "fax", "email", "ship_to_first_name", "ship_to_last_name", "ship_to_company", "ship_to_address", "ship_to_city", "ship_to_state", "ship_to_zipcode", "ship_to_country", "tax", "duty", "freight", "tax_exempt", "po_number", "md5_hash", "card_code_response", "cavv_response", "card_number", "card_type", "split_tender_id", "requested_amount", "balance_on_card") as $order => $field_name)
  88. $response[$field_name] = (isset($response[$order])) ? $response[$order] : null;
  89. /**/
  90. if(empty($response["response_code"]) || $response["response_code"] !== "1") /* A value of 1 indicates success. */
  91. {
  92. if(strlen($response["response_reason_code"]) || $response["response_reason_text"])
  93. /* translators: Exclude `%2$s`. This is an English error returned by Authorize.Net®. Please 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_pro_authnet_aim_response`. */
  94. $response["__error"] = sprintf(_x('Error #%1$s. %2$s.', "s2member-front", "s2member"), $response["response_reason_code"], rtrim($response["response_reason_text"], "."));
  95. /**/
  96. else /* Else, generate an error messsage - so something is reported back to the Customer. */
  97. $response["__error"] = _x("Error. Please contact Support for assistance.", "s2member-front", "s2member");
  98. }
  99. /*
  100. If debugging is enabled; we need to maintain a comprehensive log file.
  101. Logging now supports Multisite Networking as well.
  102. */
  103. $logv = c_ws_plugin__s2member_utilities::ver_details();
  104. $logm = c_ws_plugin__s2member_utilities::mem_details();
  105. $log4 = $_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"]."\nUser-Agent: ".$_SERVER["HTTP_USER_AGENT"];
  106. $log4 = (is_multisite() && !is_main_site()) ? ($_log4 = $current_blog->domain.$current_blog->path)."\n".$log4 : $log4;
  107. $log2 = (is_multisite() && !is_main_site()) ? "authnet-api-4-".trim(preg_replace("/[^a-z0-9]/i", "-", $_log4), "-").".log" : "authnet-api.log";
  108. /**/
  109. if(strlen($post_vars["x_card_num"]) > 4) /* Only log last 4 digits for security. */
  110. $post_vars["x_card_num"] = str_repeat("*", strlen($post_vars["x_card_num"]) - 4)/**/
  111. .substr($post_vars["x_card_num"], -4); /* Then display last 4 digits. */
  112. /**/
  113. if($GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["gateway_debug_logs"])
  114. if(is_dir($logs_dir = $GLOBALS["WS_PLUGIN__"]["s2member"]["c"]["logs_dir"]))
  115. if(is_writable($logs_dir) && c_ws_plugin__s2member_utils_logs::archive_oversize_log_files())
  116. if(($log = "-------- Input vars: ( ".$input_time." ) --------\n".var_export($post_vars, true)."\n"))
  117. if(($log .= "-------- Output string/vars: ( ".$output_time." ) --------\n".$csv."\n".var_export($response, true)))
  118. file_put_contents($logs_dir."/".$log2, $logv."\n".$logm."\n".$log4."\n".$log."\n\n", FILE_APPEND);
  119. /**/
  120. return apply_filters("ws_plugin__s2member_pro_authnet_aim_response", c_ws_plugin__s2member_pro_authnet_utilities::_authnet_aim_response_filters($response), get_defined_vars());
  121. }
  122. /**
  123. * A sort of callback function that Filters Authorize.Net® AIM responses.
  124. *
  125. * Provides alternative explanations in some cases that require special attention.
  126. *
  127. * @package s2Member\AuthNet
  128. * @since 1.5
  129. *
  130. * @param array $response An array of response variables.
  131. * @return array An array of response variables; possibly modified by this routine.
  132. */
  133. public static function _authnet_aim_response_filters($response = FALSE)
  134. {
  135. return $response; /* Nothing here yet. */
  136. }
  137. /**
  138. * Calls upon Authorize.Net® ARB, and returns the response.
  139. *
  140. * @package s2Member\AuthNet
  141. * @since 1.5
  142. *
  143. * @param array $post_vars An array of variables to send through the Authorize.Net® API call.
  144. * @return array An array of variables returned from the API call.
  145. *
  146. * @todo Continue optimizing this routine with ``empty()`` and ``isset()``.
  147. */
  148. public static function authnet_arb_response($post_vars = FALSE)
  149. {
  150. global $current_site, $current_blog; /* For Multisite support. */
  151. /**/
  152. $url = "https://".(($GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["pro_authnet_sandbox"]) ? "apitest.authorize.net" : "api.authorize.net")."/xml/v1/request.api";
  153. /**/
  154. $post_vars = (is_array($post_vars)) ? $post_vars : array(); /* Must be in array format. */
  155. /**/
  156. $post_vars["x_login"] = $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["pro_authnet_api_login_id"];
  157. $post_vars["x_tran_key"] = $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["pro_authnet_api_trans_key"];
  158. /**/
  159. $post_vars["x_invoice_num"] = (!empty($post_vars["x_invoice_num"])) ? substr($post_vars["x_invoice_num"], 0, 20) : "";
  160. $post_vars["x_description"] = (!empty($post_vars["x_description"])) ? substr($post_vars["x_description"], 0, 255) : "";
  161. $post_vars["x_description"] = c_ws_plugin__s2member_utils_strings::strip_2_kb_chars($post_vars["x_description"]);
  162. /**/
  163. $trial = (!empty($post_vars["x_trial_occurrences"])) ? true : false; /* Indicates existence of trial. */
  164. /**/
  165. if((int)$post_vars["x_length"] === 30 && $post_vars["x_unit"] === "days")
  166. {
  167. $post_vars["x_length"] = 1;
  168. $post_vars["x_unit"] = "months";
  169. }
  170. /**/
  171. if(!empty($post_vars["x_method"]) && $post_vars["x_method"] === "create")
  172. {
  173. $xml = '<?xml version="1.0" encoding="utf-8"?>';
  174. /**/
  175. $xml .= '<ARBCreateSubscriptionRequest xmlns="AnetApi/xml/v1/schema/AnetApiSchema.xsd">';
  176. /**/
  177. $xml .= '<merchantAuthentication>';
  178. $xml .= '<name>'.esc_html($post_vars["x_login"]).'</name>';
  179. $xml .= '<transactionKey>'.esc_html($post_vars["x_tran_key"]).'</transactionKey>';
  180. $xml .= '</merchantAuthentication>';
  181. /**/
  182. $xml .= '<refId>'.esc_html($post_vars["x_invoice_num"]).'</refId>';
  183. /**/
  184. $xml .= '<subscription>';
  185. /**/
  186. $xml .= '<name>'.esc_html($_SERVER["HTTP_HOST"]).'</name>';
  187. /**/
  188. $xml .= '<paymentSchedule>';
  189. $xml .= '<interval>';
  190. $xml .= '<length>'.esc_html($post_vars["x_length"]).'</length>';
  191. $xml .= '<unit>'.esc_html($post_vars["x_unit"]).'</unit>';
  192. $xml .= '</interval>';
  193. $xml .= '<startDate>'.esc_html($post_vars["x_start_date"]).'</startDate>';
  194. $xml .= '<totalOccurrences>'.esc_html($post_vars["x_total_occurrences"]).'</totalOccurrences>';
  195. $xml .= ($trial) ? '<trialOccurrences>'.esc_html($post_vars["x_trial_occurrences"]).'</trialOccurrences>' : '';
  196. $xml .= '</paymentSchedule>';
  197. /**/
  198. $xml .= '<amount>'.esc_html($post_vars["x_amount"]).'</amount>';
  199. $xml .= ($trial) ? '<trialAmount>'.esc_html($post_vars["x_trial_amount"]).'</trialAmount>' : '';
  200. /**/
  201. $xml .= '<payment>';
  202. $xml .= '<creditCard>';
  203. $xml .= '<cardNumber>'.esc_html($post_vars["x_card_num"]).'</cardNumber>';
  204. $xml .= '<expirationDate>'.esc_html($post_vars["x_exp_date"]).'</expirationDate>';
  205. $xml .= '<cardCode>'.esc_html($post_vars["x_card_code"]).'</cardCode>';
  206. $xml .= '</creditCard>';
  207. $xml .= '</payment>';
  208. /**/
  209. $xml .= '<order>';
  210. $xml .= '<invoiceNumber>'.esc_html($post_vars["x_invoice_num"]).'</invoiceNumber>';
  211. $xml .= '<description>'.esc_html($post_vars["x_description"]).'</description>';
  212. $xml .= '</order>';
  213. /**/
  214. $xml .= '<customer>';
  215. $xml .= '<email>'.esc_html($post_vars["x_email"]).'</email>';
  216. $xml .= '</customer>';
  217. /**/
  218. $xml .= '<billTo>';
  219. $xml .= '<firstName>'.esc_html(substr($post_vars["x_first_name"], 0, 50)).'</firstName>';
  220. $xml .= '<lastName>'.esc_html(substr($post_vars["x_last_name"], 0, 50)).'</lastName>';
  221. $xml .= '<address>'.esc_html(substr($post_vars["x_address"], 0, 60)).'</address>';
  222. $xml .= '<city>'.esc_html(substr($post_vars["x_city"], 0, 40)).'</city>';
  223. $xml .= '<state>'.esc_html(substr($post_vars["x_state"], 0, 2)).'</state>';
  224. $xml .= '<zip>'.esc_html(substr($post_vars["x_zip"], 0, 20)).'</zip>';
  225. $xml .= '<country>'.esc_html(substr($post_vars["x_country"], 0, 60)).'</country>';
  226. $xml .= '</billTo>';
  227. /**/
  228. $xml .= '</subscription>';
  229. /**/
  230. $xml .= '</ARBCreateSubscriptionRequest>';
  231. }
  232. /**/
  233. else if(!empty($post_vars["x_method"]) && $post_vars["x_method"] === "update")
  234. {
  235. $xml = '<?xml version="1.0" encoding="utf-8"?>';
  236. /**/
  237. $xml .= '<ARBUpdateSubscriptionRequest xmlns="AnetApi/xml/v1/schema/AnetApiSchema.xsd">';
  238. /**/
  239. $xml .= '<merchantAuthentication>';
  240. $xml .= '<name>'.esc_html($post_vars["x_login"]).'</name>';
  241. $xml .= '<transactionKey>'.esc_html($post_vars["x_tran_key"]).'</transactionKey>';
  242. $xml .= '</merchantAuthentication>';
  243. /**/
  244. $xml .= '<subscriptionId>'.esc_html($post_vars["x_subscription_id"]).'</subscriptionId>';
  245. /**/
  246. $xml .= '<subscription>';
  247. /**/
  248. $xml .= '<payment>';
  249. $xml .= '<creditCard>';
  250. $xml .= '<cardNumber>'.esc_html($post_vars["x_card_num"]).'</cardNumber>';
  251. $xml .= '<expirationDate>'.esc_html($post_vars["x_exp_date"]).'</expirationDate>';
  252. $xml .= '<cardCode>'.esc_html($post_vars["x_card_code"]).'</cardCode>';
  253. $xml .= '</creditCard>';
  254. $xml .= '</payment>';
  255. /**/
  256. $xml .= '<customer>';
  257. $xml .= '<email>'.esc_html($post_vars["x_email"]).'</email>';
  258. $xml .= '</customer>';
  259. /**/
  260. $xml .= '<billTo>';
  261. $xml .= '<firstName>'.esc_html($post_vars["x_first_name"]).'</firstName>';
  262. $xml .= '<lastName>'.esc_html($post_vars["x_last_name"]).'</lastName>';
  263. $xml .= '<address>'.esc_html($post_vars["x_address"]).'</address>';
  264. $xml .= '<city>'.esc_html($post_vars["x_city"]).'</city>';
  265. $xml .= '<state>'.esc_html($post_vars["x_state"]).'</state>';
  266. $xml .= '<zip>'.esc_html($post_vars["x_zip"]).'</zip>';
  267. $xml .= '<country>'.esc_html($post_vars["x_country"]).'</country>';
  268. $xml .= '</billTo>';
  269. /**/
  270. $xml .= '</subscription>';
  271. /**/
  272. $xml .= '</ARBUpdateSubscriptionRequest>';
  273. }
  274. /**/
  275. else if(!empty($post_vars["x_method"]) && $post_vars["x_method"] === "status")
  276. {
  277. $xml = '<?xml version="1.0" encoding="utf-8"?>';
  278. /**/
  279. $xml .= '<ARBGetSubscriptionStatusRequest xmlns="AnetApi/xml/v1/schema/AnetApiSchema.xsd">';
  280. /**/
  281. $xml .= '<merchantAuthentication>';
  282. $xml .= '<name>'.esc_html($post_vars["x_login"]).'</name>';
  283. $xml .= '<transactionKey>'.esc_html($post_vars["x_tran_key"]).'</transactionKey>';
  284. $xml .= '</merchantAuthentication>';
  285. /**/
  286. $xml .= '<subscriptionId>'.esc_html($post_vars["x_subscription_id"]).'</subscriptionId>';
  287. /**/
  288. $xml .= '</ARBGetSubscriptionStatusRequest>';
  289. }
  290. /**/
  291. else if(!empty($post_vars["x_method"]) && $post_vars["x_method"] === "cancel")
  292. {
  293. $xml = '<?xml version="1.0" encoding="utf-8"?>';
  294. /**/
  295. $xml .= '<ARBCancelSubscriptionRequest xmlns="AnetApi/xml/v1/schema/AnetApiSchema.xsd">';
  296. /**/
  297. $xml .= '<merchantAuthentication>';
  298. $xml .= '<name>'.esc_html($post_vars["x_login"]).'</name>';
  299. $xml .= '<transactionKey>'.esc_html($post_vars["x_tran_key"]).'</transactionKey>';
  300. $xml .= '</merchantAuthentication>';
  301. /**/
  302. $xml .= '<subscriptionId>'.esc_html($post_vars["x_subscription_id"]).'</subscriptionId>';
  303. /**/
  304. $xml .= '</ARBCancelSubscriptionRequest>';
  305. }
  306. /**/
  307. $req["headers"]["Accept"] = "application/xml; charset=UTF-8";
  308. $req["headers"]["Content-Type"] = "application/xml; charset=UTF-8";
  309. /**/
  310. $input_time = date("D M j, Y g:i:s a T"); /* Record input time for logging. */
  311. /**/
  312. $xml = trim(c_ws_plugin__s2member_utils_urls::remote($url, $xml, array_merge($req, array("timeout" => 20))));
  313. /**/
  314. $output_time = date("D M j, Y g:i:s a T"); /* Now record after output time. */
  315. /**/
  316. $response = c_ws_plugin__s2member_pro_authnet_utilities::_authnet_parse_arb_response($xml);
  317. /**/
  318. if(empty($response["response_code"]) || $response["response_code"] !== "I00001") /* A value of I00001 indicates success. */
  319. {
  320. if(strlen($response["response_reason_code"]) || $response["response_reason_text"])
  321. /* translators: Exclude `%2$s`. This is an English error returned by Authorize.Net®. Please 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_pro_authnet_arb_response`. */
  322. $response["__error"] = sprintf(_x('Error #%1$s. %2$s.', "s2member-front", "s2member"), $response["response_reason_code"], rtrim($response["response_reason_text"], "."));
  323. /**/
  324. else /* Else, generate an error messsage - so something is reported back to the Customer. */
  325. $response["__error"] = _x("Error. Please contact Support for assistance.", "s2member-front", "s2member");
  326. }
  327. /*
  328. If debugging is enabled; we need to maintain a comprehensive log file.
  329. Logging now supports Multisite Networking as well.
  330. */
  331. $logv = c_ws_plugin__s2member_utilities::ver_details();
  332. $logm = c_ws_plugin__s2member_utilities::mem_details();
  333. $log4 = $_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"]."\nUser-Agent: ".$_SERVER["HTTP_USER_AGENT"];
  334. $log4 = (is_multisite() && !is_main_site()) ? ($_log4 = $current_blog->domain.$current_blog->path)."\n".$log4 : $log4;
  335. $log2 = (is_multisite() && !is_main_site()) ? "authnet-api-4-".trim(preg_replace("/[^a-z0-9]/i", "-", $_log4), "-").".log" : "authnet-api.log";
  336. /**/
  337. if(strlen($post_vars["x_card_num"]) > 4) /* Only log last 4 digits for security. */
  338. $post_vars["x_card_num"] = str_repeat("*", strlen($post_vars["x_card_num"]) - 4)/**/
  339. .substr($post_vars["x_card_num"], -4); /* Then display last 4 digits. */
  340. /**/
  341. if($GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["gateway_debug_logs"])
  342. if(is_dir($logs_dir = $GLOBALS["WS_PLUGIN__"]["s2member"]["c"]["logs_dir"]))
  343. if(is_writable($logs_dir) && c_ws_plugin__s2member_utils_logs::archive_oversize_log_files())
  344. if(($log = "-------- Input vars: ( ".$input_time." ) --------\n".var_export($post_vars, true)."\n"))
  345. if(($log .= "-------- Output string/vars: ( ".$output_time." ) --------\n".$xml."\n".var_export($response, true)))
  346. file_put_contents($logs_dir."/".$log2, $logv."\n".$logm."\n".$log4."\n".$log."\n\n", FILE_APPEND);
  347. /**/
  348. return apply_filters("ws_plugin__s2member_pro_authnet_arb_response", c_ws_plugin__s2member_pro_authnet_utilities::_authnet_arb_response_filters($response), get_defined_vars());
  349. }
  350. /**
  351. * A sort of callback function that parses Authorize.Net® ARB responses.
  352. *
  353. * @package s2Member\AuthNet
  354. * @since 1.5
  355. *
  356. * @param str $xml XML markup returned by the ARB call.
  357. * @return array An array of response variables, parsed by s2Member.
  358. */
  359. public static function _authnet_parse_arb_response($xml = FALSE)
  360. {
  361. if($xml && preg_match("/\<(ErrorResponse).*?\>.*?\<code\>(.+?)\<\/code\>.*?\<\/\\1\>/is", $xml, $m))
  362. {
  363. $response["response_code"] = $response["response_reason_code"] = trim($m[2]);
  364. /**/
  365. if(preg_match("/\<(ErrorResponse).*?\>.*?\<text\>(.+?)\<\/text\>.*?\<\/\\1\>/is", $xml, $m))
  366. $response["response_text"] = $response["response_reason_text"] = trim($m[2]);
  367. }
  368. else if($xml && preg_match("/\<(ARBCreateSubscriptionResponse|ARBUpdateSubscriptionResponse|ARBGetSubscriptionStatusResponse|ARBCancelSubscriptionResponse).*?\>.*?\<code\>(.+?)\<\/code\>.*?\<\/\\1\>/is", $xml, $m))
  369. {
  370. $response["response_code"] = $response["response_reason_code"] = trim($m[2]);
  371. /**/
  372. if(preg_match("/\<(ARBCreateSubscriptionResponse|ARBUpdateSubscriptionResponse|ARBGetSubscriptionStatusResponse|ARBCancelSubscriptionResponse).*?\>.*?\<text\>(.+?)\<\/text\>.*?\<\/\\1\>/is", $xml, $m))
  373. $response["response_text"] = $response["response_reason_text"] = trim($m[2]);
  374. /**/
  375. if(preg_match("/\<(subscriptionId)\>(.+?)\<\/\\1\>/is", $xml, $m))
  376. $response["subscription_id"] = trim($m[2]);
  377. /**/
  378. if(preg_match("/\<(status)\>(.+?)\<\/\\1\>/is", $xml, $m))
  379. $response["subscription_status"] = trim($m[2]);
  380. }
  381. /**/
  382. $response = (!empty($response) && is_array($response)) ? $response : array();
  383. /**/
  384. return c_ws_plugin__s2member_utils_strings::trim_deep(stripslashes_deep($response));
  385. }
  386. /**
  387. * A sort of callback function that Filters Authorize.Net® ARB responses.
  388. *
  389. * Provides alternative explanations in some cases that require special attention.
  390. *
  391. * @package s2Member\AuthNet
  392. * @since 1.5
  393. *
  394. * @param array $response An array of response variables.
  395. * @return array An array of response variables; possibly modified by this routine.
  396. */
  397. public static function _authnet_arb_response_filters($response = FALSE)
  398. {
  399. return $response; /* Nothing here yet. */
  400. }
  401. /**
  402. * Get ``$_POST`` or ``$_REQUEST`` vars from Authorize.Net®.
  403. *
  404. * Authorize.Net® returns `x_MD5_Hash` in uppercase format for some reason.
  405. *
  406. * @package s2Member\AuthNet
  407. * @since 1.5
  408. *
  409. * @return array|bool An array of verified ``$_POST`` or ``$_REQUEST`` variables, else false.
  410. *
  411. * @todo Continue optimizing this routine with ``empty()`` and ``isset()``.
  412. * @todo Candidate for the use of ``ifsetor()``?
  413. * @todo Update to use ``strcasecmp()``.
  414. */
  415. public static function authnet_postvars()
  416. {
  417. if(!empty($_REQUEST["s2member_pro_authnet_notify"]) && !empty($_REQUEST["x_MD5_Hash"]))
  418. {
  419. $postvars = c_ws_plugin__s2member_utils_strings::trim_deep(stripslashes_deep($_REQUEST));
  420. /**/
  421. foreach($postvars as $var => $value)
  422. if(preg_match("/^s2member_/", $var))
  423. unset($postvars[$var]);
  424. /**/
  425. $aim_digest_vars = $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["pro_authnet_api_login_id"].$postvars["x_trans_id"].$postvars["x_amount"];
  426. $arb_digest_vars = $postvars["x_trans_id"].$postvars["x_amount"];
  427. /**/
  428. if(strtolower($postvars["x_MD5_Hash"]) === md5($GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["pro_authnet_api_salt_key"].$aim_digest_vars))
  429. return $postvars;
  430. /**/
  431. else if(strtolower($postvars["x_MD5_Hash"]) === md5($GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["pro_authnet_api_salt_key"].$arb_digest_vars))
  432. return $postvars;
  433. /**/
  434. else /* Nope. */
  435. return false;
  436. }
  437. else /* Nope. */
  438. return false;
  439. }
  440. /**
  441. * Calculates start date for a Recurring Payment Profile.
  442. *
  443. * @package s2Member\AuthNet
  444. * @since 1.5
  445. *
  446. * @param str $period1 Optional. A "Period Term" combination. Defaults to `0 D`.
  447. * @param str $period3 Optional. A "Period Term" combination. Defaults to `0 D`.
  448. * @return int The start time, a Unix timestamp.
  449. */
  450. public static function authnet_start_time($period1 = FALSE, $period3 = FALSE)
  451. {
  452. if(!($p1_time = 0) && ($period1 = trim(strtoupper($period1))))
  453. {
  454. list($num, $span) = preg_split("/ /", $period1, 2);
  455. /**/
  456. $days = 0; /* Days start at 0. */
  457. /**/
  458. if(is_numeric($num) && !is_numeric($span))
  459. {
  460. $days = ($span === "D") ? 1 : $days;
  461. $days = ($span === "W") ? 7 : $days;
  462. $days = ($span === "M") ? 30 : $days;
  463. $days = ($span === "Y") ? 365 : $days;
  464. }
  465. /**/
  466. $p1_days = (int)$num * (int)$days;
  467. $p1_time = $p1_days * 86400;
  468. }
  469. /**/
  470. if(!($p3_time = 0) && ($period3 = trim(strtoupper($period3))))
  471. {
  472. list($num, $span) = preg_split("/ /", $period3, 2);
  473. /**/
  474. $days = 0; /* Days start at 0. */
  475. /**/
  476. if(is_numeric($num) && !is_numeric($span))
  477. {
  478. $days = ($span === "D") ? 1 : $days;
  479. $days = ($span === "W") ? 7 : $days;
  480. $days = ($span === "M") ? 30 : $days;
  481. $days = ($span === "Y") ? 365 : $days;
  482. }
  483. /**/
  484. $p3_days = (int)$num * (int)$days;
  485. $p3_time = $p3_days * 86400;
  486. }
  487. /**/
  488. $start_time = strtotime("now") + $p1_time + $p3_time;
  489. /**/
  490. $start_time = ($start_time <= 0) ? strtotime("now") : $start_time;
  491. /**/
  492. $start_time = $start_time + 43200; /* + 12 hours. */
  493. /**/
  494. return $start_time;
  495. }
  496. /**
  497. * Calculates period in days for Authorize.Net® ARB integration.
  498. *
  499. * @package s2Member\AuthNet
  500. * @since 1.5
  501. *
  502. * @param int|str $period Optional. A numeric Period that coincides with ``$term``.
  503. * @param str $term Optional. A Term that coincides with ``$period``.
  504. * @return int A "Period Term", in days. Defaults to `0`.
  505. */
  506. public static function authnet_per_term_2_days($period = FALSE, $term = FALSE)
  507. {
  508. if(is_numeric($period) && !is_numeric($term) && ($term = strtoupper($term)))
  509. {
  510. $days = ($term === "D") ? 1 : $days;
  511. $days = ($term === "W") ? 7 : $days;
  512. $days = ($term === "M") ? 30 : $days;
  513. $days = ($term === "Y") ? 365 : $days;
  514. /**/
  515. return (int)$period * (int)$days;
  516. }
  517. else
  518. return 0;
  519. }
  520. /**
  521. * Re-formats credit card expiration dates for Authorize.Net®.
  522. *
  523. * @package s2Member\AuthNet
  524. * @since 1.5
  525. *
  526. * @param str $exp Expects a credit card expiration date in `mm/yyyy` format.
  527. * @return str A credit card expiration date in `yyyy-mm` format for Authorize.Net®.
  528. */
  529. public static function authnet_exp_date($exp = FALSE)
  530. {
  531. list($mm, $yyyy) = preg_split("/\//", $exp, 2);
  532. /**/
  533. return trim($yyyy."-".$mm, "- \t\n\r\0\x0B");
  534. }
  535. /**
  536. * Parses an Authorize.Net® Silent Post.
  537. *
  538. * Parses `s2_reference`, `s2_domain`, `s2_invoice`, `s2_start_time`, `s2_p1`, `s2_p3`, `s2_custom`
  539. * from an Authorize.Net® Silent Post *( aka: IPN )* response.
  540. *
  541. * @package s2Member\AuthNet
  542. * @since 1.5
  543. *
  544. * @param array $array Expects an array of details returned by an Authorize.Net® Silent Post.
  545. * @return array|bool The same ``$array``, but with additional details filled by this routine; else false.
  546. *
  547. * @todo Continue optimizing this routine with ``empty()`` and ``isset()``.
  548. * @todo Candidate for the use of ``ifsetor()``?
  549. */
  550. public static function authnet_parse_arb_desc($array = FALSE)
  551. {
  552. if(is_array($array) && !empty($array["x_description"]) && preg_match("/\(\((.+?)\)\)/i", $array["x_description"], $m))
  553. {
  554. list($array["s2_reference"], $array["s2_domain"], $array["s2_invoice"]) = preg_split("/~/", $m[1], 3);
  555. list($array["s2_start_time"], $array["s2_p1"], $array["s2_p3"]) = preg_split("/\:/", $array["s2_reference"], 3);
  556. /**/
  557. $array["x_description"] = preg_replace("/\(\((.+?)\)\)/i", "", $array["x_description"]);
  558. /**/
  559. $array["s2_custom"] = (!empty($array["x_subscription_id"])) ? c_ws_plugin__s2member_utils_users::get_user_custom_with($array["x_subscription_id"]) : "";
  560. $array["s2_custom"] = ($array["s2_custom"]) ? $array["s2_custom"] : $array["s2_domain"];
  561. /**/
  562. return c_ws_plugin__s2member_utils_strings::trim_deep($array);
  563. }
  564. else /* False. */
  565. return false;
  566. }
  567. /**
  568. * Determines whether or not Tax may apply.
  569. *
  570. * @package s2Member\AuthNet
  571. * @since 1.5
  572. *
  573. * @return bool True if Tax may apply, else false.
  574. */
  575. public static function authnet_tax_may_apply()
  576. {
  577. if((float)$GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["pro_default_tax"] > 0)
  578. return true;
  579. /**/
  580. else if($GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["pro_tax_rates"])
  581. return true;
  582. /**/
  583. return false;
  584. }
  585. /**
  586. * Handles the return of Tax for Pro Forms, via AJAX; through a JSON object.
  587. *
  588. * @package s2Member\AuthNet
  589. * @since 1.5
  590. *
  591. * @return null Or exits script execution after returning data for AJAX caller.
  592. *
  593. * @todo Check the use of ``strip_tags()`` in this routine?
  594. * @todo Continue optimizing this routine with ``empty()`` and ``isset()``.
  595. * @todo Candidate for the use of ``ifsetor()``?
  596. */
  597. public static function authnet_ajax_tax()
  598. {
  599. if(!empty($_POST["ws_plugin__s2member_pro_authnet_ajax_tax"]) && ($nonce = $_POST["ws_plugin__s2member_pro_authnet_ajax_tax"]) && (wp_verify_nonce($nonce, "ws-plugin--s2member-pro-authnet-ajax-tax") || c_ws_plugin__s2member_utils_encryption::decrypt($nonce) === "ws-plugin--s2member-pro-authnet-ajax-tax"))
  600. /* A wp_verify_nonce() won't always work here, because s2member-pro-min.js must be cacheable. The output from wp_create_nonce() would go stale.
  601. So instead, s2member-pro-min.js should use ``c_ws_plugin__s2member_utils_encryption::encrypt()`` as an alternate form of nonce. */
  602. {
  603. status_header(200); /* Send a 200 OK status header. */
  604. header("Content-Type: text/plain; charset=utf-8"); /* Content-Type text/plain with UTF-8. */
  605. eval('while (@ob_end_clean ());'); /* End/clean all output buffers that may or may not exist. */
  606. /**/
  607. if(!empty($_POST["ws_plugin__s2member_pro_authnet_ajax_tax_vars"]) && is_array($_p_tax_vars = c_ws_plugin__s2member_utils_strings::trim_deep(stripslashes_deep($_POST["ws_plugin__s2member_pro_authnet_ajax_tax_vars"]))))
  608. {
  609. if(is_array($attr = (!empty($_p_tax_vars["attr"])) ? unserialize(c_ws_plugin__s2member_utils_encryption::decrypt($_p_tax_vars["attr"])) : false))
  610. {
  611. $attr = (!empty($attr["coupon"])) ? c_ws_plugin__s2member_pro_authnet_utilities::authnet_apply_coupon($attr, $attr["coupon"]) : $attr;
  612. /**/
  613. $trial = ($attr["rr"] !== "BN" && $attr["tp"]) ? true : false; /* Is there a trial? */
  614. $sub_total_today = ($trial) ? $attr["ta"] : $attr["ra"]; /* What is the sub-total today? */
  615. /**/
  616. $state = strip_tags($_p_tax_vars["state"]);
  617. $country = strip_tags($_p_tax_vars["country"]);
  618. $zip = strip_tags($_p_tax_vars["zip"]);
  619. $currency = $attr["cc"]; /* Currency. */
  620. $desc = $attr["desc"]; /* Description. */
  621. /**/
  622. /* Trial is `null` in this function call. We only need to return what it costs today.
  623. However, we do tag on a "trial" element in the array so the ajax routine will know about this. */
  624. $a = c_ws_plugin__s2member_pro_authnet_utilities::authnet_cost(null, $sub_total_today, $state, $country, $zip, $currency, $desc);
  625. echo json_encode(array("trial" => $trial, "sub_total" => $a["sub_total"], "tax" => $a["tax"], "tax_per" => $a["tax_per"], "total" => $a["total"], "cur" => $a["cur"], "cur_symbol" => $a["cur_symbol"], "desc" => $a["desc"]));
  626. }
  627. }
  628. /**/
  629. exit(); /* Clean exit. */
  630. }
  631. }
  632. /**
  633. * Handles all cost calculations for Authorize.Net®.
  634. *
  635. * Returns an associative array with a possible Percentage Rate, along with the calculated Tax Amount.
  636. * Tax calculations are based on State/Province, Country, and/or Zip Code.
  637. * Updated to support multiple data fields in it's return value.
  638. *
  639. * @package s2Member\AuthNet
  640. * @since 1.5
  641. *
  642. * @param int|str $trial_sub_total Optional. A numeric Amount/cost of a possible Initial/Trial being offered.
  643. * @param int|str $sub_total Optional. A numeric Amount/cost of the purchase and/or Regular Period.
  644. * @param str $state Optional. The State/Province where the Customer is billed.
  645. * @param str $country Optional. The Country where the Customer is billed.
  646. * @param int|str $zip Optional. The Postal/Zip Code where the Customer is billed.
  647. * @param str $currency Optional. Expects a 3 character Currency Code.
  648. * @param str $desc Optional. Description of the sale.
  649. * @return array Array of calculations.
  650. *
  651. * @todo Add support for `Zip + 4` syntax?
  652. */
  653. public static function authnet_cost($trial_sub_total = FALSE, $sub_total = FALSE, $state = FALSE, $country = FALSE, $zip = FALSE, $currency = FALSE, $desc = FALSE)
  654. {
  655. $state = strtoupper(c_ws_plugin__s2member_pro_utilities::full_state($state, ($country = strtoupper($country))));
  656. $rates = strtoupper($GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["pro_tax_rates"]);
  657. $default = $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["pro_default_tax"];
  658. $ps = _x("%", "s2member-front percentage-symbol", "s2member");
  659. /**/
  660. foreach(array("trial_sub_total" => $trial_sub_total, "sub_total" => $sub_total) as $this_key => $this_sub_total)
  661. {
  662. unset($_default, $this_tax, $this_tax_per, $this_total, $configured_rates, $configured_rate, $location, $rate, $m);
  663. /**/
  664. if(is_numeric($this_sub_total) && $this_sub_total > 0) /* Must have a valid Sub-Total. */
  665. {
  666. if(preg_match("/%$/", $default)) /* Percentage-based. */
  667. {
  668. if(($_default = (float)$default) > 0)
  669. {
  670. $this_tax = round(($this_sub_total / 100) * $_default, 2);
  671. $this_tax_per = $_default.$ps;
  672. }
  673. else /* Else the Tax is 0.00. */
  674. {
  675. $this_tax = 0.00;
  676. $this_tax_per = $_default.$ps;
  677. }
  678. }
  679. else if(($_default = (float)$default) > 0)
  680. {
  681. $this_tax = round($_default, 2);
  682. $this_tax_per = ""; /* Flat. */
  683. }
  684. else /* Else the Tax is 0.00. */
  685. {
  686. $this_tax = 0.00; /* No Tax. */
  687. $this_tax_per = ""; /* Flat rate. */
  688. }
  689. /**/
  690. if(strlen($country) === 2) /* Must have a valid country. */
  691. {
  692. foreach(preg_split("/[\r\n\t]+/", $rates) as $rate)
  693. {
  694. if($rate = trim($rate)) /* Do NOT process empty lines. */
  695. {
  696. list($location, $rate) = preg_split("/\=/", $rate, 2);
  697. $location = trim($location);
  698. $rate = trim($rate);
  699. /**/
  700. if($location === $country)
  701. $configured_rates[1] = $rate;
  702. /**/
  703. else if($state && $location === $state."/".$country)
  704. $configured_rates[2] = $rate;
  705. /**/
  706. else if($state && preg_match("/^([A-Z]{2})\/(".preg_quote($country, "/").")$/", $location, $m) && strtoupper(c_ws_plugin__s2member_pro_utilities::full_state($m[1], $m[2]))."/".$m[2] === $state."/".$country)
  707. $configured_rates[2] = $rate;
  708. /**/
  709. else if($zip && preg_match("/^([0-9]+)-([0-9]+)\/(".preg_quote($country, "/").")$/", $location, $m) && $zip >= $m[1] && $zip <= $m[2] && $country === $m[3])
  710. $configured_rates[3] = $rate;
  711. /**/
  712. else if($zip && $location === $zip."/".$country)
  713. $configured_rates[4] = $rate;
  714. }
  715. }
  716. /**/
  717. if(is_array($configured_rates) && !empty($configured_rates))
  718. {
  719. krsort($configured_rates);
  720. $configured_rate = array_shift($configured_rates);
  721. /**/
  722. if(preg_match("/%$/", $configured_rate)) /* Percentage. */
  723. {
  724. if(($configured_rate = (float)$configured_rate) > 0)
  725. {
  726. $this_tax = round(($this_sub_total / 100) * $configured_rate, 2);
  727. $this_tax_per = $configured_rate.$ps;
  728. }
  729. else /* Else the Tax is 0.00. */
  730. {
  731. $this_tax = 0.00; /* No Tax. */
  732. $this_tax_per = $configured_rate.$ps;
  733. }
  734. }
  735. else if(($configured_rate = (float)$configured_rate) > 0)
  736. {
  737. $this_tax = round($configured_rate, 2);
  738. $this_tax_per = ""; /* Flat rate. */
  739. }
  740. else /* Else the Tax is 0.00. */
  741. {
  742. $this_tax = 0.00; /* No Tax. */
  743. $this_tax_per = ""; /* Flat rate. */
  744. }
  745. }
  746. }
  747. /**/
  748. $this_total = $this_sub_total + $this_tax;
  749. }
  750. else /* Else the Tax is 0.00. */
  751. {
  752. $this_tax = 0.00; /* No Tax. */
  753. $this_tax_per = ""; /* Flat rate. */
  754. $this_sub_total = 0.00; /* 0.00. */
  755. $this_total = 0.00; /* 0.00. */
  756. }
  757. /**/
  758. if($this_key === "trial_sub_total")
  759. {
  760. $trial_tax = $this_tax;
  761. $trial_tax_per = $this_tax_per;
  762. $trial_sub_total = $this_sub_total;
  763. $trial_total = $this_total;
  764. }
  765. else if($this_key === "sub_total")
  766. {
  767. $tax = $this_tax;
  768. $tax_per = $this_tax_per;
  769. $sub_total = $this_sub_total;
  770. $total = $this_total;
  771. }
  772. }
  773. /**/
  774. return array("trial_sub_total" => number_format($trial_sub_total, 2, ".", ""), "sub_total" => number_format($sub_total, 2, ".", ""), "trial_tax" => number_format($trial_tax, 2, ".", ""), "tax" => number_format($tax, 2, ".", ""), "trial_tax_per" => $trial_tax_per, "tax_per" => $tax_per, "trial_total" => number_format($trial_total, 2, ".", ""), "total" => number_format($total, 2, ".", ""), "cur" => $currency, "cur_symbol" => c_ws_plugin__s2member_utils_cur::symbol($currency), "desc" => $desc);
  775. }
  776. /**
  777. * Checks to see if a Coupon Code was supplied, and if so; what does it provide?
  778. *
  779. * @package s2Member\AuthNet
  780. * @since 1.5
  781. *
  782. * @param array $attr An array of Pro Form Attributes.
  783. * @param str $coupon_code Optional. A possible Coupon Code supplied by the Customer.
  784. * @param str $return Optional. Return type. One of `response|attr`. Defaults to `attr`.
  785. * @param array $process Optional. An array of additional processing routines to run here.
  786. * One or more of these values: `affiliates-1px-response|affiliates-silent-post|notifications`.
  787. * @return array|str Original array, with prices and description modified when/if a Coupon Code is accepted.
  788. * Or, if ``$return === "response"``, return a string response, indicating status.
  789. *
  790. * @todo See if it's possible to simplify this routine.
  791. * @todo Add support for tracking Coupon Code usage.
  792. * @todo Add support for a fixed number of uses.
  793. */
  794. public static function authnet_apply_coupon($attr = FALSE, $coupon_code = FALSE, $return = FALSE, $process = FALSE)
  795. {
  796. if(($coupon_code = trim(strtolower($coupon_code))) || ($coupon_code = trim(strtolower($attr["coupon"]))))
  797. if($attr["accept_coupons"] && $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["pro_coupon_codes"])
  798. {
  799. $cs = c_ws_plugin__s2member_utils_cur::symbol($attr["cc"]);
  800. $tx = (c_ws_plugin__s2member_pro_authnet_utilities::authnet_tax_may_apply()) ? _x(" + tax", "s2member-front", "s2member") : "";
  801. $ps = _x("%", "s2member-front percentage-symbol", "s2member");
  802. /**/
  803. if(strlen($affiliate_suffix_chars = $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["pro_affiliate_coupon_code_suffix_chars"]))
  804. if(preg_match("/^(.+?)".preg_quote($affiliate_suffix_chars, "/")."([0-9]+)$/i", $coupon_code, $m))
  805. ($full_coupon_code = $m[0]).($coupon_code = $m[1]).($affiliate_id = $m[2]);
  806. unset /* Just a little housekeeping here. */($affiliate_suffix_chars, $m);
  807. /**/
  808. foreach(c_ws_plugin__s2member_utils_strings::trim_deep(preg_split("/[\r\n\t]+/", $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["pro_coupon_codes"])) as $_line)
  809. {
  810. if(($_line = trim($_line, " \r\n\t\0\x0B|")) && is_array($_coupon = preg_split("/\|/", $_line)))
  811. {
  812. $coupon["code"] = (!empty($_coupon[0])) ? trim(strtolower($_coupon[0])) : "";
  813. /**/
  814. $coupon["percentage"] = (!empty($_coupon[1]) && preg_match("/%/", $_coupon[1])) ? (float)$_coupon[1] : 0;
  815. $coupon["flat-rate"] = (!empty($_coupon[1]) && !preg_match("/%/", $_coupon[1])) ? (float)$_coupon[1] : 0;
  816. /**/
  817. $coupon["expired"] = (!empty($_coupon[2]) && /* Expired? */ strtotime($_coupon[2]) < time()) ? $_coupon[2] : false;
  818. /**/
  819. $coupon["directive"] = (!empty($_coupon[3]) && ($_coupon[3] = strtolower($_coupon[3]))) ? preg_replace("/_/", "-", $_coupon[3]) : "all";
  820. $coupon["directive"] = (preg_match("/^(ta-only|ra-only|all)$/", $coupon["directive"])) ? $coupon["directive"] : "all";
  821. /**/
  822. $coupon["singulars"] = (!empty($_coupon[4]) && ($_coupon[4] = strtolower($_coupon[4])) && $_coupon[4] !== "all") ? $_coupon[4] : "all";
  823. $coupon["singulars"] = ($coupon["singulars"] !== "all") ? preg_split("/[\r\n\t\s;,]+/", trim(preg_replace("/[^0-9,]/", "", $coupon["singulars"]), ",")) : array("all");
  824. /**/
  825. unset /* Just a little housekeeping here. Unset these temporary variables. */($_line, $_coupon);
  826. /**/
  827. if($coupon_code === $coupon["code"] && /* And it's NOT yet expired, or lasts forever? */ !$coupon["expired"])
  828. {
  829. if($coupon["singulars"] === array("all")|| in_array($attr["singular"], $coupon["singulars"]))
  830. {
  831. $coupon_accepted = /* Yes, this Coupon Code has been accepted. */ true;
  832. /**/
  833. if($coupon["flat-rate"]) /* If it's a flat-rate Coupon. */
  834. {
  835. if(($coupon["directive"] === "ra-only" || $coupon["directive"] === "all") && $attr["sp"])
  836. {
  837. $coupon_applies = /* Applying. */ true;
  838. /**/
  839. $ta = number_format($attr["ta"], 2, ".", "");
  840. $ta = ($ta >= 0.00) ? $ta : "0.00";
  841. /**/
  842. $ra = number_format($attr["ra"] - $coupon["flat-rate"], 2, ".", "");
  843. $ra = ($ra >= 0.01) ? $ra : "0.01";
  844. /**/
  845. $desc = sprintf(_x("COUPON %s off. ( Now: %s )", "s2member-front", "s2member"), $cs.number_format($coupon["flat-rate"], 2, ".", ""), $cs.$ra.$tx);
  846. $response = sprintf(_x('<div>Coupon: <strong>%s off</strong>. ( Now: <strong>%s</strong> )</div>', "s2member-front", "s2member"), $cs.number_format($coupon["flat-rate"], 2, ".", ""), $cs.$ra.$tx);
  847. }
  848. else if($coupon["directive"] === "ta-only" && $attr["tp"] && !$attr["sp"])
  849. {
  850. $coupon_applies = /* Applying. */ true;
  851. /**/
  852. $ta = number_format($attr["ta"] - $coupon["flat-rate"], 2, ".", "");
  853. $ta = ($ta >= 0.00) ? $ta : "0.00";
  854. /**/
  855. $ra = number_format($attr["ra"], 2, ".", "");
  856. $ra = ($ra >= 0.01) ? $ra : "0.01";
  857. /**/
  858. $desc = sprintf(_x("COUPON %s off. ( Now: %s, then %s )", "s2member-front", "s2member"), $cs.number_format($coupon["flat-rate"], 2, ".", ""), $cs.c_ws_plugin__s2member_utils_time::amount_period_term($ta, $attr["tp"]." ".$attr["tt"]).$tx, $cs.c_ws_plugin__s2member_utils_time::amount_period_term($ra, $attr["rp"]." ".$attr["rt"], $attr["rr"]));
  859. $response = sprintf(_x('<div>Coupon: <strong>%s off</strong>. ( Now: <strong>%s, then %s</strong> )</div>', "s2member-front", "s2member"), $cs.number_format($coupon["flat-rate"], 2, ".", ""), $cs.c_ws_plugin__s2member_utils_time::amount_period_term($ta, $attr["tp"]." ".$attr["tt"]).$tx, $cs.c_ws_plugin__s2member_utils_time::amount_period_term($ra, $attr["rp"]." ".$attr["rt"], $attr["rr"]));
  860. }
  861. else if($coupon["directive"] === "ra-only" && $attr["tp"] && !$attr["sp"])
  862. {
  863. $coupon_applies = /* Applying. */ true;
  864. /**/
  865. $ta = number_format($attr["ta"], 2, ".", "");
  866. $ta = ($ta >= 0.00) ? $ta : "0.00";
  867. /**/
  868. $ra = number_format($attr["ra"] - $coupon["flat-rate"], 2, ".", "");
  869. $ra = ($ra >= 0.01) ? $ra : "0.01";
  870. /**/
  871. $desc = sprintf(_x("COUPON %s off. ( Now: %s, then %s )", "s2member-front", "s2member"), $cs.number_format($coupon["flat-rate"], 2, ".", ""), $cs.c_ws_plugin__s2member_utils_time::amount_period_term($ta, $attr["tp"]." ".$attr["tt"]).$tx, $cs.c_ws_plugin__s2member_utils_time::amount_period_term($ra, $attr["rp"]." ".$attr["rt"], $attr["rr"]));
  872. $response = sprintf(_x('<div>Coupon: <strong>%s off</strong>. ( Now: <strong>%s, then %s</strong> )</div>', "s2member-front", "s2member"), $cs.number_format($coupon["flat-rate"], 2, ".", ""), $cs.c_ws_plugin__s2member_utils_time::amount_period_term($ta, $attr["tp"]." ".$attr["tt"]).$tx, $cs.c_ws_plugin__s2member_utils_time::amount_period_term($ra, $attr["rp"]." ".$attr["rt"], $attr["rr"]));
  873. }
  874. else if($coupon["directive"] === "all" && $attr["tp"] && !$attr["sp"])
  875. {
  876. $coupon_applies = /* Applying. */ true;
  877. /**/
  878. $ta = number_format($attr["ta"] - $coupon["flat-rate"], 2, ".", "");
  879. $ta = ($ta >= 0.00) ? $ta : "0.00";
  880. /**/
  881. $ra = number_format($attr["ra"] - $coupon["flat-rate"], 2, ".", "");
  882. $ra = ($ra >= 0.01) ? $ra : "0.01";
  883. /**/
  884. $desc = sprintf(_x("COUPON %s off. ( Now: %s, then %s )", "s2member-front", "s2member"), $cs.number_format($coupon["flat-rate"], 2, ".", ""), $cs.c_ws_plugin__s2member_utils_time::amount_period_term($ta, $attr["tp"]." ".$attr["tt"]).$tx, $cs.c_ws_plugin__s2member_utils_time::amount_period_term($ra, $attr["rp"]." ".$attr["rt"], $attr["rr"]));
  885. $response = sprintf(_x('<div>Coupon: <strong>%s off</strong>. ( Now: <strong>%s, then %s</strong> )</div>', "s2member-front", "s2member"), $cs.number_format($coupon["flat-rate"], 2, ".", ""), $cs.c_ws_plugin__s2member_utils_time::amount_period_term($ta, $attr["tp"]." ".$attr["tt"]).$tx, $cs.c_ws_plugin__s2member_utils_time::amount_period_term($ra, $attr["rp"]." ".$attr["rt"], $attr["rr"]));
  886. }
  887. else if($coupon["directive"] === "ra-only" && !$attr["tp"] && !$attr["sp"])
  888. {
  889. $coupon_applies = /* Applying. */ true;
  890. /**/
  891. $ta = number_format($attr["ta"], 2, ".", "");
  892. $ta = ($ta >= 0.00) ? $ta : "0.00";
  893. /**/
  894. $ra = number_format($attr["ra"] - $coupon["flat-rate"], 2, ".", "");
  895. $ra = ($ra >= 0.01) ? $ra : "0.01";
  896. /**/
  897. $desc = sprintf(_x("COUPON %s off. ( Now: %s )", "s2member-front", "s2member"), $cs.number_format($coupon["flat-rate"], 2, ".", ""), $cs.c_ws_plugin__s2member_utils_time::amount_period_term($ra, $attr["rp"]." ".$attr["rt"], $attr["rr"]).$tx);
  898. $response = sprintf(_x('<div>Coupon: <strong>%s off</strong>. ( Now: <strong>%s</strong> )</div>', "s2member-front", "s2member"), $cs.number_format($coupon["flat-rate"], 2, ".", ""), $cs.c_ws_plugin__s2member_utils_time::amount_period_term($ra, $attr["rp"]." ".$attr["rt"], $attr["rr"]).$tx);
  899. }
  900. else if($coupon["directive"] === "all" && !$attr["tp"] && !$attr["sp"])
  901. {
  902. $coupon_applies = /* Applying. */ true;
  903. /**/
  904. $ta = number_format($attr["ta"] - $coupon["flat-rate"], 2, ".", "");
  905. $ta = ($ta >= 0.00) ? $ta : "0.00";
  906. /**/
  907. $ra = number_format($attr["ra"] - $coupon["flat-rate"], 2, ".", "");
  908. $ra = ($ra >= 0.01) ? $ra : "0.01";
  909. /**/
  910. $desc = sprintf(_x("COUPON %s off. ( Now: %s )", "s2member-front", "s2member"), $cs.number_format($coupon["flat-rate"], 2, ".", ""), $cs.c_ws_plugin__s2member_utils_time::amount_period_term($ra, $attr["rp"]." ".$attr["rt"], $attr["rr"]).$tx);
  911. $response = sprintf(_x('<div>Coupon: <strong>%s off</strong>. ( Now: <strong>%s</strong> )</div>', "s2member-front", "s2member"), $cs.number_format($coupon["flat-rate"], 2, ".", ""), $cs.c_ws_plugin__s2member_utils_time::amount_period_term($ra, $attr["rp"]." ".$attr["rt"], $attr["rr"]).$tx);
  912. }
  913. /**/
  914. else /* Otherwise, we need a default response to display. */
  915. $response = _x('<div>Sorry, your Coupon is not applicable.</div>', "s2member-front", "s2member");
  916. }
  917. /**/
  918. else if($coupon["percentage"]) /* Else if it's a percentage. */
  919. {
  920. if(($coupon["directive"] === "ra-only" || $coupon["directive"] === "all") && $attr["sp"])
  921. {
  922. $coupon_applies = /* Applying. */ true;
  923. /**/
  924. $p = ($attr["ta"] / 100) * $coupon["percentage"];
  925. $ta = number_format($attr["ta"], 2, ".", "");
  926. $ta = ($ta >= 0.00) ? $ta : "0.00";
  927. /**/
  928. $p = ($attr["ra"] / 100) * $coupon["percentage"];
  929. $ra = number_format($attr["ra"] - $p, 2, ".", "");
  930. $ra = ($ra >= 0.01) ? $ra : "0.01";
  931. /**/
  932. $desc = sprintf(_x("COUPON %s off. ( Now: %s )", "s2member-front", "s2member"), number_format($coupon["percentage"], 0).$ps, $cs.$ra.$tx);
  933. $response = sprintf(_x('<div>Coupon: <strong>%s off</strong>. ( Now: <strong>%s</strong> )</div>', "s2member-front", "s2member"), number_format($coupon["percentage"], 0).$ps, $cs.$ra.$tx);
  934. }
  935. else if($coupon["directive"] === "ta-only" && $attr["tp"] && !$attr["sp"])
  936. {
  937. $coupon_applies = /* Applying. */ true;
  938. /**/
  939. $p = ($attr["ta"] / 100) * $coupon["percentage"];
  940. $ta = number_format($attr["ta"] - $p, 2, ".", "");
  941. $ta = ($ta >= 0.00) ? $ta : "0.00";
  942. /**/
  943. $p = ($attr["ra"] / 100) * $coupon["percentage"];
  944. $ra = number_format($attr["ra"], 2, ".", "");
  945. $ra = ($ra >= 0.01) ? $ra : "0.01";
  946. /**/
  947. $desc = sprintf(_x("COUPON %s off. ( Now: %s, then %s )", "s2member-front", "s2member"), number_format($coupon["percentage"], 0).$ps, $cs.c_ws_plugin__s2member_utils_time::amount_period_term($ta, $attr["tp"]." ".$attr["tt"]).$tx, $cs.c_ws_plugin__s2member_utils_time::amount_period_term($ra, $attr["rp"]." ".$attr["rt"], $attr["rr"]));
  948. $response = sprintf(_x('<div>Coupon: <strong>%s off</strong>. ( Now: <strong>%s, then %s</strong> )</div>', "s2member-front", "s2member"), number_format($coupon["percentage"], 0).$ps, $cs.c_ws_plugin__s2member_utils_time::amount_period_term($ta, $attr["tp"]." ".$attr["tt"]).$tx, $cs.c_ws_plugin__s2member_utils_time::amount_period_term($ra, $attr["rp"]." ".$attr["rt"], $attr["rr"]));
  949. }
  950. else if($coupon["directive"] === "ra-only" && $attr["tp"] && !$attr["sp"])
  951. {
  952. $coupon_applies = /* Applying. */ true;
  953. /**/
  954. $p = ($attr["ta"] / 100) * $coupon["percentage"];
  955. $ta = number_format($attr["ta"], 2, ".", "");
  956. $ta = ($ta >= 0.00) ? $ta : "0.00";
  957. /**/
  958. $p = ($attr["ra"] / 100) * $coupon["percentage"];
  959. $ra = number_format($attr["ra"] - $p, 2, ".", "");
  960. $ra = ($ra >= 0.01) ? $ra : "0.01";
  961. /**/
  962. $desc = sprintf(_x("COUPON %s off. ( Now: %s, then %s )", "s2member-front", "s2member"), number_format($coupon["percentage"], 0).$ps, $cs.c_ws_plugin__s2member_utils_time::amount_period_term($ta, $attr["tp"]." ".$attr["tt"]).$tx, $cs.c_ws_plugin__s2member_utils_time::amount_period_term($ra, $attr["rp"]." ".$attr["rt"], $attr["rr"]));
  963. $response = sprintf(_x('<div>Coupon: <strong>%s off</strong>. ( Now: <strong>%s, then %s</strong> )</div>', "s2member-front", "s2member"), number_format($coupon["percentage"], 0).$ps, $cs.c_ws_plugin__s2member_utils_time::amount_period_term($ta, $attr["tp"]." ".$attr["tt"]).$tx, $cs.c_ws_plugin__s2member_utils_time::amount_period_term($ra, $attr["rp"]." ".$attr["rt"], $attr["rr"]));
  964. }
  965. else if($coupon["directive"] === "all" && $attr["tp"] && !$attr["sp"])
  966. {
  967. $coupon_applies = /* Applying. */ true;
  968. /**/
  969. $p = ($attr["ta"] / 100) * $coupon["percentage"];
  970. $ta = number_format($attr["ta"] - $p, 2, ".", "");
  971. $ta = ($ta >= 0.00) ? $ta : "0.00";
  972. /**/
  973. $p = ($attr["ra"] / 100) * $coupon["percentage"];
  974. $ra = number_format($attr["ra"] - $p, 2, ".", "");
  975. $ra = ($ra >= 0.01) ? $ra : "0.01";
  976. /**/
  977. $desc = sprintf(_x("COUPON %s off. ( Now: %s, then %s )", "s2member-front", "s2member"), number_format($coupon["percentage"], 0).$ps, $cs.c_ws_plugin__s2member_utils_time::amount_period_term($ta, $attr["tp"]." ".$attr["tt"]).$tx, $cs.c_ws_plugin__s2member_utils_time::amount_period_term($ra, $attr["rp"]." ".$attr["rt"], $attr["rr"]));
  978. $response = sprintf(_x('<div>Coupon: <strong>%s off</strong>. ( Now: <strong>%s, then %s</strong> )</div>', "s2member-front", "s2member"), number_format($coupon["percentage"], 0).$ps, $cs.c_ws_plugin__s2member_utils_time::amount_period_term($ta, $attr["tp"]." ".$attr["tt"]).$tx, $cs.c_ws_plugin__s2member_utils_time::amount_period_term($ra, $attr["rp"]." ".$attr["rt"], $attr["rr"]));
  979. }
  980. else if($coupon["directive"] === "ra-only" && !$attr["tp"] && !$attr["sp"])
  981. {
  982. $coupon_applies = /* Applying. */ true;
  983. /**/
  984. $p = ($attr["ta"] / 100) * $coupon["percentage"];
  985. $ta = number_format($attr["ta"], 2, ".", "");
  986. $ta = ($ta >= 0.00) ? $ta : "0.00";
  987. /**/
  988. $p = ($attr["ra"] / 100) * $coupon["percentage"];
  989. $ra = number_format($attr["ra"] - $p, 2, ".", "");
  990. $ra = ($ra >= 0.01) ? $ra : "0.01";
  991. /**/
  992. $desc = sprintf(_x("COUPON %s off. ( Now: %s )", "s2member-front", "s2member"), number_format($coupon["percentage"], 0).$ps, $cs.c_ws_plugin__s2member_utils_time::amount_period_term($ra, $attr["rp"]." ".$attr["rt"], $attr["rr"]).$tx);
  993. $response = sprintf(_x('<div>Coupon: <strong>%s off</strong>. ( Now: <strong>%s</strong> )</div>', "s2member-front", "s2member"), number_format($coupon["percentage"], 0).$ps, $cs.c_ws_plugin__s2member_utils_time::amount_period_term($ra, $attr["rp"]." ".$attr["rt"], $attr["rr"]).$tx);
  994. }
  995. else if($coupon["directive"] === "all" && !$attr["tp"] && !$attr["sp"])
  996. {
  997. $coupon_applies = /* Applying. */ true;
  998. /**/
  999. $p = ($attr["ta"] / 100) * $coupon["percentage"];
  1000. $ta = number_format($attr["ta"] - $p, 2, ".", "");
  1001. $ta = ($ta >= 0.00) ? $ta : "0.00";
  1002. /**/
  1003. $p = ($attr["ra"] / 100) * $coupon["percentage"];
  1004. $ra = number_format($attr["ra"] - $p, 2, ".", "");
  1005. $ra = ($ra >= 0.01) ? $ra : "0.01";
  1006. /**/
  1007. $desc = sprintf(_x("COUPON %s off. ( Now: %s )", "s2member-front", "s2member"), number_format($coupon["percentage"], 0).$ps, $cs.c_ws_plugin__s2member_utils_time::amount_period_term($ra, $attr["rp"]." ".$attr["rt"], $attr["rr"]).$tx);
  1008. $response = sprintf(_x('<div>Coupon: <strong>%s off</strong>. ( Now: <strong>%s</strong> )</div>', "s2member-front", "s2member"), number_format($coupon["percentage"], 0).$ps, $cs.c_ws_plugin__s2member_utils_time::amount_period_term($ra, $attr["rp"]." ".$attr["rt"], $attr["rr"]).$tx);
  1009. }
  1010. /**/
  1011. else /* Otherwise, we need a default response to display. */
  1012. $response = _x('<div>Sorry, your Coupon is not applicable.</div>', "s2member-front", "s2member");
  1013. }
  1014. /**/
  1015. else /* Else there was no discount applied at all. */
  1016. $response = sprintf(_x('<div>Coupon: <strong>%s0.00 off</strong>.</div>', "s2member-front", "s2member"), $cs);
  1017. }
  1018. /**/
  1019. else /* Otherwise, we need a response that indicates not applicable for this purchase. */
  1020. $response = _x('<div>Sorry, your Coupon cannot be applied to this particular purchase.</div>', "s2member-front", "s2member");
  1021. }
  1022. /**/
  1023. else if($coupon_code === $coupon["code"] && $coupon["expired"])
  1024. $response = sprintf(_x('<div>Sorry, your Coupon <strong>expired</strong>: <em>%s</em>.</div>', "s2member-front", "s2member"), $coupon["expired"]);
  1025. }
  1026. }
  1027. /**/
  1028. if(isset($coupon_applies, $desc) && $coupon_applies /* Need to modify the description dynamically? */)
  1029. /* translators: `%1$s` is new price/description, after coupon applied. `%2$s` is original description. */
  1030. $attr["desc"] = sprintf(_x('%1$s ~ ORIGINALLY: %2$s', "s2member-front", "s2member"), $desc, $attr["desc"]);
  1031. /**/
  1032. $attr["ta"] = (isset($coupon_applies, $ta) && $coupon_applies) ? /* Do we have a new Trial Amount? */ $ta : $attr["ta"];
  1033. $attr["ra"] = (isset($coupon_applies, $ra) && $coupon_applies) ? /* A new Regular Amount? */ $ra : $attr["ra"];
  1034. /**/
  1035. if(is_array($process) && /* Processing affiliates? */ (in_array("affiliates-silent-post", $process) || in_array("affiliates-1px-response", $process)))
  1036. if(isset($coupon_applies) && $coupon_applies && /* Now, is this an Affiliate Coupon Code? Contains an affiliate ID? */ !empty($affiliate_id))
  1037. if(empty($_COOKIE["idev"]) /* Special consideration here. iDevAffiliate® must NOT have already tracked this customer. */)
  1038. if(($_urls = $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["pro_affiliate_coupon_code_tracking_urls"]))
  1039. /**/
  1040. foreach(preg_split("/[\r\n\t]+/", $_urls) as $_url /* Notify each of the URLs. */)
  1041. /**/
  1042. if(($_url = preg_replace("/%%full_coupon_code%%/i", c_ws_plugin__s2member_utils_strings::esc_ds(urlencode($full_coupon_code)), $_url)))
  1043. if(($_url = preg_replace("/%%coupon_code%%/i", c_ws_plugin__s2member_utils_strings::esc_ds(urlencode($coupon_code)), $_url)))
  1044. if(($_url = preg_replace("/%%affiliate_id%%/i", c_ws_plugin__s2member_utils_strings::esc_ds(urlencode($affiliate_id)), $_url)))
  1045. if(($_url = preg_replace("/%%user_ip%%/i", c_ws_plugin__s2member_utils_strings::esc_ds(urlencode($_SERVER["REMOTE_ADDR"])), $_url)))
  1046. {
  1047. if(($_url = trim(preg_replace("/%%(.+?)%%/i", "", $_url))) /* Cleanup any remaining Replacement Codes. */)
  1048. /**/
  1049. if(!($_r = 0) && ($_url = preg_replace("/^silent-php\|/i", "", $_url, 1, $_r)) && $_r && in_array /* Processing? */("affiliates-silent-post", $process))
  1050. c_ws_plugin__s2member_utils_urls::remote /* Post silently via PHP. Relies on IP tracking. */($_url, false, array("blocking" => false));
  1051. /**/
  1052. else if(!($_r = 0) && ($_url = preg_replace("/^img-1px\|/i", "", $_url, 1, $_r)) && $_r && in_array("affiliates-1px-response", $process))
  1053. if( /* Now, we MUST also have a ``$response``, and MUST be returning ``$response``. */!empty($response) && $return === "response")
  1054. $response .= "\n".'<img src="'.esc_attr($_url).'" style="width:0; height:0; border:0;" alt="" />';
  1055. }
  1056. unset /* Just a little housekeeping here. Unset these variables. */($_urls, $_url, $_r);
  1057. /**/
  1058. if(empty($response)) /* Is ``$response`` NOT set by now? If it's not, we need a default ``$response``. */
  1059. $response = _x('<div>Sorry, your Coupon is N/A, invalid or expired.</div>', "s2member-front", "s2member");
  1060. }
  1061. /**/
  1062. else /* Otherwise, we need a default response to display. */
  1063. $response = _x('<div>Sorry, your Coupon is N/A, invalid or expired.</div>', "s2member-front", "s2member");
  1064. /**/
  1065. return ( /* Returning ``$response``? */$return === "response") ? $response : $attr;
  1066. }
  1067. }
  1068. }
  1069. ?>

s2member-pro\includes\classes\gateways\authnet\authnet-utilities.inc.php

Source Code Documentation