Jump to content

Need help with php script from paypal :)


drew7721

Recommended Posts

Hello!!

I want my php to take info from Paypal and save it into my MyQSL database.

 

I have this script from paypal : (it validates that the IPN (Instant Payment Notification)  is ok and it should be able to save the info to MySql db)

The script:

 

<?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 ('ssl://www.paypal.com', 443, $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'];

 

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/payment_currency are correct

// process payment

}

else if (strcmp ($res, "INVALID") == 0) {

// log for manual investigation

}

}

fclose ($fp);

}

?>

 

#Now I need to add something to this script that will make it connect to my mysql database and save the info from variables sent by paypal to my database. (item_name, item_number, payment_status... etc.)

===============================

2nd problem.. :  ???  ???

Now

There is also the checking of :

// 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/payment_currency are correct

// process payment

this I see where it goes in the script.... but i don't know what to write...  :-[

 

This is what it has to do...

1, // check the payment_status is Completed   

check that $payment_status= completed

2, // check that txn_id has not been previously processed

check that the $txn_id is unique in the database (has not been saved before)

3. // check that receiver_email is your Primary PayPal email

check that the $receiver_email = to my email (ex [email protected])

 

Thx a million 4 your help!! you can contact me at [email protected] for questions.. ;) Thx

Link to comment
https://forums.phpfreaks.com/topic/157873-need-help-with-php-script-from-paypal/
Share on other sites

Ok, I've tryed this code but still wont insert in my db,  I connect to db in

<?php require_once('../Connections/RestoA.php'); ?>

..  the code for that is :

 

Code for ../Connections/RestoA.php

<?php
# FileName="Connection_php_mysql.htm"
# Type="MYSQL"
# HTTP="true"
$hostname_RestoA = "my host";
$database_RestoA = "my database";
$username_RestoA = "my username";
$password_RestoA = "my pass";
$RestoA = mysql_pconnect($hostname_RestoA, $username_RestoA, $password_RestoA) or trigger_error(mysql_error(),E_USER_ERROR); 
?>

This was auto done my dreamweaver

 

So the connection is OK

now this is my transactions page .php. script:

 

<?php require_once('../Connections/RestoA.php'); ?>
<?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 ('ssl://www.paypal.com', 443, $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'];

if (!$fp) {
// HTTP ERROR
} else {
fputs ($fp, $header . $req);
while (!feof($fp)) {
$res = fgets ($fp, 1024);
if (strcmp ($res, "VERIFIED") == 0) {
mysql_query("INSERT INTO transaction VALUES('$item_name','$item_numbe','$payment_status','$payment_amount','$payment_currency','$txn_id','$receiver_email','$payer_email')"); 
// 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/payment_currency are correct
// process payment
}
else if (strcmp ($res, "INVALID") == 0) {
// log for manual investigation
}
}
fclose ($fp);
}
?>

 

As you see I try to save the info from paypal to db :

 

if (strcmp ($res, "VERIFIED") == 0) {
mysql_query("INSERT INTO transaction VALUES('$item_name','$item_numbe','$payment_status','$payment_amount','$payment_currency','$txn_id','$receiver_email','$payer_email')"); 

 

What is my mistake??

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.