PayPal IPN Script not working (PHP)
I'm adding a feature to my website.
It needs paypal IPN, the money sends to my account but my queries do not
send any data...
When I run the queries in php myadmin with data instead of variables they
all work..
I know its probably something stupid but when I pay my database is not
updating like it should and I am pulling my hair out!
Any help would be much appreciated.
<?php
include 'core/init.php';
// PHP 4.1
// 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'];
$advert_id = $_POST['custom'];
$advert_id = mysql_real_escape_string($advert_id);
$txn_id = mysql_real_escape_string($txn_id);
$payer_email = mysql_real_escape_string($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
if ($payment_status=='Completed') {
// check that txn_id has not been previously processed
$txn_id_check = mysql_query("SELECT `txn_id` FROM `log` WHERE
`txn_id` = '$txn_id'");
if (mysql_num_rows($txn_id_check) != 1){
// check that receiver_email is your Primary PayPal email
if ($receiver_email=='payments@airgunvillage.com'){
// check that payment_amount/payment_currency are correct
if ($payment_amount == '0.01' && $payment_currency == 'GBP'){
//add txn_id to databse
$log_query = mysql_query("INSERT INTO `log` VALUES
('', '$txn_id', '$payer_email')");
//update paid to 1
$update_paid = mysql_query("UPDATE `adverts` SET
`paid_status` = '1' WHERE `advert_id` = '$advert_id");
}
}
}
}
}
else if (strcmp ($res, "INVALID") == 0) {
// log for manual investigation
}
}
fclose ($fp);
}
?>
?>
If it helps also this is the HTML button
<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_xclick">
<input type="hidden" name="business" value="payments@airgunvillage.com">
<input type="hidden" name="item_name" value="Single Item Advert">
<input type="hidden" name="item_number" value="1">
<input type="hidden" name="amount" value="0.01">
<input type="hidden" name="no_shipping" value="1">
<input type="hidden" name="no_note" value="1">
<input type="hidden" name="currency_code" value="GBP">
<input type="hidden" name="lc" value="GB">
<input type="hidden" name="bn" value="BuyNow">
<input type="hidden" name="return"
value="http://www.airgunvillage.com/manageadverts.php?success">
<input type="hidden" name="cancel_return"
value="http://www.airgunvillage.com/manageadverts.php?failed">
<input type="hidden" name="rm" value="2">
<input type="hidden" name="notify_url"
value="http://www.airgunvillage.com/ipn/ipn.php">
<input type="hidden" name="custom" value="<?php echo $adverts_id?>">
<button type="submit">Pay Now - £0.01</button>
</form>
It's just really making me pull my hair out.
thanks!
PS. amount is at 0.01 for testing :)
No comments:
Post a Comment