PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/wp-e-commerce/wpsc-merchants/epay.php

https://github.com/AaronFernandes/aquestionof
PHP | 332 lines | 252 code | 38 blank | 42 comment | 56 complexity | 327754a7eb36d3946fab5ee15a7b68ae MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0
  1. <?php
  2. /*
  3. Copyright (c) 2009. All rights reserved ePay - www.epay.dk.
  4. This program is free software. You are allowed to use the software but NOT allowed to modify the software.
  5. It is also not legal to do any changes to the software and distribute it in your own name / brand.
  6. */
  7. $nzshpcrt_gateways[$num]['name'] = 'ePay';
  8. $nzshpcrt_gateways[$num]['admin_name'] = 'ePay';
  9. $nzshpcrt_gateways[$num]['internalname'] = 'epay';
  10. $nzshpcrt_gateways[$num]['function'] = 'gateway_epay';
  11. $nzshpcrt_gateways[$num]['form'] = "form_epay";
  12. $nzshpcrt_gateways[$num]['submit_function'] = "submit_epay";
  13. $nzshpcrt_gateways[$num]['payment_type'] = "credit_card";
  14. //
  15. // Extracts the current page url
  16. //
  17. function curPageURL() {
  18. $pageURL = 'http';
  19. if (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
  20. $pageURL .= "://";
  21. if ($_SERVER["SERVER_PORT"] != "80") {
  22. $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
  23. } else {
  24. $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
  25. }
  26. return $pageURL;
  27. }
  28. //
  29. // Function used to convert from 100,95 to 10095
  30. //
  31. function trimAmountForEpay($amount) {
  32. $res = "";
  33. for ($i = 0; $i < strlen($amount); $i++) {
  34. $letter = substr($amount, $i, 1);
  35. if ($letter == "0" || $letter == "1" || $letter == "2" || $letter == "3" || $letter == "4" || $letter == "5" || $letter == "6" || $letter == "7" || $letter == "8" || $letter == "9") {
  36. $res = ($res . $letter);
  37. }
  38. }
  39. return $res;
  40. }
  41. //
  42. // Generates the opener for the ePay standard payment window
  43. //
  44. function gateway_epay($seperator, $sessionid) {
  45. global $wpdb, $wpsc_cart;
  46. $purchase_log = $wpdb->get_row("SELECT * FROM `".WPSC_TABLE_PURCHASE_LOGS."` WHERE `sessionid`= ".$sessionid." LIMIT 1",ARRAY_A) ;
  47. if ($purchase_log['totalprice']==0) {
  48. echo "INVALID ORDER!";
  49. exit();
  50. }
  51. $transact_url = get_option('transact_url');
  52. echo '<html>
  53. <head>
  54. <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  55. <script type="text/javascript" src="http://www.epay.dk/js/standardwindow.js"></script>
  56. </head>
  57. <body onload="open_ePay_window()">';
  58. echo '<form action="https://ssl.ditonlinebetalingssystem.dk/popup/default.asp" method="post" name="ePay" target="ePay_window" id="ePay">
  59. <input type="hidden" name="merchantnumber" value="' . get_option('payment_epay_merchantnumber') . '">
  60. <input type="hidden" name="amount" value="' . trimAmountForEpay(wpsc_cart_total(false) * 100). '">
  61. <input type="hidden" name="currency" value="' . get_option('payment_epay_currency') . '">
  62. <input type="hidden" name="orderid" value="' . $purchase_log['id'] . '">
  63. <input type="hidden" name="windowstate" value="' . get_option('payment_epay_windowstate') . '">
  64. <input type="hidden" name="instantcallback" value="1">
  65. <input type="hidden" name="accepturl" value="' . $transact_url.$seperator."sessionid=".$sessionid."&gateway=epay&epay_accept=1" . '">
  66. <input type="hidden" name="declineurl" value="' . curPageURL() . '">
  67. <input type="hidden" name="callbackurl" value="' . get_option('siteurl').'/?epay_callback=1' . '">
  68. <input type="hidden" name="md5key" value="' . MD5(get_option('payment_epay_currency') . trimAmountForEpay(wpsc_cart_total(false) * 100) . $purchase_log['id'] . get_option('payment_epay_md5key')) . '">
  69. <input type="hidden" name="addfee" value="' . get_option('payment_epay_addfee') . '">
  70. <input type="hidden" name="splitpayment" value="' . get_option('payment_epay_splitpayment') . '">
  71. <input type="hidden" name="authsms" value="' . get_option('payment_epay_authsms') . '">
  72. <input type="hidden" name="authmail" value="' . get_option('payment_epay_authmail') . '">
  73. <input type="hidden" name="group" value="' . get_option('payment_epay_group') . '">
  74. <input type="hidden" name="instantcapture" value="' . get_option('payment_epay_instantcapture') . '">
  75. <input type="hidden" name="cms" value="wpecommerce">
  76. </form>';
  77. echo get_option('payment_epay_text_1') . '<br><br>';
  78. echo get_option('payment_epay_text_2') . '<br><br>';
  79. echo '<input type="button" value="' . get_option('payment_epay_text_3') . '" onClick="open_ePay_window()"><br><br>';
  80. if($_POST['collected_data'][get_option('epay_form_first_name')] != '')
  81. {
  82. echo $_POST['collected_data'][get_option('epay_form_first_name')];
  83. }
  84. echo '</body>
  85. </html>';
  86. exit();
  87. }
  88. function submit_epay() {
  89. return true;
  90. }
  91. //
  92. // Generates admin settings for the ePay module
  93. //
  94. function form_epay() {
  95. $output = "<tr>\n\r";
  96. $output .= " <td colspan='2'>\n\r";
  97. $output .= "<strong>Merchantnumber:</strong><br />\n\r";
  98. $output .= "<input type='text' name='wpsc_options[payment_epay_merchantnumber]' value='".( strlen(get_option('payment_epay_merchantnumber')) > 0 ? get_option('payment_epay_merchantnumber') : "ENTER EPAY MERCHANTNUMBER HERE")."'><br />\n\r";
  99. $output .= "<em>You ePay merchantnumber. Is to be found in the ePay administration from the menu \"Settings\"->\"Payment System\". Read more <a href='http://www.epay.dk/support/docs.asp?solution=1#merchantnumber' target='_blank'>here</a>.</em>\n\r";
  100. $output .= " </td>\n\r";
  101. $output .= "</tr>\n\r";
  102. $output .= "<tr>\n\r";
  103. $output .= " <td colspan='2'>\n\r";
  104. $output .= "<strong>Currency:</strong><br />\n\r";
  105. $output .= "<input type='text' name='wpsc_options[payment_epay_currency]' value='".(strlen(get_option('payment_epay_currency')) > 0 ? get_option('payment_epay_currency') : "208")."'><br />\n\r";
  106. $output .= "<em>The currency of which the payments are made. Danish (DKK) is 208. To view the complete list of currency codes please enter the ePay administration and enter the menu \"Support\" -> \"Currency codes\". Read more <a href='http://www.epay.dk/support/docs.asp?solution=1#currency' target='_blank'>here</a>.</em>\n\r";
  107. $output .= " </td>\n\r";
  108. $output .= "</tr>\n\r";
  109. $output .= "<tr>\n\r";
  110. $output .= " <td colspan='2'>\n\r";
  111. $output .= "<strong>Windowstate:</strong><br />\n\r";
  112. $output .= "<input type='text' name='wpsc_options[payment_epay_windowstate]' value='".(strlen(get_option('payment_epay_windowstate')) > 0 ? get_option('payment_epay_windowstate') : "2")."'><br />\n\r";
  113. $output .= "<em>How the payment window should behave. Set the value to 1 and the payment window will open up (as popup). Set the value to 2 and the payment window will be shown to the user in the same browser window. Read more <a href='http://www.epay.dk/support/docs.asp?solution=1#windowstate' target='_blank'>here</a>.</em>\n\r";
  114. $output .= " </td>\n\r";
  115. $output .= "</tr>\n\r";
  116. $output .= "<tr>\n\r";
  117. $output .= " <td colspan='2'>\n\r";
  118. $output .= "<strong>Popup text 1:</strong><br />\n\r";
  119. $output .= "<textarea cols='40' rows='9' name='wpsc_options[payment_epay_text_1]'>". (strlen(get_option('payment_epay_text_1')) > 0 ? get_option('payment_epay_text_1') : 'Hvis ikke Standard Betalingsvinduet &#229;bner op automatisk, s&#229; klik p&#229; knappen for at aktivere det.') . "</textarea><br />\n\r";
  120. $output .= "<em>Text when the payment window is opened.</em>\n\r";
  121. $output .= " </td>\n\r";
  122. $output .= "</tr>\n\r";
  123. $output .= "<tr>\n\r";
  124. $output .= " <td colspan='2'>\n\r";
  125. $output .= "<strong>Popup text 2:</strong><br />\n\r";
  126. $output .= "<textarea cols='40' rows='9' name='wpsc_options[payment_epay_text_2]'>".(strlen(get_option('payment_epay_text_2')) > 0 ? get_option('payment_epay_text_2') : 'Bem&#230;rk! Hvis I benytter en pop-up stopper, skal I holde CTRL tasten nede, mens I trykker p&#229; knappen.')."</textarea><br />\n\r";
  127. $output .= "<em>Text when the payment window is opened.</em>\n\r";
  128. $output .= " </td>\n\r";
  129. $output .= "</tr>\n\r";
  130. $output .= "<tr>\n\r";
  131. $output .= " <td colspan='2'>\n\r";
  132. $output .= "<strong>Popup text 3:</strong><br />\n\r";
  133. $output .= "<textarea cols='40' rows='9' name='wpsc_options[payment_epay_text_3]'>".(strlen(get_option('payment_epay_text_3')) > 0 ? get_option('payment_epay_text_3') : '&#197;ben betalingsvinduet')."</textarea><br />\n\r";
  134. $output .= "<em>Text when the payment window is opened.</em>\n\r";
  135. $output .= " </td>\n\r";
  136. $output .= "</tr>\n\r";
  137. $output .= "<tr>\n\r";
  138. $output .= " <td colspan='2'>\n\r";
  139. $output .= "<strong>Enable MD5 security:</strong><br />\n\r";
  140. $output .= "<input type='text' name='wpsc_options[payment_epay_md5mode]' value='".( strlen(get_option('payment_epay_md5mode')) > 0 ? get_option('payment_epay_md5mode') : "0")."'><br />\n\r";
  141. $output .= "<em>If MD5 security is used. 0 and MD5 is disabled. 1 and Md5 is used on the callbackurl. 2 and MD5 is used on both data to ePay and on the callbackurl. Notice that you must enter the exact same MD5 key within the ePay administration. Read more <a href='http://www.epay.dk/support/docs.asp?solution=1#md5mode' target='_blank'>here</a></em>\n\r";
  142. $output .= " </td>\n\r";
  143. $output .= "</tr>\n\r";
  144. $output .= "<tr>\n\r";
  145. $output .= " <td colspan='2'>\n\r";
  146. $output .= "<strong>MD5 security password:</strong><br />\n\r";
  147. $output .= "<input type='text' name='wpsc_options[payment_epay_md5key]' value='".get_option('payment_epay_md5key')."'><br />\n\r";
  148. $output .= "<em>The password used to MD5 security stamp. Notice that you must enter the exact same MD5 key within the ePay administration. Read more <a href='http://www.epay.dk/support/docs.asp?solution=1#md5key' target='_blank'>here</a></em>\n\r";
  149. $output .= " </td>\n\r";
  150. $output .= "</tr>\n\r";
  151. $output .= "<tr>\n\r";
  152. $output .= " <td colspan='2'>\n\r";
  153. $output .= "<strong>Add fee:</strong><br />\n\r";
  154. $output .= "<input type='text' name='wpsc_options[payment_epay_addfee]' value='".( strlen(get_option('payment_epay_addfee')) > 0 ? get_option('payment_epay_addfee') : "0")."'><br />\n\r";
  155. $output .= "<em>If the customer has to pay for the transactionfee set this value to 1. Otherwise 0. Read more <a href='http://www.epay.dk/support/docs.asp?solution=1#addfee' target='_blank'>here</a>.</em>\n\r";
  156. $output .= " </td>\n\r";
  157. $output .= "</tr>\n\r";
  158. $output .= "<tr>\n\r";
  159. $output .= " <td colspan='2'>\n\r";
  160. $output .= "<strong>Split payment:</strong><br />\n\r";
  161. $output .= "<input type='text' name='wpsc_options[payment_epay_splitpayment]' value='".( strlen(get_option('payment_epay_splitpayment')) > 0 ? get_option('payment_epay_splitpayment') : "0")."'><br />\n\r";
  162. $output .= "<em>If the payments should be captured over several times (partly orders). Set the value to 1 in order to enable splitpayment. To disable splitpayment set the value to 0. Read more <a href='http://www.epay.dk/support/docs.asp?solution=1#splitpayment' target='_blank'>here</a>.</em>\n\r";
  163. $output .= " </td>\n\r";
  164. $output .= "</tr>\n\r";
  165. $output .= "<tr>\n\r";
  166. $output .= " <td colspan='2'>\n\r";
  167. $output .= "<strong>Auth sms:</strong><br />\n\r";
  168. $output .= "<input type='text' name='wpsc_options[payment_epay_authsms]' value='".get_option('payment_epay_authsms')."'><br />\n\r";
  169. $output .= "<em>Receive an SMS as notification when the payments is made. Notice that this service is not free! Read more <a href='http://www.epay.dk/support/docs.asp?solution=1#authsms' target='_blank'>here</a>.</em>\n\r";
  170. $output .= " </td>\n\r";
  171. $output .= "</tr>\n\r";
  172. $output .= "<tr>\n\r";
  173. $output .= " <td colspan='2'>\n\r";
  174. $output .= "<strong>Auth e-mail:</strong><br />\n\r";
  175. $output .= "<input type='text' name='wpsc_options[payment_epay_authmail]' value='".get_option('payment_epay_authmail')."'><br />\n\r";
  176. $output .= "<em>Receive an e-mail as notification when the payments is made. Read more <a href='http://www.epay.dk/support/docs.asp?solution=1#authmail' target='_blank'>here</a>.</em>\n\r";
  177. $output .= " </td>\n\r";
  178. $output .= "</tr>\n\r";
  179. $output .= "<tr>\n\r";
  180. $output .= " <td colspan='2'>\n\r";
  181. $output .= "<strong>Group:</strong><br />\n\r";
  182. $output .= "<input type='text' name='wpsc_options[payment_epay_group]' value='".get_option('payment_epay_group')."'><br />\n\r";
  183. $output .= "<em>Place the payment in a specific group within the ePay admin. Read more <a href='http://www.epay.dk/support/docs.asp?solution=1#group' target='_blank'>here</a>.</em>\n\r";
  184. $output .= " </td>\n\r";
  185. $output .= "</tr>\n\r";
  186. $output .= "<tr>\n\r";
  187. $output .= " <td colspan='2'>\n\r";
  188. $output .= "<strong>Instant capture:</strong><br />\n\r";
  189. $output .= "<input type='text' name='wpsc_options[payment_epay_instantcapture]' value='".( strlen(get_option('payment_epay_instantcapture')) > 0 ? get_option('payment_epay_instantcapture') : "0")."'><br />\n\r";
  190. $output .= "<em>If the payment has to be captured as soon as it has been made. 1 and the payment will be captured. 0 will disable instant capture. Read more <a href='http://www.epay.dk/support/docs.asp?solution=1#instantcapture' target='_blank'>here</a>.</em>\n\r";
  191. $output .= " </td>\n\r";
  192. $output .= "</tr>\n\r";
  193. return $output;
  194. }
  195. //
  196. // Calculates the cardtype from the ePay internal card identifiers
  197. //
  198. function calcCardtype($cardid)
  199. {
  200. $res = "UNKNOWN!";
  201. switch(((int)$cardid)) {
  202. case 1: $res = "DANKORT"; break;
  203. case 2: $res = "VISA_DANKORT"; break;
  204. case 3: $res = "VISA_ELECTRON_FOREIGN"; break;
  205. case 4: $res = "MASTERCARD"; break;
  206. case 5: $res = "MASTERCARD_FOREIGN"; break;
  207. case 6: $res = "VISA_ELECTRON"; break;
  208. case 7: $res = "JCB"; break;
  209. case 8: $res = "DINERS"; break;
  210. case 9: $res = "MAESTRO"; break;
  211. case 10: $res = "AMERICAN_EXPRESS"; break;
  212. case 11: $res = "EDK"; break;
  213. case 12: $res = "DINERS_FOREIGN"; break;
  214. case 14: $res = "AMERICAN_EXPRESS_FOREIGN"; break;
  215. case 15: $res = "MAESTRO_FOREIGN"; break;
  216. case 16: $res = "FORBRUGSFORENINGEN"; break;
  217. case 17: $res = "EWIRE"; break;
  218. case 18: $res = "VISA"; break;
  219. case 19: $res = "IKANO"; break;
  220. case 21: $res = "Nordea e-betaling"; break;
  221. case 22: $res = "Danske Netbetaling"; break;
  222. case 23: $res = "LIC_MASTERCARD"; break;
  223. case 24: $res = "LIC_MASTERCARD_FOREIGN"; break;
  224. }
  225. return $res;
  226. }
  227. //
  228. // Function which handles the callback from ePay
  229. //
  230. function nzshpcrt_epay_callback()
  231. {
  232. global $wpdb;
  233. if ((isset($_GET['epay_callback']) && $_GET['epay_callback'] == '1') || (isset($_GET['epay_accept']) && $_GET['epay_accept'] == '1')) {
  234. if (isset($_GET['amount']) && isset($_GET['orderid']) && isset($_GET['tid']) && isset($_GET['cur']) && isset($_GET['cardid'])) {
  235. //
  236. // Extract the order
  237. //
  238. $purchase_log = $wpdb->get_row("SELECT * FROM `".WPSC_TABLE_PURCHASE_LOGS."` WHERE id = ".$_GET['orderid'] ." LIMIT 1",ARRAY_A) ;
  239. if ($purchase_log['totalprice']==0) {
  240. echo "<h1>INVALID ORDER!</h1>";
  241. exit();
  242. }
  243. //
  244. // Validate the MD5 answer from ePay
  245. //
  246. if (get_option('payment_epay_md5mode') == '1' || get_option('payment_epay_md5mode') == '2') {
  247. $ekey = "";
  248. $genkey = MD5($_GET['amount'] . $_GET['orderid'] . $_GET['tid'] . get_option('payment_epay_md5key'));
  249. if (isset($_GET['eKey'])) $ekey = $_GET['eKey'];
  250. if ($ekey != $genkey) {
  251. echo "<h1>Error in MD5! The key " . $ekey . " does not match the local generated " . $genkey . "</h1>";
  252. exit();
  253. }
  254. }
  255. $sql = "UPDATE ".WPSC_TABLE_PURCHASE_LOGS." set transactid = " . $_GET['tid'] . ", processed = 3 where id = " . $_GET['orderid'];
  256. $wpdb->query($sql);
  257. //
  258. // Only break if the answer is from callback
  259. //
  260. if (isset($_GET['epay_callback']) && $_GET['epay_callback'] == '1') {
  261. $sql = "UPDATE ".WPSC_TABLE_PURCHASE_LOGS." set notes = '" . $purchase_log['notes'] . "\nPayment approved at ePay with transactionid " . $_GET['tid'] . ", cardnopostfix " . $_GET['cardnopostfix'] . ", orderid " . $_GET['orderid'] . ", amount " . $_GET['amount'] . ", currency " . $_GET['cur'] . ", cardtype " . calcCardtype($_GET['cardid']) . "', processed = 3 where id = " . $_GET['orderid'];
  262. $wpdb->query($sql);
  263. echo "CALLBACK OK";
  264. exit();
  265. }
  266. }
  267. }
  268. }
  269. function nzshpcrt_epay_accept()
  270. {
  271. if (isset($_GET['epay_accept'])) {
  272. if ($_GET['epay_accept'] == '1') {
  273. //
  274. // Empty the shopping cart
  275. //
  276. $_SESSION['nzshpcrt_cart'] = null;
  277. $_SESSION['nzshpcrt_serialized_cart'] = null;
  278. //
  279. // Reflect the call to the callback handler
  280. //
  281. nzshpcrt_epay_callback();
  282. }
  283. }
  284. }
  285. //
  286. // Add action scripts
  287. //
  288. add_action('init', 'nzshpcrt_epay_callback');
  289. add_action('init', 'nzshpcrt_epay_accept');
  290. ?>