Jump to content

PHP Paypal IPN script, works for 1 game, but not the other?


MasterACE14

Recommended Posts

I have this script, it works fine on 1 of my games, but it doesn't want to work on my other game. I have been staring at it all day, I just can't find anything wrong with it.

 

here's the script:

<?php

  $con = mysql_connect("localhost","****","***");
if (!$con)
  {
die('Could not connect: ' . mysql_error());
  }
  
  mysql_select_db("**", $con); 

include("functions.php");

/////////////////////////////////////////////////
/////////////Begin Script below./////////////////
/////////////////////////////////////////////////

// set this to your e-mail address:
$email = "ace@crikeygames.com.au";


function doTheCurl ()
{
$req = 'cmd=_notify-validate';
foreach ($_POST as $key => $value)
{
	$value = urlencode(stripslashes($value));
	$req .= "&$key=$value";
}
$ch = curl_init();

// check to see if this is sandbox or not
if ($_POST["test_ipn"] == 1)
{
	curl_setopt($ch, CURLOPT_URL, "https://www.sandbox.paypal.com/cgi-bin/webscr");
}
else
{
	curl_setopt($ch, CURLOPT_URL, "https://www.paypal.com/cgi-bin/webscr");
}


curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$fp = curl_exec ($ch);
curl_close($ch);
return $fp;
}

function doTheHttp ()
{
$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";

// check to see if this is sandbox or not.
if ($_POST["test_ipn"] == 1)
{
	$fp = fsockopen ('www.sandbox.paypal.com', 80, $errno, $errstr, 30);
}
else
{
	$fp = fsockopen ('www.paypal.com', 80, $errno, $errstr, 30);
}

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

function doEmail ($title,$email)
{
$alsomail = "$title\n\n----HTTP POST VARS----\n\n";
foreach ($_POST as $key => $value)
{
	$alsomail .= "$key = $value \n\n";
}
foreach($_GET as $kee => $val)
{
	$gets .= "$kee = $val \n\n";
}

mail($email,$title,$alsomail . "\n\n get values \n\n" . $gets);
}

/*
now with both of those functions defined, you can run
a simple check to get back your responce:
*/

$fp = doTheCurl();
if (!$fp)
{
// doEmail("cURL ERROR, SWITCHING TO HTTP",$email);
$fp = doTheHttp();
}
$res = $fp;

/*
and after that, you can check to see if $res is
invalid or verified
*/

if (strcmp ($res, "VERIFIED") == 0)
{
    /*
    log stuff into your database here!

    Also, it's a good idea to send yourself an e-mail with all _POST params, just so you know what's going on.  The e-mail code is here, the database code is not.
    */
    
    doEmail("PayPal Purchase Success",$email);

$val = explode(",",$_POST['custom']);

// e-mail the person their password
$to = $email;
if($val[1] == "ss") {
$subject = "Realm Battles Standard Supporter Status";
}
elseif($val[1] == "uss") {
$subject = "Realm Battles Ultimate Supporter Status";	
}
// message
$message =  "Player with ID: ".$val[0];
if($val[1] == "ss") {
$message .= ' has purchased Standard Supporter Status';
}
elseif($val[1] == "uss") {
$message .= ' has purchased Ultimate Supporter Status';
}

$message .= " More Info: ";
$message .= " Val 0: ".$val[0];
$message .= " Val 1: ".$val[1];

// headers
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html;";
$headers .= " charset=iso-8859-1\r\n";
$headers .= "From: ace@crikeygames.com.au \r\n";

mail($to, $subject, $message, $headers);

if($val[1] == "ss") {
	//standard_supporterstatus($val[0]);
	mysql_query("UPDATE `UserDetails` SET `SScount`=`SScount`+'1' WHERE ID='".$val[0]."'") or die(mysql_error());
} elseif($val[1] == "uss") {
	//ultimate_supporterstatus($val[0]);
	mysql_query("UPDATE `UserDetails` SET `USScount`=`USScount`+'1' WHERE ID='".$val[0]."'") or die(mysql_error());
}

}
else if (strcmp ($res, "INVALID") == 0)
{
    /*
    here i send myself an e-mail saying there's been an
    invalid with all of the details sent to me from ipn.
    that way i have a record of it.
    */
    doEmail("INVALID PayPal Purchase!!",$email);
    
    $val = explode(",",$_POST['custom']);
    
    $to = $email;
    $subject = "Problem with Paypal Purchase";
    
    	// message
$message =  "Player with ID: ".$val[0];
if($val[1] == "ss") {
$message .= ' has purchased Standard Supporter Status';
}
elseif($val[1] == "uss") {
$message .= ' has purchased Ultimate Supporter Status';
}

$message .= " But a problem has occured ";
$message .= " More Info: ";
$message .= " Val 0: ".$val[0];
$message .= " Val 1: ".$val[1];

// headers
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html;";
$headers .= " charset=iso-8859-1\r\n";
$headers .= "From: ace@crikeygames.com.au \r\n";

mail($to, $subject, $message, $headers);

}
else
{
    /*
    here i send myself an e-mail saying there has been a
    fatal error - if the paypal server goes down this will
    happen? doesn't seem likely but it's always good to have
    some backup just incase. I also place all data sent
    from IPN in this e-mail so I have record of it.

    This can also happen with an HTTP error - just a note
    */
    doEmail("Error in IPN Code - Normally HTTP Error",$email);
}
?>

 

First I thought I must of been using the explode(); function wrong. But after some tests, turns out I'm using it correctly.

 

I was getting e-mailed this...

INVALID PayPal Purchase!!

 

----HTTP POST VARS----

 

 

 

get values

 

But I copied and pasted my custom mail(); part to the Invalid PayPal Purchase if statement and now it's not e-mailing me at all.

 

I have no idea what is wrong with it.

 

I'm positive the problem is with the code I have added to the script.

 

As the IPN script itself works fine.

 

I just can't work out what is wrong with the script.

 

Any help is GREATLY appreciated.

 

Regards ACE

Link to comment
Share on other sites

Your going to lath, I made this 3 years ago.

 

It still get used via paypal users i must lath.

 

i must be honest only had 1 bad report in 3 years lol.

 

just might help.

 

/Create a mysql database for the ipn reponse so you get all the users information,
this will also let you no that the user has payed you.

// make sure to put this in the same database as you need to get payments.

CREATE TABLE paypal_table (
  id int(11) NOT NULL auto_increment,
  payer_id varchar(60) default NULL,
  payment_date varchar(50) default NULL,
  txn_id varchar(50) default NULL,
  first_name varchar(50) default NULL,
  last_name varchar(50) default NULL,
  payer_email varchar(75) default NULL,
  payer_status varchar(50) default NULL,
  payment_type varchar(50) default NULL,
  memo tinytext,
  item_name varchar(127) default NULL,
  item_number varchar(127) default NULL,
  quantity int(11) NOT NULL default '0',
  mc_gross decimal(9,2) default NULL,
  mc_currency char(3) default NULL,
  address_name varchar(255) NOT NULL default '',
  address_street varchar(255) NOT NULL default '',
  address_city varchar(255) NOT NULL default '',
  address_state varchar(255) NOT NULL default '',
  address_zip varchar(255) NOT NULL default '',
  address_country varchar(255) NOT NULL default '',
  address_status varchar(255) NOT NULL default '',
  payer_business_name varchar(255) NOT NULL default '',
  payment_status varchar(255) NOT NULL default '',
  pending_reason varchar(255) NOT NULL default '',
  reason_code varchar(255) NOT NULL default '',
  txn_type varchar(255) NOT NULL default '',
  PRIMARY KEY  (id),
  UNIQUE KEY txn_id (txn_id),
  KEY txn_id_2 (txn_id)
) TYPE=MyISAM;


// use this for the insert of the above mysql table.
// this inserts the information into the above table as adove.

// example in the working script below.

$qry="INSERT INTO paypal_table VALUES (0 , '$payer_id', '$payment_date', '$txn_id', '$first_name', '$last_name', '$payer_email', '$payer_status', '$payment_type', '$memo', '$item_name', '$item_number', $quantity, $mc_gross, '$mc_currency', '$address_name', '".nl2br($address_street)."', '$address_city', '$address_state', '$address_zip', '$address_country', '$address_status', '$payer_business_name', '$payment_status', '$pending_reason', '$reason_code', '$txn_type')";

$result=mysql_query($qry);




//make a form to get payments.

// sandbox is for testing and paypal is for live transaction.

//example this is setup for testing the code within sandbox and sandbox is a mirrow site for paypal and has got to be setup
via the sandbox url.

<form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post">

// example this is setup to take a live payment from your live paypal account.

<form action="https://www.paypal.com/cgi-bin/webscr" method="post">

//example this is to setup the account to send a payment to you, when you setup sandbox to test the code you also need to set this to the testing email address.

<input type="hidden" name="business" value="admin@whatever.com">

// example these are for paypal to return to the ipn code to valadate payment.

<input type="hidden" name="currency_code" value="GBP">
<input type="hidden" name="return" value="http://whatever.com/ipn.php">


---------------the form prpoer format for ipn ------------------------------------------------------
<form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_xclick">
<input type="hidden" name="business" value="admin@hotmail.com">
<input type="hidden" name="item_name" value='<? echo"Freelance Programming Advert From $added_date to $exspire_date"?>'>
<input type="hidden" name="item_number" value='<?php echo $record['id'] ?>'>
<input type="hidden" name="amount" value='<?php echo $price ?>'>
<input type="hidden" name="no_note" value="1">
<input type="hidden" name="currency_code" value="GBP">
<input type="hidden" name="return" value="http://freelanceprogrammers.ath.cx/test.php">
<input type="hidden" name="rm" value="2">
<input type="hidden" name="cancel_return" value=" ">
<input type="image" src="https://www.paypal.com/en_US/i/btn/x-click-but23.gif" border="0" name="submit" alt="Make payments with PayPal - it's fast, free and secure!">
</form>

--------------------end----------------------------------------------------------------


//php working ipn code

this is a fully working version of ipn in php format follow above example.

<?php session_start();


// 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'];


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


$qry="INSERT INTO paypal_table VALUES (0 , '$payer_id', '$payment_date', '$txn_id', '$first_name', '$last_name', '$payer_email', '$payer_status', '$payment_type', '$memo', '$item_name', '$item_number', $quantity, $mc_gross, '$mc_currency', '$address_name', '".nl2br($address_street)."', '$address_city', '$address_state', '$address_zip', '$address_country', '$address_status', '$payer_business_name', '$payment_status', '$pending_reason', '$reason_code', '$txn_type')";

$result=mysql_query($qry);

echo 'payment tacken';
}
else if (strcmp ($res, "INVALID") == 0) {

echo 'no payment sorry';
}
}
fclose ($fp);
}

?>

Link to comment
Share on other sites

I'm still using my script. I still haven't touched the script since I posted it here, and it is now sending my custom e-mail to me.

 

But $_POST['custom'] isn't coming up in my e-mail. Although when I check paypal history, $_POST['custom'] is sent.

 

Player with ID: But a problem has occured More Info: Val 0: Val 1:

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.