Source Code Documentation


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


  1. <?php
  2. /**
  3. * ClickBank® Return URL 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_return_in"))
  37. {
  38. /**
  39. * ClickBank® Return URL handler (inner processing routines).
  40. *
  41. * @package s2Member\ClickBank
  42. * @since 1.5
  43. */
  44. class c_ws_plugin__s2member_pro_clickbank_return_in
  45. {
  46. /**
  47. * Handles ClickBank® Return 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 redirection.
  55. */
  56. public static function clickbank_return()
  57. {
  58. global /* For Multisite support. */ $current_site, $current_blog;
  59.  
  60. if(!empty($_GET["s2member_pro_clickbank_return"]) && $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["pro_clickbank_username"])
  61. {
  62. if(is_array($clickbank = c_ws_plugin__s2member_pro_clickbank_utilities::clickbank_postvars()) && ($_clickbank = $clickbank))
  63. {
  64. $clickbank["s2member_log"][] = "Return-Data received on: ".date("D M j, Y g:i:s a T");
  65. $clickbank["s2member_log"][] = "s2Member POST vars verified with ClickBank®.";
  66.  
  67. $clickbank["s2member_log"][] = "Sleeping for 5 seconds. Giving ClickBank® a chance to finalize processing.";
  68. sleep /* Sleep here to give ClickBank® a chance to finalize processing. Allows the API call to succeed. */(5);
  69. $clickbank["s2member_log"][] = "Awake. It's ".date("D M j, Y g:i:s a T").". Processing will continue.";
  70.  
  71. if(is_array($order = json_decode(c_ws_plugin__s2member_utils_urls::remote("https://api.clickbank.com/rest/1.3/orders/".$clickbank["cbreceipt"], false, array_merge(c_ws_plugin__s2member_pro_clickbank_utilities::clickbank_api_headers(), array("timeout" => 20))), true)) && ($order = $order["orderData"]))
  72. {
  73. $clickbank["s2member_log"][] = "Order API variables have been obtained from ClickBank®.";
  74.  
  75. $s2vars = c_ws_plugin__s2member_pro_clickbank_utilities::clickbank_parse_s2vars(http_build_query($clickbank, null, "&"), $order["txnType"]);
  76.  
  77. if(isset($s2vars["s2_p1"], $s2vars["s2_p3"]) && /* No Trial defaults to Regular Period. */ $s2vars["s2_p1"] === "0 D")
  78. $s2vars["s2_p1"] = /* Initial Period. No Trial defaults to Regular Period. */ $s2vars["s2_p3"];
  79.  
  80. $clickbank["s2vars"] = /* So they appear in the log entry for this Notification. */ $s2vars;
  81.  
  82. if(strcasecmp($order["firstName"]." ".$order["lastName"], $order["customerDisplayName"]) !== 0 && preg_match("/([^ ]+)( +)([^ ]+)/", $order["customerDisplayName"]))
  83. list($order["firstName"], $order["lastName"]) = preg_split("/ +/", $order["customerDisplayName"], 2);
  84.  
  85. if(preg_match("/^(TEST_)?SALE$/i", $order["txnType"]) && !isset($s2vars["s2_p1"], $s2vars["s2_p3"]))
  86. {
  87. $clickbank["s2member_log"][] = "ClickBank® transaction identified as ( `SALE/STANDARD` ).";
  88. $clickbank["s2member_log"][] = "Return-Data reformulated. Piping through s2Member's core/standard PayPal® processor as `txn_type` ( `web_accept` ).";
  89. $clickbank["s2member_log"][] = "Please check PayPal® RTN logs for further processing details.";
  90.  
  91. $rtn = array(); // Reset.
  92.  
  93. $rtn["txn_type"] = "web_accept";
  94.  
  95. $rtn["txn_id"] = $order["receipt"];
  96. $rtn["custom"] = $s2vars["s2_custom"];
  97.  
  98. $rtn["mc_gross"] = number_format($order["amount"], 2, ".", "");
  99. $rtn["mc_currency"] = strtoupper($order["currency"]);
  100. $rtn["tax"] = "0.00"; // No tax.
  101.  
  102. $rtn["payer_email"] = $order["email"];
  103. $rtn["first_name"] = ucwords(strtolower($order["firstName"]));
  104. $rtn["last_name"] = ucwords(strtolower($order["lastName"]));
  105.  
  106. $rtn["option_name1"] = ($s2vars["s2_referencing"]) ? "Referencing Customer ID" : "Originating Domain";
  107. $rtn["option_selection1"] = ($s2vars["s2_referencing"]) ? $s2vars["s2_referencing"] : $_SERVER["HTTP_HOST"];
  108.  
  109. $rtn["option_name2"] = "Customer IP Address";
  110. $rtn["option_selection2"] = $s2vars["s2_customer_ip"];
  111.  
  112. $rtn["item_number"] = $s2vars["s2_invoice"];
  113. $rtn["item_name"] = $s2vars["s2_desc"];
  114.  
  115. $rtn_q = "&s2member_paypal_proxy=clickbank&s2member_paypal_proxy_use=standard-emails";
  116. if(!empty /* Using a custom Return URL on success? */($_GET["s2member_pro_clickbank_return_success"]))
  117. $rtn_q .= "&s2member_paypal_return_success=".rawurlencode(trim(stripslashes($_GET["s2member_pro_clickbank_return_success"])));
  118.  
  119. $rtn_r = add_query_arg(urlencode_deep($rtn), site_url("/?s2member_pro_clickbank_return&s2member_paypal_return=1".$rtn_q));
  120. $rtn_r = c_ws_plugin__s2member_utils_urls::add_s2member_sig($rtn_r, "s2member_paypal_proxy_verification");
  121.  
  122. $clickbank["s2member_log"][] = /* Log the full Return redirection URL here. */ $rtn_r;
  123.  
  124. wp_redirect /* Proxy this through s2Member's core PayPal® processor. */($rtn_r);
  125. }
  126. else if(preg_match("/^(TEST_)?SALE$/i", $order["txnType"]) && isset($s2vars["s2_p1"], $s2vars["s2_p3"]))
  127. {
  128. $clickbank["s2member_log"][] = "ClickBank® transaction identified as ( `SALE/RECURRING` ).";
  129. $clickbank["s2member_log"][] = "Return-Data reformulated. Piping through s2Member's core/standard PayPal® processor as `txn_type` ( `subscr_signup` ).";
  130. $clickbank["s2member_log"][] = "Please check PayPal® RTN logs for further processing details.";
  131.  
  132. $rtn = array(); // Reset.
  133.  
  134. $rtn["txn_type"] = "subscr_signup";
  135. $rtn["subscr_id"] = $s2vars["s2_subscr_id"];
  136. $rtn["recurring"] = ($order["futurePayments"] > 1) ? "1" : "0";
  137.  
  138. $rtn["txn_id"] = $order["receipt"];
  139. $rtn["custom"] = $s2vars["s2_custom"];
  140.  
  141. $rtn["period1"] = $s2vars["s2_p1"];
  142. $rtn["period3"] = $s2vars["s2_p3"];
  143.  
  144. $rtn["mc_amount1"] = number_format($order["amount"], 2, ".", "");
  145. $rtn["mc_amount3"] = @number_format($order["rebillAmount"], 2, ".", "");
  146.  
  147. $rtn["mc_currency"] = strtoupper($order["currency"]);
  148. $rtn["tax"] = "0.00"; // No tax.
  149.  
  150. $rtn["payer_email"] = $order["email"];
  151. $rtn["first_name"] = ucwords(strtolower($order["firstName"]));
  152. $rtn["last_name"] = ucwords(strtolower($order["lastName"]));
  153.  
  154. $rtn["option_name1"] = ($s2vars["s2_referencing"]) ? "Referencing Customer ID" : "Originating Domain";
  155. $rtn["option_selection1"] = ($s2vars["s2_referencing"]) ? $s2vars["s2_referencing"] : $_SERVER["HTTP_HOST"];
  156.  
  157. $rtn["option_name2"] = "Customer IP Address";
  158. $rtn["option_selection2"] = $s2vars["s2_customer_ip"];
  159.  
  160. $rtn["item_number"] = $s2vars["s2_invoice"];
  161. $rtn["item_name"] = $s2vars["s2_desc"];
  162.  
  163. $rtn_q = "&s2member_paypal_proxy=clickbank&s2member_paypal_proxy_use=standard-emails";
  164. if(!empty /* Using a custom Return URL on success? */($_GET["s2member_pro_clickbank_return_success"]))
  165. $rtn_q .= "&s2member_paypal_return_success=".rawurlencode(trim(stripslashes($_GET["s2member_pro_clickbank_return_success"])));
  166.  
  167. $rtn_r = add_query_arg(urlencode_deep($rtn), site_url("/?s2member_pro_clickbank_return&s2member_paypal_return=1".$rtn_q));
  168. $rtn_r = c_ws_plugin__s2member_utils_urls::add_s2member_sig($rtn_r, "s2member_paypal_proxy_verification");
  169.  
  170. $clickbank["s2member_log"][] = /* Log the full Return redirection URL here. */ $rtn_r;
  171.  
  172. wp_redirect /* Proxy this through s2Member's core PayPal® processor. */($rtn_r);
  173. }
  174. else // Else, we were unable to determine the txnType that is being handled here.
  175. {
  176. $clickbank["s2member_log"][] = "Unexpected txnType. The ClickBank® txnType did not match a required action.";
  177.  
  178. $clickbank["s2member_log"][] = "Redirecting Customer to the Home Page, due to an error that occurred.";
  179.  
  180. echo '<script type="text/javascript">'."\n";
  181. echo "alert('".c_ws_plugin__s2member_utils_strings::esc_js_sq(_x("ERROR: Unexpected txnType. Please contact Support for assistance.\n\nThe ClickBank® txnType did not match a required action.", "s2member-front", "s2member"))."');"."\n";
  182. echo "window.location = '".c_ws_plugin__s2member_utils_strings::esc_js_sq(home_url("/"))."';";
  183. echo '</script>'."\n";
  184. }
  185. }
  186. else // Extensive log reporting here. Sometimes it takes a few seconds for the ClickBank® API to receive data for new orders. This is here in case that happens.
  187. {
  188. $clickbank["s2member_log"][] = "Unable to obtain API vars. The ClickBank® API may NOT have data for this order yet. Or, your ClickBank® API Keys are NOT configured properly under `s2Member -› ClickBank® Options`.";
  189. $clickbank["s2member_log"][] = var_export($_REQUEST, true); // Recording ``$_POST`` + ``$_GET`` vars for analysis and debugging.
  190.  
  191. $clickbank["s2member_log"][] = "Return-Data reformulated. Piping through s2Member's core/standard PayPal® processor with `proxy_use` ( `ty-email` ).";
  192. $clickbank["s2member_log"][] = "Please check PayPal® RTN logs for further processing details.";
  193.  
  194. $rtn_q = "&s2member_paypal_proxy=clickbank&s2member_paypal_proxy_use=standard-emails,ty-email";
  195. if(!empty /* Using a custom Return URL on success? */($_GET["s2member_pro_clickbank_return_success"]))
  196. $rtn_q .= "&s2member_paypal_return_success=".rawurlencode(trim(stripslashes($_GET["s2member_pro_clickbank_return_success"])));
  197.  
  198. $rtn_r = site_url("/?s2member_pro_clickbank_return&s2member_paypal_return=1".$rtn_q);
  199. $rtn_r = c_ws_plugin__s2member_utils_urls::add_s2member_sig($rtn_r, "s2member_paypal_proxy_verification");
  200.  
  201. $clickbank["s2member_log"][] = /* Log the full Return redirection URL here. */ $rtn_r;
  202.  
  203. wp_redirect /* Proxy this through s2Member's core PayPal® processor. */($rtn_r);
  204. }
  205. }
  206. else // Extensive log reporting here. This is an area where many site owners find trouble. Depending on server configuration; remote HTTPS connections may fail.
  207. {
  208. $clickbank["s2member_log"][] = "Unable to verify POST vars. This is most likely related to an invalid ClickBank® configuration. Please check: s2Member -› ClickBank® Options.";
  209. $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.";
  210. $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.";
  211. $clickbank["s2member_log"][] = var_export($_REQUEST, true); // Recording _POST + _GET vars for analysis and debugging.
  212.  
  213. $clickbank["s2member_log"][] = "Redirecting Customer to the Home Page, due to an error that occurred.";
  214.  
  215. echo '<script type="text/javascript">'."\n";
  216. echo "alert('".c_ws_plugin__s2member_utils_strings::esc_js_sq(_x("ERROR: Unable to verify POST vars. Please contact Support for assistance.\n\nThis is most likely related to an invalid ClickBank® configuration. If you are the site owner, please check: s2Member -› ClickBank® Options.", "s2member-front", "s2member"))."');"."\n";
  217. echo "window.location = '".c_ws_plugin__s2member_utils_strings::esc_js_sq(home_url("/"))."';";
  218. echo '</script>'."\n";
  219. }
  220.  
  221. $logt = c_ws_plugin__s2member_utilities::time_details ();
  222. $logv = c_ws_plugin__s2member_utilities::ver_details();
  223. $logm = c_ws_plugin__s2member_utilities::mem_details();
  224. $log4 = $_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"]."\nUser-Agent: ".$_SERVER["HTTP_USER_AGENT"];
  225. $log4 = (is_multisite() && !is_main_site()) ? ($_log4 = $current_blog->domain.$current_blog->path)."\n".$log4 : $log4;
  226. $log2 = (is_multisite() && !is_main_site()) ? "clickbank-rtn-4-".trim(preg_replace("/[^a-z0-9]/i", "-", $_log4), "-").".log" : "clickbank-rtn.log";
  227.  
  228. if($GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["gateway_debug_logs"])
  229. if(is_dir($logs_dir = $GLOBALS["WS_PLUGIN__"]["s2member"]["c"]["logs_dir"]))
  230. if(is_writable($logs_dir) && c_ws_plugin__s2member_utils_logs::archive_oversize_log_files())
  231. file_put_contents($logs_dir."/".$log2,
  232. "LOG ENTRY: ".$logt . "\n" . $logv."\n".$logm."\n".$log4."\n".
  233. c_ws_plugin__s2member_utils_logs::conceal_private_info(var_export($clickbank, true))."\n\n",
  234. FILE_APPEND);
  235.  
  236. exit(); // Exit now.
  237. }
  238. }
  239. }
  240. }
  241. ?>

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

Source Code Documentation