Help With PayPal IPN

Discussion in 'PHP' started by GeelongTECH, Jan 18, 2011.

  1. #1
    Hello,
    I need help with PayPal IPN

    I need a page (ipn.php) that will have the IPN code.

    When the payment has been made i want to to write
    require_once 'db.php';
    @mysql_query("UPDATE members SET money = money+".$value." WHERE paypal_email = '".$pp_email."'");
    @mysql_query("UPDATE members SET paypal = paypal+".$value." WHERE paypal_email = '".$pp_email."'");
    PHP:
    I need the script to get the value($value) of what they added to there account and i need it to get the mail they paid with for $pp_email

    Please Help,
    Chris
     
    GeelongTECH, Jan 18, 2011 IP
  2. lowridertj

    lowridertj Well-Known Member

    Messages:
    2,882
    Likes Received:
    40
    Best Answers:
    0
    Trophy Points:
    195
    #2
    First you will want to make sure it is paypal sending the request to the ipn file.

    Here is a complete file you can use as a drop in solution

    
    <?php
    //include database connection file
    include("db.php");
    
    // read the post from PayPal system and add 'cmd'
    $req = 'cmd=_notify-validate';
    
    foreach ($_POST as $key => $value) {
      $value = urlencode(stripslashes($value));
      $req .= "&$key=$value";
    }
    
    // post back to PayPal system to validate
    $header .= "POST /cgi-bin/webscr HTTP/1.0\r\n";
    $header .= "Content-Type: application/x-www-form-urlencoded\r\n";
    $header .= 'Content-Length: ' . strlen($req) . "\r\n\r\n";
    $fp = fsockopen ('www.paypal.com', 80, $errno, $errstr, 30);
    
    
    // assign posted variables to local variables
    // note: additional IPN variables also available -- see IPN documentation
    $item_name = $_POST['item_name'];
    $receiver_email = $_POST['receiver_email'];
    $item_number = $_POST['item_number'];
    $invoice = $_POST['invoice'];
    $payment_status = $_POST['payment_status'];
    $payment_gross = $_POST['payment_gross'];
    $txn_id = $_POST['txn_id'];
    $pp_email = $_POST['payer_email'];
    
    
    if (!$fp) {
    	$req .="&ERROR";
      // ERROR
      echo "$errstr ($errno)";
    } else {
      fputs ($fp, $header . $req);
      while (!feof($fp)) {
        $res = fgets ($fp, 1024);
        if (strcmp ($res, "VERIFIED") == 0) {
    				$req .="&FAILED";
            echo "<pre>";
            print_r($_POST);
            if($_POST[payment_status]=="Completed") {
    
    
    // update accounts and logs here
    @mysql_query("UPDATE members SET money = money+".$payment_gross." WHERE paypal_email = '".$pp_email."'");
    @mysql_query("UPDATE members SET paypal = paypal+".$payment_gross." WHERE paypal_email = '".$pp_email."'");
    
            }
    
          // check the payment_status is Completed
          // check that txn_id has not been previously processed
          // check that receiver_email is an email address in your PayPal account
          // process payment
          }
          else if (strcmp ($res, "INVALID") == 0) {
          	$req .="&FAILURE";
    				// log for manual investigation
          }
      }
      fclose ($fp);
    }
    ?>
    
    PHP:
     
    lowridertj, Jan 18, 2011 IP