Help With PayPal IPN

Discussion in 'Programming' started by GeelongTECH, Jan 29, 2011.

  1. #1
    Hello,
    I have a page ipn.php
    <?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:
    ipn.php is the file paypal needs

    but on http://example.com/account/add-funds/paypal/ i have a page where the user will make a request to add money to his account.

    I need help on what code should be on http://example.com/account/add-funds/paypal/

    Please Help,
    This is giving me a headache
    Chris :D
    </div>
     
    GeelongTECH, Jan 29, 2011 IP
  2. newgenservices

    newgenservices Well-Known Member

    Messages:
    862
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    105
    Digital Goods:
    1
    #2
    
    <form name="_xclick" target="paypal" action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post">
    <input type="hidden" name="cmd" value="_cart">
    <input type="hidden" name="business" value="my.paypal@example.com">
    <input type="hidden" name="currency_code" value="USD">
    <input type="hidden" name="item_name" value="Item Name">
    <input type="hidden" name="amount" value="1.00">
    <input type="image" src="http://www.paypal.com/en_US/i/btn/sc-but-01.gif" border="0" name="submit" alt="Make payments with PayPal - it's fast, free and secure!">
    <input type="hidden" name="add" value="1">
    <input type="hidden" name="custom" value="AnyIdentifierSayUsername">
    <input type="hidden" name="return" value="http://www.example.com/thanks.php">
    <input type="hidden" name="cancel_return" value="http://www.example.com/failed.php">
    <input type="hidden" name="notify_url" value="http://www.example.com/ipn.php">
    </form>
    
    Code (markup):
    Use the identifier you pass in custom field to collect back and process the IPN recieved. Change values as needed. Hope this helps :)
     
    newgenservices, Jan 30, 2011 IP