Source Code Documentation


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


  1. <?php
  2. /**
  3. * PayPal® utilities.
  4. *
  5. * Copyright: © 2009-2011
  6. * {@link http://www.websharks-inc.com/ WebSharks, Inc.}
  7. * (coded in the USA)
  8. *
  9. * This WordPress® plugin (s2Member Pro) is comprised of two parts:
  10. *
  11. * o (1) Its PHP code is licensed under the GPL license, as is WordPress®.
  12. * You should have received a copy of the GNU General Public License,
  13. * along with this software. In the main directory, see: /licensing/
  14. * If not, see: {@link http://www.gnu.org/licenses/}.
  15. *
  16. * o (2) All other parts of (s2Member Pro); including, but not limited to:
  17. * the CSS code, some JavaScript code, images, and design;
  18. * are licensed according to the license purchased.
  19. * See: {@link http://www.s2member.com/prices/}
  20. *
  21. * Unless you have our prior written consent, you must NOT directly or indirectly license,
  22. * sub-license, sell, resell, or provide for free; part (2) of the s2Member Pro Module;
  23. * or make an offer to do any of these things. All of these things are strictly
  24. * prohibited with part (2) of the s2Member Pro Module.
  25. *
  26. * Your purchase of s2Member Pro includes free lifetime upgrades via s2Member.com
  27. * (i.e. new features, bug fixes, updates, improvements); along with full access
  28. * to our video tutorial library: {@link http://www.s2member.com/videos/}
  29. *
  30. * @package s2Member\PayPal
  31. * @since 1.5
  32. */
  33. if(realpath(__FILE__) === realpath($_SERVER["SCRIPT_FILENAME"]))
  34. exit("Do not access this file directly.");
  35.  
  36. if(!class_exists("c_ws_plugin__s2member_pro_paypal_utilities"))
  37. {
  38. /**
  39. * PayPal® utilities.
  40. *
  41. * @package s2Member\PayPal
  42. * @since 1.5
  43. */
  44. class c_ws_plugin__s2member_pro_paypal_utilities
  45. {
  46. /**
  47. * Calculates start date for a Recurring Payment Profile.
  48. *
  49. * @package s2Member\PayPal
  50. * @since 1.5
  51. *
  52. * @param str $period1 Optional. A "Period Term" combination. Defaults to `0 D`.
  53. * @param str $period3 Optional. A "Period Term" combination. Defaults to `0 D`.
  54. * @return int The start time, a Unix timestamp.
  55. */
  56. public static function paypal_start_time($period1 = FALSE, $period3 = FALSE)
  57. {
  58. if(!($p1_time = 0) && ($period1 = trim(strtoupper($period1))))
  59. {
  60. list($num, $span) = preg_split("/ /", $period1, 2);
  61.  
  62. $days = 0; // Days start at 0.
  63.  
  64. if(is_numeric($num) && !is_numeric($span))
  65. {
  66. $days = ($span === "D") ? 1 : $days;
  67. $days = ($span === "W") ? 7 : $days;
  68. $days = ($span === "M") ? 30 : $days;
  69. $days = ($span === "Y") ? 365 : $days;
  70. }
  71.  
  72. $p1_days = (int)$num * (int)$days;
  73. $p1_time = $p1_days * 86400;
  74. }
  75.  
  76. if(!($p3_time = 0) && ($period3 = trim(strtoupper($period3))))
  77. {
  78. list($num, $span) = preg_split("/ /", $period3, 2);
  79.  
  80. $days = 0; // Days start at 0.
  81.  
  82. if(is_numeric($num) && !is_numeric($span))
  83. {
  84. $days = ($span === "D") ? 1 : $days;
  85. $days = ($span === "W") ? 7 : $days;
  86. $days = ($span === "M") ? 30 : $days;
  87. $days = ($span === "Y") ? 365 : $days;
  88. }
  89.  
  90. $p3_days = (int)$num * (int)$days;
  91. $p3_time = $p3_days * 86400;
  92. }
  93.  
  94. $start_time = strtotime("now") + $p1_time + $p3_time;
  95.  
  96. $start_time = ($start_time <= 0) ? strtotime("now") : $start_time;
  97.  
  98. $start_time = $start_time + 43200; // + 12 hours.
  99.  
  100. return $start_time;
  101. }
  102. /**
  103. * Determines whether or not Tax may apply.
  104. *
  105. * @package s2Member\PayPal
  106. * @since 1.5
  107. *
  108. * @return bool True if Tax may apply, else false.
  109. */
  110. public static function paypal_tax_may_apply()
  111. {
  112. if((float)$GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["pro_default_tax"] > 0)
  113. return true;
  114.  
  115. else if($GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["pro_tax_rates"])
  116. return true;
  117.  
  118. return false;
  119. }
  120. /**
  121. * Gets a Payflow® recurring profile.
  122. *
  123. * @package s2Member\PayPal
  124. * @since 110531
  125. *
  126. * @param string $subscr_id A paid subscription ID (aka: Recurring Profile ID).
  127. * @return array|false Array of profile details, else false.
  128. */
  129. public static function payflow_get_profile($subscr_id = FALSE)
  130. {
  131. $payflow["TRXTYPE"] = "R";
  132. $payflow["ACTION"] = "I";
  133. $payflow["TENDER"] = "C";
  134. $payflow["ORIGPROFILEID"] = $subscr_id;
  135.  
  136. if(($profile = c_ws_plugin__s2member_paypal_utilities::paypal_payflow_api_response($payflow)) && empty($profile["__error"]))
  137. return $profile;
  138.  
  139. $payflow["TENDER"] = "P";
  140. if(($profile = c_ws_plugin__s2member_paypal_utilities::paypal_payflow_api_response($payflow)) && empty($profile["__error"]))
  141. return $profile;
  142.  
  143. return false;
  144. }
  145. /**
  146. * Cancels a Payflow® recurring profile.
  147. *
  148. * @package s2Member\PayPal
  149. * @since 110531
  150. *
  151. * @param string $subscr_id A paid subscription ID (aka: Recurring Profile ID).
  152. * @param string $baid A Billing Agreement ID (aka: BAID).
  153. * @return boolean True if the profile was cancelled, else false.
  154. */
  155. public static function payflow_cancel_profile($subscr_id = FALSE, $baid = FALSE)
  156. {
  157. $payflow["TRXTYPE"] = "R";
  158. $payflow["ACTION"] = "C";
  159. $payflow["TENDER"] = "C";
  160. $payflow["ORIGPROFILEID"] = $subscr_id;
  161.  
  162. if(($cancellation = c_ws_plugin__s2member_paypal_utilities::paypal_payflow_api_response($payflow)) && empty($cancellation["__error"]))
  163. if(!$baid || c_ws_plugin__s2member_paypal_utilities::payflow_cancel_billing_agreement($baid))
  164. return true;
  165.  
  166. $payflow["TENDER"] = "P";
  167. if(($cancellation = c_ws_plugin__s2member_paypal_utilities::paypal_payflow_api_response($payflow)) && empty($cancellation["__error"]))
  168. if(!$baid || c_ws_plugin__s2member_paypal_utilities::payflow_cancel_billing_agreement($baid))
  169. return true;
  170.  
  171. return false;
  172. }
  173. /**
  174. * Cancels a Payflow® Billing Agreement.
  175. *
  176. * @package s2Member\PayPal
  177. * @since 130510
  178. *
  179. * @param string $baid A Billing Agreement ID (aka: BAID).
  180. * @return boolean True if the agreement was cancelled, else false.
  181. */
  182. public static function payflow_cancel_billing_agreement($baid = FALSE)
  183. {
  184. $payflow["ACTION"] = "U";
  185. $payflow["TENDER"] = "P";
  186. $payflow["BAID"] = $baid;
  187. $payflow["BA_STATUS"] = "cancel";
  188.  
  189. if(($cancellation = c_ws_plugin__s2member_paypal_utilities::paypal_payflow_api_response($payflow)) && empty($cancellation["__error"]))
  190. return true;
  191.  
  192. return false;
  193. }
  194. /**
  195. * Handles currency conversions for Maestro/Solo cards.
  196. *
  197. * PayPal® requires Maestro/Solo to be charged in GBP. So if a site owner is using
  198. * another currency *(i.e. something NOT in GBP)*, we have to convert all of the charge amounts dynamically.
  199. *
  200. * Coupon Codes should always be applied before this conversion takes place.
  201. * That way a site owner's configuration remains adequate.
  202. *
  203. * Tax rates should be applied after this conversion takes place.
  204. *
  205. * @package s2Member\PayPal
  206. * @since 110531
  207. *
  208. * @param array $attr An array of PayPal® Pro Form Attributes.
  209. * @param str $card_type The Card Type *(i.e. Billing Method)* selected.
  210. * @return array The same array of Pro Form Attributes, with possible currency conversions.
  211. */
  212. public static function paypal_maestro_solo_2gbp($attr = FALSE, $card_type = FALSE)
  213. {
  214. if(is_array($attr) && is_string($card_type) && in_array($card_type, array("Maestro", "Solo")))
  215. if(!empty($attr["cc"]) && strcasecmp($attr["cc"], "GBP") !== 0 && is_numeric($attr["ta"]) && is_numeric($attr["ra"]))
  216. if(($attr["ta"] <= 0 && is_numeric($c_ta = "0")) || is_numeric($c_ta = c_ws_plugin__s2member_utils_cur::convert($attr["ta"], $attr["cc"], "GBP")))
  217. if(($attr["ra"] <= 0 && is_numeric($c_ra = "0")) || is_numeric($c_ra = c_ws_plugin__s2member_utils_cur::convert($attr["ra"], $attr["cc"], "GBP")))
  218. $attr = array_merge($attr, array("cc" => "GBP", "ta" => $c_ta, "ra" => $c_ra));
  219.  
  220. return $attr; // Return array of Attributes.
  221. }
  222. /**
  223. * Handles the return of Tax for Pro Forms, via AJAX; through a JSON object.
  224. *
  225. * @package s2Member\PayPal
  226. * @since 1.5
  227. *
  228. * @return null Or exits script execution after returning data for AJAX caller.
  229. *
  230. * @todo Check the use of ``strip_tags()`` in this routine?
  231. * @todo Continue optimizing this routine with ``empty()`` and ``isset()``.
  232. * @todo Candidate for the use of ``ifsetor()``?
  233. */
  234. public static function paypal_ajax_tax()
  235. {
  236. if(!empty($_POST["ws_plugin__s2member_pro_paypal_ajax_tax"]) && ($nonce = $_POST["ws_plugin__s2member_pro_paypal_ajax_tax"]) && (wp_verify_nonce($nonce, "ws-plugin--s2member-pro-paypal-ajax-tax") || c_ws_plugin__s2member_utils_encryption::decrypt($nonce) === "ws-plugin--s2member-pro-paypal-ajax-tax"))
  237. /* 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.
  238. So instead, s2member-pro-min.js should use c_ws_plugin__s2member_utils_encryption::encrypt() as an alternate form of nonce. */
  239. {
  240. status_header(200); // Send a 200 OK status header.
  241. header("Content-Type: text/plain; charset=UTF-8"); // Content-Type text/plain with UTF-8.
  242. while (@ob_end_clean ()); // Clean any existing output buffers.
  243.  
  244. if(!empty($_POST["ws_plugin__s2member_pro_paypal_ajax_tax_vars"]) && is_array($_p_tax_vars = c_ws_plugin__s2member_utils_strings::trim_deep(stripslashes_deep($_POST["ws_plugin__s2member_pro_paypal_ajax_tax_vars"]))))
  245. {
  246. if(is_array($attr = (!empty($_p_tax_vars["attr"])) ? unserialize(c_ws_plugin__s2member_utils_encryption::decrypt($_p_tax_vars["attr"])) : false))
  247. {
  248. $attr = (!empty($attr["coupon"])) ? c_ws_plugin__s2member_pro_paypal_utilities::paypal_apply_coupon($attr, $attr["coupon"]) : $attr;
  249.  
  250. $trial = ($attr["rr"] !== "BN" && $attr["tp"]) ? true : false; // Is there a trial?
  251. $sub_total_today = ($trial) ? $attr["ta"] : $attr["ra"]; // What is the sub-total today?
  252.  
  253. $state = strip_tags($_p_tax_vars["state"]);
  254. $country = strip_tags($_p_tax_vars["country"]);
  255. $zip = strip_tags($_p_tax_vars["zip"]);
  256. $currency = $attr["cc"]; // Currency.
  257. $desc = $attr["desc"]; // Description.
  258.  
  259. /* Trial is `null` in this function call. We only need to return what it costs today.
  260. However, we do tag on a "trial" element in the array so the ajax routine will know about this. */
  261. $a = c_ws_plugin__s2member_pro_paypal_utilities::paypal_cost(null, $sub_total_today, $state, $country, $zip, $currency, $desc);
  262. 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"]));
  263. }
  264. }
  265.  
  266. exit(); // Clean exit.
  267. }
  268. }
  269. /**
  270. * Handles all cost calculations for PayPal®.
  271. *
  272. * Returns an associative array with a possible Percentage Rate, along with the calculated Tax Amount.
  273. * Tax calculations are based on State/Province, Country, and/or Zip Code.
  274. * Updated to support multiple data fields in it's return value.
  275. *
  276. * @package s2Member\PayPal
  277. * @since 1.5
  278. *
  279. * @param int|str $trial_sub_total Optional. A numeric Amount/cost of a possible Initial/Trial being offered.
  280. * @param int|str $sub_total Optional. A numeric Amount/cost of the purchase and/or Regular Period.
  281. * @param str $state Optional. The State/Province where the Customer is billed.
  282. * @param str $country Optional. The Country where the Customer is billed.
  283. * @param int|str $zip Optional. The Postal/Zip Code where the Customer is billed.
  284. * @param str $currency Optional. Expects a 3 character Currency Code.
  285. * @param str $desc Optional. Description of the sale.
  286. * @return array Array of calculations.
  287. *
  288. * @todo Add support for `Zip + 4` syntax?
  289. */
  290. public static function paypal_cost($trial_sub_total = FALSE, $sub_total = FALSE, $state = FALSE, $country = FALSE, $zip = FALSE, $currency = FALSE, $desc = FALSE)
  291. {
  292. $state = strtoupper(c_ws_plugin__s2member_pro_utilities::full_state($state, ($country = strtoupper($country))));
  293. $rates = strtoupper($GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["pro_tax_rates"]);
  294. $default = $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["pro_default_tax"];
  295. $ps = _x("%", "s2member-front percentage-symbol", "s2member");
  296.  
  297. foreach(array("trial_sub_total" => $trial_sub_total, "sub_total" => $sub_total) as $this_key => $this_sub_total)
  298. {
  299. unset($_default, $this_tax, $this_tax_per, $this_total, $configured_rates, $configured_rate, $location, $rate, $m);
  300.  
  301. if(is_numeric($this_sub_total) && $this_sub_total > 0) // Must have a valid Sub-Total.
  302. {
  303. if(preg_match("/%$/", $default)) // Percentage-based.
  304. {
  305. if(($_default = (float)$default) > 0)
  306. {
  307. $this_tax = round(($this_sub_total / 100) * $_default, 2);
  308. $this_tax_per = $_default.$ps;
  309. }
  310. else // Else the Tax is 0.00.
  311. {
  312. $this_tax = 0.00;
  313. $this_tax_per = $_default.$ps;
  314. }
  315. }
  316. else if(($_default = (float)$default) > 0)
  317. {
  318. $this_tax = round($_default, 2);
  319. $this_tax_per = ""; // Flat.
  320. }
  321. else // Else the Tax is 0.00.
  322. {
  323. $this_tax = 0.00; // No Tax.
  324. $this_tax_per = ""; // Flat rate.
  325. }
  326.  
  327. if(strlen($country) === 2) // Must have a valid country.
  328. {
  329. foreach(preg_split("/[\r\n\t]+/", $rates) as $rate)
  330. {
  331. if($rate = trim($rate)) // Do NOT process empty lines.
  332. {
  333. list($location, $rate) = preg_split("/\=/", $rate, 2);
  334. $location = trim($location);
  335. $rate = trim($rate);
  336.  
  337. if($location === $country)
  338. $configured_rates[1] = $rate;
  339.  
  340. else if($state && $location === $state."/".$country)
  341. $configured_rates[2] = $rate;
  342.  
  343. 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)
  344. $configured_rates[2] = $rate;
  345.  
  346. else if($zip && preg_match("/^([0-9]+)-([0-9]+)\/(".preg_quote($country, "/").")$/", $location, $m) && $zip >= $m[1] && $zip <= $m[2] && $country === $m[3])
  347. $configured_rates[3] = $rate;
  348.  
  349. else if($zip && $location === $zip."/".$country)
  350. $configured_rates[4] = $rate;
  351. }
  352. }
  353.  
  354. if(is_array($configured_rates) && !empty($configured_rates))
  355. {
  356. krsort($configured_rates);
  357. $configured_rate = array_shift($configured_rates);
  358.  
  359. if(preg_match("/%$/", $configured_rate)) // Percentage.
  360. {
  361. if(($configured_rate = (float)$configured_rate) > 0)
  362. {
  363. $this_tax = round(($this_sub_total / 100) * $configured_rate, 2);
  364. $this_tax_per = $configured_rate.$ps;
  365. }
  366. else // Else the Tax is 0.00.
  367. {
  368. $this_tax = 0.00; // No Tax.
  369. $this_tax_per = $configured_rate.$ps;
  370. }
  371. }
  372. else if(($configured_rate = (float)$configured_rate) > 0)
  373. {
  374. $this_tax = round($configured_rate, 2);
  375. $this_tax_per = ""; // Flat rate.
  376. }
  377. else // Else the Tax is 0.00.
  378. {
  379. $this_tax = 0.00; // No Tax.
  380. $this_tax_per = ""; // Flat rate.
  381. }
  382. }
  383. }
  384.  
  385. $this_total = $this_sub_total + $this_tax;
  386. }
  387. else // Else the Tax is 0.00.
  388. {
  389. $this_tax = 0.00; // No Tax.
  390. $this_tax_per = ""; // Flat rate.
  391. $this_sub_total = 0.00; // 0.00.
  392. $this_total = 0.00; // 0.00.
  393. }
  394.  
  395. if($this_key === "trial_sub_total")
  396. {
  397. $trial_tax = $this_tax;
  398. $trial_tax_per = $this_tax_per;
  399. $trial_sub_total = $this_sub_total;
  400. $trial_total = $this_total;
  401. }
  402. else if($this_key === "sub_total")
  403. {
  404. $tax = $this_tax;
  405. $tax_per = $this_tax_per;
  406. $sub_total = $this_sub_total;
  407. $total = $this_total;
  408. }
  409. }
  410.  
  411. 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);
  412. }
  413. /**
  414. * Checks to see if a Coupon Code was supplied, and if so; what does it provide?
  415. *
  416. * @package s2Member\PayPal
  417. * @since 1.5
  418. *
  419. * @param array $attr An array of Pro Form Attributes.
  420. * @param str $coupon_code Optional. A possible Coupon Code supplied by the Customer.
  421. * @param str $return Optional. Return type. One of `response|attr`. Defaults to `attr`.
  422. * @param array $process Optional. An array of additional processing routines to run here.
  423. * One or more of these values: `affiliates-1px-response|affiliates-silent-post|notifications`.
  424. * @return array|str Original array, with prices and description modified when/if a Coupon Code is accepted.
  425. * Or, if ``$return === "response"``, return a string response, indicating status.
  426. *
  427. * @todo See if it's possible to simplify this routine.
  428. * @todo Add support for tracking Coupon Code usage.
  429. * @todo Add support for a fixed number of uses.
  430. */
  431. public static function paypal_apply_coupon($attr = FALSE, $coupon_code = FALSE, $return = FALSE, $process = FALSE)
  432. {
  433. if(($coupon_code = trim(strtolower($coupon_code))) || ($coupon_code = trim(strtolower($attr["coupon"]))))
  434. if($attr["accept_coupons"] && $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["pro_coupon_codes"])
  435. {
  436. $cs = c_ws_plugin__s2member_utils_cur::symbol($attr["cc"]);
  437. $tx = (c_ws_plugin__s2member_pro_paypal_utilities::paypal_tax_may_apply()) ? _x(" + tax", "s2member-front", "s2member") : "";
  438. $ps = _x("%", "s2member-front percentage-symbol", "s2member");
  439.  
  440. if(strlen($affiliate_suffix_chars = $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["pro_affiliate_coupon_code_suffix_chars"]))
  441. if(preg_match("/^(.+?)".preg_quote($affiliate_suffix_chars, "/")."([0-9]+)$/i", $coupon_code, $m))
  442. ($full_coupon_code = $m[0]).($coupon_code = $m[1]).($affiliate_id = $m[2]);
  443. unset /* Just a little housekeeping here. */($affiliate_suffix_chars, $m);
  444.  
  445. foreach(c_ws_plugin__s2member_utils_strings::trim_deep(preg_split("/[\r\n\t]+/", $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["pro_coupon_codes"])) as $_line)
  446. {
  447. if(($_line = trim($_line, " \r\n\t\0\x0B|")) && is_array($_coupon = preg_split("/\|/", $_line)))
  448. {
  449. $coupon["code"] = (!empty($_coupon[0])) ? trim(strtolower($_coupon[0])) : "";
  450.  
  451. $coupon["percentage"] = (!empty($_coupon[1]) && preg_match("/%/", $_coupon[1])) ? (float)$_coupon[1] : 0;
  452. $coupon["flat-rate"] = (!empty($_coupon[1]) && !preg_match("/%/", $_coupon[1])) ? (float)$_coupon[1] : 0;
  453.  
  454. $coupon["expired"] = (!empty($_coupon[2]) && /* Expired? */ strtotime($_coupon[2]) < time()) ? $_coupon[2] : false;
  455.  
  456. $coupon["directive"] = (!empty($_coupon[3]) && ($_coupon[3] = strtolower($_coupon[3]))) ? preg_replace("/_/", "-", $_coupon[3]) : "all";
  457. $coupon["directive"] = (preg_match("/^(ta-only|ra-only|all)$/", $coupon["directive"])) ? $coupon["directive"] : "all";
  458.  
  459. $coupon["singulars"] = (!empty($_coupon[4]) && ($_coupon[4] = strtolower($_coupon[4])) && $_coupon[4] !== "all") ? $_coupon[4] : "all";
  460. $coupon["singulars"] = ($coupon["singulars"] !== "all") ? preg_split("/[\r\n\t\s;,]+/", trim(preg_replace("/[^0-9,]/", "", $coupon["singulars"]), ",")) : array("all");
  461.  
  462. unset /* Just a little housekeeping here. Unset these temporary variables. */($_line, $_coupon);
  463.  
  464. if($coupon_code === $coupon["code"] && /* And it's NOT yet expired, or lasts forever? */ !$coupon["expired"])
  465. {
  466. if($coupon["singulars"] === array("all")|| in_array($attr["singular"], $coupon["singulars"]))
  467. {
  468. $coupon_accepted = /* Yes, this Coupon Code has been accepted. */ true;
  469.  
  470. if($coupon["flat-rate"]) // If it's a flat-rate Coupon.
  471. {
  472. if(($coupon["directive"] === "ra-only" || $coupon["directive"] === "all") && $attr["sp"])
  473. {
  474. $coupon_applies = /* Applying. */ true;
  475.  
  476. $ta = number_format($attr["ta"], 2, ".", "");
  477. $ta = ($ta >= 0.00) ? $ta : "0.00";
  478.  
  479. $ra = number_format($attr["ra"] - $coupon["flat-rate"], 2, ".", "");
  480. $ra = ($ra >= 0.01) ? $ra : "0.01";
  481.  
  482. $desc = sprintf(_x("COUPON %s off. (Now: %s)", "s2member-front", "s2member"), $cs.number_format($coupon["flat-rate"], 2, ".", ""), $cs.$ra.$tx);
  483. $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);
  484. }
  485. else if($coupon["directive"] === "ta-only" && $attr["tp"] && !$attr["sp"])
  486. {
  487. $coupon_applies = /* Applying. */ true;
  488.  
  489. $ta = number_format($attr["ta"] - $coupon["flat-rate"], 2, ".", "");
  490. $ta = ($ta >= 0.00) ? $ta : "0.00";
  491.  
  492. $ra = number_format($attr["ra"], 2, ".", "");
  493. $ra = ($ra >= 0.01) ? $ra : "0.01";
  494.  
  495. $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"]));
  496. $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"]));
  497. }
  498. else if($coupon["directive"] === "ra-only" && $attr["tp"] && !$attr["sp"])
  499. {
  500. $coupon_applies = /* Applying. */ true;
  501.  
  502. $ta = number_format($attr["ta"], 2, ".", "");
  503. $ta = ($ta >= 0.00) ? $ta : "0.00";
  504.  
  505. $ra = number_format($attr["ra"] - $coupon["flat-rate"], 2, ".", "");
  506. $ra = ($ra >= 0.01) ? $ra : "0.01";
  507.  
  508. $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"]));
  509. $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"]));
  510. }
  511. else if($coupon["directive"] === "all" && $attr["tp"] && !$attr["sp"])
  512. {
  513. $coupon_applies = /* Applying. */ true;
  514.  
  515. $ta = number_format($attr["ta"] - $coupon["flat-rate"], 2, ".", "");
  516. $ta = ($ta >= 0.00) ? $ta : "0.00";
  517.  
  518. $ra = number_format($attr["ra"] - $coupon["flat-rate"], 2, ".", "");
  519. $ra = ($ra >= 0.01) ? $ra : "0.01";
  520.  
  521. $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"]));
  522. $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"]));
  523. }
  524. else if($coupon["directive"] === "ra-only" && !$attr["tp"] && !$attr["sp"])
  525. {
  526. $coupon_applies = /* Applying. */ true;
  527.  
  528. $ta = number_format($attr["ta"], 2, ".", "");
  529. $ta = ($ta >= 0.00) ? $ta : "0.00";
  530.  
  531. $ra = number_format($attr["ra"] - $coupon["flat-rate"], 2, ".", "");
  532. $ra = ($ra >= 0.01) ? $ra : "0.01";
  533.  
  534. $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);
  535. $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);
  536. }
  537. else if($coupon["directive"] === "all" && !$attr["tp"] && !$attr["sp"])
  538. {
  539. $coupon_applies = /* Applying. */ true;
  540.  
  541. $ta = number_format($attr["ta"] - $coupon["flat-rate"], 2, ".", "");
  542. $ta = ($ta >= 0.00) ? $ta : "0.00";
  543.  
  544. $ra = number_format($attr["ra"] - $coupon["flat-rate"], 2, ".", "");
  545. $ra = ($ra >= 0.01) ? $ra : "0.01";
  546.  
  547. $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);
  548. $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);
  549. }
  550.  
  551. else // Otherwise, we need a default response to display.
  552. $response = _x('<div>Sorry, your Coupon is not applicable.</div>', "s2member-front", "s2member");
  553. }
  554.  
  555. else if($coupon["percentage"]) // Else if it's a percentage.
  556. {
  557. if(($coupon["directive"] === "ra-only" || $coupon["directive"] === "all") && $attr["sp"])
  558. {
  559. $coupon_applies = /* Applying. */ true;
  560.  
  561. $p = ($attr["ta"] / 100) * $coupon["percentage"];
  562. $ta = number_format($attr["ta"], 2, ".", "");
  563. $ta = ($ta >= 0.00) ? $ta : "0.00";
  564.  
  565. $p = ($attr["ra"] / 100) * $coupon["percentage"];
  566. $ra = number_format($attr["ra"] - $p, 2, ".", "");
  567. $ra = ($ra >= 0.01) ? $ra : "0.01";
  568.  
  569. $desc = sprintf(_x("COUPON %s off. (Now: %s)", "s2member-front", "s2member"), number_format($coupon["percentage"], 0).$ps, $cs.$ra.$tx);
  570. $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);
  571. }
  572. else if($coupon["directive"] === "ta-only" && $attr["tp"] && !$attr["sp"])
  573. {
  574. $coupon_applies = /* Applying. */ true;
  575.  
  576. $p = ($attr["ta"] / 100) * $coupon["percentage"];
  577. $ta = number_format($attr["ta"] - $p, 2, ".", "");
  578. $ta = ($ta >= 0.00) ? $ta : "0.00";
  579.  
  580. $p = ($attr["ra"] / 100) * $coupon["percentage"];
  581. $ra = number_format($attr["ra"], 2, ".", "");
  582. $ra = ($ra >= 0.01) ? $ra : "0.01";
  583.  
  584. $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"]));
  585. $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"]));
  586. }
  587. else if($coupon["directive"] === "ra-only" && $attr["tp"] && !$attr["sp"])
  588. {
  589. $coupon_applies = /* Applying. */ true;
  590.  
  591. $p = ($attr["ta"] / 100) * $coupon["percentage"];
  592. $ta = number_format($attr["ta"], 2, ".", "");
  593. $ta = ($ta >= 0.00) ? $ta : "0.00";
  594.  
  595. $p = ($attr["ra"] / 100) * $coupon["percentage"];
  596. $ra = number_format($attr["ra"] - $p, 2, ".", "");
  597. $ra = ($ra >= 0.01) ? $ra : "0.01";
  598.  
  599. $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"]));
  600. $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"]));
  601. }
  602. else if($coupon["directive"] === "all" && $attr["tp"] && !$attr["sp"])
  603. {
  604. $coupon_applies = /* Applying. */ true;
  605.  
  606. $p = ($attr["ta"] / 100) * $coupon["percentage"];
  607. $ta = number_format($attr["ta"] - $p, 2, ".", "");
  608. $ta = ($ta >= 0.00) ? $ta : "0.00";
  609.  
  610. $p = ($attr["ra"] / 100) * $coupon["percentage"];
  611. $ra = number_format($attr["ra"] - $p, 2, ".", "");
  612. $ra = ($ra >= 0.01) ? $ra : "0.01";
  613.  
  614. $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"]));
  615. $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"]));
  616. }
  617. else if($coupon["directive"] === "ra-only" && !$attr["tp"] && !$attr["sp"])
  618. {
  619. $coupon_applies = /* Applying. */ true;
  620.  
  621. $p = ($attr["ta"] / 100) * $coupon["percentage"];
  622. $ta = number_format($attr["ta"], 2, ".", "");
  623. $ta = ($ta >= 0.00) ? $ta : "0.00";
  624.  
  625. $p = ($attr["ra"] / 100) * $coupon["percentage"];
  626. $ra = number_format($attr["ra"] - $p, 2, ".", "");
  627. $ra = ($ra >= 0.01) ? $ra : "0.01";
  628.  
  629. $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);
  630. $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);
  631. }
  632. else if($coupon["directive"] === "all" && !$attr["tp"] && !$attr["sp"])
  633. {
  634. $coupon_applies = /* Applying. */ true;
  635.  
  636. $p = ($attr["ta"] / 100) * $coupon["percentage"];
  637. $ta = number_format($attr["ta"] - $p, 2, ".", "");
  638. $ta = ($ta >= 0.00) ? $ta : "0.00";
  639.  
  640. $p = ($attr["ra"] / 100) * $coupon["percentage"];
  641. $ra = number_format($attr["ra"] - $p, 2, ".", "");
  642. $ra = ($ra >= 0.01) ? $ra : "0.01";
  643.  
  644. $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);
  645. $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);
  646. }
  647.  
  648. else // Otherwise, we need a default response to display.
  649. $response = _x('<div>Sorry, your Coupon is not applicable.</div>', "s2member-front", "s2member");
  650. }
  651.  
  652. else // Else there was no discount applied at all.
  653. $response = sprintf(_x('<div>Coupon: <strong>%s0.00 off</strong>.</div>', "s2member-front", "s2member"), $cs);
  654. }
  655.  
  656. else // Otherwise, we need a response that indicates not applicable for this purchase.
  657. $response = _x('<div>Sorry, your Coupon cannot be applied to this particular purchase.</div>', "s2member-front", "s2member");
  658. }
  659.  
  660. else if($coupon_code === $coupon["code"] && $coupon["expired"])
  661. $response = sprintf(_x('<div>Sorry, your Coupon <strong>expired</strong>: <em>%s</em>.</div>', "s2member-front", "s2member"), $coupon["expired"]);
  662. }
  663. }
  664.  
  665. if(isset($coupon_applies, $desc) && $coupon_applies /* Need to modify the description dynamically? */)
  666. // translators: `%1$s` is new price/description, after coupon applied. `%2$s` is original description.
  667. $attr["desc"] = sprintf(_x('%1$s ~ ORIGINALLY: %2$s', "s2member-front", "s2member"), $desc, $attr["desc"]);
  668.  
  669. $attr["ta"] = (isset($coupon_applies, $ta) && $coupon_applies) ? /* Do we have a new Trial Amount? */ $ta : $attr["ta"];
  670. $attr["ra"] = (isset($coupon_applies, $ra) && $coupon_applies) ? /* A new Regular Amount? */ $ra : $attr["ra"];
  671.  
  672. if(is_array($process) && /* Processing affiliates? */ (in_array("affiliates-silent-post", $process) || in_array("affiliates-1px-response", $process)))
  673. if(isset($coupon_applies) && $coupon_applies && /* Now, is this an Affiliate Coupon Code? Contains an affiliate ID? */ !empty($affiliate_id))
  674. if(empty($_COOKIE["idev"]) /* Special consideration here. iDevAffiliate® must NOT have already tracked this customer. */)
  675. if(($_urls = $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["pro_affiliate_coupon_code_tracking_urls"]))
  676.  
  677. foreach(preg_split("/[\r\n\t]+/", $_urls) as $_url /* Notify each of the URLs. */)
  678.  
  679. if(($_url = preg_replace("/%%full_coupon_code%%/i", c_ws_plugin__s2member_utils_strings::esc_ds(urlencode($full_coupon_code)), $_url)))
  680. if(($_url = preg_replace("/%%coupon_code%%/i", c_ws_plugin__s2member_utils_strings::esc_ds(urlencode($coupon_code)), $_url)))
  681. if(($_url = preg_replace("/%%(?:coupon_affiliate_id|affiliate_id)%%/i", c_ws_plugin__s2member_utils_strings::esc_ds(urlencode($affiliate_id)), $_url)))
  682. if(($_url = preg_replace("/%%user_ip%%/i", c_ws_plugin__s2member_utils_strings::esc_ds(urlencode($_SERVER["REMOTE_ADDR"])), $_url)))
  683. {
  684. if(($_url = trim(preg_replace("/%%(.+?)%%/i", "", $_url))) /* Cleanup any remaining Replacement Codes. */)
  685.  
  686. if(!($_r = 0) && ($_url = preg_replace("/^silent-php\|/i", "", $_url, 1, $_r)) && $_r && in_array /* Processing? */("affiliates-silent-post", $process))
  687. c_ws_plugin__s2member_utils_urls::remote /* Post silently via PHP. Relies on IP tracking. */($_url, false, array("blocking" => false));
  688.  
  689. else if(!($_r = 0) && ($_url = preg_replace("/^img-1px\|/i", "", $_url, 1, $_r)) && $_r && in_array("affiliates-1px-response", $process))
  690. if( /* Now, we MUST also have a ``$response``, and MUST be returning ``$response``. */!empty($response) && $return === "response")
  691. $response .= "\n".'<img src="'.esc_attr($_url).'" style="width:0; height:0; border:0;" alt="" />';
  692. }
  693. unset /* Just a little housekeeping here. Unset these variables. */($_urls, $_url, $_r);
  694.  
  695. if(empty($response)) // Is ``$response`` NOT set by now? If it's not, we need a default ``$response``.
  696. $response = _x('<div>Sorry, your Coupon is N/A, invalid or expired.</div>', "s2member-front", "s2member");
  697. }
  698. else // Otherwise, we need a default response to display.
  699. $response = _x('<div>Sorry, your Coupon is N/A, invalid or expired.</div>', "s2member-front", "s2member");
  700.  
  701. $attr["_coupon_applies"] = (isset($coupon_applies) && $coupon_applies) ? /* Coupon applies? */ "1" : "0";
  702. $attr["_coupon_code"] = (isset($coupon_applies) && $coupon_applies) ? /* Coupon applies? */ $coupon_code : "";
  703. $attr["_full_coupon_code"] = (isset($coupon_applies) && $coupon_applies && !empty($full_coupon_code)) ? $full_coupon_code : ((isset($coupon_applies) && $coupon_applies) ? $coupon_code : "");
  704. $attr["_coupon_affiliate_id"] = (isset($coupon_applies) && $coupon_applies && !empty($affiliate_id) && empty($_COOKIE["idev"])) ? $affiliate_id : "";
  705.  
  706. return ( /* Returning ``$response``? */$return === "response") ? $response : $attr;
  707. }
  708. }
  709. }
  710. ?>

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

Source Code Documentation