Advertisement
Guest User

Untitled

a guest
Mar 1st, 2015
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.96 KB | None | 0 0
  1. <?php
  2.  
  3. class Postback_paypal extends MX_Controller
  4. {
  5. // User values
  6. private $custom;
  7. private $payment_status;
  8. private $payment_amount;
  9. private $payment_currency;
  10. private $txn_id;
  11. private $receiver_email;
  12. private $payer_email;
  13. private $pending_reason;
  14.  
  15. // Config values
  16. private $config_paypal;
  17.  
  18. // Debug
  19. private $debug = false;
  20.  
  21. /**
  22. * Initialize and prevent direct access
  23. */
  24. public function __construct()
  25. {
  26. parent::__construct();
  27.  
  28. $this->load->config('donate');
  29. $this->config_paypal = $this->config->item('donate_paypal');
  30.  
  31. // Prevent direct access
  32. if(count($_POST) == 0)
  33. {
  34. if($this->debug)
  35. {
  36. $_POST['custom'] = "1";
  37. $_POST['payment_status'] = "Completed";
  38. $_POST['mc_gross'] = 100.0;
  39. $_POST['mc_currency'] = "USD";
  40. $_POST['txn_id'] = sha1(uniqid());
  41. $_POST['receiver_email'] = "jaccobwyattmelton@gmail.com";
  42. $_POST['payer_email'] = "raxezdev@gmail.com";
  43. }
  44. else
  45. {
  46. die("No access");
  47. }
  48. }
  49. }
  50.  
  51. /**
  52. * Process the request
  53. */
  54. public function index()
  55. {
  56. // Read the post from PayPal system and add 'cmd'
  57. $req = 'cmd=_notify-validate';
  58.  
  59. // Create our request string
  60. foreach($_POST as $key => $value)
  61. {
  62. $value = urlencode(stripslashes($value));
  63. $req .= "&$key=$value";
  64. }
  65.  
  66. if($this->config_paypal['sandbox'])
  67. {
  68. $loc = 'ssl://www.sandbox.paypal.com';
  69. $host = 'www.sandbox.paypal.com';
  70. }
  71. else
  72. {
  73. $loc = 'ssl://www.paypal.com';
  74. $host = 'www.paypal.com';
  75. }
  76.  
  77. // Define our request headers
  78. $header = "POST /cgi-bin/webscr HTTP/1.0\r\n";
  79. $header .= "Host: www.paypal.com\r\n";
  80. $header .= "Content-Type: application/x-www-form-urlencoded\r\n";
  81. $header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
  82.  
  83. // Connect to the PayPal servers, timeout of 30seconds due to delay
  84. $fp = fsockopen($loc, 443, $errno, $errstr, 30);
  85.  
  86. // Gather the values we need
  87. $this->custom = $this->input->post('custom');
  88. $this->payment_status = $this->input->post('payment_status');
  89. $this->payment_amount = $this->input->post('mc_gross');
  90. $this->payment_currency = $this->input->post('mc_currency');
  91. $this->txn_id = $this->input->post('txn_id');
  92. $this->receiver_email = $this->input->post('receiver_email');
  93. $this->payer_email = $this->input->post('payer_email');
  94.  
  95. /*
  96. * REASON LEGENDA:
  97. * multi-currency: You do not have a balance in the currency sent, and you do not have your Payment Receiving Preferences set to automatically convert and accept this payment. You must manually accept or deny this payment.
  98. * order: You set the payment action to Order and have not yet captured funds.
  99. * paymentreview: The payment is pending while it is being reviewed by PayPal for risk.
  100. * unilateral: The payment is pending because it was made to an email address that is not yet registered or confirmed.
  101. * upgrade: The payment is pending because it was made via credit card and you must upgrade your account to Business or Premier status in order to receive the funds. upgrade can also mean that you have reached the monthly limit for transactions on your account.
  102. * verify: The payment is pending because you are not yet verified. You must verify your account before you can accept this payment.
  103. * other: The payment is pending for a reason other than those listed above. For more information, contact PayPal Customer Service.
  104. */
  105. $this->pending_reason = $this->input->post('pending_reason');
  106.  
  107. //Standard we didn't validated it.
  108. $validated = 0;
  109. $error_count = 0;
  110. $error = "";
  111.  
  112. if(!$fp)
  113. {
  114. //HTTP ERROR, Could not connect to paypal.
  115. $error = 'Http error happened, could not connect to paypal.';
  116. }
  117. else
  118. {
  119. fputs($fp, $header . $req);
  120.  
  121. $res = "";
  122.  
  123. // Loop through the response
  124. while(!feof($fp))
  125. {
  126. $res .= fgets($fp, 1024);
  127. }
  128.  
  129.  
  130. if($this->debug)
  131. {
  132. $res = "DEBUG ONLY RESPONSE THAT MAKES THIS PAYMENT BECOME VERIFIED";
  133. }
  134.  
  135. if(stristr($res, "VERIFIED"))
  136. {
  137. // Make sure the currency is correct
  138. if($this->payment_currency != $this->config->item('donation_currency'))
  139. {
  140. $error .= "Invalid currency (set to ".$this->payment_currency.")<br />";
  141. $error_count++;
  142. }
  143.  
  144. // Make sure the receiver email is correct
  145. if($this->receiver_email != $this->config_paypal['email'])
  146. {
  147. $error .= "Invalid receiver email (set to ".$this->receiver_email.")<br />";
  148. $error_count++;
  149. }
  150.  
  151. // Make sure the payment has not already been processed
  152. if($this->transactionExists($this->txn_id))
  153. {
  154. $error .= "Payment has already been processed";
  155. $error_count++;
  156. }
  157.  
  158. // Make sure payment status is completed
  159. if($this->payment_status != "Completed")
  160. {
  161. $error .= "Payment status is not completed (".$this->payment_status.")<br />";
  162. $error_count++;
  163. }
  164.  
  165. //Add pending reasons
  166. if($this->pending_reason == "unilateral")
  167. {
  168. $error .= "Pending_reason: unilateral<br />";
  169. $error .= "The payment is pending because it was made to an email address that is not yet registered or confirmed.<br />";
  170. $error_count += 2;
  171. }
  172.  
  173. //If no errors where posted, process payment and add points.
  174. if($error_count == 0)
  175. {
  176. // Update the account with the given money multiplied by the money multiplier
  177. $dpReward = $this->getDpAmount();
  178.  
  179. // Update account with donation points
  180. $this->db->query("UPDATE `account_data` SET `dp` = `dp` + ? WHERE `id` = ?", array($dpReward, $this->custom));
  181.  
  182. // Update the transaction log and set validated to 1
  183. $validated = 1;
  184.  
  185. $this->updateMonthlyIncome();
  186. }
  187. }
  188. elseif(stristr($res, "INVALID"))
  189. {
  190. $error .= "PayPal validation failed: invalid transaction<br />";
  191. $error_count++;
  192. }
  193. else
  194. {
  195. $error .= "Unknown problem<br />";
  196. }
  197.  
  198. //Close the connection
  199. fclose($fp);
  200.  
  201. //insert the logs
  202. // Gather our database log datas, insert here already because of validation.
  203. $data = array(
  204. "payment_status" => $this->payment_status,
  205. "payment_amount" => $this->payment_amount,
  206. "payment_currency" => $this->payment_currency,
  207. "txn_id" => $this->txn_id,
  208. "receiver_email" => $this->receiver_email,
  209. "payer_email" => $this->payer_email,
  210. "user_id" => $this->custom,
  211. "validated" => $validated,
  212. "timestamp" => time(),
  213. "error" => (isset($error)) ? $error : "",
  214. "pending_reason" => $this->pending_reason
  215. );
  216.  
  217. $this->db->insert("paypal_logs", $data);
  218.  
  219. $this->plugins->onDonationPostback($data['user_id'], $data['payment_amount'], $this->getDpAmount());
  220.  
  221. die();
  222. }
  223. }
  224.  
  225. /**
  226. * Get the amount of DP
  227. */
  228. private function getDpAmount()
  229. {
  230. $config = $this->config->item('donate_paypal');
  231.  
  232. $points = 0;
  233.  
  234. foreach($config['values'] as $price => $reward)
  235. {
  236. if($price == round($this->payment_amount))
  237. {
  238. $points = $reward;
  239. }
  240. }
  241.  
  242. return $points;
  243. }
  244.  
  245. /**
  246. * Check if a transaction exists
  247. * @param String $txn_id
  248. * @return Boolean
  249. */
  250. private function transactionExists($txn_id)
  251. {
  252. $query = $this->db->query("SELECT COUNT(*) as `total` FROM paypal_logs WHERE txn_id=?", array($txn_id));
  253.  
  254. if($query->num_rows() > 0)
  255. {
  256. $row = $query->result_array();
  257.  
  258. if($row[0]['total'] > 0)
  259. {
  260. return true;
  261. }
  262. else
  263. {
  264. return false;
  265. }
  266. }
  267. else
  268. {
  269. return false;
  270. }
  271. }
  272.  
  273. private function updateMonthlyIncome()
  274. {
  275. $query = $this->db->query("SELECT COUNT(*) AS `total` FROM monthly_income WHERE month=?", array(date("Y-m")));
  276.  
  277. $row = $query->result_array();
  278.  
  279. if($row[0]['total'])
  280. {
  281. $this->db->query("UPDATE monthly_income SET amount = amount + ".floor($this->payment_amount)." WHERE month=?", array(date("Y-m")));
  282. }
  283. else
  284. {
  285. $this->db->query("INSERT INTO monthly_income(month, amount) VALUES(?, ?)", array(date("Y-m"), floor($this->payment_amount)));
  286. }
  287. }
  288. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement