Alex
Alex

Reputation: 2062

PayPal IPN - unable to receive email notifications

I'm trying to implement PayPal in my project and setup IPN (through PayPal sandbox) to receive payment notifications. I created a dev account and have two email addresses. One for the seller ([email protected]) and one for the buyer. I store the php file online in my site and configure the ipn url on the seller account as follows:

http://example.org/someFolder/ipnsample.php

When I try to simulate IPN I get a message IPN was sent and the handshake was verified. so the configuration seems OK, however I'm not able to receive any notifications to my mail.

PHP:

   // Send an empty HTTP 200 OK response to acknowledge receipt of the notification 
   header('HTTP/1.1 200 OK'); 

     // Assign payment notification values to local variables
  $item_name        = $_POST['item_name'];
  $item_number      = $_POST['item_number'];
  $payment_status   = $_POST['payment_status'];
  $payment_amount   = $_POST['mc_gross'];
  $payment_currency = $_POST['mc_currency'];
  $txn_id           = $_POST['txn_id'];
  $receiver_email   = $_POST['receiver_email'];
  $payer_email      = $_POST['payer_email'];

    // Build the required acknowledgement message out of the notification just received
  $req = 'cmd=_notify-validate';               // Add 'cmd=_notify-validate' to beginning of the acknowledgement

  foreach ($_POST as $key => $value) {         // Loop through the notification NV pairs
    $value = urlencode(stripslashes($value));  // Encode these values
    $req  .= "&$key=$value";                   // Add the NV pairs to the acknowledgement
  }

    // Set up the acknowledgement request headers
$header .= "POST /cgi-bin/webscr HTTP/1.0\r\n";
$header .= "Host: www.sandbox.paypal.com\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
$fp = fsockopen ('tls://www.sandbox.paypal.com', 443, $errno, $errstr, 30);

  // Open a socket for the acknowledgement request


  // Send the HTTP POST request back to PayPal for validation
  fputs($fp, $header . $req);


    while (!feof($fp)) {                     // While not EOF
    $res = fgets($fp, 1024);               // Get the acknowledgement response
    if (strcmp ($res, "VERIFIED") == 0) {  // Response contains VERIFIED - process notification

      // Send an email announcing the IPN message is VERIFIED
      $mail_From    = "[email protected]";
      $mail_To      = "[email protected]";
      $mail_Subject = "VERIFIED IPN";
      $mail_Body    = $req;
      mail($mail_To, $mail_Subject, $mail_Body, $mail_From);
    } 
    else if (strcmp ($res, "INVALID") == 0) {
      // Authentication protocol is complete - begin error handling

      // Send an email announcing the IPN message is INVALID
      $mail_From    = "[email protected]";
      $mail_To      = "[email protected]";
      $mail_Subject = "INVALID IPN";
      $mail_Body    = $req;

      mail($mail_To, $mail_Subject, $mail_Body, $mail_From);
    }
  }

fclose($fp);  // Close the file 

HTML (buy form):

<form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post" target="_top">
    <input type="hidden" name="cmd" value="_xclick">
    <input type="hidden" name="business" value="[email protected]">
    <input type="hidden" name="currency_code" value="USD">
    <input type="hidden" name="item_name" value="Trex hide">
    <input id="PayPalPrice1" type="hidden" name="amount" value="">
    <button name="submit" class="btn btn-lg btn-success "> Checkout With <b> PayPal </b> </button>
</form>

Upvotes: 1

Views: 119

Answers (1)

Pavel Kenarov
Pavel Kenarov

Reputation: 964

First check if the mail method work correctly:

  $mail_From    = "[email protected]";
  $mail_To      = "[email protected]";
  $mail_Subject = "VERIFIED IPN";
  $mail_Body    = $req;
  mail($mail_To, $mail_Subject, $mail_Body, $mail_From);

Just put the code on top of listener php file and refresh page. If the mail method work ok, just debug variable $fp with serialize($fp) method before the while()

  $mail_From    = "[email protected]";
  $mail_To      = "[email protected]";
  $mail_Subject = "VERIFIED IPN";
  $mail_Body    = serialize($fp);
  mail($mail_To, $mail_Subject, $mail_Body, $mail_From);

Upvotes: 1

Related Questions