CodeClerks

[Help] PayPal IPN



Write the reason you're deleting this FAQ

[Help] PayPal IPN

Hello,

I'm trying to create a PayPal IPN to insert data into the database after a successful transaction.

Here's my SQL:
[PHP]
-- phpMyAdmin SQL Dump
-- version 3.5.8
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Jun 22, 2013 at 01:23 PM
-- Server version: 5.1.69-cll
-- PHP Version: 5.3.17

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;

--
-- Database: `xxx_xxxxxxxxxxx`
--

-- --------------------------------------------------------

--
-- Table structure for table `transactions`
--

CREATE TABLE IF NOT EXISTS `transactions` (
`id` int(255) NOT NULL AUTO_INCREMENT,
`txn_id` varchar(255) NOT NULL,
`package_name` varchar(255) NOT NULL,
`package_days` int(255) NOT NULL,
`package_price` decimal(9,2) NOT NULL,
`payer_email` varchar(255) NOT NULL,
`buyer_ip` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
[/PHP]


Here's the PayPal IPN code:

[PHP]
<?php

$db = mysql_connect("localhost", "database", "password") or die(mysql_error());
mysql_select_db("db_name",$db) or die(mysql_error());


$package_days = $package->package_days;
// echo $package_days;
// just a test to see if database is fetching results and displaying them

// PHP 4.1s

// 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.sandbox.paypal.com', 443, $errno, $errstr, 30);

// Using PayPal sandbox/developer

// 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'];
$ip = $_SERVER['REMOTE_ADDR'];


if (!$fp) {
// HTTP ERROR
} else {
fputs ($fp, $header . $req);
while (!feof($fp)) {
$res = fgets ($fp, 1024);
if (strcmp ($res, "VERIFIED") == 0) {

$result = mysql_query("SELECT * FROM `packages`");
$package = mysql_fetch_assoc($result);

$package_days = $package['package_days'];

mysql_query("INSERT INTO transactions (txn_id, package_name, package_days, package_price, payer_email, buyer_ip) VALUES ('".$txn_id."', '".$item_name."', '".$package_days."', '".$payment_amount."', '".$payer_email."', '".$ip."')");

}

else if (strcmp ($res, "INVALID") == 0) {
// log invalid transaction, email administrator and email payer email.
}
}
fclose ($fp);
}
>
[/PHP]

For some reason I can not find out exactly why it's not inserting values into the database.. ? Maybe a fresh new pair of eyes can find the problem?

Kind Regards,
Everett

Comments

Please login or sign up to leave a comment

Join
robertman11
Do you get any errors?



Are you sure you want to delete this post?

Everett
Do you get any errors?


Hello,

No errors.. even when I have the error setting to show errors. A bit odd, any clue?

Kind Regards,
Everett



Are you sure you want to delete this post?

JaanPorkon
[PHP]$sql = "INSERT INTO transactions (txn_id, package_name, package_days, package_price, payer_email, buyer_ip) VALUES ('".$txn_id."', '".$item_name."', '".$package_days."', '".$payment_amount."', '".$payer_email."', '".$ip."')";

if(mysql_query($sql) == false)
{
echo mysql_error();
}
[/PHP]

mysql_query returns true or false and resource.. you are not checking for it.
Try my code.. it shows errors when mysql_query returns false



Are you sure you want to delete this post?