Source Code Documentation


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


  1. <?php
  2. /**
  3. * Google® 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\Google
  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_google_utilities"))
  37. {
  38. /**
  39. * Google® utilities.
  40. *
  41. * @package s2Member\Google
  42. * @since 1.5
  43. */
  44. class c_ws_plugin__s2member_pro_google_utilities
  45. {
  46. /**
  47. * Builds an HMAC-SHA1 signature for XML data transfer verification.
  48. *
  49. * @package s2Member\Google
  50. * @since 1.5
  51. *
  52. * @param str $xml An XML data string to sign.
  53. * @return str An HMAC-SHA1 signature string.
  54. */
  55. public static function google_sign ($xml = FALSE)
  56. {
  57. $key = $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["pro_google_merchant_key"];
  58. /**/
  59. return c_ws_plugin__s2member_utils_strings::hmac_sha1_sign ((string)$xml, $key);
  60. }
  61. /**
  62. * Formulates request Authorization headers.
  63. *
  64. * @package s2Member\Google
  65. * @since 1.5
  66. *
  67. * @return array Request Authorization headers for Google® API communication.
  68. */
  69. public static function google_api_headers ()
  70. {
  71. $req["headers"]["Accept"] = "application/xml; charset=UTF-8";
  72. $req["headers"]["Content-Type"] = "application/xml; charset=UTF-8";
  73. $req["headers"]["Authorization"] = "Basic " . base64_encode ($GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["pro_google_merchant_id"] . ":" . $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["pro_google_merchant_key"]);
  74. /**/
  75. return $req; /* Return array with headers.*/
  76. }
  77. /**
  78. * Converts a "Period Term" into a Google® periodicity for XML subscription attribute.
  79. *
  80. * @package s2Member\Google
  81. * @since 1.5
  82. *
  83. * @param str $period_term A "Period Term" combination.
  84. * @return str The Google® Checkout equivalent for ``$period_term``.
  85. * One of `DAILY`, `WEEKLY`, `SEMI_MONTHLY`, `MONTHLY`, `EVERY_TWO_MONTHS`, `QUARTERLY`, or `YEARLY`.
  86. * Defaults to `MONTHLY` if ``$period_term`` is not configured properly.
  87. */
  88. public static function google_periodicity ($period_term = FALSE)
  89. {
  90. list ($num, $span) = preg_split ("/ /", strtoupper ($period_term), 2);
  91. $num = (int)$num; /* Force this to an integer. */
  92. /**/
  93. if ($num === 1 && $span === "D")
  94. return "DAILY";
  95. /**/
  96. else if ($num === 1 && $span === "W")
  97. return "WEEKLY";
  98. /**/
  99. else if ($num === 2 && $span === "W")
  100. return "SEMI_MONTHLY";
  101. /**/
  102. else if ($num === 1 && $span === "M")
  103. return "MONTHLY";
  104. /**/
  105. else if ($num === 2 && $span === "M")
  106. return "EVERY_TWO_MONTHS";
  107. /**/
  108. else if ($num === 3 && $span === "M")
  109. return "QUARTERLY";
  110. /**/
  111. else if ($num === 1 && $span === "Y")
  112. return "YEARLY";
  113. /**/
  114. return "MONTHLY";
  115. }
  116. /**
  117. * Parses s2Vars from Google® IPN Notifications.
  118. *
  119. * @package s2Member\Google
  120. * @since 1.5
  121. *
  122. * @param str $xml XML data section returned by Google® for s2Vars.
  123. * @return array|bool An array of s2Vars, else false on failure.
  124. */
  125. public static function google_parse_s2vars ($xml = FALSE)
  126. {
  127. if (preg_match_all ("/<([^\>]+)>([^\<]+)<\/([^\>]+)>/", $xml, $m) && is_array ($m[1]))
  128. {
  129. foreach ($m[1] as $key => $var)
  130. $s2vars[$var] = wp_specialchars_decode ($m[2][$key]);
  131. /**/
  132. return $s2vars;
  133. }
  134. else
  135. return false;
  136. }
  137. /**
  138. * Get ``$_POST`` or ``$_REQUEST`` vars from Google®.
  139. *
  140. * @package s2Member\Google
  141. * @since 1.5
  142. *
  143. * @return array|bool An array of verified ``$_POST`` or ``$_REQUEST`` variables, else false.
  144. *
  145. * @todo Continue optimizing this routine with ``empty()`` and ``isset()``.
  146. * @todo Candidate for the use of ``ifsetor()``?
  147. */
  148. public static function google_postvars ()
  149. {
  150. if (!empty ($_REQUEST["s2member_pro_google_notify"]) && !empty ($_REQUEST["serial-number"]))
  151. {
  152. $postback["_type"] = "notification-history-request";
  153. $postback["serial-number"] = trim (stripslashes ((string)$_REQUEST["serial-number"]));
  154. /**/
  155. $endpoint = ($GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["pro_google_sandbox"]) ? "sandbox.google.com/checkout" : "checkout.google.com";
  156. /**/
  157. if (($response = c_ws_plugin__s2member_utils_urls::remote ("https://" . $endpoint . "/api/checkout/v2/reportsForm/Merchant/" . $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["pro_google_merchant_id"], $postback, array_merge (c_ws_plugin__s2member_pro_google_utilities::google_api_headers (), array ("timeout" => 20)))) && wp_parse_str ($response, $postvars) !== "nill" && !empty ($postvars["_type"]))
  158. return c_ws_plugin__s2member_utils_strings::trim_deep ($postvars);
  159. else /* Nope. Return false. */
  160. return false;
  161. }
  162. else /* Nope. */
  163. return false;
  164. }
  165. /**
  166. * Calculates start date for a Recurring Payment Profile.
  167. *
  168. * @package s2Member\Google
  169. * @since 1.5
  170. *
  171. * @param str $period1 Optional. A "Period Term" combination. Defaults to `0 D`.
  172. * @param str $period3 Optional. A "Period Term" combination. Defaults to `0 D`.
  173. * @return int The start time, a Unix timestamp.
  174. */
  175. public static function google_start_time ($period1 = FALSE, $period3 = FALSE)
  176. {
  177. if (!($p1_time = 0) && ($period1 = trim (strtoupper ($period1))))
  178. {
  179. list ($num, $span) = preg_split ("/ /", $period1, 2);
  180. /**/
  181. $days = 0; /* Days start at 0. */
  182. /**/
  183. if (is_numeric ($num) && !is_numeric ($span))
  184. {
  185. $days = ($span === "D") ? 1 : $days;
  186. $days = ($span === "W") ? 7 : $days;
  187. $days = ($span === "M") ? 30 : $days;
  188. $days = ($span === "Y") ? 365 : $days;
  189. }
  190. /**/
  191. $p1_days = (int)$num * (int)$days;
  192. $p1_time = $p1_days * 86400;
  193. }
  194. /**/
  195. if (!($p3_time = 0) && ($period3 = trim (strtoupper ($period3))))
  196. {
  197. list ($num, $span) = preg_split ("/ /", $period3, 2);
  198. /**/
  199. $days = 0; /* Days start at 0. */
  200. /**/
  201. if (is_numeric ($num) && !is_numeric ($span))
  202. {
  203. $days = ($span === "D") ? 1 : $days;
  204. $days = ($span === "W") ? 7 : $days;
  205. $days = ($span === "M") ? 30 : $days;
  206. $days = ($span === "Y") ? 365 : $days;
  207. }
  208. /**/
  209. $p3_days = (int)$num * (int)$days;
  210. $p3_time = $p3_days * 86400;
  211. }
  212. /**/
  213. $start_time = strtotime ("now") + $p1_time + $p3_time;
  214. /**/
  215. $start_time = ($start_time <= 0) ? strtotime ("now") : $start_time;
  216. /**/
  217. $start_time = $start_time + 43200; /* + 12 hours. */
  218. /* This prevents date clashes with Google's API server. */
  219. /**/
  220. return $start_time;
  221. }
  222. }
  223. }
  224. ?>

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

Source Code Documentation