How to confirm payment in paypal using IPN

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vjayis
    New Member
    • Mar 2008
    • 134

    How to confirm payment in paypal using IPN

    Hi

    i m a working with paypal to integrate it with my website.,

    users can book the hall regarding time basis.

    so i m sending the amount to paypal by paynow button created through my paypal test account.

    and i inserted the ipn coding in the notify_url specified.

    here is the code

    Code:
    <?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.sandbox.paypal.com', 80, $errno, $errstr, 30);
    // assign posted variables 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'];
    $payment_date = $_POST['payment_date'];
    $first_name = $_POST['first_name'];
    $last_name = $_POST['last_name'];
    $payment_type = $_POST['payment_type'];
    $payment_status = $_POST['payment_status'];
    $payment_gross = $_POST['payment_gross'];
    $payment_fee = $_POST['payment_fee'];
    $settle_amount = $_POST['settle_amount'];
    $memo = $_POST['memo'];
    $payer_email = $_POST['payer_email'];
    $receiver_email = $_POST['receiver_email'];
    $txn_id = $_POST['txn_id'];
    $txn_type = $_POST['txn_type'];
    $payer_status = $_POST['payer_status'];
    $address_street = $_POST['address_street'];
    $address_city = $_POST['address_city'];
    $address_state = $_POST['address_state'];
    $address_zip = $_POST['address_zip'];
    $address_country = $_POST['address_country'];
    $address_status = $_POST['address_status'];
    $item_name = $_POST['item_name'];
    $item_number = $_POST['item_number'];
    $tax = $_POST['tax'];
    $option_name1 = $_POST['option_name1'];
    $option_selection1 = $_POST['option_selection1'];
    $option_name2 = $_POST['option_name2'];
    $option_selection2 = $_POST['option_selection2'];
    $for_auction = $_POST['for_auction'];
    $invoice = $_POST['invoice'];
    $subscr_id = $_POST['subscr_id'];
    if (!$fp) {
    // HTTP ERROR
    } else {
    fputs ($fp, $header . $req);
    while (!feof($fp)) {
    $res = fgets ($fp, 1024);
    
    if (strcmp ($res, "VERIFIED") == 0) {
    // check the payment_status is Completed
    // check that txn_id has not been previously processed
    // check that receiver_email is your Primary PayPal email
    // check that payment_amount and payment_currency are correct
    // process payment
    // Send a custom email to the value of payer_email
    // Set variables needed for email.
    
    //manually checked by writing the output into a text file
    $fp1 = fopen('data1.txt', 'w');
    fwrite($fp1, $payment_status.$receiver_email);
    fwrite($fp1, $req);
    fclose($fp1);
    
    $from = "Example name";
    $subject = "Thanks for your purchase";
    $msg = "Thank you for your purchase. You have successfully finished your payment. ...";
    mail($payer_email, $subject, $msg, "From: $from");
    }
    else if (strcmp ($res, "INVALID") == 0) {
    // log for manual investigation
    }
    }
    fclose ($fp);
    }
    ?>
    all works fine.

    payment gets paid and data's are transferred to the notify_url.

    I checked it by writing all the output data's into a text file.

    but when the payment is complete the payment_status is "Pending" when it returns to the notify_url.

    it gets changed to "Confirmed" only when i log in to my paypal test account and accept the payment.

    why that it so happen..

    i need to update my database when payment is completed and issue the user a receipt that he had done the booking successfully.

    Can i proceed when payment_status is "Pending".

    or what should i do.

    thanks

    regards
    vijay
  • vjayis
    New Member
    • Mar 2008
    • 134

    #2
    Oops., found the answer.,

    Payment Review is enabled in my sandbox account.

    Solution:
    Turn off this feature in your sandbox account by logging in at https://developer.paypal.com >> click on test accounts >> then, next to your sandbox account, click on 'enabled' under Payment review to disable it.

    found it in paypal developer central.

    Comment

    Working...