Source Code Documentation


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. * @return boolean True if the profile was cancelled, else false.
  153. */
  154. public static function payflow_cancel_profile($subscr_id = FALSE)
  155. {
  156. $payflow["TRXTYPE"] = "R";
  157. $payflow["ACTION"] = "C";
  158. $payflow["TENDER"] = "C";
  159. $payflow["ORIGPROFILEID"] = $subscr_id;
  160. /**/
  161. if(($cancellation = c_ws_plugin__s2member_paypal_utilities::paypal_payflow_api_response($payflow)) && empty($cancellation["__error"]))
  162. return true;
  163. /**/
  164. $payflow["TENDER"] = "P";
  165. if(($cancellation = c_ws_plugin__s2member_paypal_utilities::paypal_payflow_api_response($payflow)) && empty($cancellation["__error"]))
  166. return true;
  167. /**/
  168. return false;
  169. }
  170. /**
  171. * Handles currency conversions for Maestro/Solo cards.
  172. *
  173. * PayPal® requires Maestro/Solo to be charged in GBP. So if a site owner is using
  174. * another currency *( i.e. something NOT in GBP )*, we have to convert all of the charge amounts dynamically.
  175. *
  176. * Coupon Codes should always be applied before this conversion takes place.
  177. * That way a site owner's configuration remains adequate.
  178. *
  179. * Tax rates should be applied after this conversion takes place.
  180. *
  181. * @package s2Member\PayPal
  182. * @since 110531
  183. *
  184. * @param array $attr An array of PayPal® Pro Form Attributes.
  185. * @param str $card_type The Card Type *( i.e. Billing Method )* selected.
  186. * @return array The same array of Pro Form Attributes, with possible currency conversions.
  187. */
  188. public static function paypal_maestro_solo_2gbp($attr = FALSE, $card_type = FALSE)
  189. {
  190. if(is_array($attr) && is_string($card_type) && in_array($card_type, array("Maestro", "Solo")))
  191. if(!empty($attr["cc"]) && strcasecmp($attr["cc"], "GBP") !== 0 && is_numeric($attr["ta"]) && is_numeric($attr["ra"]))
  192. 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")))
  193. 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")))
  194. $attr = array_merge($attr, array("cc" => "GBP", "ta" => $c_ta, "ra" => $c_ra));
  195. /**/
  196. return $attr; /* Return array of Attributes. */
  197. }
  198. /**
  199. * Handles the return of Tax for Pro Forms, via AJAX; through a JSON object.
  200. *
  201. * @package s2Member\PayPal
  202. * @since 1.5
  203. *
  204. * @return null Or exits script execution after returning data for AJAX caller.
  205. *
  206. * @todo Check the use of ``strip_tags()`` in this routine?
  207. * @todo Continue optimizing this routine with ``empty()`` and ``isset()``.
  208. * @todo Candidate for the use of ``ifsetor()``?
  209. */
  210. public static function paypal_ajax_tax()
  211. {
  212. 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"))
  213. /* 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.
  214. So instead, s2member-pro-min.js should use c_ws_plugin__s2member_utils_encryption::encrypt() as an alternate form of nonce. */
  215. {
  216. status_header(200); /* Send a 200 OK status header. */
  217. header("Content-Type: text/plain; charset=utf-8"); /* Content-Type text/plain with UTF-8. */
  218. eval('while (@ob_end_clean ());'); /* End/clean all output buffers that may or may not exist. */
  219. /**/
  220. 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"]))))
  221. {
  222. if(is_array($attr = (!empty($_p_tax_vars["attr"])) ? unserialize(c_ws_plugin__s2member_utils_encryption::decrypt($_p_tax_vars["attr"])) : false))
  223. {
  224. $attr = (!empty($attr["coupon"])) ? c_ws_plugin__s2member_pro_paypal_utilities::paypal_apply_coupon($attr, $attr["coupon"]) : $attr;
  225. /**/
  226. $trial = ($attr["rr"] !== "BN" && $attr["tp"]) ? true : false; /* Is there a trial? */
  227. $sub_total_today = ($trial) ? $attr["ta"] : $attr["ra"]; /* What is the sub-total today? */
  228. /**/
  229. $state = strip_tags($_p_tax_vars["state"]);
  230. $country = strip_tags($_p_tax_vars["country"]);
  231. $zip = strip_tags($_p_tax_vars["zip"]);
  232. $currency = $attr["cc"]; /* Currency. */
  233. $desc = $attr["desc"]; /* Description. */
  234. /**/
  235. /* Trial is `null` in this function call. We only need to return what it costs today.
  236. However, we do tag on a "trial" element in the array so the ajax routine will know about this. */
  237. $a = c_ws_plugin__s2member_pro_paypal_utilities::paypal_cost(null, $sub_total_today, $state, $country, $zip, $currency, $desc);
  238. 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"]));
  239. }
  240. }
  241. /**/
  242. exit(); /* Clean exit. */
  243. }
  244. }
  245. /**
  246. * Handles all cost calculations for PayPal®.
  247. *
  248. * Returns an associative array with a possible Percentage Rate, along with the calculated Tax Amount.
  249. * Tax calculations are based on State/Province, Country, and/or Zip Code.
  250. * Updated to support multiple data fields in it's return value.
  251. *
  252. * @package s2Member\PayPal
  253. * @since 1.5
  254. *
  255. * @param int|str $trial_sub_total Optional. A numeric Amount/cost of a possible Initial/Trial being offered.
  256. * @param int|str $sub_total Optional. A numeric Amount/cost of the purchase and/or Regular Period.
  257. * @param str $state Optional. The State/Province where the Customer is billed.
  258. * @param str $country Optional. The Country where the Customer is billed.
  259. * @param int|str $zip Optional. The Postal/Zip Code where the Customer is billed.
  260. * @param str $currency Optional. Expects a 3 character Currency Code.
  261. * @param str $desc Optional. Description of the sale.
  262. * @return array Array of calculations.
  263. *
  264. * @todo Add support for `Zip + 4` syntax?
  265. */
  266. public static function paypal_cost($trial_sub_total = FALSE, $sub_total = FALSE, $state = FALSE, $country = FALSE, $zip = FALSE, $currency = FALSE, $desc = FALSE)
  267. {
  268. $state = strtoupper(c_ws_plugin__s2member_pro_utilities::full_state($state, ($country = strtoupper($country))));
  269. $rates = strtoupper($GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["pro_tax_rates"]);
  270. $default = $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["pro_default_tax"];
  271. $ps = _x("%", "s2member-front percentage-symbol", "s2member");
  272. /**/
  273. foreach(array("trial_sub_total" => $trial_sub_total, "sub_total" => $sub_total) as $this_key => $this_sub_total)
  274. {
  275. unset($_default, $this_tax, $this_tax_per, $this_total, $configured_rates, $configured_rate, $location, $rate, $m);
  276. /**/
  277. if(is_numeric($this_sub_total) && $this_sub_total > 0) /* Must have a valid Sub-Total. */
  278. {
  279. if(preg_match("/%$/", $default)) /* Percentage-based. */
  280. {
  281. if(($_default = (float)$default) > 0)
  282. {
  283. $this_tax = round(($this_sub_total / 100) * $_default, 2);
  284. $this_tax_per = $_default.$ps;
  285. }
  286. else /* Else the Tax is 0.00. */
  287. {
  288. $this_tax = 0.00;
  289. $this_tax_per = $_default.$ps;
  290. }
  291. }
  292. else if(($_default = (float)$default) > 0)
  293. {
  294. $this_tax = round($_default, 2);
  295. $this_tax_per = ""; /* Flat. */
  296. }
  297. else /* Else the Tax is 0.00. */
  298. {
  299. $this_tax = 0.00; /* No Tax. */
  300. $this_tax_per = ""; /* Flat rate. */
  301. }
  302. /**/
  303. if(strlen($country) === 2) /* Must have a valid country. */
  304. {
  305. foreach(preg_split("/[\r\n\t]+/", $rates) as $rate)
  306. {
  307. if($rate = trim($rate)) /* Do NOT process empty lines. */
  308. {
  309. list($location, $rate) = preg_split("/\=/", $rate, 2);
  310. $location = trim($location);
  311. $rate = trim($rate);
  312. /**/
  313. if($location === $country)
  314. $configured_rates[1] = $rate;
  315. /**/
  316. else if($state && $location === $state."/".$country)
  317. $configured_rates[2] = $rate;
  318. /**/
  319. 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)
  320. $configured_rates[2] = $rate;
  321. /**/
  322. else if($zip && preg_match("/^([0-9]+)-([0-9]+)\/(".preg_quote($country, "/").")$/", $location, $m) && $zip >= $m[1] && $zip <= $m[2] && $country === $m[3])
  323. $configured_rates[3] = $rate;
  324. /**/
  325. else if($zip && $location === $zip."/".$country)
  326. $configured_rates[4] = $rate;
  327. }
  328. }
  329. /**/
  330. if(is_array($configured_rates) && !empty($configured_rates))
  331. {
  332. krsort($configured_rates);
  333. $configured_rate = array_shift($configured_rates);
  334. /**/
  335. if(preg_match("/%$/", $configured_rate)) /* Percentage. */
  336. {
  337. if(($configured_rate = (float)$configured_rate) > 0)
  338. {
  339. $this_tax = round(($this_sub_total / 100) * $configured_rate, 2);
  340. $this_tax_per = $configured_rate.$ps;
  341. }
  342. else /* Else the Tax is 0.00. */
  343. {
  344. $this_tax = 0.00; /* No Tax. */
  345. $this_tax_per = $configured_rate.$ps;
  346. }
  347. }
  348. else if(($configured_rate = (float)$configured_rate) > 0)
  349. {
  350. $this_tax = round($configured_rate, 2);
  351. $this_tax_per = ""; /* Flat rate. */
  352. }
  353. else /* Else the Tax is 0.00. */
  354. {
  355. $this_tax = 0.00; /* No Tax. */
  356. $this_tax_per = ""; /* Flat rate. */
  357. }
  358. }
  359. }
  360. /**/
  361. $this_total = $this_sub_total + $this_tax;
  362. }
  363. else /* Else the Tax is 0.00. */
  364. {
  365. $this_tax = 0.00; /* No Tax. */
  366. $this_tax_per = ""; /* Flat rate. */
  367. $this_sub_total = 0.00; /* 0.00. */
  368. $this_total = 0.00; /* 0.00. */
  369. }
  370. /**/
  371. if($this_key === "trial_sub_total")
  372. {
  373. $trial_tax = $this_tax;
  374. $trial_tax_per = $this_tax_per;
  375. $trial_sub_total = $this_sub_total;
  376. $trial_total = $this_total;
  377. }
  378. else if($this_key === "sub_total")
  379. {
  380. $tax = $this_tax;
  381. $tax_per = $this_tax_per;
  382. $sub_total = $this_sub_total;
  383. $total = $this_total;
  384. }
  385. }
  386. /**/
  387. 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);
  388. }
  389. /**
  390. * Checks to see if a Coupon Code was supplied, and if so; what does it provide?
  391. *
  392. * @package s2Member\PayPal
  393. * @since 1.5
  394. *
  395. * @param array $attr An array of Pro Form Attributes.
  396. * @param str $coupon_code Optional. A possible Coupon Code supplied by the Customer.
  397. * @param str $return Optional. Return type. One of `response|attr`. Defaults to `attr`.
  398. * @param array $process Optional. An array of additional processing routines to run here.
  399. * One or more of these values: `affiliates-1px-response|affiliates-silent-post|notifications`.
  400. * @return array|str Original array, with prices and description modified when/if a Coupon Code is accepted.
  401. * Or, if ``$return === "response"``, return a string response, indicating status.
  402. *
  403. * @todo See if it's possible to simplify this routine.
  404. * @todo Add support for tracking Coupon Code usage.
  405. * @todo Add support for a fixed number of uses.
  406. */
  407. public static function paypal_apply_coupon($attr = FALSE, $coupon_code = FALSE, $return = FALSE, $process = FALSE)
  408. {
  409. if(($coupon_code = trim(strtolower($coupon_code))) || ($coupon_code = trim(strtolower($attr["coupon"]))))
  410. if($attr["accept_coupons"] && $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["pro_coupon_codes"])
  411. {
  412. $cs = c_ws_plugin__s2member_utils_cur::symbol($attr["cc"]);
  413. $tx = (c_ws_plugin__s2member_pro_paypal_utilities::paypal_tax_may_apply()) ? _x(" + tax", "s2member-front", "s2member") : "";
  414. $ps = _x("%", "s2member-front percentage-symbol", "s2member");
  415. /**/
  416. if(strlen($affiliate_suffix_chars = $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["pro_affiliate_coupon_code_suffix_chars"]))
  417. if(preg_match("/^(.+?)".preg_quote($affiliate_suffix_chars, "/")."([0-9]+)$/i", $coupon_code, $m))
  418. ($full_coupon_code = $m[0]).($coupon_code = $m[1]).($affiliate_id = $m[2]);
  419. unset /* Just a little housekeeping here. */($affiliate_suffix_chars, $m);
  420. /**/
  421. foreach(c_ws_plugin__s2member_utils_strings::trim_deep(preg_split("/[\r\n\t]+/", $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["pro_coupon_codes"])) as $_line)
  422. {
  423. if(($_line = trim($_line, " \r\n\t\0\x0B|")) && is_array($_coupon = preg_split("/\|/", $_line)))
  424. {
  425. $coupon["code"] = (!empty($_coupon[0])) ? trim(strtolower($_coupon[0])) : "";
  426. /**/
  427. $coupon["percentage"] = (!empty($_coupon[1]) && preg_match("/%/", $_coupon[1])) ? (float)$_coupon[1] : 0;
  428. $coupon["flat-rate"] = (!empty($_coupon[1]) && !preg_match("/%/", $_coupon[1])) ? (float)$_coupon[1] : 0;
  429. /**/
  430. $coupon["expired"] = (!empty($_coupon[2]) && /* Expired? */ strtotime($_coupon[2]) < time()) ? $_coupon[2] : false;
  431. /**/
  432. $coupon["directive"] = (!empty($_coupon[3]) && ($_coupon[3] = strtolower($_coupon[3]))) ? preg_replace("/_/", "-", $_coupon[3]) : "all";
  433. $coupon["directive"] = (preg_match("/^(ta-only|ra-only|all)$/", $coupon["directive"])) ? $coupon["directive"] : "all";
  434. /**/
  435. $coupon["singulars"] = (!empty($_coupon[4]) && ($_coupon[4] = strtolower($_coupon[4])) && $_coupon[4] !== "all") ? $_coupon[4] : "all";
  436. $coupon["singulars"] = ($coupon["singulars"] !== "all") ? preg_split("/[\r\n\t\s;,]+/", trim(preg_replace("/[^0-9,]/", "", $coupon["singulars"]), ",")) : array("all");
  437. /**/
  438. unset /* Just a little housekeeping here. Unset these temporary variables. */($_line, $_coupon);
  439. /**/
  440. if($coupon_code === $coupon["code"] && /* And it's NOT yet expired, or lasts forever? */ !$coupon["expired"])
  441. {
  442. if($coupon["singulars"] === array("all")|| in_array($attr["singular"], $coupon["singulars"]))
  443. {
  444. $coupon_accepted = /* Yes, this Coupon Code has been accepted. */ true;
  445. /**/
  446. if($coupon["flat-rate"]) /* If it's a flat-rate Coupon. */
  447. {
  448. if(($coupon["directive"] === "ra-only" || $coupon["directive"] === "all") && $attr["sp"])
  449. {
  450. $coupon_applies = /* Applying. */ true;
  451. /**/
  452. $ta = number_format($attr["ta"], 2, ".", "");
  453. $ta = ($ta >= 0.00) ? $ta : "0.00";
  454. /**/
  455. $ra = number_format($attr["ra"] - $coupon["flat-rate"], 2, ".", "");
  456. $ra = ($ra >= 0.01) ? $ra : "0.01";
  457. /**/
  458. $desc = sprintf(_x("COUPON %s off. ( Now: %s )", "s2member-front", "s2member"), $cs.number_format($coupon["flat-rate"], 2, ".", ""), $cs.$ra.$tx);
  459. $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);
  460. }
  461. else if($coupon["directive"] === "ta-only" && $attr["tp"] && !$attr["sp"])
  462. {
  463. $coupon_applies = /* Applying. */ true;
  464. /**/
  465. $ta = number_format($attr["ta"] - $coupon["flat-rate"], 2, ".", "");
  466. $ta = ($ta >= 0.00) ? $ta : "0.00";
  467. /**/
  468. $ra = number_format($attr["ra"], 2, ".", "");
  469. $ra = ($ra >= 0.01) ? $ra : "0.01";
  470. /**/
  471. $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"]));
  472. $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"]));
  473. }
  474. else if($coupon["directive"] === "ra-only" && $attr["tp"] && !$attr["sp"])
  475. {
  476. $coupon_applies = /* Applying. */ true;
  477. /**/
  478. $ta = number_format($attr["ta"], 2, ".", "");
  479. $ta = ($ta >= 0.00) ? $ta : "0.00";
  480. /**/
  481. $ra = number_format($attr["ra"] - $coupon["flat-rate"], 2, ".", "");
  482. $ra = ($ra >= 0.01) ? $ra : "0.01";
  483. /**/
  484. $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"]));
  485. $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"]));
  486. }
  487. else if($coupon["directive"] === "all" && $attr["tp"] && !$attr["sp"])
  488. {
  489. $coupon_applies = /* Applying. */ true;
  490. /**/
  491. $ta = number_format($attr["ta"] - $coupon["flat-rate"], 2, ".", "");
  492. $ta = ($ta >= 0.00) ? $ta : "0.00";
  493. /**/
  494. $ra = number_format($attr["ra"] - $coupon["flat-rate"], 2, ".", "");
  495. $ra = ($ra >= 0.01) ? $ra : "0.01";
  496. /**/
  497. $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"]));
  498. $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"]));
  499. }
  500. else if($coupon["directive"] === "ra-only" && !$attr["tp"] && !$attr["sp"])
  501. {
  502. $coupon_applies = /* Applying. */ true;
  503. /**/
  504. $ta = number_format($attr["ta"], 2, ".", "");
  505. $ta = ($ta >= 0.00) ? $ta : "0.00";
  506. /**/
  507. $ra = number_format($attr["ra"] - $coupon["flat-rate"], 2, ".", "");
  508. $ra = ($ra >= 0.01) ? $ra : "0.01";
  509. /**/
  510. $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);
  511. $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);
  512. }
  513. else if($coupon["directive"] === "all" && !$attr["tp"] && !$attr["sp"])
  514. {
  515. $coupon_applies = /* Applying. */ true;
  516. /**/
  517. $ta = number_format($attr["ta"] - $coupon["flat-rate"], 2, ".", "");
  518. $ta = ($ta >= 0.00) ? $ta : "0.00";
  519. /**/
  520. $ra = number_format($attr["ra"] - $coupon["flat-rate"], 2, ".", "");
  521. $ra = ($ra >= 0.01) ? $ra : "0.01";
  522. /**/
  523. $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);
  524. $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);
  525. }
  526. /**/
  527. else /* Otherwise, we need a default response to display. */
  528. $response = _x('<div>Sorry, your Coupon is not applicable.</div>', "s2member-front", "s2member");
  529. }
  530. /**/
  531. else if($coupon["percentage"]) /* Else if it's a percentage. */
  532. {
  533. if(($coupon["directive"] === "ra-only" || $coupon["directive"] === "all") && $attr["sp"])
  534. {
  535. $coupon_applies = /* Applying. */ true;
  536. /**/
  537. $p = ($attr["ta"] / 100) * $coupon["percentage"];
  538. $ta = number_format($attr["ta"], 2, ".", "");
  539. $ta = ($ta >= 0.00) ? $ta : "0.00";
  540. /**/
  541. $p = ($attr["ra"] / 100) * $coupon["percentage"];
  542. $ra = number_format($attr["ra"] - $p, 2, ".", "");
  543. $ra = ($ra >= 0.01) ? $ra : "0.01";
  544. /**/
  545. $desc = sprintf(_x("COUPON %s off. ( Now: %s )", "s2member-front", "s2member"), number_format($coupon["percentage"], 0).$ps, $cs.$ra.$tx);
  546. $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);
  547. }
  548. else if($coupon["directive"] === "ta-only" && $attr["tp"] && !$attr["sp"])
  549. {
  550. $coupon_applies = /* Applying. */ true;
  551. /**/
  552. $p = ($attr["ta"] / 100) * $coupon["percentage"];
  553. $ta = number_format($attr["ta"] - $p, 2, ".", "");
  554. $ta = ($ta >= 0.00) ? $ta : "0.00";
  555. /**/
  556. $p = ($attr["ra"] / 100) * $coupon["percentage"];
  557. $ra = number_format($attr["ra"], 2, ".", "");
  558. $ra = ($ra >= 0.01) ? $ra : "0.01";
  559. /**/
  560. $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"]));
  561. $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"]));
  562. }
  563. else if($coupon["directive"] === "ra-only" && $attr["tp"] && !$attr["sp"])
  564. {
  565. $coupon_applies = /* Applying. */ true;
  566. /**/
  567. $p = ($attr["ta"] / 100) * $coupon["percentage"];
  568. $ta = number_format($attr["ta"], 2, ".", "");
  569. $ta = ($ta >= 0.00) ? $ta : "0.00";
  570. /**/
  571. $p = ($attr["ra"] / 100) * $coupon["percentage"];
  572. $ra = number_format($attr["ra"] - $p, 2, ".", "");
  573. $ra = ($ra >= 0.01) ? $ra : "0.01";
  574. /**/
  575. $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"]));
  576. $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"]));
  577. }
  578. else if($coupon["directive"] === "all" && $attr["tp"] && !$attr["sp"])
  579. {
  580. $coupon_applies = /* Applying. */ true;
  581. /**/
  582. $p = ($attr["ta"] / 100) * $coupon["percentage"];
  583. $ta = number_format($attr["ta"] - $p, 2, ".", "");
  584. $ta = ($ta >= 0.00) ? $ta : "0.00";
  585. /**/
  586. $p = ($attr["ra"] / 100) * $coupon["percentage"];
  587. $ra = number_format($attr["ra"] - $p, 2, ".", "");
  588. $ra = ($ra >= 0.01) ? $ra : "0.01";
  589. /**/
  590. $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"]));
  591. $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"]));
  592. }
  593. else if($coupon["directive"] === "ra-only" && !$attr["tp"] && !$attr["sp"])
  594. {
  595. $coupon_applies = /* Applying. */ true;
  596. /**/
  597. $p = ($attr["ta"] / 100) * $coupon["percentage"];
  598. $ta = number_format($attr["ta"], 2, ".", "");
  599. $ta = ($ta >= 0.00) ? $ta : "0.00";
  600. /**/
  601. $p = ($attr["ra"] / 100) * $coupon["percentage"];
  602. $ra = number_format($attr["ra"] - $p, 2, ".", "");
  603. $ra = ($ra >= 0.01) ? $ra : "0.01";
  604. /**/
  605. $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);
  606. $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);
  607. }
  608. else if($coupon["directive"] === "all" && !$attr["tp"] && !$attr["sp"])
  609. {
  610. $coupon_applies = /* Applying. */ true;
  611. /**/
  612. $p = ($attr["ta"] / 100) * $coupon["percentage"];
  613. $ta = number_format($attr["ta"] - $p, 2, ".", "");
  614. $ta = ($ta >= 0.00) ? $ta : "0.00";
  615. /**/
  616. $p = ($attr["ra"] / 100) * $coupon["percentage"];
  617. $ra = number_format($attr["ra"] - $p, 2, ".", "");
  618. $ra = ($ra >= 0.01) ? $ra : "0.01";
  619. /**/
  620. $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);
  621. $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);
  622. }
  623. /**/
  624. else /* Otherwise, we need a default response to display. */
  625. $response = _x('<div>Sorry, your Coupon is not applicable.</div>', "s2member-front", "s2member");
  626. }
  627. /**/
  628. else /* Else there was no discount applied at all. */
  629. $response = sprintf(_x('<div>Coupon: <strong>%s0.00 off</strong>.</div>', "s2member-front", "s2member"), $cs);
  630. }
  631. /**/
  632. else /* Otherwise, we need a response that indicates not applicable for this purchase. */
  633. $response = _x('<div>Sorry, your Coupon cannot be applied to this particular purchase.</div>', "s2member-front", "s2member");
  634. }
  635. /**/
  636. else if($coupon_code === $coupon["code"] && $coupon["expired"])
  637. $response = sprintf(_x('<div>Sorry, your Coupon <strong>expired</strong>: <em>%s</em>.</div>', "s2member-front", "s2member"), $coupon["expired"]);
  638. }
  639. }
  640. /**/
  641. if(isset($coupon_applies, $desc) && $coupon_applies /* Need to modify the description dynamically? */)
  642. /* translators: `%1$s` is new price/description, after coupon applied. `%2$s` is original description. */
  643. $attr["desc"] = sprintf(_x('%1$s ~ ORIGINALLY: %2$s', "s2member-front", "s2member"), $desc, $attr["desc"]);
  644. /**/
  645. $attr["ta"] = (isset($coupon_applies, $ta) && $coupon_applies) ? /* Do we have a new Trial Amount? */ $ta : $attr["ta"];
  646. $attr["ra"] = (isset($coupon_applies, $ra) && $coupon_applies) ? /* A new Regular Amount? */ $ra : $attr["ra"];
  647. /**/
  648. if(is_array($process) && /* Processing affiliates? */ (in_array("affiliates-silent-post", $process) || in_array("affiliates-1px-response", $process)))
  649. if(isset($coupon_applies) && $coupon_applies && /* Now, is this an Affiliate Coupon Code? Contains an affiliate ID? */ !empty($affiliate_id))
  650. if(empty($_COOKIE["idev"]) /* Special consideration here. iDevAffiliate® must NOT have already tracked this customer. */)
  651. if(($_urls = $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["pro_affiliate_coupon_code_tracking_urls"]))
  652. /**/
  653. foreach(preg_split("/[\r\n\t]+/", $_urls) as $_url /* Notify each of the URLs. */)
  654. /**/
  655. if(($_url = preg_replace("/%%full_coupon_code%%/i", c_ws_plugin__s2member_utils_strings::esc_ds(urlencode($full_coupon_code)), $_url)))
  656. if(($_url = preg_replace("/%%coupon_code%%/i", c_ws_plugin__s2member_utils_strings::esc_ds(urlencode($coupon_code)), $_url)))
  657. if(($_url = preg_replace("/%%affiliate_id%%/i", c_ws_plugin__s2member_utils_strings::esc_ds(urlencode($affiliate_id)), $_url)))
  658. if(($_url = preg_replace("/%%user_ip%%/i", c_ws_plugin__s2member_utils_strings::esc_ds(urlencode($_SERVER["REMOTE_ADDR"])), $_url)))
  659. {
  660. if(($_url = trim(preg_replace("/%%(.+?)%%/i", "", $_url))) /* Cleanup any remaining Replacement Codes. */)
  661. /**/
  662. if(!($_r = 0) && ($_url = preg_replace("/^silent-php\|/i", "", $_url, 1, $_r)) && $_r && in_array /* Processing? */("affiliates-silent-post", $process))
  663. c_ws_plugin__s2member_utils_urls::remote /* Post silently via PHP. Relies on IP tracking. */($_url, false, array("blocking" => false));
  664. /**/
  665. else if(!($_r = 0) && ($_url = preg_replace("/^img-1px\|/i", "", $_url, 1, $_r)) && $_r && in_array("affiliates-1px-response", $process))
  666. if( /* Now, we MUST also have a ``$response``, and MUST be returning ``$response``. */!empty($response) && $return === "response")
  667. $response .= "\n".'<img src="'.esc_attr($_url).'" style="width:0; height:0; border:0;" alt="" />';
  668. }
  669. unset /* Just a little housekeeping here. Unset these variables. */($_urls, $_url, $_r);
  670. /**/
  671. if(empty($response)) /* Is ``$response`` NOT set by now? If it's not, we need a default ``$response``. */
  672. $response = _x('<div>Sorry, your Coupon is N/A, invalid or expired.</div>', "s2member-front", "s2member");
  673. }
  674. /**/
  675. else /* Otherwise, we need a default response to display. */
  676. $response = _x('<div>Sorry, your Coupon is N/A, invalid or expired.</div>', "s2member-front", "s2member");
  677. /**/
  678. return ( /* Returning ``$response``? */$return === "response") ? $response : $attr;
  679. }
  680. }
  681. }
  682. ?>

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

Source Code Documentation