LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-27-2008, 01:11 PM   #1
trscookie
Member
 
Registered: Apr 2004
Location: oxford
Distribution: gentoo
Posts: 463

Rep: Reputation: 30
php paypal payments


Hello all,

I am trying to set up a method of payment on my website using paypal to pay for services. When you create a paypal button is it possible, on a successful payment to write a record to my mysql database?

If so are there any websites to explain how to do this, i could not find any examples on the paypal homepage

Many thanks in advance.
 
Old 10-27-2008, 02:06 PM   #2
brazilnut
Member
 
Registered: Nov 2007
Posts: 113

Rep: Reputation: 16
The feature your looking for is know as IPN (a.k.a. Instant Payment Notification), there are code examples on the website and a whole forum on their forum site, you'll get lot's of examples of code there, but generally not much actual help, so at that point come back here to LQ...

http://www.paypal.com/cgi-bin/webscr...n-code-outside
http://www.paypaldeveloper.com/pdn/forums
 
Old 11-15-2008, 10:45 AM   #3
trscookie
Member
 
Registered: Apr 2004
Location: oxford
Distribution: gentoo
Posts: 463

Original Poster
Rep: Reputation: 30
taken to sandbox!!!???

Hi, I have tried the phpcode that was supplied on the PayPal website to create a payment page, however when i fill in the details it takes me to the sandbox page rather than submitting the payment, is there something that i am missing?

the toolkit that i have been using:
toolkit-php-0.51.zip

kindest regards
trscookie.
 
Old 11-26-2008, 11:46 AM   #4
nehaandrew
Member
 
Registered: Nov 2008
Posts: 53

Rep: Reputation: 15
TrsCookie,
Can you please share the code (If there aren't any IP violations you forsee i.e.) you are using to achieve the PayPal integration. I am currently working on a project which requires PayPal integration too. It would reaaaaally help me out if you could give me pointers to any resources that could help me out with this too.


With Best Regards,
Neha Andrew

Linux

Last edited by nehaandrew; 11-30-2008 at 01:20 AM.
 
Old 11-26-2008, 03:55 PM   #5
trscookie
Member
 
Registered: Apr 2004
Location: oxford
Distribution: gentoo
Posts: 463

Original Poster
Rep: Reputation: 30
Hi, yes I will post the code. From what I can tell it works, however I haven't yet processed a payment. I will upload it some time soon, my files are on my host and do not have access to them at this time.
 
Old 11-26-2008, 04:34 PM   #6
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 116Reputation: 116
Paypal is a PITA to integrate for IPN. The sandbox is not reliable and often does not work like the live site.

Paypal provides documentation for their SDK which tells you how to do it. Visit their site and download the PDF files.

Here are some code fragments that I use on my site. This first fragment is called order_transfer_paypal.php and is used to actually send the sale information to the Paypal site and transfer the user to that site so that they can transfer funds using their account:

Code:
	(do lots of order processing things here, such as write appropriate things to the database.  Then ship
the transaction to Paypal.

        $postfields ="cmd=".urlencode("ext-enter")."&";
	$postfields.="redirect_cmd=".urlencode("_xclick")."&";
	$postfields.="business=".urlencode($pp_business)."&";
	$postfields.="amount=$total&";
	$postfields.="currency_code=USD&";
	$postfields.="notify_url=".urlencode("$securesite/paypal_notify.php")."&";
	$postfields.="return=".urlencode("$securesite/paypal_return.php")."&";
	$postfields.="cancel_return=".urlencode("$securesite/paypal_cancel.php")."&";
	$postfields.="invoice=".$cart_id." &";
	$postfields.="item_name=".urlencode("Payment for Order Number $cart_id");
	$ch = curl_init();
	curl_setopt ($ch, CURLOPT_POST,true);
	curl_setopt ($ch, CURLOPT_POSTFIELDS, $postfields); 
	//curl_setopt($ch,CURLOPT_POSTFIELDS,"business=$pp_business");
	curl_setopt($ch, CURLOPT_HEADER, false);
	curl_setopt ($ch, CURLOPT_URL,$pp_gateway);
	curl_setopt ($ch, CURLOPT_FORBID_REUSE, false);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	$response = curl_exec($ch);
	$response = str_replace("top.location.replace(document.location);"," ",
$response);
	echo $response;
//exit();
	if (curl_errno($ch)) {
   		print curl_error($ch);
	} else {
	    	curl_close($ch);
	}
}
This next php script processes the return that comes from Paypal after the customer makes the payment. When the payment is cleared, whether that is immediately or after several days, this script (paypal_notify.php) is invoked.

Note that this script is named in the message that is sent to Paypal in the order_transfer_paypal.php script

Code:
php
// paypal notification URL for instant payments on paypal
// needed to verify transactions and payment status 
require("cartfcns.php");
require_once("config.php");
$cart_id=$_REQUEST['invoice'];
$txn_id=$_REQUEST['txn_id'];
$payment_status=$_REQUEST['payment_status'];
$receiver_email=$_REQUEST['receiver_email'];
mysql_connect("$host","$user","$pass");
mysql_select_db("$database");
$qstr="SELECT * FROM custlist WHERE cart_id='$cart_id'";
$result=mysql_query($qstr);
$row = mysql_fetch_array($result,MYSQL_ASSOC);
$_REQUEST['cmd']="_notify-validate";
$usearray = array();
foreach($_REQUEST as $key=>$value) {
	 $usearray[]=var_export($key,true).'='.urlencode(var_export($value,true));
}
$outstr=implode('&',$usearray);
$outstr=str_replace("%27","",$outstr);
$outstr=str_replace("'","",$outstr);
$session = curl_init($pp_gateway);
curl_setopt ($session, CURLOPT_POST, true);
curl_setopt ($session, CURLOPT_POSTFIELDS, $outstr); 
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($session);
curl_close($session);
//$response="VERIFIED";
if($response == "VERIFIED"){
	if($row['cart_id']=="" or !($receiver_email == $pp_business) or !($row['amount'] == $_REQUEST['mc_gross']) or !($_REQUEST['mc_currency']=='USD')){
		$fp=fopen("PPcommandline.txt","a");
		fputs($fp,$outstr);
		fclose($fp);
		die();
	}
	$doadownload = false;
	$qstr = "UPDATE custlist SET verification='$payment_status',txn_id='$txn_id' WHERE cart_id='$cart_id'";
 	$result=mysql_query($qstr);
	if($payment_status == "Completed"){
		$email=$row['email'];
		$custname=$row['name'];
		$message="Dear $custname;\n\nYour Paypal payment has been approved.\n";
		$qstr = "SELECT * from purchlist WHERE cart_id='$cart_id'";
		$result=mysql_query($qstr);
		$row = mysql_fetch_array($result,MYSQL_ASSOC);
		if($row) {
			$doadownload=true;
			$ii=1;
			$useurl = array();
			$prodlist = array();
			$regno = array();
			while($row){
				$useurl[$ii]=$row['URL'];
				$prodlist[$ii]=$row['code_no'];
				$regno[$ii]=Make_Registration($prodlist[$ii]);
				$ii=$ii+1;
				$row = mysql_fetch_array($result,MYSQL_ASSOC);
			}
			$ii=$ii-1;
		}//end of if($row)
		if($doadownload) {
			$message.="You may now download the products you have ordered by clicking on the enclosed links.  If your products require a keyfile, you will receive that by a separate email.\n\nPlease complete your downloads within 72 hours; beyond that time the enclosed links may not be valid.";
			$jj=1;
			while($jj <= $ii){
				$qstr="SELECT keyfile,item FROM products WHERE code_no='".$prodlist[$jj]."'"; 
				$result=mysql_query($qstr);
				$row = mysql_fetch_array($result,MYSQL_ASSOC);
				$ourkey=$row['keyfile'];
				if ($ourkey <> '' and $ourkey <> 'RegOnly'){
					include "mail_attachment.php";
					$ourkey=$files_path.$ourkey;
					$keymsg="Do not reply to this email; the mailbox is not monitored by a human and the reply will be discarded.\n  Thank you for your order.  This email contains as an attachment a keyfile that is critically important to the operation of The Property Manager.\n  After installing your download, unzip this keyfile directly into your installation folder, which by default will be C:\ThePropertyManager.\n  When you first start the program, it will require you to enter your registration number.  That number is ".$regno[$jj].", which is case sensitive and must be entered exactly as written here.\nIf you have any troubles, contact support@softwareforlandlords.com";
					$subject =   "This is your program keyfile and registration";  
					mail_attachment ($receipt , $email, $subject, $keymsg, $ourkey);
				}// end if $ourkey
				$message.="<a href=\"$securesite/dl_center/index.php?".$useurl[$jj]."\" style=\"color:blue;text-decoration:underline\">Download ".$row['item']."</a>\n";
				if(!($regno[$jj]=="")){
					$message.="<P ALIGN=CENTER><FONT FACE=\"Times New Roman\"><FONT SIZE=3>Your program registration code is ".$regno[$jj].".  Please make a note of it because when you first start the program, you will have to enter it.\n\n";
				}
				$jj=$jj+1;
			}//end while jj <= ii
			$message.="If you have any problems downloading or installing your software, please contact support@softwareforlandlords.com";
		} else {
			$message.="Your order will be shipped within the next business day.\n\n";
		}//end if doadownload
		$mtitle="Paypal Approval of Online Order $cart_id";
		mail("$receipt,$email","$mtitle", "$message", "From:$receipt");
	}// end if payment_status is COMPLETED or PROCESSED
} //end if response=VERIFIED
?>
When the user leaves the Paypal site, this script is invoked (paypal_return.php). Note that this script is named in the original message sent to Paypal from order_transfer_paypal.php.

Code:
<?php
// possible payment status values, per PayPal
//Canceled-Reversal
//Completed
//Denied
//Expired
//Failed
//In-Progress
//Partially-Refunded
//Pending
//Processed
//Refunded
//Reversed
//Voided 
if($_REQUEST['st']=="Pending") {
	$result_string = "Thank you for your payment.  Your order will be shipped as soon as your PayPal payment is cleared.";
} else if($_REQUEST['st']=="Denied" or $_REQUEST['st']=="Failed") {
	$result_string = "Your PayPal payment did not go through.  Please contact Just So Software to make other payment arrangements.";
} else if($_REQUEST['st']=="Completed" or $_REQUEST['st']=="Processed") {
	$result_string = "Thank you for your order.  It will be shipped promptly.  If you have requested a download, an email has been sent to you with the download links to follow.";
} else {
	$result_string = "There has been an unknown error in your paypal payment.  The response from Paypal was: ".$_REQUEST['st'].". Please contact Just So Software with this information.";
}
?>
<html>

<head>

<title>Completion form</title>

<style>

A:Link {color:000000;text-decoration:none;}

A:Visited {color:000000;text-decoration:none;}

A:Hover {color:F70404;text-decoration:underline;}

</style>

</head>

<BODY TEXT="#000000" BGCOLOR="#FFFFFF" LINK="#660101" VLINK="#660101"
ALINK="#F70404">
<CENTER><?php echo $result_string;?></CENTER>
</BODY>
</HTML>
If the user cancels the paypal transaction while at paypal, control is returned to this script (paypal_cancel.php). Note that this script is named in the message sent originally to paypal.

Code:
<html>

<head>

<title>Completion form</title>

<style>

A:Link {color:000000;text-decoration:none;}

A:Visited {color:000000;text-decoration:none;}

A:Hover {color:F70404;text-decoration:underline;}

</style>

</head>

<BODY TEXT="#000000" BGCOLOR="#FFFFFF" LINK="#660101" VLINK="#660101"
ALINK="#F70404">
<CENTER>Your payment was canceled.</CENTER>
</BODY>
</HTML>
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
recover php session id after PayPal UMG:Chicken_Soüp Programming 1 02-04-2008 06:51 AM
php post method paypal submit UMG:Chicken_Soüp Programming 2 02-03-2008 03:19 AM
Looking for php script to keep track of payments received michaeljoser Linux - Software 3 11-05-2007 06:26 AM
LXer: Belgium's Leading Electronic Payments Company Selects Open Source Solution OpenTrust LXer Syndicated Linux News 0 09-07-2006 09:03 AM
ebay and paypal GraemeK General 7 02-14-2004 06:35 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:38 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration