Source Code Documentation


pro\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 /* For Multisite support. */ $current_site, $current_blog;
  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["s2member_paypal_proxy"] = "google";
  104. $ipn["s2member_paypal_proxy_use"] = "standard-emails";
  105. $ipn["s2member_paypal_proxy_verification"] = c_ws_plugin__s2member_paypal_utilities::paypal_proxy_key_gen();
  106.  
  107. c_ws_plugin__s2member_utils_urls::remote (site_url ("/?s2member_paypal_notify=1"), $ipn, array ("timeout" => 20));
  108. }
  109.  
  110. else if (preg_match ("/^new-order-notification$/i", $google["_type"])
  111. && 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"]))
  112. && $s2vars_item1["s2_subscr_id"] && !$s2vars_item1["s2_subscr_payment"])
  113. {
  114. $google["s2member_log"][] = "Google® transaction identified as ( `SALE/SUBSCRIPTION` ).";
  115. $google["s2member_log"][] = "IPN reformulated. Piping through s2Member's core/standard PayPal® processor as `txn_type` ( `subscr_signup` ).";
  116. $google["s2member_log"][] = "Please check PayPal® IPN logs for further processing details.";
  117.  
  118. $processing = $processed = true;
  119. $ipn = array (); // Reset.
  120.  
  121. $ipn["txn_type"] = "subscr_signup";
  122. $ipn["subscr_id"] = $s2vars_item1["s2_subscr_id"];
  123.  
  124. $ipn["recurring"] = (!($times = $google["order-summary_shopping-cart_items_item-2_subscription_payments_subscription-payment-1_times"]) || $times > 1) ? "1" : "0";
  125.  
  126. $ipn["txn_id"] = $google["order-summary_google-order-number"];
  127.  
  128. $ipn["custom"] = $s2vars_item1["s2_custom"];
  129.  
  130. $ipn["period1"] = $s2vars_item1["s2_period1"]; // Just use s2Member's period calculations to make this easier.
  131. $ipn["period3"] = $s2vars_item1["s2_period3"]; // Just use s2Member's period calculations to make this easier.
  132.  
  133. $ipn["mc_amount1"] = number_format ($google["order-summary_shopping-cart_items_item-1_unit-price"], 2, ".", "");
  134. $ipn["mc_amount3"] = number_format ($google["order-summary_shopping-cart_items_item-2_subscription_recurrent-item_unit-price"], 2, ".", "");
  135.  
  136. $ipn["mc_gross"] = (preg_match ("/^[1-9]/", $ipn["period1"])) ? $ipn["mc_amount1"] : $ipn["mc_amount3"];
  137.  
  138. $ipn["mc_currency"] = strtoupper ($google["order-summary_order-total_currency"]);
  139. $ipn["tax"] = number_format ($google["order-summary_order-adjustment_total-tax"], 2, ".", "");
  140.  
  141. $ipn["payer_email"] = $google["buyer-billing-address_email"];
  142. $ipn["first_name"] = $google["buyer-billing-address_structured-name_first-name"];
  143. $ipn["last_name"] = $google["buyer-billing-address_structured-name_last-name"];
  144.  
  145. $ipn["option_name1"] = ($s2vars_item1["s2_referencing"]) ? "Referencing Customer ID" : "Originating Domain";
  146. $ipn["option_selection1"] = ($s2vars_item1["s2_referencing"]) ? $s2vars_item1["s2_referencing"] : $_SERVER["HTTP_HOST"];
  147.  
  148. $ipn["option_name2"] = "Customer IP Address"; // IP Address.
  149. $ipn["option_selection2"] = $s2vars_item1["s2_customer_ip"];
  150.  
  151. $ipn["item_number"] = $s2vars_item1["s2_item_number"];
  152. $ipn["item_name"] = $google["order-summary_shopping-cart_items_item-1_item-name"];
  153.  
  154. $ipn["s2member_paypal_proxy"] = "google";
  155. $ipn["s2member_paypal_proxy_use"] = "standard-emails";
  156. $ipn["s2member_paypal_proxy_use"] .= ($ipn["mc_gross"] > 0) ? ",subscr-signup-as-subscr-payment" : "";
  157. $ipn["s2member_paypal_proxy_verification"] = c_ws_plugin__s2member_paypal_utilities::paypal_proxy_key_gen();
  158.  
  159. c_ws_plugin__s2member_utils_urls::remote (site_url ("/?s2member_paypal_notify=1"), $ipn, array ("timeout" => 20));
  160. }
  161.  
  162. else if (preg_match ("/^new-order-notification$/i", $google["_type"])
  163. && 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"]))
  164. && $s2vars_item1["s2_subscr_id"] && $s2vars_item1["s2_subscr_payment"])
  165. {
  166. $google["s2member_log"][] = "Google® transaction identified as ( `SUBSCRIPTION PAYMENT` ).";
  167. $google["s2member_log"][] = "IPN reformulated. Piping through s2Member's core/standard PayPal® processor as `txn_type` ( `subscr_payment` ).";
  168. $google["s2member_log"][] = "Please check PayPal® IPN logs for further processing details.";
  169.  
  170. $processing = $processed = true;
  171. $ipn = array (); // Reset.
  172.  
  173. $ipn["txn_type"] = "subscr_payment";
  174. $ipn["subscr_id"] = $s2vars_item1["s2_subscr_id"];
  175.  
  176. $ipn["txn_id"] = $google["order-summary_google-order-number"];
  177.  
  178. $ipn["custom"] = $s2vars_item1["s2_custom"];
  179.  
  180. $ipn["mc_gross"] = number_format ($google["order-summary_order-total"], 2, ".", "");
  181. $ipn["mc_currency"] = strtoupper ($google["order-summary_order-total_currency"]);
  182. $ipn["tax"] = number_format ($google["order-summary_order-adjustment_total-tax"], 2, ".", "");
  183.  
  184. $ipn["payer_email"] = $google["buyer-billing-address_email"];
  185. $ipn["first_name"] = $google["buyer-billing-address_structured-name_first-name"];
  186. $ipn["last_name"] = $google["buyer-billing-address_structured-name_last-name"];
  187.  
  188. $ipn["option_name1"] = ($s2vars_item1["s2_referencing"]) ? "Referencing Customer ID" : "Originating Domain";
  189. $ipn["option_selection1"] = ($s2vars_item1["s2_referencing"]) ? $s2vars_item1["s2_referencing"] : $_SERVER["HTTP_HOST"];
  190.  
  191. $ipn["option_name2"] = "Customer IP Address"; // IP Address.
  192. $ipn["option_selection2"] = $s2vars_item1["s2_customer_ip"];
  193.  
  194. $ipn["item_number"] = $s2vars_item1["s2_item_number"];
  195. $ipn["item_name"] = $google["order-summary_shopping-cart_items_item-1_item-name"];
  196.  
  197. $ipn["s2member_paypal_proxy"] = "google";
  198. $ipn["s2member_paypal_proxy_use"] = "standard-emails";
  199. $ipn["s2member_paypal_proxy_verification"] = c_ws_plugin__s2member_paypal_utilities::paypal_proxy_key_gen();
  200.  
  201. c_ws_plugin__s2member_utils_urls::remote (site_url ("/?s2member_paypal_notify=1"), $ipn, array ("timeout" => 20));
  202. }
  203.  
  204. else if (preg_match ("/^cancelled-subscription-notification$/i", $google["_type"])
  205. && 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"]))
  206. && $s2vars_item1["s2_subscr_id"])
  207. {
  208. $google["s2member_log"][] = "Google® transaction identified as ( `SUBSCRIPTION CANCELLATION` ).";
  209. $google["s2member_log"][] = "IPN reformulated. Piping through s2Member's core/standard PayPal® processor as `txn_type` ( `subscr_cancel` ).";
  210. $google["s2member_log"][] = "Please check PayPal® IPN logs for further processing details.";
  211.  
  212. $processing = $processed = true;
  213. $ipn = array (); // Reset.
  214.  
  215. $ipn["txn_type"] = "subscr_cancel";
  216. $ipn["subscr_id"] = $s2vars_item1["s2_subscr_id"];
  217.  
  218. $ipn["custom"] = $s2vars_item1["s2_custom"];
  219.  
  220. $ipn["period1"] = $s2vars_item1["s2_period1"];
  221. $ipn["period3"] = $s2vars_item1["s2_period3"];
  222.  
  223. $ipn["payer_email"] = $google["order-summary_risk-information_billing-address_email"];
  224. $ipn["first_name"] = preg_replace ("/( )(.+)/", "", $google["order-summary_risk-information_billing-address_contact-name"]);
  225. $ipn["last_name"] = preg_replace ("/(.+?)( )/", "", $google["order-summary_risk-information_billing-address_contact-name"]);
  226.  
  227. $ipn["option_name1"] = ($s2vars_item1["s2_referencing"]) ? "Referencing Customer ID" : "Originating Domain";
  228. $ipn["option_selection1"] = ($s2vars_item1["s2_referencing"]) ? $s2vars_item1["s2_referencing"] : $_SERVER["HTTP_HOST"];
  229.  
  230. $ipn["option_name2"] = "Customer IP Address"; // IP Address.
  231. $ipn["option_selection2"] = $s2vars_item1["s2_customer_ip"];
  232.  
  233. $ipn["item_number"] = $s2vars_item1["s2_item_number"];
  234. $ipn["item_name"] = $google["order-summary_shopping-cart_items_item-1_item-name"];
  235.  
  236. $ipn["s2member_paypal_proxy"] = "google";
  237. $ipn["s2member_paypal_proxy_use"] = "standard-emails";
  238. $ipn["s2member_paypal_proxy_verification"] = c_ws_plugin__s2member_paypal_utilities::paypal_proxy_key_gen();
  239.  
  240. c_ws_plugin__s2member_utils_urls::remote (site_url ("/?s2member_paypal_notify=1"), $ipn, array ("timeout" => 20));
  241. }
  242.  
  243. else if (preg_match ("/^(refund|chargeback)-amount-notification$/i", $google["_type"]) // Do NOT process partial refunds/chargebacks.
  244. && 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"]))
  245. && ((preg_match ("/^refund/", $google["_type"]) && $google["latest-fee-refund-amount"] >= $google["order-summary_total-charge-amount"])
  246. || (preg_match ("/^chargeback/", $google["_type"]) && $google["latest-chargeback-amount"] >= $google["order-summary_total-charge-amount"])))
  247. {
  248. $google["s2member_log"][] = "Google® transaction identified as ( `REFUND|CHARGEBACK` ).";
  249. $google["s2member_log"][] = "IPN reformulated. Piping through s2Member's core/standard PayPal® processor as `payment_status` ( `refunded|reversed` ).";
  250. $google["s2member_log"][] = "Please check PayPal® IPN logs for further processing details.";
  251.  
  252. $processing = $processed = true;
  253. $ipn = array (); // Reset.
  254.  
  255. $ipn["custom"] = $s2vars_item1["s2_custom"];
  256.  
  257. if ($s2vars_item1["s2_subscr_id"] && !$s2vars_item1["s2_txn_id"])
  258. $ipn["parent_txn_id"] = $s2vars_item1["s2_subscr_id"];
  259.  
  260. else if ($s2vars_item1["s2_txn_id"] && !$s2vars_item1["s2_subscr_id"])
  261. $ipn["parent_txn_id"] = $s2vars_item1["s2_txn_id"];
  262.  
  263. else // Default to Google's Order #.
  264. $ipn["parent_txn_id"] = $google["order-summary_google-order-number"];
  265.  
  266. if (preg_match ("/^refund/", $google["_type"])) // Use refunded amounts.
  267. {
  268. $ipn["payment_status"] = "refunded"; // Refunding.
  269. $ipn["mc_fee"] = "-" . number_format ($google["latest-fee-refund-amount"], 2, ".", "");
  270. $ipn["mc_gross"] = "-" . number_format ($google["latest-refund-amount"], 2, ".", "");
  271. $ipn["mc_currency"] = strtoupper ($google["latest-refund-amount_currency"]);
  272. $ipn["tax"] = "-" . number_format ("0.00", 2, ".", "");
  273. }
  274. else if (preg_match ("/^chargeback/", $google["_type"])) // Chargeback.
  275. {
  276. $ipn["payment_status"] = "reversed"; // Reversed/chargeback.
  277. $ipn["mc_fee"] = "-" . number_format ($google["latest-chargeback-fee-amount"], 2, ".", "");
  278. $ipn["mc_gross"] = "-" . number_format ($google["latest-chargeback-amount"], 2, ".", "");
  279. $ipn["mc_currency"] = strtoupper ($google["latest-chargeback-amount_currency"]);
  280. $ipn["tax"] = "-" . number_format ("0.00", 2, ".", "");
  281. }
  282.  
  283. $ipn["payer_email"] = $google["order-summary_risk-information_billing-address_email"];
  284. $ipn["first_name"] = preg_replace ("/( )(.+)/", "", $google["order-summary_risk-information_billing-address_contact-name"]);
  285. $ipn["last_name"] = preg_replace ("/(.+?)( )/", "", $google["order-summary_risk-information_billing-address_contact-name"]);
  286.  
  287. $ipn["option_name1"] = ($s2vars_item1["s2_referencing"]) ? "Referencing Customer ID" : "Originating Domain";
  288. $ipn["option_selection1"] = ($s2vars_item1["s2_referencing"]) ? $s2vars_item1["s2_referencing"] : $_SERVER["HTTP_HOST"];
  289.  
  290. $ipn["option_name2"] = "Customer IP Address"; // IP Address.
  291. $ipn["option_selection2"] = $s2vars_item1["s2_customer_ip"];
  292.  
  293. $ipn["item_number"] = $s2vars_item1["s2_item_number"];
  294. $ipn["item_name"] = $google["order-summary_shopping-cart_items_item-1_item-name"];
  295.  
  296. $ipn["s2member_paypal_proxy"] = "google";
  297. $ipn["s2member_paypal_proxy_use"] = "standard-emails";
  298. $ipn["s2member_paypal_proxy_verification"] = c_ws_plugin__s2member_paypal_utilities::paypal_proxy_key_gen();
  299.  
  300. c_ws_plugin__s2member_utils_urls::remote (site_url ("/?s2member_paypal_notify=1"), $ipn, array ("timeout" => 20));
  301. }
  302.  
  303. else if (!$processed) // If nothing was processed, here we add a message to the logs indicating the IPN was ignored.
  304. $google["s2member_log"][] = "Ignoring this IPN request. The transaction does NOT require any action on the part of s2Member.";
  305. }
  306. else // Extensive log reporting here. This is an area where many site owners find trouble. Depending on server configuration; remote HTTPS connections may fail.
  307. {
  308. $google["s2member_log"][] = "Unable to verify POST vars. This is most likely related to an invalid Google® configuration. Please check: s2Member -› Google® Options.";
  309. $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.";
  310. $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.";
  311. $google["s2member_log"][] = var_export ($_REQUEST, true); // Recording _POST + _GET vars for analysis and debugging.
  312. }
  313. /*
  314. We need to log this final event before it occurs, so that is makes it into the log entry.
  315. */
  316. $google["s2member_log"][] = "Sending Google® an XML Notification Acknowlegment w/ original serial number.";
  317. /*
  318. If debugging/logging is enabled; we need to append $google to the log file.
  319. Logging now supports Multisite Networking as well.
  320. */
  321. $logt = c_ws_plugin__s2member_utilities::time_details ();
  322. $logv = c_ws_plugin__s2member_utilities::ver_details ();
  323. $logm = c_ws_plugin__s2member_utilities::mem_details ();
  324. $log4 = $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"] . "\nUser-Agent: " . $_SERVER["HTTP_USER_AGENT"];
  325. $log4 = (is_multisite () && !is_main_site ()) ? ($_log4 = $current_blog->domain . $current_blog->path) . "\n" . $log4 : $log4;
  326. $log2 = (is_multisite () && !is_main_site ()) ? "google-ipn-4-" . trim (preg_replace ("/[^a-z0-9]/i", "-", $_log4), "-") . ".log" : "google-ipn.log";
  327.  
  328. if ($GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["gateway_debug_logs"])
  329. if (is_dir ($logs_dir = $GLOBALS["WS_PLUGIN__"]["s2member"]["c"]["logs_dir"]))
  330. if (is_writable ($logs_dir) && c_ws_plugin__s2member_utils_logs::archive_oversize_log_files ())
  331. file_put_contents ($logs_dir . "/" . $log2,
  332. "LOG ENTRY: ".$logt . "\n" . $logv . "\n" . $logm . "\n" . $log4 . "\n" .
  333. c_ws_plugin__s2member_utils_logs::conceal_private_info(var_export ($google, true)) . "\n\n",
  334. FILE_APPEND);
  335.  
  336. $confirmation = '<?xml version="1.0" encoding="UTF-8"?>';
  337. $confirmation .= '<notification-acknowledgment xmlns="http://checkout.google.com/schema/2"';
  338. $confirmation .= ' serial-number="' . esc_attr (trim (stripslashes ($_REQUEST["serial-number"]))) . '" />';
  339.  
  340. status_header (200); // Send a 200 OK status header.
  341. header ("Content-Type: application/xml"); // Google® expects application/xml here.
  342. while (@ob_end_clean ()); // Clean any existing output buffers.
  343.  
  344. exit ($confirmation); // Exit w/ serial number confirmation.
  345. }
  346. }
  347. }
  348. }
  349. ?>

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

Source Code Documentation