Advertisement
Guest User

Untitled

a guest
Nov 12th, 2015
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.85 KB | None | 0 0
  1. <?php
  2. // CONFIG: Enable debug mode. This means we'll log requests into 'ipn.log' in the same directory.
  3. // Especially useful if you encounter network errors or other intermittent problems with IPN (validation).
  4. // Set this to 0 once you go live or don't require logging.
  5. define("DEBUG", 1);
  6. // Set to 0 once you're ready to go live
  7. define("USE_SANDBOX", 1);
  8. define("LOG_FILE", "./ipn.log");
  9. // Read POST data
  10. // reading posted data directly from $_POST causes serialization
  11. // issues with array data in POST. Reading raw POST data from input stream instead.
  12. $raw_post_data = file_get_contents('php://input');
  13. $raw_post_array = explode('&', $raw_post_data);
  14. $myPost = array();
  15. foreach ($raw_post_array as $keyval) {
  16. $keyval = explode ('=', $keyval);
  17. if (count($keyval) == 2)
  18. $myPost[$keyval[0]] = urldecode($keyval[1]);
  19. }
  20. // read the post from PayPal system and add 'cmd'
  21. $req = 'cmd=_notify-validate';
  22. if(function_exists('get_magic_quotes_gpc')) {
  23. $get_magic_quotes_exists = true;
  24. }
  25. foreach ($myPost as $key => $value) {
  26. if($get_magic_quotes_exists == true && get_magic_quotes_gpc() == 1) {
  27. $value = urlencode(stripslashes($value));
  28. } else {
  29. $value = urlencode($value);
  30. }
  31. $req .= "&$key=$value";
  32. }
  33. // Post IPN data back to PayPal to validate the IPN data is genuine
  34. // Without this step anyone can fake IPN data
  35. if(USE_SANDBOX == true) {
  36. $paypal_url = "https://www.sandbox.paypal.com/cgi-bin/webscr";
  37. } else {
  38. $paypal_url = "https://www.paypal.com/cgi-bin/webscr";
  39. }
  40. $ch = curl_init($paypal_url);
  41. if ($ch == FALSE) {
  42. return FALSE;
  43. }
  44. curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
  45. curl_setopt($ch, CURLOPT_POST, 1);
  46. curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
  47. curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
  48. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
  49. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
  50. curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
  51. if(DEBUG == true) {
  52. curl_setopt($ch, CURLOPT_HEADER, 1);
  53. curl_setopt($ch, CURLINFO_HEADER_OUT, 1);
  54. }
  55. // CONFIG: Optional proxy configuration
  56. //curl_setopt($ch, CURLOPT_PROXY, $proxy);
  57. //curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
  58. // Set TCP timeout to 30 seconds
  59. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
  60. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: Close'));
  61. // CONFIG: Please download 'cacert.pem' from "http://curl.haxx.se/docs/caextract.html" and set the directory path
  62. // of the certificate as shown below. Ensure the file is readable by the webserver.
  63. // This is mandatory for some environments.
  64. //$cert = __DIR__ . "./cacert.pem";
  65. //curl_setopt($ch, CURLOPT_CAINFO, $cert);
  66. $res = curl_exec($ch);
  67. if (curl_errno($ch) != 0) // cURL error
  68. {
  69. if(DEBUG == true) {
  70. 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);
  71. }
  72. curl_close($ch);
  73. exit;
  74. } else {
  75. // Log the entire HTTP response if debug is switched on.
  76. if(DEBUG == true) {
  77. 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);
  78. error_log(date('[Y-m-d H:i e] '). "HTTP response of validation request: $res" . PHP_EOL, 3, LOG_FILE);
  79. }
  80. curl_close($ch);
  81. }
  82. // Inspect IPN validation result and act accordingly
  83. // Split response headers and payload, a better way for strcmp
  84. $tokens = explode("\r\n\r\n", trim($res));
  85. $res = trim(end($tokens));
  86. if (strcmp ($res, "VERIFIED") == 0) {
  87. // check whether the payment_status is Completed
  88. // check that txn_id has not been previously processed
  89. // check that receiver_email is your PayPal email
  90. // check that payment_amount/payment_currency are correct
  91. // process payment and mark item as paid.
  92. // assign posted variables to local variables
  93. //$item_name = $_POST['item_name'];
  94. //$item_number = $_POST['item_number'];
  95. //$payment_status = $_POST['payment_status'];
  96. //$payment_amount = $_POST['mc_gross'];
  97. //$payment_currency = $_POST['mc_currency'];
  98. //$txn_id = $_POST['txn_id'];
  99. //$receiver_email = $_POST['receiver_email'];
  100. //$payer_email = $_POST['payer_email'];
  101.  
  102. $mail_From = "IPN@example.com";
  103. $mail_To = "flarmiey@gmail.com";
  104. $mail_Subject = "VERIFIED IPN";
  105. $mail_Body = $req;
  106. mail($mail_To, $mail_Subject, $mail_Body, $mail_From);
  107.  
  108.  
  109. if(DEBUG == true) {
  110. error_log(date('[Y-m-d H:i e] '). "Verified IPN: $req ". PHP_EOL, 3, LOG_FILE);
  111. }
  112. } else if (strcmp ($res, "INVALID") == 0) {
  113. // log for manual investigation
  114. // Add business logic here which deals with invalid IPN messages
  115. if(DEBUG == true) {
  116. error_log(date('[Y-m-d H:i e] '). "Invalid IPN: $req" . PHP_EOL, 3, LOG_FILE);
  117. }
  118.  
  119. $mail_From = "IPN@example.com";
  120. $mail_To = "flarmiey@gmail.com";
  121. $mail_Subject = "no IPN";
  122. $mail_Body = $req;
  123. mail($mail_To, $mail_Subject, $mail_Body, $mail_From);
  124.  
  125. }
  126. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement