ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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...
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
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.