Source Code Documentation


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 $current_site, $current_blog; /* For Multisite support. */
  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_q = "&s2member_paypal_proxy=clickbank&s2member_paypal_proxy_use=standard-emails";
  112. $ipn_q .= "&s2member_paypal_proxy_verification=" . urlencode (c_ws_plugin__s2member_paypal_utilities::paypal_proxy_key_gen ());
  113. /**/
  114. c_ws_plugin__s2member_utils_urls::remote (site_url ("/?s2member_paypal_notify=1" . $ipn_q), $ipn, array ("timeout" => 20));
  115. }
  116. /**/
  117. else if (preg_match ("/^(?:TEST_)?SALE$/i", $clickbank["ctransaction"]) && preg_match ("/^RECURRING$/i", $clickbank["cprodtype"]))
  118. {
  119. $clickbank["s2member_log"][] = "ClickBank® transaction identified as ( `SALE/RECURRING` ).";
  120. $clickbank["s2member_log"][] = "IPN reformulated. Piping through s2Member's core/standard PayPal® processor as `txn_type` ( `subscr_signup` ).";
  121. $clickbank["s2member_log"][] = "Please check PayPal® IPN logs for further processing details.";
  122. /**/
  123. $processing = $processed = true;
  124. $ipn = array (); /* Reset. */
  125. /**/
  126. $ipn["txn_type"] = "subscr_signup";
  127. $ipn["subscr_id"] = $s2vars["s2_subscr_id"];
  128. $ipn["recurring"] = ($clickbank["cfuturepayments"] > 1) ? "1" : "0";
  129. /**/
  130. $ipn["txn_id"] = $clickbank["ctransreceipt"];
  131. /**/
  132. $ipn["custom"] = $s2vars["s2_custom"];
  133. /**/
  134. $ipn["period1"] = $s2vars["s2_p1"];
  135. $ipn["period3"] = $s2vars["s2_p3"];
  136. /**/
  137. $ipn["mc_amount1"] = number_format ($clickbank["corderamount"] / 100, 2, ".", "");
  138. $ipn["mc_amount3"] = number_format ($clickbank["crebillamnt"] / 100, 2, ".", "");
  139. /**/
  140. $ipn["mc_gross"] = (preg_match ("/^[1-9]/", $ipn["period1"])) ? $ipn["mc_amount1"] : $ipn["mc_amount3"];
  141. /**/
  142. $ipn["mc_currency"] = strtoupper ($clickbank["ccurrency"]);
  143. $ipn["tax"] = number_format ("0.00", 2, ".", "");
  144. /**/
  145. $ipn["payer_email"] = $clickbank["ccustemail"];
  146. $ipn["first_name"] = ucwords (strtolower ($clickbank["ccustfirstname"]));
  147. $ipn["last_name"] = ucwords (strtolower ($clickbank["ccustlastname"]));
  148. /**/
  149. $ipn["option_name1"] = ($s2vars["s2_referencing"]) ? "Referencing Customer ID" : "Originating Domain";
  150. $ipn["option_selection1"] = ($s2vars["s2_referencing"]) ? $s2vars["s2_referencing"] : $_SERVER["HTTP_HOST"];
  151. /**/
  152. $ipn["option_name2"] = "Customer IP Address";
  153. $ipn["option_selection2"] = $s2vars["s2_customer_ip"];
  154. /**/
  155. $ipn["item_number"] = $s2vars["s2_invoice"];
  156. $ipn["item_name"] = $s2vars["s2_desc"];
  157. /**/
  158. $ipn_q = "&s2member_paypal_proxy=clickbank&s2member_paypal_proxy_use=standard-emails";
  159. $ipn_q .= ($ipn["mc_gross"] > 0) ? ",subscr-signup-as-subscr-payment" : ""; /* Use as first payment? */
  160. $ipn_q .= "&s2member_paypal_proxy_verification=" . urlencode (c_ws_plugin__s2member_paypal_utilities::paypal_proxy_key_gen ());
  161. /**/
  162. c_ws_plugin__s2member_utils_urls::remote (site_url ("/?s2member_paypal_notify=1" . $ipn_q), $ipn, array ("timeout" => 20));
  163. }
  164. /**/
  165. else if (preg_match ("/^(?:TEST_)?BILL$/i", $clickbank["ctransaction"]) && preg_match ("/^RECURRING$/i", $clickbank["cprodtype"]))
  166. {
  167. $clickbank["s2member_log"][] = "ClickBank® transaction identified as ( `BILL/RECURRING` ).";
  168. $clickbank["s2member_log"][] = "IPN reformulated. Piping through s2Member's core/standard PayPal® processor as `txn_type` ( `subscr_payment` ).";
  169. $clickbank["s2member_log"][] = "Please check PayPal® IPN logs for further processing details.";
  170. /**/
  171. $processing = $processed = true;
  172. $ipn = array (); /* Reset. */
  173. /**/
  174. $ipn["txn_type"] = "subscr_payment";
  175. $ipn["subscr_id"] = $s2vars["s2_subscr_id"];
  176. /**/
  177. $ipn["txn_id"] = $clickbank["ctransreceipt"];
  178. /**/
  179. $ipn["custom"] = $s2vars["s2_custom"];
  180. /**/
  181. $ipn["mc_gross"] = number_format ($clickbank["corderamount"] / 100, 2, ".", "");
  182. $ipn["mc_currency"] = strtoupper ($clickbank["ccurrency"]);
  183. $ipn["tax"] = number_format ("0.00", 2, ".", "");
  184. /**/
  185. $ipn["payer_email"] = $clickbank["ccustemail"];
  186. $ipn["first_name"] = ucwords (strtolower ($clickbank["ccustfirstname"]));
  187. $ipn["last_name"] = ucwords (strtolower ($clickbank["ccustlastname"]));
  188. /**/
  189. $ipn["option_name1"] = ($s2vars["s2_referencing"]) ? "Referencing Customer ID" : "Originating Domain";
  190. $ipn["option_selection1"] = ($s2vars["s2_referencing"]) ? $s2vars["s2_referencing"] : $_SERVER["HTTP_HOST"];
  191. /**/
  192. $ipn["option_name2"] = "Customer IP Address";
  193. $ipn["option_selection2"] = $s2vars["s2_customer_ip"];
  194. /**/
  195. $ipn["item_number"] = $s2vars["s2_invoice"];
  196. $ipn["item_name"] = $s2vars["s2_desc"];
  197. /**/
  198. $ipn_q = "&s2member_paypal_proxy=clickbank&s2member_paypal_proxy_use=standard-emails";
  199. $ipn_q .= "&s2member_paypal_proxy_verification=" . urlencode (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_q), $ipn, array ("timeout" => 20));
  202. }
  203. /**/
  204. else if (preg_match ("/^(?:TEST_)?(?:RFND|CGBK|INSF)$/i", $clickbank["ctransaction"])) /* Product Type irrelevant here; checked below. */
  205. {
  206. $clickbank["s2member_log"][] = "ClickBank® transaction identified as ( `RFND|CGBK|INSF` ).";
  207. $clickbank["s2member_log"][] = "IPN reformulated. Piping through s2Member's core/standard PayPal® processor as `payment_status` ( `refunded|reversed` ).";
  208. $clickbank["s2member_log"][] = "Please check PayPal® IPN logs for further processing details.";
  209. /**/
  210. $processing = $processed = true;
  211. $ipn = array (); /* Reset. */
  212. /**/
  213. $ipn["payment_status"] = (preg_match ("/^(?:TEST_)?RFND$/", $clickbank["ctransaction"])) ? "refunded" : "reversed";
  214. /**/
  215. $ipn["parent_txn_id"] = (preg_match ("/^RECURRING$/i", $clickbank["cprodtype"]) && $s2vars["s2_subscr_id"]) ? $s2vars["s2_subscr_id"] : $clickbank["ctransreceipt"];
  216. /**/
  217. $ipn["custom"] = $s2vars["s2_custom"];
  218. /**/
  219. $ipn["mc_fee"] = "-" . number_format ("0.00", 2, ".", "");
  220. $ipn["mc_gross"] = "-" . number_format (abs ($clickbank["corderamount"]) / 100, 2, ".", "");
  221. $ipn["mc_currency"] = strtoupper ($clickbank["ccurrency"]);
  222. $ipn["tax"] = "-" . number_format ("0.00", 2, ".", "");
  223. /**/
  224. $ipn["payer_email"] = $clickbank["ccustemail"];
  225. $ipn["first_name"] = ucwords (strtolower ($clickbank["ccustfirstname"]));
  226. $ipn["last_name"] = ucwords (strtolower ($clickbank["ccustlastname"]));
  227. /**/
  228. $ipn["option_name1"] = ($s2vars["s2_referencing"]) ? "Referencing Customer ID" : "Originating Domain";
  229. $ipn["option_selection1"] = ($s2vars["s2_referencing"]) ? $s2vars["s2_referencing"] : $_SERVER["HTTP_HOST"];
  230. /**/
  231. $ipn["option_name2"] = "Customer IP Address";
  232. $ipn["option_selection2"] = $s2vars["s2_customer_ip"];
  233. /**/
  234. $ipn["item_number"] = $s2vars["s2_invoice"];
  235. $ipn["item_name"] = $s2vars["s2_desc"];
  236. /**/
  237. $ipn_q = "&s2member_paypal_proxy=clickbank&s2member_paypal_proxy_use=standard-emails";
  238. $ipn_q .= "&s2member_paypal_proxy_verification=" . urlencode (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_q), $ipn, array ("timeout" => 20));
  241. }
  242. /**/
  243. if ( /* Here we handle Recurring cancellations, and/or EOT ( End Of Term ) through $clickbank["crebillstatus"]. */
  244. (preg_match ("/^(?:TEST_)?(?:SALE|BILL)$/i", $clickbank["ctransaction"]) && preg_match ("/^RECURRING$/i", $clickbank["cprodtype"]) && (preg_match ("/^COMPLETED$/i", $clickbank["crebillstatus"]) || $clickbank["cfuturepayments"] <= 0))/**/
  245. || (preg_match ("/^(?:TEST_)?CANCEL-REBILL$/i", $clickbank["ctransaction"]) && preg_match ("/^RECURRING$/i", $clickbank["cprodtype"])))
  246. {
  247. $clickbank["s2member_log"][] = "ClickBank® transaction identified as ( `RECURRING/COMPLETED` or `CANCEL-REBILL` ).";
  248. $clickbank["s2member_log"][] = "IPN reformulated. Piping through s2Member's core/standard PayPal® processor as `txn_type` ( `subscr_cancel` ).";
  249. $clickbank["s2member_log"][] = "Please check PayPal® IPN logs for further processing details.";
  250. /**/
  251. $processing = $processed = true;
  252. $ipn = array (); /* Reset. */
  253. /**/
  254. $ipn["txn_type"] = "subscr_cancel";
  255. $ipn["subscr_id"] = $s2vars["s2_subscr_id"];
  256. /**/
  257. $ipn["custom"] = $s2vars["s2_custom"];
  258. /**/
  259. $ipn["period1"] = $s2vars["s2_p1"];
  260. $ipn["period3"] = $s2vars["s2_p3"];
  261. /**/
  262. $ipn["payer_email"] = $clickbank["ccustemail"];
  263. $ipn["first_name"] = ucwords (strtolower ($clickbank["ccustfirstname"]));
  264. $ipn["last_name"] = ucwords (strtolower ($clickbank["ccustlastname"]));
  265. /**/
  266. $ipn["option_name1"] = ($s2vars["s2_referencing"]) ? "Referencing Customer ID" : "Originating Domain";
  267. $ipn["option_selection1"] = ($s2vars["s2_referencing"]) ? $s2vars["s2_referencing"] : $_SERVER["HTTP_HOST"];
  268. /**/
  269. $ipn["option_name2"] = "Customer IP Address";
  270. $ipn["option_selection2"] = $s2vars["s2_customer_ip"];
  271. /**/
  272. $ipn["item_number"] = $s2vars["s2_invoice"];
  273. $ipn["item_name"] = $s2vars["s2_desc"];
  274. /**/
  275. $ipn_q = "&s2member_paypal_proxy=clickbank&s2member_paypal_proxy_use=standard-emails";
  276. $ipn_q .= "&s2member_paypal_proxy_verification=" . urlencode (c_ws_plugin__s2member_paypal_utilities::paypal_proxy_key_gen ());
  277. /**/
  278. c_ws_plugin__s2member_utils_urls::remote (site_url ("/?s2member_paypal_notify=1" . $ipn_q), $ipn, array ("timeout" => 20));
  279. }
  280. /**/
  281. if (!$processed) /* If nothing was processed, here we add a message to the logs indicating the IPN was ignored. */
  282. $clickbank["s2member_log"][] = "Ignoring this IPN request. The transaction does NOT require any action on the part of s2Member.";
  283. }
  284. else /* Extensive log reporting here. This is an area where many site owners find trouble. Depending on server configuration; remote HTTPS connections may fail. */
  285. {
  286. $clickbank["s2member_log"][] = "Unable to verify POST vars. This is most likely related to an invalid ClickBank® configuration. Please check: s2Member -> ClickBank® Options.";
  287. $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.";
  288. $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.";
  289. $clickbank["s2member_log"][] = var_export ($_REQUEST, true); /* Recording _POST + _GET vars for analysis and debugging. */
  290. }
  291. /*
  292. If debugging/logging is enabled; we need to append $clickbank to the log file.
  293. Logging now supports Multisite Networking as well.
  294. */
  295. $logv = c_ws_plugin__s2member_utilities::ver_details ();
  296. $logm = c_ws_plugin__s2member_utilities::mem_details ();
  297. $log4 = $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"] . "\nUser-Agent: " . $_SERVER["HTTP_USER_AGENT"];
  298. $log4 = (is_multisite () && !is_main_site ()) ? ($_log4 = $current_blog->domain . $current_blog->path) . "\n" . $log4 : $log4;
  299. $log2 = (is_multisite () && !is_main_site ()) ? "clickbank-ipn-4-" . trim (preg_replace ("/[^a-z0-9]/i", "-", $_log4), "-") . ".log" : "clickbank-ipn.log";
  300. /**/
  301. if ($GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["gateway_debug_logs"])
  302. if (is_dir ($logs_dir = $GLOBALS["WS_PLUGIN__"]["s2member"]["c"]["logs_dir"]))
  303. if (is_writable ($logs_dir) && c_ws_plugin__s2member_utils_logs::archive_oversize_log_files ())
  304. file_put_contents ($logs_dir . "/" . $log2, $logv . "\n" . $logm . "\n" . $log4 . "\n" . var_export ($clickbank, true) . "\n\n", FILE_APPEND);
  305. /**/
  306. status_header (200); /* Send a 200 OK status header. */
  307. header ("Content-Type: text/plain; charset=utf-8"); /* Content-Type text/plain with UTF-8. */
  308. eval ('while (@ob_end_clean ());'); /* End/clean all output buffers that may or may not exist. */
  309. /**/
  310. exit (); /* Exit now. */
  311. }
  312. }
  313. }
  314. }
  315. ?>

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

Source Code Documentation