Source Code Documentation


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 $current_site, $current_blog; /* For Multisite support. */
  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_q = "&s2member_paypal_proxy=alipay&s2member_paypal_proxy_use=standard-emails";
  109. $ipn_q .= "&s2member_paypal_proxy_verification=" . urlencode (c_ws_plugin__s2member_paypal_utilities::paypal_proxy_key_gen ());
  110. /**/
  111. c_ws_plugin__s2member_utils_urls::remote (site_url ("/?s2member_paypal_notify=1" . $ipn_q), $ipn, array ("timeout" => 20));
  112. }
  113. else if (preg_match ("/^(TRADE_CLOSED|TRADE_SUCCESS)$/i", $alipay["trade_status"]) && $alipay["refund_status"])
  114. {
  115. $alipay["s2member_log"][] = "AliPay® transaction identified as ( `REFUND_SUCCESS` ).";
  116. $alipay["s2member_log"][] = "IPN reformulated. Piping through s2Member's core/standard PayPal® processor as `payment_status` ( `refunded` ).";
  117. $alipay["s2member_log"][] = "Please check PayPal® IPN logs for further processing details.";
  118. /**/
  119. list ($alipay["invoice"], $alipay["item_number"], $alipay["referencing"], $alipay["customer_ip"]) = preg_split ("/~/", $alipay["out_trade_no"]);
  120. list ($alipay["first_name"], $alipay["last_name"]) = preg_split ("/@/", $alipay["buyer_email"], 2);
  121. /**/
  122. $ipn = array (); /* Reset. */
  123. /**/
  124. $ipn["payment_status"] = "refunded";
  125. /**/
  126. $ipn["parent_txn_id"] = $alipay["trade_no"];
  127. /**/
  128. $ipn["custom"] = $alipay["extra_common_param"];
  129. /**/
  130. $ipn["mc_fee"] = "-" . number_format ("0.00", 2, ".", "");
  131. $ipn["mc_gross"] = "-" . number_format (abs ($alipay["total_fee"]), 2, ".", "");
  132. $ipn["mc_currency"] = strtoupper ("CNY"); /* Yuan. */
  133. $ipn["tax"] = "-" . number_format ("0.00", 2, ".", "");
  134. /**/
  135. $ipn["payer_email"] = $alipay["buyer_email"];
  136. $ipn["first_name"] = $alipay["first_name"];
  137. $ipn["last_name"] = $alipay["last_name"];
  138. /**/
  139. $ipn["option_name1"] = ($alipay["referencing"]) ? "Referencing Customer ID" : "Originating Domain";
  140. $ipn["option_selection1"] = ($alipay["referencing"]) ? $alipay["referencing"] : $_SERVER["HTTP_HOST"];
  141. /**/
  142. $ipn["option_name2"] = "Customer IP Address";
  143. $ipn["option_selection2"] = $alipay["customer_ip"];
  144. /**/
  145. $ipn["item_number"] = $alipay["item_number"];
  146. $ipn["item_name"] = $alipay["body"];
  147. /**/
  148. $ipn_q = "&s2member_paypal_proxy=alipay&s2member_paypal_proxy_use=standard-emails";
  149. $ipn_q .= "&s2member_paypal_proxy_verification=" . urlencode (c_ws_plugin__s2member_paypal_utilities::paypal_proxy_key_gen ());
  150. /**/
  151. c_ws_plugin__s2member_utils_urls::remote (site_url ("/?s2member_paypal_notify=1" . $ipn_q), $ipn, array ("timeout" => 20));
  152. }
  153. else
  154. $alipay["s2member_log"][] = "Ignoring this IPN request. The status does NOT require any action on the part of s2Member.";
  155. }
  156. else
  157. $alipay["s2member_log"][] = "Ignoring duplicate IPN. s2Member has already processed AliPay® Notification ID: " . $alipay["notify_id"] . ".";
  158. }
  159. else /* Extensive log reporting here. This is an area where many site owners find trouble. Depending on server configuration; remote HTTPS connections may fail. */
  160. {
  161. $alipay["s2member_log"][] = "Unable to verify POST vars. This is most likely related to an invalid AliPay® configuration. Please check: s2Member -> AliPay® Options.";
  162. $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.";
  163. $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.";
  164. $alipay["s2member_log"][] = var_export ($_REQUEST, true); /* Recording _POST + _GET vars for analysis and debugging. */
  165. }
  166. /*
  167. If debugging/logging is enabled; we need to append $alipay to the log file.
  168. Logging now supports Multisite Networking as well.
  169. */
  170. $logv = c_ws_plugin__s2member_utilities::ver_details ();
  171. $logm = c_ws_plugin__s2member_utilities::mem_details ();
  172. $log4 = $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"] . "\nUser-Agent: " . $_SERVER["HTTP_USER_AGENT"];
  173. $log4 = (is_multisite () && !is_main_site ()) ? ($_log4 = $current_blog->domain . $current_blog->path) . "\n" . $log4 : $log4;
  174. $log2 = (is_multisite () && !is_main_site ()) ? "alipay-ipn-4-" . trim (preg_replace ("/[^a-z0-9]/i", "-", $_log4), "-") . ".log" : "alipay-ipn.log";
  175. /**/
  176. if ($GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["gateway_debug_logs"])
  177. if (is_dir ($logs_dir = $GLOBALS["WS_PLUGIN__"]["s2member"]["c"]["logs_dir"]))
  178. if (is_writable ($logs_dir) && c_ws_plugin__s2member_utils_logs::archive_oversize_log_files ())
  179. file_put_contents ($logs_dir . "/" . $log2, $logv . "\n" . $logm . "\n" . $log4 . "\n" . var_export ($alipay, true) . "\n\n", FILE_APPEND);
  180. /**/
  181. status_header (200); /* 200 OK status header. */
  182. header ("Content-Type: text/plain; charset=utf-8"); /* Content-Type text/plain with UTF-8. */
  183. eval ('while (@ob_end_clean ());'); /* End/clean all output buffers that may or may not exist. */
  184. /**/
  185. exit ("success"); /* Exit now with "success". */
  186. }
  187. }
  188. }
  189. }
  190. ?>

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

Source Code Documentation