PageRenderTime 56ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/alx/barceloneta
PHP | 541 lines | 448 code | 61 blank | 32 comment | 74 complexity | 2e7eae092a63a0d2216e6666d47e0a43 MD5 | raw file
  1. <?php
  2. $nzshpcrt_gateways[$num]['name'] = 'Paypal';
  3. $nzshpcrt_gateways[$num]['internalname'] = 'paypal_multiple';
  4. $nzshpcrt_gateways[$num]['function'] = 'gateway_paypal_multiple';
  5. $nzshpcrt_gateways[$num]['form'] = "form_paypal_multiple";
  6. $nzshpcrt_gateways[$num]['submit_function'] = "submit_paypal_multiple";
  7. function gateway_paypal_multiple($seperator, $sessionid) {
  8. global $wpdb;
  9. $purchase_log_sql = "SELECT * FROM `".$wpdb->prefix."purchase_logs` WHERE `sessionid`= ".$sessionid." LIMIT 1";
  10. $purchase_log = $wpdb->get_results($purchase_log_sql,ARRAY_A) ;
  11. //exit(print_r($purchase_log,1));
  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. //written by allen
  15. //exit("<pre>".print_r($cart,true)."</pre>");
  16. $member_subtype = get_product_meta($cart[0]['prodid'],'is_permenant',true);
  17. $status = get_product_meta($cart[0]['prodid'],'is_membership',true);
  18. $is_member = $status;
  19. $is_perm = $member_subtype;
  20. //end of written by allen
  21. $transact_url = get_option('transact_url');
  22. // paypal connection variables
  23. $data['business'] = get_option('paypal_multiple_business');
  24. $data['return'] = urlencode($transact_url.$seperator."sessionid=".$sessionid."&gateway=paypal");
  25. $data['cancel_return'] = urlencode($transact_url);
  26. $data['notify_url'] =urlencode(get_option('siteurl')."/?ipn_request=true");
  27. $data['rm'] = '2';
  28. // look up the currency codes and local price
  29. $currency_code = $wpdb->get_results("SELECT `code` FROM `".$wpdb->prefix."currency_list` WHERE `id`='".get_option('currency_type')."' LIMIT 1",ARRAY_A);
  30. $local_currency_code = $currency_code[0]['code'];
  31. $paypal_currency_code = get_option('paypal_curcode');
  32. //exit(get_option('currency_type'). " ".$paypal_currency_code);
  33. // Stupid paypal only accepts payments in one of 5 currencies. Convert from the currency of the users shopping cart to the curency which the user has specified in their paypal preferences.
  34. $curr=new CURRENCYCONVERTER();
  35. $data['currency_code'] = $paypal_currency_code;
  36. // $data['lc'] = 'US';
  37. $data['lc'] = 'NZ';
  38. $data['bn'] = 'wp_e-commerce';
  39. $data['no_shipping'] = '2';
  40. if(get_option('address_override') == 1) {
  41. $data['address_override'] = '1';
  42. }
  43. $data['no_note'] = '1';
  44. switch($paypal_currency_code) {
  45. case "JPY":
  46. $decimal_places = 0;
  47. break;
  48. case "HUF":
  49. $decimal_places = 0;
  50. default:
  51. $decimal_places = 2;
  52. break;
  53. }
  54. $i = 1;
  55. $all_donations = true;
  56. $all_no_shipping = true;
  57. $total = nzshpcrt_overall_total_price($_SESSION['selected_country'],false,true);
  58. $discount = nzshpcrt_apply_coupon($total,$_SESSION['coupon_num']);
  59. if(($discount > 0) && ($_SESSION['coupon_num'] != null)) {
  60. $data['item_name_'.$i] = "Your Shopping Cart";
  61. $data['amount_'.$i] = number_format(sprintf("%01.2f", $discount),$decimal_places,'.','');
  62. $data['quantity_'.$i] = 1;
  63. // $data['item_number_'.$i] = 0;
  64. $data['shipping_'.$i] = 0;
  65. $data['shipping2_'.$i] = 0;
  66. $data['handling_'.$i] = 0;
  67. $i++;
  68. } else {
  69. foreach($cart as $item) {
  70. $product_data = $wpdb->get_results("SELECT * FROM `".$wpdb->prefix."product_list` WHERE `id`='".$item['prodid']."' LIMIT 1",ARRAY_A);
  71. $product_data = $product_data[0];
  72. $variation_count = count($product_variations);
  73. $variation_sql = "SELECT * FROM `".$wpdb->prefix."cart_item_variations` WHERE `cart_id`='".$item['id']."'";
  74. $variation_data = $wpdb->get_results($variation_sql,ARRAY_A);
  75. $variation_count = count($variation_data);
  76. if($variation_count >= 1) {
  77. $variation_list = " (";
  78. $j = 0;
  79. foreach($variation_data as $variation) {
  80. if($j > 0) {
  81. $variation_list .= ", ";
  82. }
  83. $value_id = $variation['value_id'];
  84. $value_data = $wpdb->get_results("SELECT * FROM `".$wpdb->prefix."variation_values` WHERE `id`='".$value_id."' LIMIT 1",ARRAY_A);
  85. $variation_list .= $value_data[0]['name'];
  86. $j++;
  87. }
  88. $variation_list .= ")";
  89. } else {
  90. $variation_list = '';
  91. }
  92. $local_currency_productprice = $item['price'];
  93. $local_currency_shipping = $item['pnp'];
  94. //exit($local_currency_productprice . " " . $local_currency_code);
  95. if($paypal_currency_code != $local_currency_code) {
  96. $paypal_currency_productprice = $curr->convert($local_currency_productprice,$paypal_currency_code,$local_currency_code);
  97. $paypal_currency_shipping = $curr->convert($local_currency_shipping,$paypal_currency_code,$local_currency_code);
  98. } else {
  99. $paypal_currency_productprice = $local_currency_productprice;
  100. $paypal_currency_shipping = $local_currency_shipping;
  101. }
  102. //exit("---->".$paypal_currency_shipping);
  103. $data['item_name_'.$i] = urlencode(stripslashes($product_data['name']).$variation_list);
  104. $data['amount_'.$i] = number_format(sprintf("%01.2f", $paypal_currency_productprice),$decimal_places,'.','');
  105. $data['quantity_'.$i] = $item['quantity'];
  106. $data['item_number_'.$i] = $product_data['id'];
  107. if($item['donation'] !=1) {
  108. $all_donations = false;
  109. $data['shipping_'.$i] = number_format($paypal_currency_shipping,$decimal_places,'.','');
  110. $data['shipping2_'.$i] = number_format($paypal_currency_shipping,$decimal_places,'.','');
  111. } else {
  112. $data['shipping_'.$i] = number_format(0,$decimal_places,'.','');
  113. $data['shipping2_'.$i] = number_format(0,$decimal_places,'.','');
  114. }
  115. if($product_data['no_shipping'] != 1) {
  116. $all_no_shipping = false;
  117. }
  118. $data['handling_'.$i] = '';
  119. $i++;
  120. }
  121. }
  122. $data['tax'] = '';
  123. $base_shipping = $purchase_log[0]['base_shipping'];
  124. //exit($base_shipping);
  125. if(($base_shipping > 0) && ($all_donations == false) && ($all_no_shipping == false)) {
  126. $data['handling_cart'] = number_format($base_shipping,$decimal_places,'.','');
  127. }
  128. $data['custom'] = '';
  129. $data['invoice'] = $sessionid;
  130. // User details
  131. if($_POST['collected_data'][get_option('paypal_form_first_name')] != '') {
  132. $data['first_name'] = urlencode($_POST['collected_data'][get_option('paypal_form_first_name')]);
  133. }
  134. if($_POST['collected_data'][get_option('paypal_form_last_name')] != '') {
  135. $data['last_name'] = urlencode($_POST['collected_data'][get_option('paypal_form_last_name')]);
  136. }
  137. if($_POST['collected_data'][get_option('paypal_form_address')] != '') {
  138. $address_rows = explode("\n\r",$_POST['collected_data'][get_option('paypal_form_address')]);
  139. $data['address1'] = urlencode(str_replace(array("\n", "\r"), '', $address_rows[0]));
  140. unset($address_rows[0]);
  141. if($address_rows != null) {
  142. $data['address2'] = implode(", ",$address_rows);
  143. } else {
  144. $data['address2'] = '';
  145. }
  146. }
  147. if($_POST['collected_data'][get_option('paypal_form_city')] != '') {
  148. $data['city'] = urlencode($_POST['collected_data'][get_option('paypal_form_city')]);
  149. }
  150. if(preg_match("/^[a-zA-Z]{2}$/",$_SESSION['selected_country'])) {
  151. $data['country'] = $_SESSION['selected_country'];
  152. }
  153. if(is_numeric($_POST['collected_data'][get_option('paypal_form_post_code')])) {
  154. $data['zip'] = urlencode($_POST['collected_data'][get_option('paypal_form_post_code')]);
  155. }
  156. // Change suggested by waxfeet@gmail.com, if email to be sent is not there, dont send an email address
  157. $email_data = $wpdb->get_results("SELECT `id`,`type` FROM `".$wpdb->prefix."collect_data_forms` WHERE `type` IN ('email') AND `active` = '1'",ARRAY_A);
  158. foreach((array)$email_data as $email) {
  159. $data['email'] = $_POST['collected_data'][$email['id']];
  160. }
  161. if(($_POST['collected_data'][get_option('email_form_field')] != null) && ($data['email'] == null)) {
  162. $data['email'] = $_POST['collected_data'][get_option('email_form_field')];
  163. }
  164. $data['upload'] = '1';
  165. $data['cmd'] = "_ext-enter";
  166. $data['redirect_cmd'] = "_cart";
  167. $datacount = count($data);
  168. $num = 0;
  169. foreach($data as $key=>$value) {
  170. $amp = '&';
  171. $num++;
  172. if($num == $datacount) {
  173. $amp = '';
  174. }
  175. //$output .= $key.'='.urlencode($value).$amp;
  176. $output .= $key.'='.$value.$amp;
  177. }
  178. if(get_option('paypal_ipn') == 0) { //ensures that digital downloads still work for people without IPN, less secure, though
  179. //$wpdb->query("UPDATE `".$wpdb->prefix."purchase_logs` SET `processed` = '2' WHERE `sessionid` = ".$sessionid." LIMIT 1");
  180. }
  181. //written by allen
  182. if ($is_member == '1') {
  183. $membership_length = get_product_meta($cart[0]['prodid'],'membership_length',true);
  184. if ($is_perm == '1'){
  185. $permsub = '&src=1';
  186. } else {
  187. $permsub = '';
  188. }
  189. $output = 'cmd=_xclick-subscriptions&business='.urlencode($data['business']).'&no_note=1&item_name='.urlencode($data['item_name_1']).'&return='.urlencode($data['return']).'&cancel_return='.urlencode($data['cancel_return']).$permsub.'&a3='.urlencode($data['amount_1']).'&p3='.urlencode($membership_length['length']).'&t3='.urlencode(strtoupper($membership_length['unit']));
  190. }
  191. // echo "<a href='".get_option('paypal_multiple_url')."?".$output."'>Test the URL here</a>";
  192. // exit("<pre>".print_r($data,true)."</pre>");
  193. header("Location: ".get_option('paypal_multiple_url')."?".$output);
  194. exit();
  195. }
  196. function nzshpcrt_paypal_ipn()
  197. {
  198. global $wpdb;
  199. // needs to execute on page start
  200. // look at page 36
  201. if(($_GET['ipn_request'] == 'true') && (get_option('paypal_ipn') == 1)) {
  202. // read the post from PayPal system and add 'cmd'
  203. $req = 'cmd=_notify-validate';
  204. $message = "";
  205. foreach ($_POST as $key => $value) {
  206. $value = urlencode(stripslashes($value));
  207. $req .= "&$key=$value";
  208. }
  209. //$req .= "&ipn_request=true";
  210. $replace_strings[0] = 'http://';
  211. $replace_strings[1] = 'https://';
  212. $replace_strings[2] = '/cgi-bin/webscr';
  213. $paypal_url = str_replace($replace_strings, "",get_option('paypal_multiple_url'));
  214. // post back to PayPal system to validate
  215. $header .= "POST /cgi-bin/webscr HTTP/1.0\r\n";
  216. $header .= "Content-Type: application/x-www-form-urlencoded\r\n";
  217. $header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
  218. $fp = fsockopen ($paypal_url, 80, $errno, $errstr, 30);
  219. // assign posted variables to local variables
  220. $sessionid = $_POST['invoice'];
  221. $transaction_id = $_POST['txn_id'];
  222. $verification_data['item_name'] = $_POST['item_name'];
  223. $verification_data['item_number'] = $_POST['item_number'];
  224. $verification_data['payment_status'] = $_POST['payment_status'];
  225. $verification_data['payment_amount'] = $_POST['mc_gross'];
  226. $verification_data['payment_currency'] = $_POST['mc_currency'];
  227. $verification_data['txn_id'] = $_POST['txn_id'];
  228. $verification_data['receiver_email'] = $_POST['receiver_email'];
  229. $verification_data['payer_email'] = $_POST['payer_email'];
  230. if(!$fp) {
  231. //mail(get_option('purch_log_email'),'IPN CONNECTION FAILS IT',("Fix the paypal URL, it is currently:\n\r". $paypal_url));
  232. // HTTP ERROR
  233. } else {
  234. fputs ($fp, $header . $req);
  235. while (!feof($fp)) {
  236. $res = fgets ($fp, 1024);
  237. if(strcmp ($res, "VERIFIED") == 0){
  238. switch($verification_data['payment_status']) {
  239. case 'Processed': // I think this is mostly equivalent to Completed
  240. case 'Completed':
  241. $wpdb->query("UPDATE `".$wpdb->prefix."purchase_logs` SET `processed` = '2' WHERE `sessionid` = ".$sessionid." LIMIT 1");
  242. transaction_results($sessionid, false, $transaction_id);
  243. break;
  244. case 'Failed': // if it fails, delete it
  245. $log_id = $wpdb->get_var("SELECT `id` FROM `".$wpdb->prefix."purchase_logs` WHERE `sessionid`='$sessionid' LIMIT 1");
  246. $delete_log_form_sql = "SELECT * FROM `".$wpdb->prefix."cart_contents` WHERE `purchaseid`='$log_id'";
  247. $cart_content = $wpdb->get_results($delete_log_form_sql,ARRAY_A);
  248. foreach((array)$cart_content as $cart_item) {
  249. $cart_item_variations = $wpdb->query("DELETE FROM `".$wpdb->prefix."cart_item_variations` WHERE `cart_id` = '".$cart_item['id']."'", ARRAY_A);
  250. }
  251. $wpdb->query("DELETE FROM `".$wpdb->prefix."cart_contents` WHERE `purchaseid`='$log_id'");
  252. $wpdb->query("DELETE FROM `".$wpdb->prefix."submited_form_data` WHERE `log_id` IN ('$log_id')");
  253. $wpdb->query("DELETE FROM `".$wpdb->prefix."purchase_logs` WHERE `id`='$log_id' LIMIT 1");
  254. break;
  255. case 'Pending': // need to wait for "Completed" before processing
  256. $sql = "UPDATE `".$wpdb->prefix."purchase_logs` SET `transactid` = '".$transaction_id."', `date` = '".time()."' WHERE `sessionid` = ".$sessionid." LIMIT 1";
  257. $wpdb->query($sql) ;
  258. break;
  259. default: // if nothing, do nothing, safest course of action here.
  260. break;
  261. }
  262. } else if (strcmp ($res, "INVALID") == 0) {
  263. // Its already logged, not much need to do more
  264. }
  265. }
  266. fclose ($fp);
  267. }
  268. /*
  269. * Detect use of sandbox mode, if sandbox mode is present, send debugging email.
  270. */
  271. if(stristr(get_option('paypal_multiple_url'), "sandbox")) {
  272. $message = "This is a debugging message sent because it appears that you are using sandbox mode.\n\rIt is only sent if the paypal URL contains the word \"sandbox\"\n\r\n\r";
  273. $message .= "OUR_POST:\n\r".print_r($header . $req,true)."\n\r\n\r";
  274. $message .= "THEIR_POST:\n\r".print_r($_POST,true)."\n\r\n\r";
  275. $message .= "GET:\n\r".print_r($_GET,true)."\n\r\n\r";
  276. $message .= "SERVER:\n\r".print_r($_SERVER,true)."\n\r\n\r";
  277. $wpdb->query("INSERT INTO `paypal_log` ( `id` , `text` , `date` ) VALUES ( '', '$message', NOW( ) );");
  278. mail(get_option('purch_log_email'), "IPN Data", $message);
  279. }
  280. }
  281. }
  282. function submit_paypal_multiple(){
  283. if($_POST['paypal_multiple_business'] != null) {
  284. update_option('paypal_multiple_business', $_POST['paypal_multiple_business']);
  285. }
  286. if($_POST['paypal_multiple_url'] != null) {
  287. update_option('paypal_multiple_url', $_POST['paypal_multiple_url']);
  288. }
  289. if($_POST['paypal_curcode'] != null) {
  290. update_option('paypal_curcode', $_POST['paypal_curcode']);
  291. }
  292. if($_POST['paypal_curcode'] != null) {
  293. update_option('paypal_curcode', $_POST['paypal_curcode']);
  294. }
  295. if($_POST['paypal_ipn'] != null) {
  296. update_option('paypal_ipn', (int)$_POST['paypal_ipn']);
  297. }
  298. if($_POST['address_override'] != null) {
  299. update_option('address_override', (int)$_POST['address_override']);
  300. }
  301. foreach((array)$_POST['paypal_form'] as $form => $value) {
  302. update_option(('paypal_form_'.$form), $value);
  303. }
  304. return true;
  305. }
  306. function form_paypal_multiple() {
  307. $select_currency[get_option('paypal_curcode')] = "selected='true'";
  308. $output = "
  309. <tr>
  310. <td>
  311. PayPal Username
  312. </td>
  313. <td>
  314. <input type='text' size='40' value='".get_option('paypal_multiple_business')."' name='paypal_multiple_business' />
  315. </td>
  316. </tr>
  317. <tr>
  318. <td>
  319. PayPal Url
  320. </td>
  321. <td>
  322. <input type='text' size='40' value='".get_option('paypal_multiple_url')."' name='paypal_multiple_url' /> <br />
  323. <strong>Note:</strong>The URL to use for the paypal gateway is: https://www.paypal.com/cgi-bin/webscr
  324. </td>
  325. </tr>
  326. ";
  327. $paypal_ipn = get_option('paypal_ipn');
  328. $paypal_ipn1 = "";
  329. $paypal_ipn2 = "";
  330. switch($paypal_ipn) {
  331. case 0:
  332. $paypal_ipn2 = "checked ='true'";
  333. break;
  334. case 1:
  335. $paypal_ipn1 = "checked ='true'";
  336. break;
  337. }
  338. $output .= "
  339. <tr>
  340. <td>
  341. PayPal IPN
  342. </td>
  343. <td>
  344. <input type='radio' value='1' name='paypal_ipn' id='paypal_ipn1' ".$paypal_ipn1." /> <label for='paypal_ipn1'>".TXT_WPSC_YES."</label> &nbsp;
  345. <input type='radio' value='0' name='paypal_ipn' id='paypal_ipn2' ".$paypal_ipn2." /> <label for='paypal_ipn2'>".TXT_WPSC_NO."</label>
  346. </td>
  347. </tr>
  348. <tr>
  349. <td>
  350. PayPal Accepted Currency (e.g. USD, AUD)
  351. </td>
  352. <td>
  353. <select name='paypal_curcode'>
  354. <option ".$select_currency['USD']." value='USD'>U.S. Dollar</option>
  355. <option ".$select_currency['CAD']." value='CAD'>Canadian Dollar</option>
  356. <option ".$select_currency['AUD']." value='AUD'>Australian Dollar</option>
  357. <option ".$select_currency['EUR']." value='EUR'>Euro</option>
  358. <option ".$select_currency['GBP']." value='GBP'>Pound Sterling</option>
  359. <option ".$select_currency['JPY']." value='JPY'>Yen</option>
  360. <option ".$select_currency['NZD']." value='NZD'>New Zealand Dollar</option>
  361. <option ".$select_currency['CHF']." value='CHF'>Swiss Franc</option>
  362. <option ".$select_currency['HKD']." value='HKD'>Hong Kong Dollar</option>
  363. <option ".$select_currency['SGD']." value='SGD'>Singapore Dollar</option>
  364. <option ".$select_currency['SEK']." value='SEK'>Swedish Krona</option>
  365. <option ".$select_currency['HUF']." value='HUF'>Hungarian Forint</option>
  366. <option ".$select_currency['DKK']." value='DKK'>Danish Krone</option>
  367. <option ".$select_currency['PLN']." value='PLN'>Polish Zloty</option>
  368. <option ".$select_currency['NOK']." value='NOK'>Norwegian Krone</option>
  369. <option ".$select_currency['CZK']." value='CZK'>Czech Koruna</option>
  370. </select>
  371. </td>
  372. </tr>";
  373. $address_override = get_option('address_override');
  374. $address_override1 = "";
  375. $address_override2 = "";
  376. switch($address_override) {
  377. case 1:
  378. $address_override1 = "checked ='true'";
  379. break;
  380. case 0:
  381. default:
  382. $address_override2 = "checked ='true'";
  383. break;
  384. }
  385. $output .= "
  386. <tr>
  387. <td>
  388. Override the users address stored on paypal:
  389. </td>
  390. <td>
  391. <input type='radio' value='1' name='address_override' id='address_override1' ".$address_override1." /> <label for='address_override1'>".TXT_WPSC_YES."</label> &nbsp;
  392. <input type='radio' value='0' name='address_override' id='address_override2' ".$address_override2." /> <label for='address_override2'>".TXT_WPSC_NO."</label>
  393. </td>
  394. </tr>
  395. <tr class='update_gateway' >
  396. <td colspan='2'>
  397. <div class='submit'>
  398. <input type='submit' value='Update &raquo;' name='updateoption'/>
  399. </div>
  400. </td>
  401. </tr>
  402. <tr class='firstrowth'>
  403. <td style='border-bottom: medium none;' colspan='2'>
  404. <strong class='form_group'>Forms Sent to Gateway</strong>
  405. </td>
  406. </tr>
  407. <tr>
  408. <td>
  409. First Name Field
  410. </td>
  411. <td>
  412. <select name='paypal_form[first_name]'>
  413. ".nzshpcrt_form_field_list(get_option('paypal_form_first_name'))."
  414. </select>
  415. </td>
  416. </tr>
  417. <tr>
  418. <td>
  419. Last Name Field
  420. </td>
  421. <td>
  422. <select name='paypal_form[last_name]'>
  423. ".nzshpcrt_form_field_list(get_option('paypal_form_last_name'))."
  424. </select>
  425. </td>
  426. </tr>
  427. <tr>
  428. <td>
  429. Address Field
  430. </td>
  431. <td>
  432. <select name='paypal_form[address]'>
  433. ".nzshpcrt_form_field_list(get_option('paypal_form_address'))."
  434. </select>
  435. </td>
  436. </tr>
  437. <tr>
  438. <td>
  439. City Field
  440. </td>
  441. <td>
  442. <select name='paypal_form[city]'>
  443. ".nzshpcrt_form_field_list(get_option('paypal_form_city'))."
  444. </select>
  445. </td>
  446. </tr>
  447. <tr>
  448. <td>
  449. State Field
  450. </td>
  451. <td>
  452. <select name='paypal_form[state]'>
  453. ".nzshpcrt_form_field_list(get_option('paypal_form_state'))."
  454. </select>
  455. </td>
  456. </tr>
  457. <tr>
  458. <td>
  459. Postal code/Zip code Field
  460. </td>
  461. <td>
  462. <select name='paypal_form[post_code]'>
  463. ".nzshpcrt_form_field_list(get_option('paypal_form_post_code'))."
  464. </select>
  465. </td>
  466. </tr>
  467. <tr>
  468. <td>
  469. Country Field
  470. </td>
  471. <td>
  472. <select name='paypal_form[country]'>
  473. ".nzshpcrt_form_field_list(get_option('paypal_form_country'))."
  474. </select>
  475. </td>
  476. </tr> ";
  477. return $output;
  478. }
  479. add_action('init', 'nzshpcrt_paypal_ipn');
  480. ?>