Source Code Documentation


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


  1. <?php
  2. /**
  3. * ClickBank® 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\ClickBank
  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_clickbank_notify_in"))
  37. {
  38. /**
  39. * ClickBank® IPN Handler (inner processing routines).
  40. *
  41. * @package s2Member\ClickBank
  42. * @since 1.5
  43. */
  44. class c_ws_plugin__s2member_pro_clickbank_notify_in
  45. {
  46. /**
  47. * Handles ClickBank® IPN URL processing.
  48. *
  49. * @package s2Member\ClickBank
  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 clickbank_notify ()
  57. {
  58. global /* For Multisite support. */ $current_site, $current_blog;
  59.  
  60. if (!empty ($_GET["s2member_pro_clickbank_notify"]) && $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["pro_clickbank_username"])
  61. {
  62. @ignore_user_abort (true); // Continue processing even if/when connection is broken by the sender.
  63.  
  64. if (is_array ($clickbank = c_ws_plugin__s2member_pro_clickbank_utilities::clickbank_postvars ()) && ($_clickbank = $clickbank))
  65. {
  66. $clickbank["s2member_log"][] = "IPN received on: " . date ("D M j, Y g:i:s a T");
  67. $clickbank["s2member_log"][] = "s2Member POST vars verified with ClickBank®.";
  68.  
  69. $s2vars = c_ws_plugin__s2member_pro_clickbank_utilities::clickbank_parse_s2vars ($clickbank["cvendthru"], $clickbank["ctransaction"]);
  70.  
  71. if (isset ($s2vars["s2_p1"], $s2vars["s2_p3"]) && /* No Trial defaults to Regular Period. */ $s2vars["s2_p1"] === "0 D")
  72. $s2vars["s2_p1"] = /* Initial Period. No Trial defaults to Regular Period. */ $s2vars["s2_p3"];
  73.  
  74. $clickbank["s2vars"] = /* So they appear in the log entry for this Notification. */ $s2vars;
  75.  
  76. if (strcasecmp ($clickbank["ccustfirstname"] . " " . $clickbank["ccustlastname"], $clickbank["ccustfullname"]) !== 0 && preg_match ("/(?:[^ ]+)(?: +)(?:[^ ]+)/", $clickbank["ccustfullname"]))
  77. list ($clickbank["ccustfirstname"], $clickbank["ccustlastname"]) = preg_split ("/ +/", $clickbank["ccustfullname"], 2);
  78.  
  79. if (preg_match ("/^(?:TEST_)?SALE$/i", $clickbank["ctransaction"]) && preg_match ("/^STANDARD$/i", $clickbank["cprodtype"]))
  80. {
  81. $clickbank["s2member_log"][] = "ClickBank® transaction identified as ( `SALE/STANDARD` ).";
  82. $clickbank["s2member_log"][] = "IPN reformulated. Piping through s2Member's core/standard PayPal® processor as `txn_type` ( `web_accept` ).";
  83. $clickbank["s2member_log"][] = "Please check PayPal® IPN logs for further processing details.";
  84.  
  85. $processing = $processed = true;
  86. $ipn = array (); // Reset.
  87.  
  88. $ipn["txn_type"] = "web_accept";
  89.  
  90. $ipn["txn_id"] = $clickbank["ctransreceipt"];
  91.  
  92. $ipn["custom"] = $s2vars["s2_custom"];
  93.  
  94. $ipn["mc_gross"] = number_format ($clickbank["corderamount"] / 100, 2, ".", "");
  95. $ipn["mc_currency"] = strtoupper ($clickbank["ccurrency"]);
  96. $ipn["tax"] = number_format ("0.00", 2, ".", "");
  97.  
  98. $ipn["payer_email"] = $clickbank["ccustemail"];
  99. $ipn["first_name"] = ucwords (strtolower ($clickbank["ccustfirstname"]));
  100. $ipn["last_name"] = ucwords (strtolower ($clickbank["ccustlastname"]));
  101.  
  102. $ipn["option_name1"] = ($s2vars["s2_referencing"]) ? "Referencing Customer ID" : "Originating Domain";
  103. $ipn["option_selection1"] = ($s2vars["s2_referencing"]) ? $s2vars["s2_referencing"] : $_SERVER["HTTP_HOST"];
  104.  
  105. $ipn["option_name2"] = "Customer IP Address";
  106. $ipn["option_selection2"] = $s2vars["s2_customer_ip"];
  107.  
  108. $ipn["item_number"] = $s2vars["s2_invoice"];
  109. $ipn["item_name"] = $s2vars["s2_desc"];
  110.  
  111. $ipn["s2member_paypal_proxy"] = "clickbank";
  112. $ipn["s2member_paypal_proxy_use"] = "standard-emails";
  113. $ipn["s2member_paypal_proxy_verification"] = c_ws_plugin__s2member_paypal_utilities::paypal_proxy_key_gen();
  114.  
  115. c_ws_plugin__s2member_utils_urls::remote (site_url ("/?s2member_paypal_notify=1"), $ipn, array ("timeout" => 20));
  116. }
  117.  
  118. else if (preg_match ("/^(?:TEST_)?SALE$/i", $clickbank["ctransaction"]) && preg_match ("/^RECURRING$/i", $clickbank["cprodtype"]))
  119. {
  120. $clickbank["s2member_log"][] = "ClickBank® transaction identified as ( `SALE/RECURRING` ).";
  121. $clickbank["s2member_log"][] = "IPN reformulated. Piping through s2Member's core/standard PayPal® processor as `txn_type` ( `subscr_signup` ).";
  122. $clickbank["s2member_log"][] = "Please check PayPal® IPN logs for further processing details.";
  123.  
  124. $processing = $processed = true;
  125. $ipn = array (); // Reset.
  126.  
  127. $ipn["txn_type"] = "subscr_signup";
  128. $ipn["subscr_id"] = $s2vars["s2_subscr_id"];
  129. $ipn["recurring"] = ($clickbank["cfuturepayments"] > 1) ? "1" : "0";
  130.  
  131. $ipn["txn_id"] = $clickbank["ctransreceipt"];
  132.  
  133. $ipn["custom"] = $s2vars["s2_custom"];
  134.  
  135. $ipn["period1"] = $s2vars["s2_p1"];
  136. $ipn["period3"] = $s2vars["s2_p3"];
  137.  
  138. $ipn["mc_amount1"] = number_format ($clickbank["corderamount"] / 100, 2, ".", "");
  139. $ipn["mc_amount3"] = number_format ($clickbank["crebillamnt"] / 100, 2, ".", "");
  140.  
  141. $ipn["mc_gross"] = (preg_match ("/^[1-9]/", $ipn["period1"])) ? $ipn["mc_amount1"] : $ipn["mc_amount3"];
  142.  
  143. $ipn["mc_currency"] = strtoupper ($clickbank["ccurrency"]);
  144. $ipn["tax"] = number_format ("0.00", 2, ".", "");
  145.  
  146. $ipn["payer_email"] = $clickbank["ccustemail"];
  147. $ipn["first_name"] = ucwords (strtolower ($clickbank["ccustfirstname"]));
  148. $ipn["last_name"] = ucwords (strtolower ($clickbank["ccustlastname"]));
  149.  
  150. $ipn["option_name1"] = ($s2vars["s2_referencing"]) ? "Referencing Customer ID" : "Originating Domain";
  151. $ipn["option_selection1"] = ($s2vars["s2_referencing"]) ? $s2vars["s2_referencing"] : $_SERVER["HTTP_HOST"];
  152.  
  153. $ipn["option_name2"] = "Customer IP Address";
  154. $ipn["option_selection2"] = $s2vars["s2_customer_ip"];
  155.  
  156. $ipn["item_number"] = $s2vars["s2_invoice"];
  157. $ipn["item_name"] = $s2vars["s2_desc"];
  158.  
  159. $ipn["s2member_paypal_proxy"] = "clickbank";
  160. $ipn["s2member_paypal_proxy_use"] = "standard-emails";
  161. $ipn["s2member_paypal_proxy_use"] .= ($ipn["mc_gross"] > 0) ? ",subscr-signup-as-subscr-payment" : "";
  162. $ipn["s2member_paypal_proxy_verification"] = c_ws_plugin__s2member_paypal_utilities::paypal_proxy_key_gen();
  163.  
  164. c_ws_plugin__s2member_utils_urls::remote (site_url ("/?s2member_paypal_notify=1"), $ipn, array ("timeout" => 20));
  165. }
  166.  
  167. else if (preg_match ("/^(?:TEST_)?BILL$/i", $clickbank["ctransaction"]) && preg_match ("/^RECURRING$/i", $clickbank["cprodtype"]))
  168. {
  169. $clickbank["s2member_log"][] = "ClickBank® transaction identified as ( `BILL/RECURRING` ).";
  170. $clickbank["s2member_log"][] = "IPN reformulated. Piping through s2Member's core/standard PayPal® processor as `txn_type` ( `subscr_payment` ).";
  171. $clickbank["s2member_log"][] = "Please check PayPal® IPN logs for further processing details.";
  172.  
  173. $processing = $processed = true;
  174. $ipn = array (); // Reset.
  175.  
  176. $ipn["txn_type"] = "subscr_payment";
  177. $ipn["subscr_id"] = $s2vars["s2_subscr_id"];
  178.  
  179. $ipn["txn_id"] = $clickbank["ctransreceipt"];
  180.  
  181. $ipn["custom"] = $s2vars["s2_custom"];
  182.  
  183. $ipn["mc_gross"] = number_format ($clickbank["corderamount"] / 100, 2, ".", "");
  184. $ipn["mc_currency"] = strtoupper ($clickbank["ccurrency"]);
  185. $ipn["tax"] = number_format ("0.00", 2, ".", "");
  186.  
  187. $ipn["payer_email"] = $clickbank["ccustemail"];
  188. $ipn["first_name"] = ucwords (strtolower ($clickbank["ccustfirstname"]));
  189. $ipn["last_name"] = ucwords (strtolower ($clickbank["ccustlastname"]));
  190.  
  191. $ipn["option_name1"] = ($s2vars["s2_referencing"]) ? "Referencing Customer ID" : "Originating Domain";
  192. $ipn["option_selection1"] = ($s2vars["s2_referencing"]) ? $s2vars["s2_referencing"] : $_SERVER["HTTP_HOST"];
  193.  
  194. $ipn["option_name2"] = "Customer IP Address";
  195. $ipn["option_selection2"] = $s2vars["s2_customer_ip"];
  196.  
  197. $ipn["item_number"] = $s2vars["s2_invoice"];
  198. $ipn["item_name"] = $s2vars["s2_desc"];
  199.  
  200. $ipn["s2member_paypal_proxy"] = "clickbank";
  201. $ipn["s2member_paypal_proxy_use"] = "standard-emails";
  202. $ipn["s2member_paypal_proxy_verification"] = c_ws_plugin__s2member_paypal_utilities::paypal_proxy_key_gen();
  203.  
  204. c_ws_plugin__s2member_utils_urls::remote (site_url ("/?s2member_paypal_notify=1"), $ipn, array ("timeout" => 20));
  205. }
  206.  
  207. else if (preg_match ("/^(?:TEST_)?(?:RFND|CGBK|INSF)$/i", $clickbank["ctransaction"])) // Product Type irrelevant here; checked below.
  208. {
  209. $clickbank["s2member_log"][] = "ClickBank® transaction identified as ( `RFND|CGBK|INSF` ).";
  210. $clickbank["s2member_log"][] = "IPN reformulated. Piping through s2Member's core/standard PayPal® processor as `payment_status` ( `refunded|reversed` ).";
  211. $clickbank["s2member_log"][] = "Please check PayPal® IPN logs for further processing details.";
  212.  
  213. $processing = $processed = true;
  214. $ipn = array (); // Reset.
  215.  
  216. $ipn["payment_status"] = (preg_match ("/^(?:TEST_)?RFND$/", $clickbank["ctransaction"])) ? "refunded" : "reversed";
  217.  
  218. $ipn["parent_txn_id"] = (preg_match ("/^RECURRING$/i", $clickbank["cprodtype"]) && $s2vars["s2_subscr_id"]) ? $s2vars["s2_subscr_id"] : $clickbank["ctransreceipt"];
  219.  
  220. $ipn["custom"] = $s2vars["s2_custom"];
  221.  
  222. $ipn["mc_fee"] = "-" . number_format ("0.00", 2, ".", "");
  223. $ipn["mc_gross"] = "-" . number_format (abs ($clickbank["corderamount"]) / 100, 2, ".", "");
  224. $ipn["mc_currency"] = strtoupper ($clickbank["ccurrency"]);
  225. $ipn["tax"] = "-" . number_format ("0.00", 2, ".", "");
  226.  
  227. $ipn["payer_email"] = $clickbank["ccustemail"];
  228. $ipn["first_name"] = ucwords (strtolower ($clickbank["ccustfirstname"]));
  229. $ipn["last_name"] = ucwords (strtolower ($clickbank["ccustlastname"]));
  230.  
  231. $ipn["option_name1"] = ($s2vars["s2_referencing"]) ? "Referencing Customer ID" : "Originating Domain";
  232. $ipn["option_selection1"] = ($s2vars["s2_referencing"]) ? $s2vars["s2_referencing"] : $_SERVER["HTTP_HOST"];
  233.  
  234. $ipn["option_name2"] = "Customer IP Address";
  235. $ipn["option_selection2"] = $s2vars["s2_customer_ip"];
  236.  
  237. $ipn["item_number"] = $s2vars["s2_invoice"];
  238. $ipn["item_name"] = $s2vars["s2_desc"];
  239.  
  240. $ipn["s2member_paypal_proxy"] = "clickbank";
  241. $ipn["s2member_paypal_proxy_use"] = "standard-emails";
  242. $ipn["s2member_paypal_proxy_verification"] = c_ws_plugin__s2member_paypal_utilities::paypal_proxy_key_gen();
  243.  
  244. c_ws_plugin__s2member_utils_urls::remote (site_url ("/?s2member_paypal_notify=1"), $ipn, array ("timeout" => 20));
  245. }
  246.  
  247. if ( // Here we handle Recurring cancellations, and/or EOT (End Of Term) through $clickbank["crebillstatus"].
  248. (preg_match ("/^(?:TEST_)?(?:SALE|BILL)$/i", $clickbank["ctransaction"]) && preg_match ("/^RECURRING$/i", $clickbank["cprodtype"]) && (preg_match ("/^COMPLETED$/i", $clickbank["crebillstatus"]) || $clickbank["cfuturepayments"] <= 0))
  249. || (preg_match ("/^(?:TEST_)?CANCEL-REBILL$/i", $clickbank["ctransaction"]) && preg_match ("/^RECURRING$/i", $clickbank["cprodtype"])))
  250. {
  251. $clickbank["s2member_log"][] = "ClickBank® transaction identified as ( `RECURRING/COMPLETED` or `CANCEL-REBILL` ).";
  252. $clickbank["s2member_log"][] = "IPN reformulated. Piping through s2Member's core/standard PayPal® processor as `txn_type` ( `subscr_cancel` ).";
  253. $clickbank["s2member_log"][] = "Please check PayPal® IPN logs for further processing details.";
  254.  
  255. $processing = $processed = true;
  256. $ipn = array (); // Reset.
  257.  
  258. $ipn["txn_type"] = "subscr_cancel";
  259. $ipn["subscr_id"] = $s2vars["s2_subscr_id"];
  260.  
  261. $ipn["custom"] = $s2vars["s2_custom"];
  262.  
  263. $ipn["period1"] = $s2vars["s2_p1"];
  264. $ipn["period3"] = $s2vars["s2_p3"];
  265.  
  266. $ipn["payer_email"] = $clickbank["ccustemail"];
  267. $ipn["first_name"] = ucwords (strtolower ($clickbank["ccustfirstname"]));
  268. $ipn["last_name"] = ucwords (strtolower ($clickbank["ccustlastname"]));
  269.  
  270. $ipn["option_name1"] = ($s2vars["s2_referencing"]) ? "Referencing Customer ID" : "Originating Domain";
  271. $ipn["option_selection1"] = ($s2vars["s2_referencing"]) ? $s2vars["s2_referencing"] : $_SERVER["HTTP_HOST"];
  272.  
  273. $ipn["option_name2"] = "Customer IP Address";
  274. $ipn["option_selection2"] = $s2vars["s2_customer_ip"];
  275.  
  276. $ipn["item_number"] = $s2vars["s2_invoice"];
  277. $ipn["item_name"] = $s2vars["s2_desc"];
  278.  
  279. $ipn["s2member_paypal_proxy"] = "clickbank";
  280. $ipn["s2member_paypal_proxy_use"] = "standard-emails";
  281. $ipn["s2member_paypal_proxy_verification"] = c_ws_plugin__s2member_paypal_utilities::paypal_proxy_key_gen();
  282.  
  283. c_ws_plugin__s2member_utils_urls::remote (site_url ("/?s2member_paypal_notify=1"), $ipn, array ("timeout" => 20));
  284. }
  285.  
  286. if (!$processed) // If nothing was processed, here we add a message to the logs indicating the IPN was ignored.
  287. $clickbank["s2member_log"][] = "Ignoring this IPN request. The transaction does NOT require any action on the part of s2Member.";
  288. }
  289. else // Extensive log reporting here. This is an area where many site owners find trouble. Depending on server configuration; remote HTTPS connections may fail.
  290. {
  291. $clickbank["s2member_log"][] = "Unable to verify POST vars. This is most likely related to an invalid ClickBank® configuration. Please check: s2Member -› ClickBank® Options.";
  292. $clickbank["s2member_log"][] = "If you're absolutely SURE that your ClickBank® 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 ClickBank® over an HTTPS connection.";
  293. $clickbank["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.";
  294. $clickbank["s2member_log"][] = var_export ($_REQUEST, true); // Recording _POST + _GET vars for analysis and debugging.
  295. }
  296. /*
  297. If debugging/logging is enabled; we need to append $clickbank to the log file.
  298. Logging now supports Multisite Networking as well.
  299. */
  300. $logt = c_ws_plugin__s2member_utilities::time_details ();
  301. $logv = c_ws_plugin__s2member_utilities::ver_details ();
  302. $logm = c_ws_plugin__s2member_utilities::mem_details ();
  303. $log4 = $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"] . "\nUser-Agent: " . $_SERVER["HTTP_USER_AGENT"];
  304. $log4 = (is_multisite () && !is_main_site ()) ? ($_log4 = $current_blog->domain . $current_blog->path) . "\n" . $log4 : $log4;
  305. $log2 = (is_multisite () && !is_main_site ()) ? "clickbank-ipn-4-" . trim (preg_replace ("/[^a-z0-9]/i", "-", $_log4), "-") . ".log" : "clickbank-ipn.log";
  306.  
  307. if ($GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["gateway_debug_logs"])
  308. if (is_dir ($logs_dir = $GLOBALS["WS_PLUGIN__"]["s2member"]["c"]["logs_dir"]))
  309. if (is_writable ($logs_dir) && c_ws_plugin__s2member_utils_logs::archive_oversize_log_files ())
  310. file_put_contents ($logs_dir . "/" . $log2,
  311. "LOG ENTRY: ".$logt . "\n" . $logv . "\n" . $logm . "\n" . $log4 . "\n" .
  312. c_ws_plugin__s2member_utils_logs::conceal_private_info(var_export ($clickbank, true)) . "\n\n",
  313. FILE_APPEND);
  314.  
  315. status_header (200); // Send a 200 OK status header.
  316. header ("Content-Type: text/plain; charset=UTF-8"); // Content-Type text/plain with UTF-8.
  317. while (@ob_end_clean ()); // Clean any existing output buffers.
  318.  
  319. exit (); // Exit now.
  320. }
  321. }
  322. }
  323. }
  324. ?>

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

Source Code Documentation