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

/wp-content/plugins/donate-plus/paypal.php

https://gitlab.com/billyprice1/website
PHP | 375 lines | 198 code | 41 blank | 136 comment | 22 complexity | 34662d2d809cc712cb1d349b8b93dce6 MD5 | raw file
  1. <?php
  2. /*
  3. //************************************************************
  4. //************************************************************
  5. //** Bugs fixed by... **
  6. //** **
  7. //** Copyright Encentra 2011 **
  8. //** www.encentra.se **
  9. //** consultant: Johan Rufus Lagerström **
  10. //************************************************************
  11. //************************************************************
  12. */
  13. #########################################################
  14. # #
  15. # File : PayPal.php #
  16. # Version : 1.9 #
  17. # Last Modified : 12/15/2005 #
  18. # Copyright : This program is free software; you #
  19. # can redistribute it and/or #modify it under the terms #
  20. # of the GNU General Public License as published by the #
  21. # Free Software Foundation #
  22. # See the #GNU General Public License for more details. #
  23. # DO NOT REMOVE LINK #
  24. # Visit: http://www.belahost.com for updates/scripts #
  25. #########################################################
  26. # THIS SCRIPT IS FREEWARE AND IS NOT FOR RE-SALE #
  27. #########################################################
  28. require("../../../wp-blog-header.php");
  29. global $wpdb;
  30. $dplus = get_option('DonatePlus');
  31. $email_IPN_results = get_option('IPN_email');
  32. $tmp_nl = "\r\n";
  33. if(class_exists('DonatePlus'))$donateplus = new DonatePlus();
  34. #1 = Live on PayPal Network
  35. #2 = Testing with www.BelaHost.com/pp
  36. #3 = Testing with the PayPal Sandbox
  37. $verifymode = $dplus['testing_mode']; # be sure to change value for testing/live!
  38. # Send notifications to here
  39. $send_mail_to = $dplus['ty_email'];
  40. # subject of messages
  41. $sysname = "Donate Plus - Paypal IPN Transaction";
  42. # Your primary PayPal e-mail address
  43. //$paypal_email = $dplus['paypal_email'];
  44. # Your sendmail path
  45. //$mailpath = "/usr/sbin/sendmail -t";
  46. #the name you wish to see the messages from
  47. //$from_name = $dplus['ty_name'];
  48. #the emails will be coming from
  49. //$from_email = $dplus['ty_email'];
  50. # Convert Super globals For backward compatibility
  51. if(phpversion() <= "4.0.6") {$_POST=($HTTP_POST_VARS);}
  52. # Check for IPN post if non then return 404 error.
  53. if (!$_POST['txn_type']){
  54. if( $email_IPN_results ) send_mail($send_mail_to,$sysname." [ERROR - 404]","IPN Fail: 404 error!","",__LINE__);
  55. header("Status: 404 Not Found");
  56. die();
  57. }else{
  58. header("Status: 200 OK");
  59. }
  60. # Now we Read the Posted IPN
  61. $postvars = array();
  62. //print_r($_POST);
  63. foreach ($_POST as $ipnvars => $ipnvalue){
  64. $postvars[] = $ipnvars; $postvals[] = $ipnvalue;
  65. }
  66. # Now we ADD "cmd=_notify-validate" for Post back Validation
  67. $postipn = 'cmd=_notify-validate';
  68. $orgipn = '<b>Posted IPN variables in order received:</b><br><br>';
  69. # Prepare for validation
  70. for($x=0; $x < count($postvars); $x++){
  71. $y = $x+1;
  72. $postkey = $postvars[$x];
  73. $postval = $postvals[$x];
  74. $postipn .= "&".$postkey."=".urlencode($postval);
  75. $orgipn .= "<b>#".$y."</b> Key: ".$postkey." <b>=</b> ".$postval."<br>";
  76. }
  77. if($verifymode == 1){ //1 = Live on PayPal Network
  78. ## Verify Mode 1: This will post the IPN variables to the Paypal Network for Validation
  79. $port = fsockopen ('ssl://www.paypal.com', 443, $errno, $errstr, 30);
  80. //$port = fsockopen ("paypal.com", 80, $errno, $errstr, 30);
  81. $header = "POST /cgi-bin/webscr HTTP/1.0\r\n".
  82. "Host: www.paypal.com\r\n".
  83. "Content-Type: application/x-www-form-urlencoded\r\n".
  84. "Content-Length: ".strlen($postipn)."\r\n\r\n";
  85. }elseif ($verifymode == 2){ //2 = Testing with www.BelaHost.com/pp
  86. ## Verify Mode 2: This will post the IPN variables to Belahost Test Script for validation
  87. ## Located at www.belahost.com/pp/index.php
  88. $port = fsockopen ("www.belahost.com", 80, $errno, $errstr, 30);
  89. $header = "POST /pp/ HTTP/1.0\r\n".
  90. "Host: www.belahost.com\r\n".
  91. "Content-Type: application/x-www-form-urlencoded\r\n".
  92. "Content-Length: ".strlen($postipn)."\r\n\r\n";
  93. }elseif ($verifymode == 3){ //3 = Testing with the PayPal Sandbox
  94. $port = fsockopen ("ssl://www.sandbox.paypal.com", 443, $errno, $errstr, 30);
  95. $header = "POST /cgi-bin/webscr HTTP/1.0\r\n".
  96. "Host: www.sandbox.paypal.com\r\n".
  97. "Content-Type: application/x-www-form-urlencoded\r\n".
  98. "Content-Length: ".strlen($postipn)."\r\n\r\n";
  99. }else{
  100. $error=1;
  101. //echo "CheckMode: ".$verifymode." is invalid!";
  102. if( $email_IPN_results ) send_mail($send_mail_to,$sysname." [ERROR - Misc]","Fail: CheckMode: ".$verifymode." is invalid!","",__LINE__);
  103. die();
  104. }
  105. # Error at this point: If at this point you need to check your Firewall or your Port restrictions?
  106. if (!$port && !$error){
  107. //echo "Problem: Error Number: ".$errno." Error String: ".$errstr;
  108. #Here is a small email notification so you know if your system ever fails
  109. if( $email_IPN_results ) send_mail($send_mail_to,$sysname." [ERROR - Misc]","Your Paypal System failed due to $errno and string $errstr","",__LINE__);
  110. die();
  111. }else{
  112. # If No Errors to this point then we proceed with the processing.
  113. # Open port to paypal or test site and post Varibles.
  114. fputs ($port, $header.$postipn);
  115. while (!feof($port)){
  116. $reply = fgets ($port, 1024);
  117. $reply = trim ($reply);
  118. }
  119. # Prepare a Debug Report
  120. $ipnreport = $orgipn."<br><b>"."IPN Reply: ".$reply."</b>";
  121. # Buyer Information
  122. $address_city = $_POST['address_city'];
  123. $address_country = $_POST['address_country'];
  124. $address_country_code = $_POST['address_country_code'];
  125. $address_name = $_POST ['address_name'];
  126. $address_state = $_POST['address_state'];
  127. $address_status = $_POST['address_status'];
  128. $address_street = $_POST['address_street'];
  129. $address_zip = $_POST['address_zip'];
  130. $first_name = $_POST['first_name'];
  131. $last_name = $_POST['last_name'];
  132. $payer_business_name = $_POST['payer_business_name'];
  133. $payer_email = $_POST['payer_email'];
  134. $payer_id = $_POST['payer_id'];
  135. $payer_status = $_POST['payer_status'];
  136. $residence_country = $_POST['residence_country'];
  137. # Below Instant BASIC Payment Notifiction Variables
  138. $business = $_POST['business'];
  139. $item_name = $_POST['item_name'];
  140. $item_number = $_POST['item_number'];
  141. $quantity = $_POST['quantity'];
  142. $receiver_email = $_POST['receiver_email'];
  143. $receiver_id = $_POST['receiver_id'];
  144. #Advanced and Customer information
  145. $custom = $_POST['custom'];
  146. $invoice = $_POST['invoice'];
  147. $memo = $_POST['memo'];
  148. $option_name1 = $_POST['option_name1']; //name
  149. $option_name2 = $_POST['option_name2'];
  150. $option_selection1 = $_POST['option_selection1']; //email
  151. $option_selection2 = $_POST['option_selection2']; //comment now changed to name
  152. $tax = $_POST['tax'];
  153. #Website Payment Pro and Other IPN Variables
  154. $auth_id = $_POST['auth_id'];
  155. $auth_exp = $_POST['auth_exp'];
  156. $auth_amount = $_POST['auth_amount'];
  157. $auth_status = $_POST['auth_status'];
  158. # Shopping Cart Information
  159. $mc_gross = $_POST['mc_gross'];
  160. $mc_handling = $_POST['mc_handling'];
  161. $mc_shipping = $_POST['mc_shipping'];
  162. $num_cart_items = $_POST['num_cart_items'];
  163. # Other Transaction Information
  164. $parent_txn_id = $_POST['parent_txn_id'];
  165. $payment_date = $_POST['payment_date'];
  166. $payment_status = $_POST['payment_status'];
  167. $payment_type = $_POST['payment_type'];
  168. $pending_reason = $_POST['pending_reason'];
  169. $reason_code = $_POST['reason_code'];
  170. $remaining_settle = $_POST['remaining_settle'];
  171. $transaction_entity = $_POST['transaction_entity'];
  172. $txn_id = $_POST['txn_id'];
  173. $txn_type = $_POST['txn_type'];
  174. # Currency and Exchange Information
  175. $exchange_rate = $_POST['exchange_rate'];
  176. $mc_currency = $_POST['mc_currency'];
  177. $mc_fee = $_POST['mc_fee'];
  178. $payment_fee = $_POST['payment_fee'];
  179. $payment_gross = $_POST['payment_gross'];
  180. $settle_amount = $_POST['settle_amount'];
  181. $settle_currency = $_POST['settle_currency'];
  182. # Auction Information
  183. $for_auction = $_POST['for_auction'];
  184. $auction_buyer_id = $_POST['auction_buyer_id'];
  185. $auction_closing_date = $_POST['auction_closing_date'];
  186. $auction_multi_item = $_POST['auction_multi_item'];
  187. # Below are Subscription - Instant Payment Notifiction Variables
  188. $subscr_date = $_POST['subscr_date'];
  189. $subscr_effective = $_POST['subscr_effective'];
  190. $period1 = $_POST['period1'];
  191. $period2 = $_POST['period2'];
  192. $period3 = $_POST['period3'];
  193. $amount1 = $_POST['amount1'];
  194. $amount2 = $_POST['amount2'];
  195. $amount3 = $_POST['amount3'];
  196. $mc_amount1 = $_POST['mc_amount1'];
  197. $mc_amount2 = $_POST['mc_amount2'];
  198. $mc_amount3 = $_POST['mc_amount3'];
  199. $recurring = $_POST['recurring'];
  200. $reattempt = $_POST['reattempt'];
  201. $retry_at = $_POST['retry_at'];
  202. $recur_times = $_POST['recur_times'];
  203. $username = $_POST['username'];
  204. $password = $_POST['password'];
  205. $subscr_id = $_POST['subscr_id'];
  206. # Complaint Variables Used when paypal logs a complaint
  207. $case_id = $_POST['case_id'];
  208. $case_type = $_POST['case_type'];
  209. $case_creation_date = $_POST['case_creation_date'];
  210. #Last but not least
  211. $notify_version = $_POST['notify_version'];
  212. $verify_sign = $_POST['verify_sign'];
  213. #There are certain variables that we will not store for cart since they are dynamic
  214. #such as mc_gross_x as they will be forever changing/increasing your script must check these
  215. #IPN was Confirmed as both Genuine and VERIFIED
  216. if(!strcmp($reply, "VERIFIED")){
  217. /* Now that IPN was VERIFIED below are a few things which you may want to do at this point.
  218. 1. Check that the "payment_status" variable is: "Completed"
  219. 2. If it is Pending you may want to wait or inform your customer?
  220. 3. You should Check your datebase to ensure this "txn_id" or "subscr_id" is not a duplicate. txn_id is not sent with subscriptions!
  221. 4. Check "payment_gross" or "mc_gross" matches match your prices!
  222. 5. You definately want to check the "receiver_email" or "business" is yours.
  223. 6. We have included an insert to database for table paypal_ipn
  224. */
  225. $split = explode(':', $item_number);
  226. if (strpos($option_selection1,'don') !== false) {
  227. $display = 0;
  228. } else {
  229. $display = 1;
  230. }
  231. $uID = $split[1];
  232. $url = 'http://xbmc.org';
  233. if(!$mc_gross)$mc_gross = $payment_gross;
  234. $table_name = $wpdb->prefix."donations";
  235. //kontrollera om transaktionen redan finns sparad
  236. $tmp_txn_id = $wpdb->escape($txn_id);
  237. $tmp_count = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM $table_name WHERE txn_id='$tmp_txn_id';"));
  238. //if($payment_status == 'Completed'){
  239. if($tmp_count == 0){
  240. //all pin:s som kommer in tolkar vi som complete
  241. $tmp_payment_status = "Completed";
  242. //USE SECURE INSERT!
  243. $wpdb->query(
  244. $wpdb->prepare("INSERT INTO $table_name
  245. ( name, email, url, comment, display, amount, currency, date, user_id, status, txn_id )
  246. VALUES ( %s, %s, %s, %s, %d, %s, %s, %s, %d, %s, %s )",
  247. $option_selection2, $payer_email, $url, strip_tags($memo), $display, $mc_gross, $mc_currency, date('Y-m-d H:i:s'), $uID, $tmp_payment_status, $txn_id )
  248. );
  249. //send payer thank you email about where to download
  250. global $currency;
  251. $subject = stripslashes($dplus['ty_subject']);
  252. $prefix = $currency[$mc_currency]['symbol'];
  253. $amount = $prefix.$mc_gross.' '.$mc_currency;
  254. $payer_msg = nl2br($donateplus->TagReplace(stripslashes($dplus['ty_emailmsg']), $option_name1, $amount));
  255. //$payer_msg = utf8_encode($payer_msg);
  256. //echo '<br />'.$payer_msg;
  257. $headers = 'MIME-Version: 1.0'."\r\n";
  258. //$headers .= 'Content-type: text/html; charset=iso-8859-1'."\r\n";
  259. $headers .= 'Content-type: text/html; charset=utf-8'."\r\n";
  260. $headers .= 'From: '.$dplus['ty_name'].' <'.$dplus['ty_email'].'>'."\r\n";
  261. //wp_mail($option_selection1, $subject, $payer_msg, $headers);
  262. //wp_mail($notify_email, 'New Donation Recieved!', "Donation from $option_name1 for $payment_amount");
  263. //echo $postquery;
  264. //if( $email_IPN_results ) send_mail($send_mail_to,$sysname." [PIN - Completed]","\n Verified IPN Transaction [Completed] \n \n$ipnreport\n","",__LINE__);
  265. }else{
  266. //not "Completed"
  267. //tar bort nedan, annars trilalr det in 10 mail per donation
  268. //send_mail($send_mail_to,$sysname." [PIN - $payment_status]","\n Verified IPN Transaction [$payment_status] \n \n$ipnreport\n","",__LINE__);
  269. }
  270. }elseif(!strcmp($reply, "INVALID")){ # IPN was Not Validated as Genuine and is INVALID
  271. /* Now that IPN was INVALID below are a few things which you may want to do at this point.
  272. 1. Check your code for any post back Validation problems!
  273. 2. Investigate the Fact that this Could be an attack on your script IPN!
  274. 3. If updating your DB, Ensure this "txn_id" is Not a Duplicate!
  275. */
  276. # Remove Echo line below when live
  277. //echo $ipnreport;
  278. if( $email_IPN_results ) send_mail($send_mail_to,$sysname." [ERROR - Invalid]","Invalid IPN Transaction".$tmp_nl.$tmp_nl.$ipnreport,"",__LINE__);
  279. }else{ #ERROR
  280. # If your script reaches this point there is a problem. Communication from your script to test/paypal pages could be 1 reason.
  281. echo $ipnreport;
  282. if( $email_IPN_results ) send_mail($send_mail_to,$sysname." [ERROR - Misc]","FATAL ERROR No Reply at all".$tmp_nl.$tmp_nl.$ipnreport,"",__LINE__);
  283. }
  284. #Terminate the Socket connection and Exit
  285. fclose ($port);
  286. die();
  287. }
  288. /* =================================
  289. Below are functions
  290. ================================= */
  291. # Email function
  292. function send_mail($to, $subj, $body, $headers="",$tmp_line=0){
  293. //global
  294. global $tmp_nl;
  295. //var_dump till en sträng
  296. $posts = var_export($_POST, true);
  297. //body
  298. $tmp_body = "===================================".$tmp_nl.
  299. $subj." [line: $tmp_line]".$tmp_nl.
  300. "===================================".$tmp_nl.
  301. $body.$tmp_nl.
  302. $tmp_nl.
  303. "===================================".$tmp_nl.
  304. $posts;
  305. //skickar mail
  306. wp_mail($to, $subj, $tmp_body, $headers);
  307. /*
  308. global $from_name, $from_email, $mailpath;
  309. # E-mail Configuration
  310. $announce_subject = "$subj";
  311. $announce_from_email = "$from_email";
  312. $announce_from_name = "$from_name";
  313. $announce_to_email = "$to";
  314. $MP = "$mailpath";
  315. $spec_envelope = 1;
  316. # End email config
  317. # Access Sendmail
  318. # Conditionally match envelope address
  319. if(isset($spec_envelope))
  320. {
  321. $MP .= " -f $announce_from_email";
  322. }
  323. $fd = popen($MP,"w");
  324. fputs($fd, "To: $announce_to_email\n");
  325. fputs($fd, "From: $announce_from_name <$announce_from_email>\n");
  326. fputs($fd, "Subject: $announce_subject\n");
  327. fputs($fd, "X-Mailer: MyPayPal_Mailer\n");
  328. fputs($fd, "Content-Type: text/html\n");
  329. fputs($fd, $body); # $body will be sent when the function is used
  330. pclose($fd);
  331. */
  332. }
  333. ?>