PageRenderTime 72ms CodeModel.GetById 20ms RepoModel.GetById 2ms app.codeStats 0ms

/builder2/elements/pix_mail/paypal_ipn.php

https://gitlab.com/PhaseHosting/whmcs-customisations
PHP | 187 lines | 145 code | 17 blank | 25 comment | 20 complexity | dc65a8258de3712ba5d6ef0b756b911d MD5 | raw file
  1. <?php
  2. // FLATPACK - Paypal forms ipn emails notifications
  3. // By PixFort
  4. // http://pixfort.com
  5. // Copyright PixFort 2016
  6. // CONFIG: Enable debug mode. This means we'll log requests into 'ipn.log' in the same directory.
  7. // Especially useful if you encounter network errors or other intermittent problems with IPN (validation).
  8. // Set this to 0 once you go live or don't require logging.
  9. define("DEBUG", 1);
  10. // Set to 0 once you're ready to go live
  11. define("USE_SANDBOX", 1);
  12. define("LOG_FILE", "./ipn.log");
  13. include("config.php");
  14. $subject = "FLATPACK PayPal Form Notification";
  15. // Read POST data
  16. // reading posted data directly from $_POST causes serialization
  17. // issues with array data in POST. Reading raw POST data from input stream instead.
  18. $raw_post_data = file_get_contents('php://input');
  19. $raw_post_array = explode('&', $raw_post_data);
  20. $myPost = array();
  21. foreach ($raw_post_array as $keyval) {
  22. $keyval = explode ('=', $keyval);
  23. if (count($keyval) == 2)
  24. $myPost[$keyval[0]] = urldecode($keyval[1]);
  25. }
  26. // read the post from PayPal system and add 'cmd'
  27. $req = 'cmd=_notify-validate';
  28. if(function_exists('get_magic_quotes_gpc')) {
  29. $get_magic_quotes_exists = true;
  30. }
  31. foreach ($myPost as $key => $value) {
  32. if($get_magic_quotes_exists == true && get_magic_quotes_gpc() == 1) {
  33. $value = urlencode(stripslashes($value));
  34. } else {
  35. $value = urlencode($value);
  36. }
  37. $req .= "&$key=$value";
  38. }
  39. // Post IPN data back to PayPal to validate the IPN data is genuine
  40. // Without this step anyone can fake IPN data
  41. if(USE_SANDBOX == true) {
  42. $paypal_url = "https://www.sandbox.paypal.com/cgi-bin/webscr";
  43. } else {
  44. $paypal_url = "https://www.paypal.com/cgi-bin/webscr";
  45. }
  46. $ch = curl_init($paypal_url);
  47. if ($ch == FALSE) {
  48. return FALSE;
  49. }
  50. curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
  51. curl_setopt($ch, CURLOPT_POST, 1);
  52. curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
  53. curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
  54. // curl_setopt($ch, CURLOPT_SSLVERSION, 4);
  55. // curl_setopt($ch, CURLOPT_SSL_CIPHER_LIST, "TLSv1");
  56. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
  57. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
  58. curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
  59. if(DEBUG == true) {
  60. curl_setopt($ch, CURLOPT_HEADER, 1);
  61. curl_setopt($ch, CURLINFO_HEADER_OUT, 1);
  62. }
  63. // CONFIG: Optional proxy configuration
  64. //curl_setopt($ch, CURLOPT_PROXY, $proxy);
  65. //curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
  66. // Set TCP timeout to 30 seconds
  67. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
  68. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: Close'));
  69. // CONFIG: Please download 'cacert.pem' from "http://curl.haxx.se/docs/caextract.html" and set the directory path
  70. // of the certificate as shown below. Ensure the file is readable by the webserver.
  71. // This is mandatory for some environments.
  72. //$cert = __DIR__ . "./cacert.pem";
  73. //curl_setopt($ch, CURLOPT_CAINFO, $cert);
  74. $res = curl_exec($ch);
  75. error_log(date('[Y-m-d H:i e] '). "HTTP request of validation request:". curl_getinfo($ch, CURLINFO_HEADER_OUT) ." for IPN payload: $req" . PHP_EOL, 3, LOG_FILE);
  76. error_log(date('[Y-m-d H:i e] '). "HTTP response of validation request: $res" . PHP_EOL, 3, LOG_FILE);
  77. if (curl_errno($ch) != 0) // cURL error
  78. {
  79. if(DEBUG == true) {
  80. error_log(date('[Y-m-d H:i e] '). "Can't connect to PayPal to validate IPN message: " . curl_error($ch) . PHP_EOL, 3, LOG_FILE);
  81. }
  82. curl_close($ch);
  83. exit;
  84. } else {
  85. // Log the entire HTTP response if debug is switched on.
  86. if(DEBUG == true) {
  87. error_log(date('[Y-m-d H:i e] '). "HTTP request of validation request:". curl_getinfo($ch, CURLINFO_HEADER_OUT) ." for IPN payload: $req" . PHP_EOL, 3, LOG_FILE);
  88. error_log(date('[Y-m-d H:i e] '). "HTTP response of validation request: $res" . PHP_EOL, 3, LOG_FILE);
  89. }
  90. curl_close($ch);
  91. }
  92. // Inspect IPN validation result and act accordingly
  93. // Split response headers and payload, a better way for strcmp
  94. $tokens = explode("\r\n\r\n", trim($res));
  95. $res = trim(end($tokens));
  96. if (strcmp ($res, "VERIFIED") == 0) {
  97. // check whether the payment_status is Completed
  98. // check that txn_id has not been previously processed
  99. // check that receiver_email is your PayPal email
  100. // check that payment_amount/payment_currency are correct
  101. // process payment and mark item as paid.
  102. // assign posted variables to local variables
  103. $item_name = $_POST['item_name'];
  104. $item_number = $_POST['item_number'];
  105. $payment_status = $_POST['payment_status'];
  106. $payment_amount = $_POST['mc_gross'];
  107. $payment_currency = $_POST['mc_currency'];
  108. $txn_id = $_POST['txn_id'];
  109. $receiver_email = $_POST['receiver_email'];
  110. $payer_email = $_POST['payer_email'];
  111. $payer_fname = $_POST['first_name'];
  112. $payer_lname = $_POST['last_name'];
  113. if($receiver_email){$to_Email=$receiver_email;}
  114. $msg_header = "Thank you for your purchase.<br>";
  115. $final_msg = "Item Name : " . $item_name . "<br>";
  116. $final_msg .= "Item Number : " . $item_number . "<br>";
  117. $final_msg .= "Payment status : " . $payment_status . "<br>";
  118. $final_msg .= "Payment amount : " . $payment_amount . "<br>";
  119. $final_msg .= "Payment currency : " . $payment_currency . "<br>";
  120. $final_msg .= "txn_id : " . $txn_id . "<br>";
  121. $final_msg .= "Receiver email : " . $receiver_email . "<br>";
  122. $final_msg .= "Payer email : " . $payer_email . "<br>";
  123. $final_msg .= "Payer First Name : " . $payer_fname . "<br>";
  124. $final_msg .= "Payer First Name : " . $payer_lname . "<br>";
  125. $final_msg .= "<br>---------------------------------------------------<br>";
  126. $msg_footer = "Paypal Payment Form by PixFort<br>";
  127. $msg_footer .= "Envato: http://themeforest.net/user/pixfort<br>";
  128. $msg_footer .= "Website: http://pixfort.com<br>";
  129. $headers = 'From: '.$to_Email.'' . "\r\n" .
  130. 'Reply-To: '.$to_Email.'' . "\r\n" .
  131. 'X-Mailer: PHP/' . phpversion().'' . "\r\n" .
  132. "Content-Type: text/html;charset=utf-8";
  133. $seller_msg = $final_msg . $msg_footer;
  134. $buyer_msg = $msg_header . $final_msg;
  135. // send mail
  136. $sentMail1 = @mail($to_Email, $subject, $seller_msg, $headers);
  137. $sentMail2 = @mail($payer_email, $subject, $buyer_msg, $headers);
  138. if(!$sentMail1) {
  139. error_log(date('[Y-m-d H:i e] '). "PixFort Notification sentMail1 send failed". PHP_EOL, 3, LOG_FILE);
  140. }else{
  141. error_log(date('[Y-m-d H:i e] '). "PixFort Notification sentMail1 sent successfully". PHP_EOL, 3, LOG_FILE);
  142. }
  143. if(!$sentMail2) {
  144. error_log(date('[Y-m-d H:i e] '). "PixFort Notification sentMail2 send failed". PHP_EOL, 3, LOG_FILE);
  145. }else{
  146. error_log(date('[Y-m-d H:i e] '). "PixFort Notification sentMail2 sent successfully". PHP_EOL, 3, LOG_FILE);
  147. }
  148. if(DEBUG == true) {
  149. error_log(date('[Y-m-d H:i e] '). "Verified IPN: $req ". PHP_EOL, 3, LOG_FILE);
  150. }
  151. } else if (strcmp ($res, "INVALID") == 0) {
  152. // log for manual investigation
  153. // Add business logic here which deals with invalid IPN messages
  154. if(DEBUG == true) {
  155. error_log(date('[Y-m-d H:i e] '). "Invalid IPN: $req" . PHP_EOL, 3, LOG_FILE);
  156. }
  157. }
  158. ?>