PageRenderTime 47ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/wp-shopping-cart/merchants/chronopay.php

https://github.com/alx/barceloneta
PHP | 512 lines | 431 code | 62 blank | 19 comment | 61 complexity | f11ee06fab499bedd75b5bdaf20b81f5 MD5 | raw file
  1. <?php
  2. $nzshpcrt_gateways[$num]['name'] = 'Chronopay';
  3. $nzshpcrt_gateways[$num]['internalname'] = 'chronopay';
  4. $nzshpcrt_gateways[$num]['function'] = 'gateway_chronopay';
  5. $nzshpcrt_gateways[$num]['form'] = "form_chronopay";
  6. $nzshpcrt_gateways[$num]['submit_function'] = "submit_chronopay";
  7. function gateway_chronopay($seperator, $sessionid)
  8. {
  9. global $wpdb;
  10. $purchase_log_sql = "SELECT * FROM `".$wpdb->prefix."purchase_logs` WHERE `sessionid`= ".$sessionid." LIMIT 1";
  11. $purchase_log = $wpdb->get_results($purchase_log_sql,ARRAY_A) ;
  12. $cart_sql = "SELECT * FROM `".$wpdb->prefix."cart_contents` WHERE `purchaseid`='".$purchase_log[0]['id']."'";
  13. $cart = $wpdb->get_results($cart_sql,ARRAY_A) ;
  14. // Chronopay post variables
  15. $chronopay_url = get_option('chronopay_url');
  16. $data['product_id'] = get_option('chronopay_product_id');
  17. $data['product_name'] = get_option('chronopay_product_name');
  18. $data['product_price_currency'] = get_option('chronopay_curcode');
  19. $data['language'] = get_option('chronopay_language');
  20. $data['cb_url'] = get_option('siteurl')."/?chronopay_callback=true";
  21. $data['cb_type'] = 'P';
  22. $data['decline_url'] = get_option('siteurl')."/?chronopay_callback=true";
  23. $data['cs1'] = $sessionid;
  24. $data['cs2'] = 'chronopay';
  25. $salt = get_option('chronopay_salt');
  26. $data['cs3'] = md5($salt . md5($sessionid . $salt)); // placed in here for security so that the return call can be validated as 'real'
  27. // User details
  28. if($_POST['collected_data'][get_option('chronopay_form_first_name')] != '')
  29. {
  30. $data['f_name'] = $_POST['collected_data'][get_option('chronopay_form_first_name')];
  31. }
  32. if($_POST['collected_data'][get_option('chronopay_form_last_name')] != "")
  33. {
  34. $data['s_name'] = $_POST['collected_data'][get_option('chronopay_form_last_name')];
  35. }
  36. if($_POST['collected_data'][get_option('chronopay_form_address')] != '')
  37. {
  38. $data['street'] = str_replace("\n",', ', $_POST['collected_data'][get_option('chronopay_form_address')]);
  39. }
  40. if($_POST['collected_data'][get_option('chronopay_form_city')] != '')
  41. {
  42. $data['city'] = $_POST['collected_data'][get_option('chronopay_form_city')];
  43. }
  44. if(preg_match("/^[a-zA-Z]{2}$/",$_SESSION['selected_country']))
  45. {
  46. $data['country'] = $_SESSION['selected_country'];
  47. }
  48. // Change suggested by waxfeet@gmail.com, if email to be sent is not there, dont send an email address
  49. $email_data = $wpdb->get_results("SELECT `id`,`type` FROM `".$wpdb->prefix."collect_data_forms` WHERE `type` IN ('email') AND `active` = '1'",ARRAY_A);
  50. foreach((array)$email_data as $email)
  51. {
  52. $data['email'] = $_POST['collected_data'][$email['id']];
  53. }
  54. if(($_POST['collected_data'][get_option('email_form_field')] != null) && ($data['email'] == null))
  55. {
  56. $data['email'] = $_POST['collected_data'][get_option('email_form_field')];
  57. }
  58. // Get Currency details abd price
  59. $currency_code = $wpdb->get_results("SELECT `code` FROM `".$wpdb->prefix."currency_list` WHERE `id`='".get_option(currency_type)."' LIMIT 1",ARRAY_A);
  60. $local_currency_code = $currency_code[0]['code'];
  61. $chronopay_currency_code = get_option('chronopay_curcode');
  62. // Chronopay only processes in the set currency. This is USD or EUR dependent on what the Chornopay account is set up with.
  63. // This must match the Chronopay settings set up in wordpress. Convert to the chronopay currency and calculate total.
  64. $curr=new CURRENCYCONVERTER();
  65. $decimal_places = 2;
  66. $total_price = 0;
  67. $i = 1;
  68. $all_donations = true;
  69. $all_no_shipping = true;
  70. foreach($cart as $item)
  71. {
  72. $product_data = $wpdb->get_results("SELECT * FROM `".$wpdb->prefix."product_list` WHERE `id`='".$item['prodid']."' LIMIT 1",ARRAY_A);
  73. $product_data = $product_data[0];
  74. $variation_count = count($product_variations);
  75. $variation_sql = "SELECT * FROM `".$wpdb->prefix."cart_item_variations` WHERE `cart_id`='".$item['id']."'";
  76. $variation_data = $wpdb->get_results($variation_sql,ARRAY_A);
  77. $variation_count = count($variation_data);
  78. if($variation_count >= 1)
  79. {
  80. $variation_list = " (";
  81. $j = 0;
  82. foreach($variation_data as $variation)
  83. {
  84. if($j > 0)
  85. {
  86. $variation_list .= ", ";
  87. }
  88. $value_id = $variation['venue_id'];
  89. $value_data = $wpdb->get_results("SELECT * FROM `".$wpdb->prefix."variation_values` WHERE `id`='".$value_id."' LIMIT 1",ARRAY_A);
  90. $variation_list .= $value_data[0]['name'];
  91. $j++;
  92. }
  93. $variation_list .= ")";
  94. }
  95. else
  96. {
  97. $variation_list = '';
  98. }
  99. $local_currency_productprice = $item['price'];
  100. $local_currency_shipping = nzshpcrt_determine_item_shipping($item['prodid'], 1, $_SESSION['delivery_country']);
  101. if($chronopay_currency_code != $local_currency_code)
  102. {
  103. $chronopay_currency_productprice = $curr->convert($local_currency_productprice,$chronopay_currency_code,$local_currency_code);
  104. $chronopay_currency_shipping = $curr->convert($local_currency_shipping,$chronopay_currency_code,$local_currency_code);
  105. }
  106. else
  107. {
  108. $chronopay_currency_productprice = $local_currency_productprice;
  109. $chronopay_currency_shipping = $local_currency_shipping;
  110. }
  111. $data['item_name_'.$i] = $product_data['name'].$variation_list;
  112. $data['amount_'.$i] = number_format(sprintf("%01.2f", $chronopay_currency_productprice),$decimal_places,'.','');
  113. $data['quantity_'.$i] = $item['quantity'];
  114. $data['item_number_'.$i] = $product_data['id'];
  115. if($item['donation'] !=1)
  116. {
  117. $all_donations = false;
  118. $data['shipping_'.$i] = number_format($chronopay_currency_shipping,$decimal_places,'.','');
  119. $data['shipping2_'.$i] = number_format($chronopay_currency_shipping,$decimal_places,'.','');
  120. }
  121. else
  122. {
  123. $data['shipping_'.$i] = number_format(0,$decimal_places,'.','');
  124. $data['shipping2_'.$i] = number_format(0,$decimal_places,'.','');
  125. }
  126. if($product_data['no_shipping'] != 1) {
  127. $all_no_shipping = false;
  128. }
  129. $total_price = $total_price + ($data['amount_'.$i] * $data['quantity_'.$i]);
  130. if( $all_no_shipping != false )
  131. $total_price = $total_price + $data['shipping_'.$i] + $data['shipping2_'.$i];
  132. $i++;
  133. }
  134. $base_shipping = nzshpcrt_determine_base_shipping(0, $_SESSION['delivery_country']);
  135. if(($base_shipping > 0) && ($all_donations == false) && ($all_no_shipping == false))
  136. {
  137. if($chronopay_currency_code != $local_currency_code)
  138. {
  139. $base_shipping = $curr->convert($base_shipping,$chronopay_currency_code,$local_currency_code);
  140. }
  141. $data['handling_cart'] = number_format($base_shipping,$decimal_places,'.','');
  142. $total_price += number_format($base_shipping,$decimal_places,'.','');
  143. }
  144. $data['product_price'] = $total_price;
  145. // Create Form to post to Chronopay
  146. $output = "
  147. <form id=\"chronopay_form\" name=\"chronopay_form\" method=\"post\" action=\"$chronopay_url\">\n";
  148. foreach($data as $n=>$v) {
  149. $output .= " <input type=\"hidden\" name=\"$n\" value=\"$v\" />\n";
  150. }
  151. $output .= " <input type=\"submit\" value=\"Continue to ChronoPay\" />
  152. </form>
  153. ";
  154. // echo form..
  155. if( get_option('chronopay_debug') == 1)
  156. {
  157. echo ("DEBUG MODE ON!!<br/>");
  158. echo("The following form is created and would be posted to Chronopay for processing. Press submit to continue:<br/>");
  159. echo("<pre>".htmlspecialchars($output)."</pre>");
  160. }
  161. echo($output);
  162. if(get_option('chronopay_debug') == 0)
  163. {
  164. echo "<script language=\"javascript\" type=\"text/javascript\">document.getElementById('chronopay_form').submit();</script>";
  165. }
  166. exit();
  167. }
  168. function nzshpcrt_chronopay_callback()
  169. {
  170. global $wpdb;
  171. // needs to execute on page start
  172. // look at page 36
  173. if($_GET['chronopay_callback'] == 'true' && $_POST['cs2'] == 'chronopay')
  174. {
  175. // This is a call from chronopay. validate that it is from a chronopay server in the and process.
  176. // validate cs3 variable to see if it makes sense for security
  177. $salt = get_option('chronopay_salt');
  178. $gen_hash = md5($salt . md5($_POST['cs1'] . $salt));
  179. if($gen_hash == $_POST['cs3'])
  180. {
  181. // Added in to fake a TX number for testing. ChronoPay dev accounts do not return a trans_id.
  182. //if($_POST['transaction_id'] == '')
  183. // $_POST['transaction_id'] = 'testid123123';
  184. // process response.
  185. $sessionid = trim(stripslashes($_POST['cs1']));
  186. $transaction_id = trim(stripslashes($_POST['transaction_id']));
  187. $verification_data['trans_id'] = trim(stripslashes($_POST['transaction_id']));
  188. $verification_data['trans_type'] = trim(stripslashes($_POST['transaction_type']));
  189. switch($verification_data['trans_type'])
  190. {
  191. case 'onetime': // All successful processing statuses.
  192. case 'initial':
  193. case 'rebill':
  194. $wpdb->query("UPDATE `".$wpdb->prefix."purchase_logs` SET
  195. `processed` = '2',
  196. `transactid` = '".$transaction_id."',
  197. `date` = '".time()."'
  198. WHERE `sessionid` = ".$sessionid." LIMIT 1");
  199. transaction_results($sessionid, false, $transaction_id);
  200. break;
  201. case 'decline': // if it fails, delete it
  202. $log_id = $wpdb->get_var("SELECT `id` FROM `".$wpdb->prefix."purchase_logs` WHERE `sessionid`='$sessionid' LIMIT 1");
  203. $delete_log_form_sql = "SELECT * FROM `".$wpdb->prefix."cart_contents` WHERE `purchaseid`='$log_id'";
  204. $cart_content = $wpdb->get_results($delete_log_form_sql,ARRAY_A);
  205. foreach((array)$cart_content as $cart_item)
  206. {
  207. $cart_item_variations = $wpdb->query("DELETE FROM `".$wpdb->prefix."cart_item_variations` WHERE `cart_id` = '".$cart_item['id']."'", ARRAY_A);
  208. }
  209. $wpdb->query("DELETE FROM `".$wpdb->prefix."cart_contents` WHERE `purchaseid`='$log_id'");
  210. $wpdb->query("DELETE FROM `".$wpdb->prefix."submited_form_data` WHERE `log_id` IN ('$log_id')");
  211. $wpdb->query("DELETE FROM `".$wpdb->prefix."purchase_logs` WHERE `id`='$log_id' LIMIT 1");
  212. break;
  213. case 'Pending': // need to wait for "Completed" before processing
  214. $sql = "UPDATE `".$wpdb->prefix."purchase_logs` SET `transactid` = '".$transaction_id."', `date` = '".time()."' WHERE `sessionid` = ".$sessionid." LIMIT 1";
  215. $wpdb->query($sql) ;
  216. break;
  217. default: // if nothing, do nothing, safest course of action here.
  218. break;
  219. }
  220. }
  221. else
  222. {
  223. // Security Hash failed!!.. notify someone..
  224. $message = "This message has been sent because a call to your ChronoPay function was made by a server that did not have the correct security key. This could mean someone is trying to hack your payment site. The details of the call are below.\n\r\n\r";
  225. $message .= "OUR_POST:\n\r".print_r($header . $req,true)."\n\r\n\r";
  226. $message .= "THEIR_POST:\n\r".print_r($_POST,true)."\n\r\n\r";
  227. $message .= "GET:\n\r".print_r($_GET,true)."\n\r\n\r";
  228. $message .= "SERVER:\n\r".print_r($_SERVER,true)."\n\r\n\r";
  229. mail(get_option('purch_log_email'), "ChronoPay Security Key Failed!", $message);
  230. }
  231. // If in debug, email details
  232. if(get_option('chronopay_debug') == 1)
  233. {
  234. $message = "This is a debugging message sent because it appears that you are in debug mode.\n\rEnsure ChronoPay debug is turned off once you are happy with the function.\n\r\n\r";
  235. $message .= "OUR_POST:\n\r".print_r($header . $req,true)."\n\r\n\r";
  236. $message .= "THEIR_POST:\n\r".print_r($_POST,true)."\n\r\n\r";
  237. $message .= "GET:\n\r".print_r($_GET,true)."\n\r\n\r";
  238. $message .= "SERVER:\n\r".print_r($_SERVER,true)."\n\r\n\r";
  239. mail(get_option('purch_log_email'), "ChronoPay Data", $message);
  240. }
  241. }
  242. }
  243. function nzshpcrt_chronopay_results()
  244. {
  245. // Function used to translate the ChronoPay returned cs1=sessionid POST variable into the recognised GET variable for the transaction results page.
  246. if($_POST['cs1'] !='' && $_GET['sessionid'] == '')
  247. {
  248. $_GET['sessionid'] = $_POST['cs1'];
  249. }
  250. }
  251. function submit_chronopay()
  252. {
  253. if($_POST['chronopay_product_id'] != null)
  254. {
  255. update_option('chronopay_product_id', $_POST['chronopay_product_id']);
  256. }
  257. if($_POST['chronopay_product_name'] != null)
  258. {
  259. update_option('chronopay_product_name', $_POST['chronopay_product_name']);
  260. }
  261. if($_POST['chronopay_curcode'] != null)
  262. {
  263. update_option('chronopay_curcode', $_POST['chronopay_curcode']);
  264. }
  265. if($_POST['chronopay_language'] != null)
  266. {
  267. update_option('chronopay_language', $_POST['chronopay_language']);
  268. }
  269. if($_POST['chronopay_url'] != null)
  270. {
  271. update_option('chronopay_url', $_POST['chronopay_url']);
  272. }
  273. if($_POST['chronopay_salt'] != null)
  274. {
  275. update_option('chronopay_salt', $_POST['chronopay_salt']);
  276. }
  277. if($_POST['chronopay_debug'] != null)
  278. {
  279. update_option('chronopay_debug', $_POST['chronopay_debug']);
  280. }
  281. foreach((array)$_POST['chronopay_form'] as $form => $value)
  282. {
  283. update_option(('chronopay_form_'.$form), $value);
  284. }
  285. return true;
  286. }
  287. function form_chronopay()
  288. {
  289. $select_currency[get_option('chronopay_curcode')] = "selected='true'";
  290. $select_language[get_option('chronopay_language')] = "selected='true'";
  291. $chronopay_url = ( get_option('chronopay_url')=='' ? 'https://secure.chronopay.com/index_shop.cgi' : get_option('chronopay_url') );
  292. $chronopay_salt = ( get_option('chronopay_salt')=='' ? 'changeme' : get_option('chronopay_salt') );
  293. $chronopay_debug = get_option('chronopay_debug');
  294. $chronopay_debug1 = "";
  295. $chronopay_debug2 = "";
  296. switch($chronopay_debug)
  297. {
  298. case 0:
  299. $chronopay_debug2 = "checked ='true'";
  300. break;
  301. case 1:
  302. $chronopay_debug1 = "checked ='true'";
  303. break;
  304. }
  305. $output = "
  306. <tr>
  307. <td>ChronoPay Product ID</td>
  308. <td><input type='text' size='40' value='".get_option('chronopay_product_id')."' name='chronopay_product_id' /></td>
  309. </tr>
  310. <tr>
  311. <td>&nbsp;</td>
  312. <td><small>This should be set to your product ID that has been set up in the ChronoPay client interface.
  313. This is the ChronoPay product that all purchases will be processed against. The cost will be changed depending on the grand total of the users cart.</small></td>
  314. </tr>
  315. <tr>
  316. <td>ChronoPay Product Name</td>
  317. <td><input type='text' size='40' value='".get_option('chronopay_product_name')."' name='chronopay_product_name' /></td>
  318. </tr>
  319. <tr>
  320. <td>&nbsp;</td>
  321. <td><small>This is not important and is usually set to the name of the web shop. It is displayed on the ChronoPay secure processing page.</small></td>
  322. </tr>
  323. <tr>
  324. <td>ChronoPay Accepted Currency (USD, EUR)</td>
  325. <td><select name='chronopay_curcode'>
  326. <option ".$select_currency['USD']." value='USD'>USD - U.S. Dollar</option>
  327. <option ".$select_currency['EUR']." value='EUR'>EUR - Euros</option>
  328. </select>
  329. </td>
  330. </tr>
  331. <tr>
  332. <td>&nbsp;</td>
  333. <td><small>The currency code that ChronoPay will process the payment in. All products must be set up in this currency.</small></td>
  334. </tr>
  335. <tr>
  336. <td>ChronoPay Language</td>
  337. <td><select name='chronopay_language'>
  338. <option ".$select_language['EN']." value='EN'>Engish</option>
  339. <option ".$select_language['ES']." value='ES'>Spanish</option>
  340. <option ".$select_language['NL']." value='NL'>Dutch</option>
  341. <option ".$select_language['RU']." value='RU'>Russian</option>
  342. </select>
  343. </td>
  344. </tr>
  345. <tr>
  346. <td>&nbsp;</td>
  347. <td><small>The language that the ChronoPay secure processing page will be displayed in.</small></td>
  348. </tr>
  349. <tr>
  350. <td>ChronoPay processing URL</td>
  351. <td><input type='text' size='40' value='".$chronopay_url."' name='chronopay_url' /></td>
  352. </tr>
  353. <tr>
  354. <td>&nbsp;</td>
  355. <td><small>URL of the secure payment page customers are sent to for payment processing. If unsure leave at default setting.</small></td>
  356. </tr>
  357. <tr>
  358. <td>ChronoPay return URL</td>
  359. <td><input type='text' size='40' value='".get_option('transact_url')."' name='chronopay_return_url' /></td>
  360. </tr>
  361. <tr>
  362. <td>&nbsp;</td>
  363. <td><small>Enter this URL in the ChronoPay web client against the Product ID that you have set up. This page is the transaction details page that you have configured in Shop Options. It can not be edited on this page.</small></td>
  364. </tr>
  365. <tr>
  366. <td>ChronoPay Security Key</td>
  367. <td><input type='text' size='40' value='".$chronopay_salt."' name='chronopay_salt' /></td>
  368. </tr>
  369. <tr>
  370. <td>&nbsp;</td>
  371. <td><small>A bit of security... This is a keyword that is used to ensure transaction approval calls from ChronoPay to this application are real and were instigated from this server. Enter a unique word into this field.</small></td>
  372. </tr>
  373. <tr>
  374. <td>ChronoPay Debug Mode</td>
  375. <td>
  376. <input type='radio' value='1' name='chronopay_debug' id='chronopay_debug1' ".$chronopay_debug1." /> <label for='chronopay_debug1'>".TXT_WPSC_YES."</label> &nbsp;
  377. <input type='radio' value='0' name='chronopay_debug' id='chronopay_debug2' ".$chronopay_debug2." /> <label for='chronopay_debug2'>".TXT_WPSC_NO."</label>
  378. </td>
  379. </tr>
  380. <tr>
  381. <td>&nbsp;</td>
  382. <td><small>Debug mode is used to write HTTP communications between the ChronoPay server and your host to a log file. This should only be activated for testing!</small></td>
  383. </tr>
  384. <tr class='update_gateway' >
  385. <td colspan='2'>
  386. <div class='submit'>
  387. <input type='submit' value='Update &raquo;' name='updateoption'/>
  388. </div>
  389. </td>
  390. </tr>
  391. <tr class='firstrowth'>
  392. <td style='border-bottom: medium none;' colspan='2'>
  393. <strong class='form_group'>Forms Sent to Gateway</strong>
  394. </td>
  395. </tr>
  396. <tr>
  397. <td>First Name Field</td>
  398. <td><select name='chronopay_form[first_name]'>
  399. ".nzshpcrt_form_field_list(get_option('chronopay_form_first_name'))."
  400. </select>
  401. </td>
  402. </tr>
  403. <tr>
  404. <td>Last Name Field</td>
  405. <td><select name='chronopay_form[last_name]'>
  406. ".nzshpcrt_form_field_list(get_option('chronopay_form_last_name'))."
  407. </select>
  408. </td>
  409. </tr>
  410. <tr>
  411. <td>Address Field</td>
  412. <td><select name='chronopay_form[address]'>
  413. ".nzshpcrt_form_field_list(get_option('chronopay_form_address'))."
  414. </select>
  415. </td>
  416. </tr>
  417. <tr>
  418. <td>City Field</td>
  419. <td><select name='chronopay_form[city]'>
  420. ".nzshpcrt_form_field_list(get_option('chronopay_form_city'))."
  421. </select>
  422. </td>
  423. </tr>
  424. <tr>
  425. <td>State Field</td>
  426. <td><select name='chronopay_form[state]'>
  427. ".nzshpcrt_form_field_list(get_option('chronopay_form_state'))."
  428. </select>
  429. </td>
  430. </tr>
  431. <tr>
  432. <td>Postal code/Zip code Field</td>
  433. <td><select name='chronopay_form[post_code]'>
  434. ".nzshpcrt_form_field_list(get_option('chronopay_form_post_code'))."
  435. </select>
  436. </td>
  437. </tr>
  438. <tr>
  439. <td>Country Field</td>
  440. <td><select name='chronopay_form[country]'>
  441. ".nzshpcrt_form_field_list(get_option('chronopay_form_country'))."
  442. </select>
  443. </td>
  444. </tr>
  445. ";
  446. return $output;
  447. }
  448. add_action('init', 'nzshpcrt_chronopay_callback');
  449. add_action('init', 'nzshpcrt_chronopay_results');
  450. ?>