Source Code Documentation


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


  1. <?php
  2. /**
  3. * AliPay® 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\AliPay
  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_alipay_notify_in"))
  37. {
  38. /**
  39. * AliPay® IPN Handler (inner processing routines).
  40. *
  41. * @package s2Member\AliPay
  42. * @since 1.5
  43. */
  44. class c_ws_plugin__s2member_pro_alipay_notify_in
  45. {
  46. /**
  47. * Handles AliPay® IPN URL processing.
  48. *
  49. * @package s2Member\AliPay
  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 alipay_notify ()
  57. {
  58. global /* For Multisite support. */ $current_site, $current_blog;
  59.  
  60. if (!empty ($_POST["notify_type"]) && preg_match ("/^trade_status_sync$/i", $_POST["notify_type"]) && $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["pro_alipay_seller_email"])
  61. {
  62. @ignore_user_abort (true); // Continue processing even if/when connection is broken by the sender.
  63.  
  64. if (is_array ($alipay = c_ws_plugin__s2member_pro_alipay_utilities::alipay_postvars ()) && ($_alipay = $alipay))
  65. {
  66. $alipay["s2member_log"][] = "IPN received on: " . date ("D M j, Y g:i:s a T");
  67. $alipay["s2member_log"][] = "s2Member POST vars verified through a POST back to AliPay®.";
  68.  
  69. if (!is_array ($alipay_already_p = get_transient ("s2m_" . md5 ("s2member_pro_alipay_notify_ids"))) || !in_array ($alipay["notify_id"], $alipay_already_p))
  70. {
  71. $alipay_already_p = (is_array ($alipay_already_p)) ? array_push ($alipay_already_p, $alipay["notify_id"]) : array ($alipay["notify_id"]);
  72. set_transient ("s2m_" . md5 ("s2member_pro_alipay_notify_ids"), array_slice (array_unique ($alipay_already_p), 0, 1000), 31556926);
  73.  
  74. if (preg_match ("/^(TRADE_FINISHED|TRADE_SUCCESS)$/i", $alipay["trade_status"]) && !$alipay["refund_status"])
  75. {
  76. $alipay["s2member_log"][] = "AliPay® transaction identified as ( `TRADE_FINISHED|TRADE_SUCCESS` ).";
  77. $alipay["s2member_log"][] = "IPN reformulated. Piping through s2Member's core/standard PayPal® processor as `txn_type` ( `web_accept` ).";
  78. $alipay["s2member_log"][] = "Please check PayPal® IPN logs for further processing details.";
  79.  
  80. list ($alipay["invoice"], $alipay["item_number"], $alipay["referencing"], $alipay["customer_ip"]) = preg_split ("/~/", $alipay["out_trade_no"]);
  81. list ($alipay["first_name"], $alipay["last_name"]) = preg_split ("/@/", $alipay["buyer_email"], 2);
  82.  
  83. $ipn = array (); // Reset.
  84.  
  85. $ipn["txn_type"] = "web_accept";
  86.  
  87. $ipn["txn_id"] = $alipay["trade_no"];
  88.  
  89. $ipn["custom"] = $alipay["extra_common_param"];
  90.  
  91. $ipn["mc_gross"] = number_format ($alipay["total_fee"], 2, ".", "");
  92. $ipn["mc_currency"] = strtoupper ("CNY"); // Yuan.
  93. $ipn["tax"] = number_format ("0.00", 2, ".", "");
  94.  
  95. $ipn["payer_email"] = $alipay["buyer_email"];
  96. $ipn["first_name"] = $alipay["first_name"];
  97. $ipn["last_name"] = $alipay["last_name"];
  98.  
  99. $ipn["option_name1"] = ($alipay["referencing"]) ? "Referencing Customer ID" : "Originating Domain";
  100. $ipn["option_selection1"] = ($alipay["referencing"]) ? $alipay["referencing"] : $_SERVER["HTTP_HOST"];
  101.  
  102. $ipn["option_name2"] = "Customer IP Address";
  103. $ipn["option_selection2"] = $alipay["customer_ip"];
  104.  
  105. $ipn["item_number"] = $alipay["item_number"];
  106. $ipn["item_name"] = $alipay["body"];
  107.  
  108. $ipn["s2member_paypal_proxy"] = "alipay";
  109. $ipn["s2member_paypal_proxy_use"] = "standard-emails";
  110. $ipn["s2member_paypal_proxy_verification"] = c_ws_plugin__s2member_paypal_utilities::paypal_proxy_key_gen();
  111.  
  112. c_ws_plugin__s2member_utils_urls::remote (site_url ("/?s2member_paypal_notify=1"), $ipn, array ("timeout" => 20));
  113. }
  114. else if (preg_match ("/^(TRADE_CLOSED|TRADE_SUCCESS)$/i", $alipay["trade_status"]) && $alipay["refund_status"])
  115. {
  116. $alipay["s2member_log"][] = "AliPay® transaction identified as ( `REFUND_SUCCESS` ).";
  117. $alipay["s2member_log"][] = "IPN reformulated. Piping through s2Member's core/standard PayPal® processor as `payment_status` ( `refunded` ).";
  118. $alipay["s2member_log"][] = "Please check PayPal® IPN logs for further processing details.";
  119.  
  120. list ($alipay["invoice"], $alipay["item_number"], $alipay["referencing"], $alipay["customer_ip"]) = preg_split ("/~/", $alipay["out_trade_no"]);
  121. list ($alipay["first_name"], $alipay["last_name"]) = preg_split ("/@/", $alipay["buyer_email"], 2);
  122.  
  123. $ipn = array (); // Reset.
  124.  
  125. $ipn["payment_status"] = "refunded";
  126.  
  127. $ipn["parent_txn_id"] = $alipay["trade_no"];
  128.  
  129. $ipn["custom"] = $alipay["extra_common_param"];
  130.  
  131. $ipn["mc_fee"] = "-" . number_format ("0.00", 2, ".", "");
  132. $ipn["mc_gross"] = "-" . number_format (abs ($alipay["total_fee"]), 2, ".", "");
  133. $ipn["mc_currency"] = strtoupper ("CNY"); // Yuan.
  134. $ipn["tax"] = "-" . number_format ("0.00", 2, ".", "");
  135.  
  136. $ipn["payer_email"] = $alipay["buyer_email"];
  137. $ipn["first_name"] = $alipay["first_name"];
  138. $ipn["last_name"] = $alipay["last_name"];
  139.  
  140. $ipn["option_name1"] = ($alipay["referencing"]) ? "Referencing Customer ID" : "Originating Domain";
  141. $ipn["option_selection1"] = ($alipay["referencing"]) ? $alipay["referencing"] : $_SERVER["HTTP_HOST"];
  142.  
  143. $ipn["option_name2"] = "Customer IP Address";
  144. $ipn["option_selection2"] = $alipay["customer_ip"];
  145.  
  146. $ipn["item_number"] = $alipay["item_number"];
  147. $ipn["item_name"] = $alipay["body"];
  148.  
  149. $ipn["s2member_paypal_proxy"] = "alipay";
  150. $ipn["s2member_paypal_proxy_use"] = "standard-emails";
  151. $ipn["s2member_paypal_proxy_verification"] = c_ws_plugin__s2member_paypal_utilities::paypal_proxy_key_gen();
  152.  
  153. c_ws_plugin__s2member_utils_urls::remote (site_url ("/?s2member_paypal_notify=1"), $ipn, array ("timeout" => 20));
  154. }
  155. else
  156. $alipay["s2member_log"][] = "Ignoring this IPN request. The status does NOT require any action on the part of s2Member.";
  157. }
  158. else
  159. $alipay["s2member_log"][] = "Ignoring duplicate IPN. s2Member has already processed AliPay® Notification ID: " . $alipay["notify_id"] . ".";
  160. }
  161. else // Extensive log reporting here. This is an area where many site owners find trouble. Depending on server configuration; remote HTTPS connections may fail.
  162. {
  163. $alipay["s2member_log"][] = "Unable to verify POST vars. This is most likely related to an invalid AliPay® configuration. Please check: s2Member -› AliPay® Options.";
  164. $alipay["s2member_log"][] = "If you're absolutely SURE that your AliPay® 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 AliPay® over an HTTPS connection.";
  165. $alipay["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.";
  166. $alipay["s2member_log"][] = var_export ($_REQUEST, true); // Recording _POST + _GET vars for analysis and debugging.
  167. }
  168. /*
  169. If debugging/logging is enabled; we need to append $alipay to the log file.
  170. Logging now supports Multisite Networking as well.
  171. */
  172. $logt = c_ws_plugin__s2member_utilities::time_details ();
  173. $logv = c_ws_plugin__s2member_utilities::ver_details ();
  174. $logm = c_ws_plugin__s2member_utilities::mem_details ();
  175. $log4 = $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"] . "\nUser-Agent: " . $_SERVER["HTTP_USER_AGENT"];
  176. $log4 = (is_multisite () && !is_main_site ()) ? ($_log4 = $current_blog->domain . $current_blog->path) . "\n" . $log4 : $log4;
  177. $log2 = (is_multisite () && !is_main_site ()) ? "alipay-ipn-4-" . trim (preg_replace ("/[^a-z0-9]/i", "-", $_log4), "-") . ".log" : "alipay-ipn.log";
  178.  
  179. if ($GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["gateway_debug_logs"])
  180. if (is_dir ($logs_dir = $GLOBALS["WS_PLUGIN__"]["s2member"]["c"]["logs_dir"]))
  181. if (is_writable ($logs_dir) && c_ws_plugin__s2member_utils_logs::archive_oversize_log_files ())
  182. file_put_contents ($logs_dir . "/" . $log2,
  183. "LOG ENTRY: ".$logt . "\n" . $logv . "\n" . $logm . "\n" . $log4 . "\n" .
  184. c_ws_plugin__s2member_utils_logs::conceal_private_info(var_export ($alipay, true)) . "\n\n",
  185. FILE_APPEND);
  186.  
  187. status_header (200); // 200 OK status header.
  188. header ("Content-Type: text/plain; charset=UTF-8"); // Content-Type text/plain with UTF-8.
  189. while (@ob_end_clean ()); // Clean any existing output buffers.
  190.  
  191. exit ("success"); // Exit now with "success".
  192. }
  193. }
  194. }
  195. }
  196. ?>

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

Source Code Documentation