PageRenderTime 41ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/User/PHP/AquaticPrimeEsellerateMySQL.php

https://bitbucket.org/tgunr/aqpmanager
PHP | 250 lines | 179 code | 26 blank | 45 comment | 23 complexity | 8dd407796f987a5687a9379da13b82ec MD5 | raw file
Possible License(s): MIT
  1. <?php
  2. /**
  3. * AquaticPrime eSellerate payment processor
  4. * Generates license files and emails them after handling eSellerate XML Order Notices
  5. * @author Tom Harrington, Atomic Bird (some portions adapted from examples by Lucas Newman)
  6. * @copyright Copyright &copy; 2006 Tom Harrington
  7. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  8. */
  9. require("Config.php");
  10. require("AquaticPrime.php");
  11. require("eSellerateXML.php");
  12. // Esellerate preview orders will not be processed unless $debug is 1.
  13. $debug = 0;
  14. // Fields from OrderInfo you want in the database. Customize as needed, only ORDER_NUMBER is required.
  15. $orderInfoFields = array("CUSTOMER_IP",
  16. "ORDER_NUMBER",
  17. "STATUS",
  18. "TRAN_DATE",
  19. "FIRST_NAME",
  20. "LAST_NAME",
  21. "COMPANY",
  22. "ADDRESS1",
  23. "ADDRESS2",
  24. "CITY",
  25. "STATE",
  26. "POSTAL",
  27. "COUNTRY",
  28. "PHONE",
  29. "EMAIL",
  30. "SHIP_FIRST_NAME",
  31. "SHIP_LAST_NAME",
  32. "SHIP_COMPANY",
  33. "SHIP_ADDRESS1",
  34. "SHIP_ADDRESS2",
  35. "SHIP_CITY",
  36. "SHIP_STATE",
  37. "SHIP_POSTAL",
  38. "SHIP_COUNTRY",
  39. "CONTACT_ME",
  40. "ORDER_DISCOUNT",
  41. "SHIP_WEIGHT",
  42. "SHIP_METHOD",
  43. "SHIP_AMOUNT",
  44. "ESELLER_ID",
  45. "ESELLER_NAME",
  46. "METHOD",
  47. "SUB_METHOD",
  48. "COUPON_ID",
  49. "TRACKING_ID",
  50. "VAT_COUNTRY",
  51. "CURRENCY_CODE",
  52. "AFFILIATE_ID",
  53. "AFFILIATE_NAME",
  54. "PORTAL_ID",
  55. "PORTAL_NAME",
  56. "CUSTOM_0",
  57. "CUSTOM_1",
  58. "CUSTOM_2",
  59. "CUSTOM_3",
  60. "CUSTOM_4",
  61. "CUSTOM_5",
  62. "CUSTOM_6",
  63. "CUSTOM_7",
  64. "CUSTOM_8",
  65. "CUSTOM_9"
  66. );
  67. $orderLinesFields = array("SKU_ID",
  68. "SKU_TITLE",
  69. "SHORT_DESCRIPTION",
  70. "QUANTITY",
  71. "UNIT_PRICE",
  72. "PLATFORM",
  73. "REGISTRATION_NAME",
  74. "PROMPTED_VALUE",
  75. "REGISTRATION_OTHER",
  76. "EXPIRATION_DATE",
  77. "SERIAL_NUMBER",
  78. "PROCESSING_FEE",
  79. "SALES_TAX_AMOUNT",
  80. "AFFILIATE_COMMISION",
  81. "VOLUME_DISCOUNT",
  82. "CROSS_SELL_DISCOUNT",
  83. "UP_SELL_GROUP_ID",
  84. "UP_SELL_PARENT_SKU_ID"
  85. );
  86. /***************************************************
  87. The main body of the module. Note that the web server
  88. must be configured to allow access to raw post data.
  89. This can be done by adding the line:
  90. php_flag always_populate_raw_post_data on
  91. to .htaccess on Apache.
  92. Note: I have sometimes found that this is not necessary,
  93. but I'm not sure why. Add it if you run into a problem.
  94. It makes the $HTTP_RAW_POST_DATA variable valid.
  95. ***************************************************/
  96. // Work around a PHP 5.2.2 bug preventing POST data from reaching the script.
  97. // See <http://bugs.php.net/bug.php?id=41293> for details.
  98. if ($_SERVER["REQUEST_METHOD"] == "POST") {
  99. if ( !isset( $HTTP_RAW_POST_DATA ) ) {
  100. $HTTP_RAW_POST_DATA = file_get_contents("php://input");
  101. }
  102. }
  103. $xml_parser = xml_parser_create();
  104. xml_set_element_handler($xml_parser, "startElement", "endElement");
  105. xml_set_character_data_handler($xml_parser, "characterData");
  106. if (!xml_parse($xml_parser, $HTTP_RAW_POST_DATA, TRUE))
  107. {
  108. $msg = sprintf("XML error: %s at line %d",
  109. xml_error_string(xml_get_error_code($xml_parser)),
  110. xml_get_current_line_number($xml_parser));
  111. xml_parser_free($xml_parser);
  112. ReportFatalError($msg);
  113. }
  114. xml_parser_free($xml_parser);
  115. // We have all the data in the $XmlData array
  116. // Check the secret text
  117. if ($XmlData["ORDERNOTICEDS"]["ORDERINFO"]["ORDER_NOTICE_SECRET"]["_data"] != $order_notice_secret) {
  118. ReportFatalError("Invalid order notice secret\n");
  119. }
  120. // Ignore Preview orders
  121. if (($debug == 0) && ($XmlData["ORDERNOTICEDS"]["ORDERINFO"]["STATUS"]["_data"] == "PREVIEW"))
  122. {
  123. ReportFatalError("No processing of preview order except in debug mode.\n");
  124. }
  125. // We will open a MySql database and store the serial numbers
  126. if (!TryOpenDb())
  127. {
  128. ReportFatalError($DbError);
  129. }
  130. // The tables in eSellerate.sql are type InnoDB, so we can have the safety of transactions.
  131. // The corresponding COMMIT is at the end of this file. The ROLLBACK, if necessary, is over in ReportFatalError().
  132. mysql_query("BEGIN");
  133. //$date = date("Y/m/d");
  134. // Fix up the transaction date to give MySQL something it likes.
  135. $tran_date_str = $XmlData["ORDERNOTICEDS"]["ORDERINFO"]["TRAN_DATE"]["_data"];
  136. $tran_date_stamp = strtotime($tran_date_str);
  137. $tran_date_for_sql = strftime("%Y-%m-%d", $tran_date_stamp);
  138. $XmlData["ORDERNOTICEDS"]["ORDERINFO"]["TRAN_DATE"]["_data"] = $tran_date_for_sql;
  139. // Write the OrderInfo stuff to the database
  140. $orderNumber = $XmlData["ORDERNOTICEDS"]["ORDERINFO"]["ORDER_NUMBER"]["_data"];
  141. // First check and see whether the order's already in the database.
  142. $sqlResult = mysql_query("SELECT ORDER_NUMBER from OrderInfo WHERE ORDER_NUMBER=\"$orderNumber\"");
  143. if (!$sqlResult) {
  144. ReportFatalError(mysql_error());
  145. }
  146. $numRows = mysql_num_rows($sqlResult);
  147. mysql_free_result($sqlResult);
  148. if ($numRows > 0) {
  149. ReportFatalError("Order $orderNumber is already in the database\n");
  150. }
  151. // It wasn't there? OK, put it there.
  152. // Build a query string
  153. $queryStringValues = array();
  154. foreach ($orderInfoFields as $currentField) {
  155. $queryStringValues[] = "\"" . mysql_real_escape_string($XmlData["ORDERNOTICEDS"]["ORDERINFO"][$currentField]["_data"]) . "\"";
  156. }
  157. $queryString = "INSERT INTO OrderInfo (" . join(", ", $orderInfoFields) . ")" .
  158. " VALUES (" . join(", ", $queryStringValues) . ")";
  159. if ($debug == 1) {
  160. echo "$queryString\n";
  161. }
  162. // Do the insert
  163. $sqlResult = mysql_query($queryString);
  164. if (!$sqlResult) {
  165. ReportFatalError(mysql_error());
  166. }
  167. // For Aquatic Prime we really need the date and time (in case someone orders more than one copy
  168. // the same day), so the date from eSellerate won't cut it.
  169. $sn_date = strftime("%Y-%m-%d %H:%m:%S");
  170. // Process each order line
  171. for ($i = 0; $i < $nOrderLines; ++$i)
  172. {
  173. // Now do the AquaticPrime stuff
  174. if (in_array($XmlData["ORDERNOTICEDS"]["ORDERLINES"][$i]["SKU_ID"]["_data"], $aquaticPrimeSKUs)) {
  175. $product = $XmlData["ORDERNOTICEDS"]["ORDERLINES"][$i]["SKU_TITLE"]["_data"];
  176. $name = $XmlData["ORDERNOTICEDS"]["ORDERLINES"][$i]["REGISTRATION_NAME"]["_data"];
  177. if ($name == "") {
  178. $name = $XmlData["ORDERNOTICEDS"]["ORDERINFO"]["FIRST_NAME"]["_data"] . " " .
  179. $XmlData["ORDERNOTICEDS"]["ORDERINFO"]["LAST_NAME"]["_data"];
  180. }
  181. $email = $XmlData["ORDERNOTICEDS"]["ORDERINFO"]["EMAIL"]["_data"];
  182. $unit_price = $XmlData["ORDERNOTICEDS"]["ORDERLINES"][$i]["UNIT_PRICE"]["_data"];
  183. $count = $XmlData["ORDERNOTICEDS"]["ORDERLINES"][$i]["QUANTITY"]["_data"];
  184. // eSellerate only gives you the date, not the time (so we don't do RFC 2822 formatting here).
  185. $transactionID = $orderNumber;
  186. // Create our license dictionary to be signed
  187. $dict = array("Product" => $product,
  188. "Name" => $name,
  189. "Email" => $email,
  190. "Licenses" => $count,
  191. "Timestamp" => $sn_date,
  192. "TransactionID" => $transactionID);
  193. $license = licenseDataForDictionary($dict, $key, $privateKey);
  194. // Note that the database size for SERIAL_NUMBER was raised from 255 (eSellerate's size) to
  195. // a MySQL TEXT field to fit alternate registration schemes.
  196. $XmlData["ORDERNOTICEDS"]["ORDERLINES"][$i]["SERIAL_NUMBER"]["_data"] = $license;
  197. $to = $email;
  198. $from = str_replace(array("##NAME##", "##EMAIL##"), array($name, $email), $from);
  199. $subject = str_replace(array("##NAME##", "##EMAIL##"), array($name, $email), $subject);
  200. $message = str_replace(array("##NAME##", "##EMAIL##", "##LICENSES##"), array($name, $email, $count), $message);
  201. $licenseName = str_replace(array("##NAME##", "##EMAIL##"), array($name, $email), $licenseName);
  202. $bcc = str_replace(array("##NAME##", "##EMAIL##"), array($name, $email), $bcc);
  203. sendMail($to, $from, $subject, $message, $license, $licenseName, $bcc);
  204. }
  205. // Build a query string
  206. $queryStringValues = array();
  207. foreach ($orderLinesFields as $currentField) {
  208. $queryStringValues[] = "\"" . mysql_real_escape_string($XmlData["ORDERNOTICEDS"]["ORDERLINES"][$i][$currentField]["_data"]) . "\"";
  209. }
  210. $queryString = "INSERT INTO OrderLines (" . join(", ", $orderLinesFields) . ", ORDER_NUMBER, SN_DATE)" .
  211. " VALUES (" . join(", ", $queryStringValues) . ", \"$orderNumber\", \"$sn_date\")";
  212. if ($debug == 1) {
  213. echo "$queryString\n";
  214. }
  215. // Do the insert
  216. $sqlResult = mysql_query($queryString);
  217. if (!$sqlResult) {
  218. ReportFatalError(mysql_error());
  219. }
  220. }
  221. mysql_query("COMMIT");
  222. CloseDb();
  223. ?>