Source Code Documentation


s2member-pro\includes\classes\gateways\google\google-notify-in.inc.php


  1. <?php
  2. /**
  3. * Google® IPN Handler ( inner processing routines ).
  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_notify_in"))
  37. {
  38. /**
  39. * Google® IPN Handler ( inner processing routines ).
  40. *
  41. * @package s2Member\Google
  42. * @since 1.5
  43. */
  44. class c_ws_plugin__s2member_pro_google_notify_in
  45. {
  46. /**
  47. * Handles Google® IPN URL processing.
  48. *
  49. * @package s2Member\Google
  50. * @since 1.5
  51. *
  52. * @attaches-to ``add_action("init");``
  53. *
  54. * @return null Or exits script execution after handling the Notification.
  55. */
  56. public static function google_notify ()
  57. {
  58. global $current_site, $current_blog; /* For Multisite support. */
  59. /**/
  60. if (!empty ($_GET["s2member_pro_google_notify"]) && $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["pro_google_merchant_id"])
  61. {
  62. @ignore_user_abort (true); /* Continue processing even if/when connection is broken by the sender. */
  63. /**/
  64. if (is_array ($google = c_ws_plugin__s2member_pro_google_utilities::google_postvars ()) && ($_google = $google))
  65. {
  66. $google["s2member_log"][] = "IPN received on: " . date ("D M j, Y g:i:s a T");
  67. $google["s2member_log"][] = "s2Member POST vars verified with Google®.";
  68. /**/
  69. if (preg_match ("/^new-order-notification$/i", $google["_type"])/**/
  70. && is_array ($s2vars_item1 = c_ws_plugin__s2member_pro_google_utilities::google_parse_s2vars ($google["order-summary_shopping-cart_items_item-1_merchant-private-item-data"]))
  71. /**/ && !$s2vars_item1["s2_subscr_id"])
  72. {
  73. $google["s2member_log"][] = "Google® transaction identified as ( `SALE/BUY-NOW` ).";
  74. $google["s2member_log"][] = "IPN reformulated. Piping through s2Member's core/standard PayPal® processor as `txn_type` ( `web_accept` ).";
  75. $google["s2member_log"][] = "Please check PayPal® IPN logs for further processing details.";
  76. /**/
  77. $processing = $processed = true;
  78. $ipn = array (); /* Reset. */
  79. /**/
  80. $ipn["txn_type"] = "web_accept";
  81. /**/
  82. $ipn["txn_id"] = ($s2vars_item1["s2_txn_id"]) ? $s2vars_item1["s2_txn_id"] : $google["order-summary_google-order-number"];
  83. /**/
  84. $ipn["custom"] = $s2vars_item1["s2_custom"];
  85. /**/
  86. $ipn["mc_gross"] = number_format ($google["order-summary_order-total"], 2, ".", "");
  87. $ipn["mc_currency"] = strtoupper ($google["order-summary_order-total_currency"]);
  88. $ipn["tax"] = number_format ($google["order-summary_order-adjustment_total-tax"], 2, ".", "");
  89. /**/
  90. $ipn["payer_email"] = $google["buyer-billing-address_email"];
  91. $ipn["first_name"] = $google["buyer-billing-address_structured-name_first-name"];
  92. $ipn["last_name"] = $google["buyer-billing-address_structured-name_last-name"];
  93. /**/
  94. $ipn["option_name1"] = ($s2vars_item1["s2_referencing"]) ? "Referencing Customer ID" : "Originating Domain";
  95. $ipn["option_selection1"] = ($s2vars_item1["s2_referencing"]) ? $s2vars_item1["s2_referencing"] : $_SERVER["HTTP_HOST"];
  96. /**/
  97. $ipn["option_name2"] = "Customer IP Address"; /* IP Address. */
  98. $ipn["option_selection2"] = $s2vars_item1["s2_customer_ip"];
  99. /**/
  100. $ipn["item_number"] = $s2vars_item1["s2_item_number"];
  101. $ipn["item_name"] = $google["order-summary_shopping-cart_items_item-1_item-name"];
  102. /**/
  103. $ipn_q = "&s2member_paypal_proxy=google&s2member_paypal_proxy_use=standard-emails";
  104. $ipn_q .= "&s2member_paypal_proxy_verification=" . urlencode (c_ws_plugin__s2member_paypal_utilities::paypal_proxy_key_gen ());
  105. /**/
  106. c_ws_plugin__s2member_utils_urls::remote (site_url ("/?s2member_paypal_notify=1" . $ipn_q), $ipn, array ("timeout" => 20));
  107. }
  108. /**/
  109. else if (preg_match ("/^new-order-notification$/i", $google["_type"])/**/
  110. && is_array ($s2vars_item1 = c_ws_plugin__s2member_pro_google_utilities::google_parse_s2vars ($google["order-summary_shopping-cart_items_item-1_merchant-private-item-data"]))
  111. /**/ && $s2vars_item1["s2_subscr_id"] && !$s2vars_item1["s2_subscr_payment"])
  112. {
  113. $google["s2member_log"][] = "Google® transaction identified as ( `SALE/SUBSCRIPTION` ).";
  114. $google["s2member_log"][] = "IPN reformulated. Piping through s2Member's core/standard PayPal® processor as `txn_type` ( `subscr_signup` ).";
  115. $google["s2member_log"][] = "Please check PayPal® IPN logs for further processing details.";
  116. /**/
  117. $processing = $processed = true;
  118. $ipn = array (); /* Reset. */
  119. /**/
  120. $ipn["txn_type"] = "subscr_signup";
  121. $ipn["subscr_id"] = $s2vars_item1["s2_subscr_id"];
  122. /**/
  123. $ipn["recurring"] = (!($times = $google["order-summary_shopping-cart_items_item-2_subscription_payments_subscription-payment-1_times"]) || $times > 1) ? "1" : "0";
  124. /**/
  125. $ipn["txn_id"] = $google["order-summary_google-order-number"];
  126. /**/
  127. $ipn["custom"] = $s2vars_item1["s2_custom"];
  128. /**/
  129. $ipn["period1"] = $s2vars_item1["s2_period1"]; /* Just use s2Member's period calculations to make this easier. */
  130. $ipn["period3"] = $s2vars_item1["s2_period3"]; /* Just use s2Member's period calculations to make this easier. */
  131. /**/
  132. $ipn["mc_amount1"] = number_format ($google["order-summary_shopping-cart_items_item-1_unit-price"], 2, ".", "");
  133. $ipn["mc_amount3"] = number_format ($google["order-summary_shopping-cart_items_item-2_subscription_recurrent-item_unit-price"], 2, ".", "");
  134. /**/
  135. $ipn["mc_gross"] = (preg_match ("/^[1-9]/", $ipn["period1"])) ? $ipn["mc_amount1"] : $ipn["mc_amount3"];
  136. /**/
  137. $ipn["mc_currency"] = strtoupper ($google["order-summary_order-total_currency"]);
  138. $ipn["tax"] = number_format ($google["order-summary_order-adjustment_total-tax"], 2, ".", "");
  139. /**/
  140. $ipn["payer_email"] = $google["buyer-billing-address_email"];
  141. $ipn["first_name"] = $google["buyer-billing-address_structured-name_first-name"];
  142. $ipn["last_name"] = $google["buyer-billing-address_structured-name_last-name"];
  143. /**/
  144. $ipn["option_name1"] = ($s2vars_item1["s2_referencing"]) ? "Referencing Customer ID" : "Originating Domain";
  145. $ipn["option_selection1"] = ($s2vars_item1["s2_referencing"]) ? $s2vars_item1["s2_referencing"] : $_SERVER["HTTP_HOST"];
  146. /**/
  147. $ipn["option_name2"] = "Customer IP Address"; /* IP Address. */
  148. $ipn["option_selection2"] = $s2vars_item1["s2_customer_ip"];
  149. /**/
  150. $ipn["item_number"] = $s2vars_item1["s2_item_number"];
  151. $ipn["item_name"] = $google["order-summary_shopping-cart_items_item-1_item-name"];
  152. /**/
  153. $ipn_q = "&s2member_paypal_proxy=google&s2member_paypal_proxy_use=standard-emails";
  154. $ipn_q .= ($ipn["mc_gross"] > 0) ? ",subscr-signup-as-subscr-payment" : ""; /* Use as first payment? */
  155. $ipn_q .= "&s2member_paypal_proxy_verification=" . urlencode (c_ws_plugin__s2member_paypal_utilities::paypal_proxy_key_gen ());
  156. /**/
  157. c_ws_plugin__s2member_utils_urls::remote (site_url ("/?s2member_paypal_notify=1" . $ipn_q), $ipn, array ("timeout" => 20));
  158. }
  159. /**/
  160. else if (preg_match ("/^new-order-notification$/i", $google["_type"])/**/
  161. && is_array ($s2vars_item1 = c_ws_plugin__s2member_pro_google_utilities::google_parse_s2vars ($google["order-summary_shopping-cart_items_item-1_merchant-private-item-data"]))
  162. /**/ && $s2vars_item1["s2_subscr_id"] && $s2vars_item1["s2_subscr_payment"])
  163. {
  164. $google["s2member_log"][] = "Google® transaction identified as ( `SUBSCRIPTION PAYMENT` ).";
  165. $google["s2member_log"][] = "IPN reformulated. Piping through s2Member's core/standard PayPal® processor as `txn_type` ( `subscr_payment` ).";
  166. $google["s2member_log"][] = "Please check PayPal® IPN logs for further processing details.";
  167. /**/
  168. $processing = $processed = true;
  169. $ipn = array (); /* Reset. */
  170. /**/
  171. $ipn["txn_type"] = "subscr_payment";
  172. $ipn["subscr_id"] = $s2vars_item1["s2_subscr_id"];
  173. /**/
  174. $ipn["txn_id"] = $google["order-summary_google-order-number"];
  175. /**/
  176. $ipn["custom"] = $s2vars_item1["s2_custom"];
  177. /**/
  178. $ipn["mc_gross"] = number_format ($google["order-summary_order-total"], 2, ".", "");
  179. $ipn["mc_currency"] = strtoupper ($google["order-summary_order-total_currency"]);
  180. $ipn["tax"] = number_format ($google["order-summary_order-adjustment_total-tax"], 2, ".", "");
  181. /**/
  182. $ipn["payer_email"] = $google["buyer-billing-address_email"];
  183. $ipn["first_name"] = $google["buyer-billing-address_structured-name_first-name"];
  184. $ipn["last_name"] = $google["buyer-billing-address_structured-name_last-name"];
  185. /**/
  186. $ipn["option_name1"] = ($s2vars_item1["s2_referencing"]) ? "Referencing Customer ID" : "Originating Domain";
  187. $ipn["option_selection1"] = ($s2vars_item1["s2_referencing"]) ? $s2vars_item1["s2_referencing"] : $_SERVER["HTTP_HOST"];
  188. /**/
  189. $ipn["option_name2"] = "Customer IP Address"; /* IP Address. */
  190. $ipn["option_selection2"] = $s2vars_item1["s2_customer_ip"];
  191. /**/
  192. $ipn["item_number"] = $s2vars_item1["s2_item_number"];
  193. $ipn["item_name"] = $google["order-summary_shopping-cart_items_item-1_item-name"];
  194. /**/
  195. $ipn_q = "&s2member_paypal_proxy=google&s2member_paypal_proxy_use=standard-emails";
  196. $ipn_q .= "&s2member_paypal_proxy_verification=" . urlencode (c_ws_plugin__s2member_paypal_utilities::paypal_proxy_key_gen ());
  197. /**/
  198. c_ws_plugin__s2member_utils_urls::remote (site_url ("/?s2member_paypal_notify=1" . $ipn_q), $ipn, array ("timeout" => 20));
  199. }
  200. /**/
  201. else if (preg_match ("/^cancelled-subscription-notification$/i", $google["_type"])/**/
  202. && is_array ($s2vars_item1 = c_ws_plugin__s2member_pro_google_utilities::google_parse_s2vars ($google["order-summary_shopping-cart_items_item-1_merchant-private-item-data"]))
  203. /**/ && $s2vars_item1["s2_subscr_id"])
  204. {
  205. $google["s2member_log"][] = "Google® transaction identified as ( `SUBSCRIPTION CANCELLATION` ).";
  206. $google["s2member_log"][] = "IPN reformulated. Piping through s2Member's core/standard PayPal® processor as `txn_type` ( `subscr_cancel` ).";
  207. $google["s2member_log"][] = "Please check PayPal® IPN logs for further processing details.";
  208. /**/
  209. $processing = $processed = true;
  210. $ipn = array (); /* Reset. */
  211. /**/
  212. $ipn["txn_type"] = "subscr_cancel";
  213. $ipn["subscr_id"] = $s2vars_item1["s2_subscr_id"];
  214. /**/
  215. $ipn["custom"] = $s2vars_item1["s2_custom"];
  216. /**/
  217. $ipn["period1"] = $s2vars_item1["s2_period1"];
  218. $ipn["period3"] = $s2vars_item1["s2_period3"];
  219. /**/
  220. $ipn["payer_email"] = $google["order-summary_risk-information_billing-address_email"];
  221. $ipn["first_name"] = preg_replace ("/( )(.+)/", "", $google["order-summary_risk-information_billing-address_contact-name"]);
  222. $ipn["last_name"] = preg_replace ("/(.+?)( )/", "", $google["order-summary_risk-information_billing-address_contact-name"]);
  223. /**/
  224. $ipn["option_name1"] = ($s2vars_item1["s2_referencing"]) ? "Referencing Customer ID" : "Originating Domain";
  225. $ipn["option_selection1"] = ($s2vars_item1["s2_referencing"]) ? $s2vars_item1["s2_referencing"] : $_SERVER["HTTP_HOST"];
  226. /**/
  227. $ipn["option_name2"] = "Customer IP Address"; /* IP Address. */
  228. $ipn["option_selection2"] = $s2vars_item1["s2_customer_ip"];
  229. /**/
  230. $ipn["item_number"] = $s2vars_item1["s2_item_number"];
  231. $ipn["item_name"] = $google["order-summary_shopping-cart_items_item-1_item-name"];
  232. /**/
  233. $ipn_q = "&s2member_paypal_proxy=google&s2member_paypal_proxy_use=standard-emails";
  234. $ipn_q .= "&s2member_paypal_proxy_verification=" . urlencode (c_ws_plugin__s2member_paypal_utilities::paypal_proxy_key_gen ());
  235. /**/
  236. c_ws_plugin__s2member_utils_urls::remote (site_url ("/?s2member_paypal_notify=1" . $ipn_q), $ipn, array ("timeout" => 20));
  237. }
  238. /**/
  239. else if (preg_match ("/^(refund|chargeback)-amount-notification$/i", $google["_type"]) /* Do NOT process partial refunds/chargebacks. */
  240. && is_array ($s2vars_item1 = c_ws_plugin__s2member_pro_google_utilities::google_parse_s2vars ($google["order-summary_shopping-cart_items_item-1_merchant-private-item-data"]))
  241. /**/ && ((preg_match ("/^refund/", $google["_type"]) && $google["latest-fee-refund-amount"] >= $google["order-summary_total-charge-amount"])
  242. /**/ || (preg_match ("/^chargeback/", $google["_type"]) && $google["latest-chargeback-amount"] >= $google["order-summary_total-charge-amount"])))
  243. {
  244. $google["s2member_log"][] = "Google® transaction identified as ( `REFUND|CHARGEBACK` ).";
  245. $google["s2member_log"][] = "IPN reformulated. Piping through s2Member's core/standard PayPal® processor as `payment_status` ( `refunded|reversed` ).";
  246. $google["s2member_log"][] = "Please check PayPal® IPN logs for further processing details.";
  247. /**/
  248. $processing = $processed = true;
  249. $ipn = array (); /* Reset. */
  250. /**/
  251. $ipn["custom"] = $s2vars_item1["s2_custom"];
  252. /**/
  253. if ($s2vars_item1["s2_subscr_id"] && !$s2vars_item1["s2_txn_id"])
  254. $ipn["parent_txn_id"] = $s2vars_item1["s2_subscr_id"];
  255. /**/
  256. else if ($s2vars_item1["s2_txn_id"] && !$s2vars_item1["s2_subscr_id"])
  257. $ipn["parent_txn_id"] = $s2vars_item1["s2_txn_id"];
  258. /**/
  259. else /* Default to Google's Order #. */
  260. $ipn["parent_txn_id"] = $google["order-summary_google-order-number"];
  261. /**/
  262. if (preg_match ("/^refund/", $google["_type"])) /* Use refunded amounts. */
  263. {
  264. $ipn["payment_status"] = "refunded"; /* Refunding. */
  265. $ipn["mc_fee"] = "-" . number_format ($google["latest-fee-refund-amount"], 2, ".", "");
  266. $ipn["mc_gross"] = "-" . number_format ($google["latest-refund-amount"], 2, ".", "");
  267. $ipn["mc_currency"] = strtoupper ($google["latest-refund-amount_currency"]);
  268. $ipn["tax"] = "-" . number_format ("0.00", 2, ".", "");
  269. }
  270. else if (preg_match ("/^chargeback/", $google["_type"])) /* Chargeback. */
  271. {
  272. $ipn["payment_status"] = "reversed"; /* Reversed/chargeback. */
  273. $ipn["mc_fee"] = "-" . number_format ($google["latest-chargeback-fee-amount"], 2, ".", "");
  274. $ipn["mc_gross"] = "-" . number_format ($google["latest-chargeback-amount"], 2, ".", "");
  275. $ipn["mc_currency"] = strtoupper ($google["latest-chargeback-amount_currency"]);
  276. $ipn["tax"] = "-" . number_format ("0.00", 2, ".", "");
  277. }
  278. /**/
  279. $ipn["payer_email"] = $google["order-summary_risk-information_billing-address_email"];
  280. $ipn["first_name"] = preg_replace ("/( )(.+)/", "", $google["order-summary_risk-information_billing-address_contact-name"]);
  281. $ipn["last_name"] = preg_replace ("/(.+?)( )/", "", $google["order-summary_risk-information_billing-address_contact-name"]);
  282. /**/
  283. $ipn["option_name1"] = ($s2vars_item1["s2_referencing"]) ? "Referencing Customer ID" : "Originating Domain";
  284. $ipn["option_selection1"] = ($s2vars_item1["s2_referencing"]) ? $s2vars_item1["s2_referencing"] : $_SERVER["HTTP_HOST"];
  285. /**/
  286. $ipn["option_name2"] = "Customer IP Address"; /* IP Address. */
  287. $ipn["option_selection2"] = $s2vars_item1["s2_customer_ip"];
  288. /**/
  289. $ipn["item_number"] = $s2vars_item1["s2_item_number"];
  290. $ipn["item_name"] = $google["order-summary_shopping-cart_items_item-1_item-name"];
  291. /**/
  292. $ipn_q = "&s2member_paypal_proxy=google&s2member_paypal_proxy_use=standard-emails";
  293. $ipn_q .= "&s2member_paypal_proxy_verification=" . urlencode (c_ws_plugin__s2member_paypal_utilities::paypal_proxy_key_gen ());
  294. /**/
  295. c_ws_plugin__s2member_utils_urls::remote (site_url ("/?s2member_paypal_notify=1" . $ipn_q), $ipn, array ("timeout" => 20));
  296. }
  297. /**/
  298. else if (!$processed) /* If nothing was processed, here we add a message to the logs indicating the IPN was ignored. */
  299. $google["s2member_log"][] = "Ignoring this IPN request. The transaction does NOT require any action on the part of s2Member.";
  300. }
  301. else /* Extensive log reporting here. This is an area where many site owners find trouble. Depending on server configuration; remote HTTPS connections may fail. */
  302. {
  303. $google["s2member_log"][] = "Unable to verify POST vars. This is most likely related to an invalid Google® configuration. Please check: s2Member -> Google® Options.";
  304. $google["s2member_log"][] = "If you're absolutely SURE that your Google® configuration is valid, you may want to run some tests on your server, just to be sure \$_POST variables are populated, and that your server is able to connect to Google® over an HTTPS connection.";
  305. $google["s2member_log"][] = "s2Member uses the WP_Http class for remote connections; which will try to use cURL first, and then fall back on the FOPEN method when cURL is not available. On a Windows® server, you may have to disable your cURL extension. Instead, set allow_url_fopen = yes in your php.ini file. The cURL extension (usually) does NOT support SSL connections on a Windows® server.";
  306. $google["s2member_log"][] = var_export ($_REQUEST, true); /* Recording _POST + _GET vars for analysis and debugging. */
  307. }
  308. /*
  309. We need to log this final event before it occurs, so that is makes it into the log entry.
  310. */
  311. $google["s2member_log"][] = "Sending Google® an XML Notification Acknowlegment w/ original serial number.";
  312. /*
  313. If debugging/logging is enabled; we need to append $google to the log file.
  314. Logging now supports Multisite Networking as well.
  315. */
  316. $logv = c_ws_plugin__s2member_utilities::ver_details ();
  317. $logm = c_ws_plugin__s2member_utilities::mem_details ();
  318. $log4 = $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"] . "\nUser-Agent: " . $_SERVER["HTTP_USER_AGENT"];
  319. $log4 = (is_multisite () && !is_main_site ()) ? ($_log4 = $current_blog->domain . $current_blog->path) . "\n" . $log4 : $log4;
  320. $log2 = (is_multisite () && !is_main_site ()) ? "google-ipn-4-" . trim (preg_replace ("/[^a-z0-9]/i", "-", $_log4), "-") . ".log" : "google-ipn.log";
  321. /**/
  322. if ($GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["gateway_debug_logs"])
  323. if (is_dir ($logs_dir = $GLOBALS["WS_PLUGIN__"]["s2member"]["c"]["logs_dir"]))
  324. if (is_writable ($logs_dir) && c_ws_plugin__s2member_utils_logs::archive_oversize_log_files ())
  325. file_put_contents ($logs_dir . "/" . $log2, $logv . "\n" . $logm . "\n" . $log4 . "\n" . var_export ($google, true) . "\n\n", FILE_APPEND);
  326. /**/
  327. $confirmation = '<?xml version="1.0" encoding="UTF-8"?>';
  328. $confirmation .= '<notification-acknowledgment xmlns="http://checkout.google.com/schema/2"';
  329. $confirmation .= ' serial-number="' . esc_attr (trim (stripslashes ($_REQUEST["serial-number"]))) . '" />';
  330. /**/
  331. status_header (200); /* Send a 200 OK status header. */
  332. header ("Content-Type: application/xml"); /* Google® expects application/xml here. */
  333. eval ('while (@ob_end_clean ());'); /* End/clean all output buffers that may or may not exist. */
  334. /**/
  335. exit ($confirmation); /* Exit w/ serial number confirmation. */
  336. }
  337. }
  338. }
  339. }
  340. ?>

s2member-pro\includes\classes\gateways\google\google-notify-in.inc.php

Source Code Documentation