PageRenderTime 55ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/src/merchants/terracoin.php

https://bitbucket.org/fuzzybearbtc/terracoin-wp-e-commerce
PHP | 336 lines | 242 code | 19 blank | 75 comment | 25 complexity | 6cb61f925364aa10d8df6c9a516c93d1 MD5 | raw file
  1. <?php
  2. /**
  3. * @file Terracoin for the WP e-Commerce shopping cart plugin for WordPress
  4. * @author Mike Gogulski - http://www.nostate.com/ http://www.gogulski.com/
  5. *
  6. * Donations: 1DcZfySDvUoNBzf2mwReVy3VL93WtwnALr
  7. */
  8. /*
  9. * This is free and unencumbered software released into the public domain.
  10. *
  11. * Anyone is free to copy, modify, publish, use, compile, sell, or
  12. * distribute this software, either in source code form or as a compiled
  13. * binary, for any purpose, commercial or non-commercial, and by any
  14. * means.
  15. *
  16. * In jurisdictions that recognize copyright laws, the author or authors
  17. * of this software dedicate any and all copyright interest in the
  18. * software to the public domain. We make this dedication for the benefit
  19. * of the public at large and to the detriment of our heirs and
  20. * successors. We intend this dedication to be an overt act of
  21. * relinquishment in perpetuity of all present and future rights to this
  22. * software under copyright law.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  27. * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  28. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  29. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  30. * OTHER DEALINGS IN THE SOFTWARE.
  31. *
  32. * For more information, please refer to <http://unlicense.org/>
  33. */
  34. $nzshpcrt_gateways[$num]['name'] = 'Terracoin';
  35. $nzshpcrt_gateways[$num]['internalname'] = 'terracoin';
  36. $nzshpcrt_gateways[$num]['function'] = 'gateway_terracoin';
  37. $nzshpcrt_gateways[$num]['form'] = "form_terracoin";
  38. $nzshpcrt_gateways[$num]['submit_function'] = "submit_terracoin";
  39. $nzshpcrt_gateways[$num]['payment_type'] = "credit_card";
  40. add_filter("the_content", "terracoin_checkout_complete_display_filter", 99);
  41. add_filter("wp_mail", "terracoin_checkout_complete_mail_filter", 99);
  42. add_filter("cron_schedules", "terracoin_create_cron_schedule", 10);
  43. add_action("terracoin_cron", "terracoin_cron");
  44. register_deactivation_hook(__FILE__ . DIRECTORY_SEPARATOR . "../wp-shopping-cart.php", "terracoin_disable_cron");
  45. /**
  46. * Set up a custom cron schedule to run every 5 minutes.
  47. *
  48. * Invoked via the cron_schedules filter.
  49. *
  50. * @param array $schedules
  51. */
  52. function terracoin_create_cron_schedule($schedules = '') {
  53. $schedules['every5minutes'] = array(
  54. 'interval' => 300,
  55. 'display' => __('Every five minutes'),
  56. );
  57. return $schedules;
  58. }
  59. /**
  60. * Cancel the Terracoin processing cron job.
  61. *
  62. * Invoked at deactivation of WP e-Commerce
  63. */
  64. function terracoin_disable_cron() {
  65. wp_clear_scheduled_hook("terracoin_cron");
  66. }
  67. function terracoin_debug($message) {
  68. error_log($message);
  69. }
  70. /**
  71. * Cron job to process outstanding Terracoin transactions.
  72. */
  73. function terracoin_cron() {
  74. /*
  75. * Find transactions where purchase status = 1 and gateway = terracoin.
  76. * Terracoin address for the transaction is stored in transactid
  77. */
  78. global $wpdb;
  79. terracoin_debug("entering cron");
  80. $transactions = $wpdb->get_results("SELECT id,totalprice,sessionid,transactid,date FROM " . WPSC_TABLE_PURCHASE_LOGS . " WHERE gateway='terracoin' AND processed='1'");
  81. if (count($transactions) < 1)
  82. return;
  83. terracoin_debug("have transactions to process");
  84. include_once("library/terracoin.inc");
  85. $terracoin_client = new TerracoinClient(get_option("terracoin_scheme"),
  86. get_option("terracoin_username"),
  87. get_option("terracoin_password"),
  88. get_option("terracoin_address"),
  89. get_option("terracoin_port"),
  90. get_option("terracoin_certificate_path"));
  91. if (TRUE !== ($fault = $terracoin_client->can_connect())) {
  92. error_log('The Terracoin server is presently unavailable. Fault: ' . $fault);
  93. return;
  94. }
  95. terracoin_debug("server reachable");
  96. foreach ($transactions as $transaction) {
  97. $address = $transaction->transactid;
  98. $order_id = $transaction->id;
  99. $order_total = $transaction->totalprice;
  100. $sessionid = $transaction->sessionid;
  101. $order_date = $transaction->date;
  102. terracoin_debug("processing: " . var_export($transaction, TRUE));
  103. try {
  104. $paid = $terracoin_client->query("getreceivedbyaddress", $address, get_option("terracoin_confirms"));
  105. } catch (TerracoinClientException $e) {
  106. error_log("Terracoin server communication failed on getreceivedbyaddress " . $address . " with fault string " . $e->getMessage());
  107. continue;
  108. }
  109. if ($paid >= $order_total) {
  110. terracoin_debug("paid in full");
  111. // PAID IN FULL
  112. // Update payment log
  113. $wpdb->query("UPDATE " . WPSC_TABLE_PURCHASE_LOGS . " SET processed='2' WHERE id='" . $order_id . "'");
  114. // Email customer
  115. transaction_results($sessionid, false);
  116. continue;
  117. }
  118. if (time() > $order_date + get_option("terracoin_timeout") * 60 * 60) {
  119. terracoin_debug("order expired");
  120. // ORDER EXPIRED
  121. // Update payment log
  122. $wpdb->query("UPDATE " . WPSC_TABLE_PURCHASE_LOGS . " SET processed='5' WHERE id='" . $order_id . "'");
  123. // Can't email the customer via transaction_results
  124. // TODO: Email the customer, delete the order
  125. }
  126. }
  127. terracoin_debug("leaving cron");
  128. }
  129. function terracoin_checkout_complete_display_filter($content = "") {
  130. if (!isset($_SESSION['terracoin_address_display']) || empty($_SESSION['terracoin_address_display']))
  131. return $content;
  132. $cart = unserialize($_SESSION['wpsc_cart']);
  133. $content = preg_replace('/@@TOTAL@@/', $cart->total_price, $content);
  134. $content = preg_replace('/@@ADDRESS@@/', $_SESSION['terracoin_address_display'], $content);
  135. $content = preg_replace('/@@TIMEOUT@@/', get_option('terracoin_timeout'), $content);
  136. $content = preg_replace('/@@CONFIRMATIONS@@/', get_option('terracoin_confirms'), $content);
  137. unset($_SESSION['terracoin_address_display']);
  138. return $content;
  139. }
  140. function terracoin_checkout_complete_mail_filter($mail) {
  141. if (!isset($_SESSION['terracoin_address_mail']) || empty($_SESSION['terracoin_address_mail']))
  142. return $mail;
  143. $cart = unserialize($_SESSION['wpsc_cart']);
  144. $mail['message'] = preg_replace('/@@TOTAL@@/', $cart->total_price, $mail['message']);
  145. $mail['message'] = preg_replace('/@@ADDRESS@@/', $_SESSION['terracoin_address_mail'], $mail['message']);
  146. $mail['message'] = preg_replace('/@@TIMEOUT@@/', get_option('terracoin_timeout'), $mail['message']);
  147. $mail['message'] = preg_replace('/@@CONFIRMATIONS@@/', get_option('terracoin_confirms'), $mail['message']);
  148. unset($_SESSION['terracoin_address_mail']);
  149. return $mail;
  150. }
  151. function terracoin_checkout_fail($sessionid, $message, $fault = "") {
  152. global $wpdb;
  153. $wpdb->query("UPDATE " . WPSC_TABLE_PURCHASE_LOGS . " SET processed='5' WHERE sessionid=" . $sessionid);
  154. $_SESSION['WpscGatewayErrorMessage'] = $message;
  155. $_SESSION['terracoin'] = 'fail';
  156. error_log($message . ": " . $fault);
  157. header("Location: " . get_option("checkout_url"));
  158. }
  159. /**
  160. * Process Terracoin checkout.
  161. *
  162. * @param string $separator
  163. * @param integer $sessionid
  164. * @todo Document better
  165. */
  166. function gateway_terracoin($separator, $sessionid) {
  167. global $wpdb, $wpsc_cart;
  168. include_once("library/terracoin.inc");
  169. $terracoin_client = new TerracoinClient(get_option("terracoin_scheme"),
  170. get_option("terracoin_username"),
  171. get_option("terracoin_password"),
  172. get_option("terracoin_address"),
  173. get_option("terracoin_port"),
  174. get_option("terracoin_certificate_path"));
  175. if (TRUE !== ($fault = $terracoin_client->can_connect())) {
  176. terracoin_checkout_fail($session, 'The Terracoin server is presently unavailable. Please contact the site administrator.', $fault);
  177. return;
  178. }
  179. $row = $wpdb->get_row("SELECT id,totalprice FROM " . WPSC_TABLE_PURCHASE_LOGS . " WHERE sessionid=" . $sessionid);
  180. $label = $row->id . " " . $row->totalprice;
  181. try {
  182. $address = $terracoin_client->query("getnewaddress", $label);
  183. } catch (TerracoinClientException $e) {
  184. terracoin_checkout_fail($session, 'The Terracoin server is presently unavailable. Please contact the site administrator.', $e->getMessage());
  185. return;
  186. }
  187. if (!Terracoin::checkAddress($address)) {
  188. terracoin_checkout_fail($session, 'The Terracoin server returned an invalid address. Please contact the site administrator.', $e->getMessage());
  189. return;
  190. }
  191. //var_dump($_SESSION);
  192. unset($_SESSION['WpscGatewayErrorMessage']);
  193. // Set the transaction to pending payment and log the Terracoin address as its transaction ID
  194. $wpdb->query("UPDATE " . WPSC_TABLE_PURCHASE_LOGS . " SET processed='1', transactid='" . $address . "' WHERE sessionid=" . $sessionid);
  195. $_SESSION['terracoin'] = 'success';
  196. $_SESSION['terracoin_address_display'] = $address;
  197. $_SESSION['terracoin_address_mail'] = $address;
  198. header("Location: " . get_option('transact_url') . $separator . "sessionid=" . $sessionid);
  199. exit();
  200. }
  201. /**
  202. * Set Terracoin payment options and start the cronjob.
  203. * @todo validate values
  204. */
  205. function submit_terracoin() {
  206. $options = array(
  207. "terracoin_scheme",
  208. "terracoin_certificate_path",
  209. "terracoin_username",
  210. "terracoin_password",
  211. "terracoin_port",
  212. "terracoin_address",
  213. "terracoin_timeout",
  214. "terracoin_confirms",
  215. "payment_instructions",
  216. );
  217. foreach ($options as $o)
  218. if ($_POST[$o] != NULL)
  219. update_option($o, $_POST[$o]);
  220. wp_clear_scheduled_hook("terracoin_cron");
  221. wp_schedule_event(time(), "every5minutes", "terracoin_cron");
  222. return true;
  223. }
  224. /**
  225. * Produce the HTML for the Terracoin settings form.
  226. */
  227. function form_terracoin() {
  228. global $wpdb;
  229. $terracoin_scheme = (get_option('terracoin_scheme') == '' ? 'http' : get_option('terracoin_scheme'));
  230. $terracoin_certificate_path = get_option('terracoin_certificate_path');
  231. $terracoin_username = get_option('terracoin_username');
  232. $terracoin_password = get_option('terracoin_password');
  233. $terracoin_address = (get_option('terracoin_address') == '' ? 'localhost' : get_option('terracoin_address'));
  234. $terracoin_port = (get_option('terracoin_port') == '' ? '8338' : get_option('terracoin_port'));
  235. $terracoin_timeout = (get_option('terracoin_timeout') == '' ? '72' : get_option('terracoin_timeout'));
  236. $terracoin_confirms = (get_option('terracoin_confirms') == '' ? '0' : get_option('terracoin_confirms'));
  237. if (get_option('payment_instructions') != '')
  238. $payment_instructions = get_option('payment_instructions');
  239. else {
  240. $payment_instructions = '<strong>Please send your payment of TRC @@TOTAL@@ to Terracoin address @@ADDRESS@@.</strong> ';
  241. $payment_instructions .= 'If your payment is not received within @@TIMEOUT@@ hour(s) with at least @@CONFIRMATIONS@@ network confirmations, ';
  242. $payment_instructions .= 'your transaction will be canceled.';
  243. }
  244. // Create the Terracoin currency if it doesn't already exist
  245. $sql = "SELECT currency FROM " . WPSC_TABLE_CURRENCY_LIST . " WHERE currency='Terracoin'";
  246. if (!$wpdb->get_row($sql)) {
  247. $sql = "INSERT INTO " . WPSC_TABLE_CURRENCY_LIST . " VALUES (NULL, 'Terracoin', 'TC', 'Terracoin', '', '', 'TRC', '0', '0', 'antarctica', '1')";
  248. $wpdb->query($sql);
  249. }
  250. $output = "
  251. <tr>
  252. <td>&nbsp;</td>
  253. <td><small>Connection data for your terracoin server HTTP-JSON-RPC interface.</small></td>
  254. </tr>
  255. <tr>
  256. <td>Server scheme (HTTP or HTTPS)</td>
  257. <td><input type='text' size='40' value='"
  258. . $terracoin_scheme . "' name='terracoin_scheme' /></td>
  259. </tr>
  260. <tr>
  261. <td>SSL certificate path</td>
  262. <td><input type='text' size='40' value='"
  263. . $terracoin_certificate_path . "' name='terracoin_certificate_path' /></td>
  264. </tr>
  265. <tr>
  266. <td>Server username</td>
  267. <td><input type='text' size='40' value='"
  268. . $terracoin_username . "' name='terracoin_username' /></td>
  269. </tr>
  270. <tr>
  271. <td>Server password</td>
  272. <td><input type='text' size='40' value='"
  273. . $terracoin_password . "' name='terracoin_password' /></td>
  274. </tr>
  275. <tr>
  276. <td>Server address (usually localhost)</td>
  277. <td><input type='text' size='40' value='"
  278. . $terracoin_address . "' name='terracoin_address' /></td>
  279. </tr>
  280. <tr>
  281. <td>Server port (usually 8338)</td>
  282. <td><input type='text' size='40' value='"
  283. . $terracoin_port . "' name='terracoin_port' /></td>
  284. </tr>
  285. <tr>
  286. <td>Transaction timeout (hours)</td>
  287. <td><input type='text' size='40' value='"
  288. . $terracoin_timeout . "' name='terracoin_timeout' /></td>
  289. </tr>
  290. <tr>
  291. <td>Transaction confirmations required</td>
  292. <td><input type='text' size='40' value='"
  293. . $terracoin_confirms . "' name='terracoin_confirms' /></td>
  294. </tr>
  295. <tr>
  296. <td colspan='2'>
  297. <strong>Enter the template for payment instructions to be give to the customer on checkout.</strong><br />
  298. <textarea cols='40' rows='9' name='wpsc_options[payment_instructions]'>"
  299. . $payment_instructions . "</textarea><br />
  300. Valid template tags:
  301. <ul>
  302. <li>@@TOTAL@@ - The order total</li>
  303. <li>@@ADDRESS@@ - The Terracoin address generated for the transaction</li>
  304. <li>@@TIMEOUT@@ - Transaction timeout (hours)</li>
  305. <li>@@CONFIRMATIONS@@ - Transaction confirmations required</li>
  306. </ul>
  307. </td>
  308. </tr>
  309. <tr>
  310. <td colspan='2'>
  311. Like Terracoin for WP e-Commerce? Your gifts to 1DcZfySDvUoNBzf2mwReVy3VL93WtwnALr are <strong>greatly</strong> appreciated. Thank you!
  312. </td>
  313. </tr>
  314. ";
  315. return $output;
  316. }
  317. ?>