PayPal sandbox ipn status never verified - paypal

I'm testing a PayPal ipn listener and it seems it NEVER returns VERIFIED status... Here's the script :
<?php
//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.1\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Host:www.sandbox.paypal.com\r\n";
$header .= "Connection: close\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
//$fp = fsockopen ('ssl://www.paypal.com', 443, $errno, $errstr, 30);
$fp = fsockopen ('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30);
//
//error connecting to paypal
if (!$fp) {
//
}
//successful connection
if ($fp) {
fputs ($fp, $header . $req);
//while (!feof($fp)) {
$res = stream_get_contents($fp, 1024);
//$res = fgets ($fp, 1024);
$res = trim($res); //NEW & IMPORTANT
if (strcmp($res, "VERIFIED") == 0) {
// if status is COMPLETED insert order into database
if ($_POST['payment_status'] == "Completed") {
$subject = 'Instant Payment Notification - COMPLETED';
$to = 'my_email_address#gmail.com'; // your email
$body = "An instant payment notification was successfully recieved\n";
$body .= "from ".$_POST['payer_email']." on ".date('m/d/Y');
$body .= " at ".date('g:i A')."\n\nDetails:\n";
foreach ($_POST as $key => $value) { $body .= "\n".$key." : ".$value; }
mail($to, $subject, $body);
}
}
else {
//insert into DB in a table for bad payments for you to process later
$subject = 'Instant Payment Notification - ELSE status';
$to = 'my_email_address#gmail.com'; // your email
$body = "Else clause \n";
$body .= "from ".$_POST['payer_email']." on ".date('m/d/Y');
$body .= " at ".date('g:i A')."\n\nDetails:\n";
foreach ($_POST as $key => $value) { $body .= "\n".$key." : ".$value; }
mail($to, $subject, $body);
}
//}
fclose($fp);
}
?>
I've tried the code with while (!feof($fp)) and $res = fgets ($fp, 1024)
I never get the VERIFIED email and I'm even not sure if it is a coding issue... That's why I'm asking here : maybe some of you had this problem before and can help me. Thanks in advance.
OK, here is the $res posted back :
HTTP/1.1 200 OK
Date: Wed, 26 Feb 2014 12:03:23 GMT
Server: Apache
domain=.paypal.com; path=/; Secure; HttpOnly
Set-Cookie: cookie_check=yes; expires=Sat, 24-Feb-2024 12:03:24 GMT;domain=.paypal.com; path=/; Secure; HttpOnly
Set-Cookie: navcmd=_notify-validate; domain=.paypal.com; path=/; Secure; HttpOnly
Set-Cookie: navlns=0.0; expires=Fri, 26-Feb-2016 12:03:24 GMT; domain=.paypal.com; path=/; Secure; HttpOnly
Set-Cookie: Apache=10.72.109.11.1393416203743713; path=/; expires=Fri, 19-Feb-44 12:03:

Related

PHP mail opens in Gmail but not in Outlook

When I send a mail with PHP it displays correct in Gmail but not in Outlook. It's a mail with a PDF attachment and some text. The PDF is created with fpdf and sent as an attachment, this works fine in Gmail and Outlook.
The only problem there is, in Gmail the text of the emails is displayed correctly and in Outlook it's just a blank page and the text is an extra attachment.
Here is my code:
// encode data
$pdfdoc = $pdf->Output("", "S");
$attachment = chunk_split(base64_encode($pdfdoc));
// email stuff (change data below)
$to = $contactEmail;
$from = $myEmail;
$subject = "MySubjectHere";
$message = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html><head></head><body>
<p>Hi '$contactName.',</p><br>
<p>Please find your pdf attached per your request.</p>
<p>Feel free to call or email if you have any questions.</p>
<p>Kind regards,</p><br>
<p>MyNameHere</p>
<p><strong>Manager</strong></p>
<img alt="image" src="http://LinkToImage.png"/>
<p style="color:#76923c;">
<strong>P:</strong> 01 2345 6789|
<strong>W:</strong> www.example.com</p>
<p style="color:#76923c;">
<strong>A:</strong> 94 Test Street.
</p></body></html>';
// a random hash will be necessary to send mixed content
$separator = md5(time());
// carriage return type (we use a PHP end of line constant)
$eol = PHP_EOL;
// attachment name
$filename = $id."_".str_replace(" ","_",$Name).'.pdf';
// main header
$headers = "From: ".$from.$eol;
$headers .= "Bcc: ".$from.$eol;
$headers .= "MIME-Version: 1.0".$eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"";
// no more headers after this, we start the body! //
$body = "--".$separator.$eol;
$body .= "Content-Transfer-Encoding: 7bit".$eol.$eol;
// message
$body .= "--".$separator.$eol;
$body .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$body .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$body .= $message.$eol;
// attachment
$body .= "--".$separator.$eol;
$body .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol;
$body .= "Content-Transfer-Encoding: base64".$eol;
$body .= "Content-Disposition: attachment".$eol.$eol;
$body .= $attachment.$eol;
$body .= "--".$separator."--";
// send message
mail($to, $subject, $body, $headers);
I found where it went wrong:
$body .= $message.$eol;
Should be
$body .= $message.$eol.$eol;
Finally found it after 3 days!
Thanks for the responses anyway!
$to = $contactEmail;
$from = $myEmail;
$boundary = md5(date('r', time()));
$msg = "This is a multi-part message in MIME format.\n\n";
$msg .= "--$boundary\n";
$msg .= "Content-Type: text/html; charset=utf-8; format=flowed; delsp=yes\n";
$msg .= "Content-Transfer-Encoding: 7bit\n\n";
$msg .= "<h1>Website Enquiry</h1>
<p><strong>Name:</strong> $name<br />
<strong>Email:</strong> $email<br />
<strong>Phone:</strong> $phone<br />
<strong>Subject Name:</strong> $subject<br />
<strong>Question:</strong> $question</p>";
$e_headers = "From: " . $from . "\n";
$e_headers .= "MIME-Version: 1.0\n";
$e_headers .= "Content-Type: multipart/mixed; boundary=\"$boundary\"\n";
mail($to, $subject, $msg, $e_headers);
This is the code I would use for a HTML contact form.
You shouldn't need to pass the content through the PHP_EOL, just the attachment
$separator = md5(time());
$eol = "\r\n";
$message=""; // Always empty
$mailmessage="<h1>Yes got it</h1>"; //html content
$headers = "From: ".$email.$eol;
$headers .= "Bcc:". $Bcc. "\n";
$headers .= "Cc:". $cc. "\n";
$headers .= "Return-Path: <".$email.">\n";
$headers .= "MIME-Version: 1.0" . $eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"" . $eol;
$headers .= "Content-Transfer-Encoding: 7bit" . $eol;
$body .= "--" . $separator . "\r\n";
$body .= "Content-type:text/plain; charset=iso-8859-1\r\n";
$body .= "Content-Transfer-Encoding: 7bit\r\n";
$body .= $message . "\r\n";
$body = "--" . $separator . $eol;
$body .= "Content-type: text/html; charset=\"utf-8\"\r\n";
$body .= "Content-Transfer-Encoding: 8bit\r\n\r\n";
$body .= $mailmessage . $eol.$eol;
$body .= "--" . $separator . $eol;
$body .= "Content-Type: application/octet-stream; name=\"" . $file . "\"" . $eol;
$body .= "Content-Transfer-Encoding: base64" . $eol.$eol;
$body .= "Content-Disposition: attachment" . $eol;
$body .= $content . $eol;
$body .= "--" . $separator . "--";
mail($to, $subject, $body, $headers);

Mail Sending in TCPDF

I have created a PDF using tcpdf function, then i wants to send it to a mail as attachment. i have used following coding at end of the creation of PDF. But it working in some server, but not working in some other servers
$from = "manikandan#niral.us";
$subject = "test";
$message = $_REQUEST['msg'];
// a random hash will be necessary to send mixed content
$separator = md5(time());
// carriage return type (we use a PHP end of line constant)
$eol = PHP_EOL;
// $eol = "\r\n";
// attachment name
$filename = "Yourinvices.pdf";
// encode data (puts attachment in proper format)
$pdfdoc = $pdf->Output('Patient'.$invnumserial.'.pdf', 'S');
$attachment = chunk_split(base64_encode($pdfdoc));
// encode data (multipart mandatory)
$headers = "From: ".$from.$eol;
$headers .= "MIME-Version: 1.0".$eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"".$eol.$eol;
$headers .= "Content-Transfer-Enconding: 7bit".$eol;
$headers .= "This is a MIME encoded message.".$eol.$eol;
// message
$headers .= "--".$separator.$eol;
$headers .= "Content-Type: text/html; charsrt=\"iso-8859-1\"".$eol;
$headers .= $message.$eol.$eol;
// attachment
$headers .= "--".$separator.$eol;
$headers .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol;
//$headers .= "Content-Type: application/zip; name=\"".$filename."\"".$eol;
$headers .= "Content-Transfer-Encoding: base64".$eol;
$headers .= "Content-Disposition: attachment".$eol.$eol;
$headers .= $attachment.$eol.$eol;
$headers .= "--".$separator."--";
// send message
if(#mail($to, $subject, $message, $headers))
{
Redirect("../../patient_invoicelist.php?msg=1");
}
else Redirect("../../patient_invoicelist.php?msg=2");
Use PHPMailer to send Emails with Attachments. It will be very easy to send emails with attachments using this library.
Download PHPMailer library from this link. You will also find examples to use this class.
http://sourceforge.net/projects/phpmailer/files/phpmailer%20for%20php5_6/PHPMailer%20v5.1/
$mail->SetFrom('noreply#test.com', 'test');
$mail->Subject = $subject;
$mail->AddAttachment($Pdf_path_here);
$mail->MsgHTML($body);
$mail->AddAddress($to, "");
if(!$mail->Send())
{
echo "sent";
}
else
{
echo "error";
}

PHP Mail Showing Html Tag

Instead of adding the following Header information it is showing HTML tag in PHP Mail.
$headers = "From: ".$name."<".$email.">\r\n";
$headers .= "Reply To: ".$email."\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "X-Mailer: PHP v".phpversion()."\r\n";
$headers .= "Content-Type: text/html; charset=iso-8859-1\r\n";
$headers .= "Content-Transfer-Encoding: 8bit\r\n";
Please help me.
Try this,
$subject = 'Mail Subject';
$message = 'Test';
$headers = 'MIME-Version: 1.0' . "\r\n" . 'Content-type: text/html; charset=UTF-8' . "\r\n";
$headers .= "X-Mailer: PHP \r\n";
$headers .= 'From: ".$name."<".$email.">' . "\r\n";
$headers .= 'Reply-To: ".$email."' . "\r\n";
mail($to, $subject, $message, $headers);
Example from my mail()
$header = "From: Backapp.ru <noreply#backapp.ru>\r\nContent-type: text/html; charset=utf-8";
The basic usage of mail() function with headers is:
<?php
$to = "you#local.com";
$subject = "My email test.";
$message = "Hello";
$headers = "From: myplace#local.com\r\n";
$headers .= "Reply-To: myplace2#local.com\r\n";
$headers .= "Return-Path: myplace#local.com\r\n";
$headers .= "CC: sombodyelse#local.com\r\n";
$headers .= "BCC: hidden#special.com\r\n";
if ( mail($to,$subject,$message,$headers) ) {
echo "The email has been sent!";
} else {
echo "The email has failed!";
}
?>
You don't send us your output so we don't know wheres the problem.

Why did Implementing HTTP/1.1 break my IPN script?

Here is the original code I had in my PHP script:
$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";
I got an email from Paypal saying that I needed to upgrade my IPN script so that it uses HTTP/1.1. So here is what I changed my code to, based on their directions:
$header .="POST /cgi-bin/webscr HTTP/1.1\r\n";
$header .="Content-Type: application/x-www-form-urlencoded\r\n";
$header .="Host: www.paypal.com\r\n";
$header .="Connection: close\r\n";
Payments have gone through today, but the IPN is no longer updating my database and this is the only change I made to it. Any ideas on what to do?
Thanks!
Yes. I have just done battle with this. What worked for me was to remove the connection close header, and add a trim to the response back from PP. Here are the headers:
$header = "POST /cgi-bin/webscr HTTP/1.1\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Host: www.paypal.com\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
Here is the fsockopen:
$fp = fsockopen ('ssl://www.paypal.com', 443, $errno, $errstr, 30);
and here is the trim on the response back from PP:
if (!$fp) {
// HTTP ERROR
error_mail("Could not open socket");
//
} else {
fputs ($fp, $header . $req);
while (!feof($fp)) {
$res = trim(fgets ($fp, 1024));
}
//
// check the payment_status is Completed
// check that receiver_email is your Primary PayPal email
//
if ((strcmp ($res, "VERIFIED") == 0) && ($payment_status == "Completed") && ($receiver_email == $valid_receiver_email)) {
That worked for me.

PayPal IPN Help (Not UPDATE Database)

Hi there all been trying to get this working for about 2 days now. The Payment is going through no problem but it not update the Database :(
Here is the Code :
<?php
require_once(WWW_DIR."/lib/users.php");
// Connect to database
$con = mysql_connect("localhost","root","******");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
// 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); // Test
//$fp = fsockopen ('ssl://www.paypal.com', 443, $errno, $errstr, 30); // Live
// assign posted variables to local variables
$item_name = $_POST['item_name'];
$payment_status = $_POST['payment_status'];
$payment_amount = $_POST['mc_gross'];
$txn_id = $_POST['txn_id'];
$payer_email = $_POST['payer_email'];
$name = $_POST['first_name'] . " " . $_POST['last_name'];
$address_street = $_POST['address_street'];
$address_zip = $_POST['address_zip'];
$address_city = $_POST['address_city'];
$contact_phone = $_POST['contact_phone'];
$email = $_POST['payer_email'];
$uid = $users->currentUserId();
//timezone set
date_default_timezone_set('Europe/London');
if (!$fp)
{
// HTTP ERROR
} else
{
fputs($fp, $header . $req);
while (!feof($fp))
{
$res = fgets($fp, 1024);
if (strcmp($res, "VERIFIED") == 0) {
if($payment_status != "Completed"){
if($payment_amount == 2.00)
{
$role = 6;
}
else
if($payment_amount == 5.00)
{
$role = 8;
}
else
if($payment_amount == 8.00)
{
$role = 9;
}
//update user records
$sql = sprintf("UPDATE users SET role = %d WHERE ID = %d", $role, $uid);
mysql_query($sql);
//log payment
//mysql_query("INSERT INTO payments (email, item_name, payment_status, txn_id, payment_ammount) VALUES('" . mysql_escape_string($email) . "', '" . $item_name . "', '" . $payment_status . "', '" . $txn_id . "', '" . $payment_amount . "' ) ") or die(mysql_error());
}} else
if (strcmp($res, "INVALID") == 0)
{
// log for manual investigation
}
}
fclose($fp);
}
?>
If you have any Questions just ask I will try and Answer ASAP if you could get this working I would be very happy
Thanks
Change:
// 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";
To:
// post back to PayPal system to validate
$header .= "POST /cgi-bin/webscr HTTP/1.1\r\n";
$header .= "Host: www.sandbox.paypal.com\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n";
$header .= "Connection: close\r\n\r\n";
Change:
if (strcmp($res, "VERIFIED") == 0) {
To:
if (strcmp(trim($res), "VERIFIED") == 0) {
And lastly, change:
if (strcmp($res, "INVALID") == 0)
To:
if (strcmp(trim($res), "INVALID") == 0)
Reason:
The PayPal Sandbox is configured to reject HTTP 1.0 requests without a Host header specified, so your current script will never properly validate.