Jump to content

Sending to Database Ex Post Facto Paypal transaction


s1rspyr0

Recommended Posts

have a tricky one here (or what i think is a tricky one, anyways). Is there any way at all to send an sql query AFTER a user has successfully made a PayPal transaction. The reason I find this to be tricky, is because I don't want the query executed until the transaction is complete, so they don't just click on the paypal button without completing to receive unpaid for goods. And as I'm typing this, i think i just figured out how to, lol. Paypal has an option after a successful payment to go to a specified web page. So I guess now my question has changed to....

 

How would I go about writing a php page that is only accessible after a paypal transaction has been completed successfully? is there a bit of code that says the extent of (if this page wasn't opened via blah blah blah then don't allow access to this page)?

Link to comment
Share on other sites

IPN. Google it if you want.

 

Heres my IPN file which stores info into a mysql database hopefully you can get an idea from that:

 

<?php

require "paypal_integration_class/paypal.class.php";
require "connect.php";

$p = new paypal_class;
$p->paypal_url = "https://www.paypal.com/cgi-bin/webscr";

if($p->validate_ipn()){
if($p->ipn_data['payment_status']=='Completed'){
    if($p->ipn_data['receiver_email']=='donations@nmdgaming.com'){
        //Prepare values
        $tax_id              = esc($p->ipn_data['txn_id']);
        $item_number         = esc($p->ipn_data['item_number']);
        $first_name          = esc($p->ipn_data['first_name']);
        $last_name           = esc($p->ipn_data['last_name']);
        $payer_email         = esc($p->ipn_data['payer_email']);
        $amount              = (float) ($p->ipn_data['mc_gross']-$p->ipn_data['mc_fee']);
        $mc_gross            = esc($p->ipn_data['mc_gross']);
        $mc_fee              = esc($p->ipn_data['mc_fee']);
        $receiver_email      = esc($p->ipn_data['receiver_email']);
        $payment_type        = esc($p->ipn_data['payment_type']);
        $payment_status      = esc($p->ipn_data['payment_status']);
        $payment_date        = esc($p->ipn_data['payment_date']);
        $payer_business_name = esc($p->ipn_data['payer_business_name']);
        $payer_status        = esc($p->ipn_data['payer_status']);
        $residence_country   = esc($p->ipn_data['residence_country']);
        $mc_currency         = esc($p->ipn_data['mc_currency']);
        $payer_id            = esc($p->ipn_data['payer_id']);
        $receiver_id         = esc($p->ipn_data['receiver_id']);
        $parent_txn_id       = esc($p->ipn_data['parent_txn_id']);
    
        //Create query
        $query1 = "INSERT INTO donations
                    (`txn_id`, `item_number`, `first_name`, `last_name`, `payer_email`,
                    `amount`, `mc_gross`, `mc_fee`, `receiver_email`, `payment_type`,
                    `payment_status`, `payment_date`, `payer_business_name`, `payer_status`,
                    `residence_country`, `mc_currency`, `payer_id`, `receiver_id`)
                  VALUES
                    ('{$tax_id}', '{$item_number}', '{$first_name}', '{$last_name}', '{$payer_email}',
                    '{$amount}', '{$mc_gross}', '{$mc_fee}', '{$receiver_email}', '{$payment_type}',
                    '{$payment_status}', '{$payment_date}', '{$payer_business_name}', '{$payer_status}',
                    '{$residence_country}', '{$mc_currency}', '{$payer_id}', '{$receiver_id}')";
    
        //Execute query
        $result1 = mysql_query($query1) or die("Error:<br />".mysql_error()."<br />Query:<br />$query");
        
        mysql_free_result($result1);
    }
}elseif($p->ipn_data['payment_status']=='Refunded'){
    $parent_txn_id       = esc($p->ipn_data['parent_txn_id']);
    
      $query2 = "DELETE FROM donations WHERE txn_id='$parent_txn_id'";
      
      $result2 = mysql_query($query2) or die("Error:<br />".mysql_error()."<br />Query:<br />$query");
      
      mysql_free_result($result2);
  }
}

function esc($str)
{
global $link;
return mysql_real_escape_string($str,$link);
}


mysql_free_result($link);
mysql_close($link);
?>

 

        'item_number' => '1',
      'amount' => $_POST['amount'],
      'notify_url' => $url.'/ipn.php',
      'return' => 'http://forums.nmdgaming.com/viewtopic.php?f=9&t=342',
  );

 

You need notify_url set to hidden when someone is using a form or use your paypal account settings and make every transaction send towards ipn

 

Hope i helped you out.

Link to comment
Share on other sites

that is GREAT stuff man! I'm liking this place ALOT more than the other php forums. You guys are actually trying to point me in the right direction rather than telling me "This isn't the place to learn, go to google and read a tutorial" while tutorials are nice, if you don't know what you're looking for, there a bit useless, heh. so again, thanks so much everyone! i'll let ya know how it works out.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.