问题
I am having trouble getting Paypal's IPN integrated into my php, i have the following script and it keeps falling through to the default case when a payment is made in the paypal sandbox.
Any help would be appreciated!
$request = "cmd=_notify-validate";
foreach ($_POST as $varname => $varvalue){
$email .= "$varname: $varvalue\n";
if(function_exists('get_magic_quotes_gpc') and get_magic_quotes_gpc()){
$varvalue = urlencode(stripslashes($varvalue));
}
else {
$value = urlencode($value);
}
$request .= "&$varname=$varvalue";
}
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,"https://www.sandbox.paypal.com/cgi-bin/webscr");
//curl_setopt($ch,CURLOPT_URL,"https://www.paypal.com");
curl_setopt($ch,CURLOPT_POST,true);
curl_setopt($ch,CURLOPT_POSTFIELDS,$request);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,false);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
$result = curl_exec($ch);
curl_close($ch);
switch($result){
case "VERIFIED":
mail('test@test.com','worked','worked');
break;
case "INVALID":
mail('test@test.com','invaild','invaild');
break;
default:
mail('test@test.com','failed','failed');
}
if i email myself $result it is just blank.
EDIT: I found out it is a server-side issue with my LAMP but am unsure of how to fix it.
Note: i do have curl installed on the server, but i am not sure if it is configured properly.
回答1:
I'd suggest doing some debugging using var_dump and Paypal's test tool located here: https://developer.paypal.com/cgi-bin/devscr?cmd=_ipn-link-session
I can understand it becomes difficult and time consuming when working with Third Party Services.
It may be worth simply grabbing the POST data, serializing it and applying it to a variable so you can test without PayPal hitting your callback.
I'd do something like this initially to grab the PayPal POST.
<?php
file_put_contents(serialize($_POST), 'post.log');
//Now you have the post request serialized we can grab the contents and apply it to a variable for fast testing.
?>
Start of your code:
<?php
$_POST = unserialize(file_get_content('post.log'));
//Now you can execute the script via command line or within your browser without requiring PayPal's testing tool. Use var_dump to investigate what's the issue.
$request = "cmd=_notify-validate";
foreach ($_POST as $varname => $varvalue){
$email .= "$varname: $varvalue\n";
if(function_exists('get_magic_quotes_gpc') and get_magic_quotes_gpc()){
$varvalue = urlencode(stripslashes($varvalue));
}
else {
$value = urlencode($value);
}
$request .= "&$varname=$varvalue";
}
?>
NOW: This is a bit more effective and efficient when it comes to testing. In your example you were emailing yourself, but not including $result anywhere within the body of the mail function. http://php.net/manual/en/function.mail.php
PayPal's IPN example uses fsock, although CURL is more effective and easier to use. Also there have been some recent issues with PayPal's sandbox changing. https://www.paypal-community.com/t5/Selling-on-your-website/IPN-response-problem/m-p/519862/message-uid/519862#U519862
Also : To determine what the main cause is, as you've said it appears to be your LAMP stack. Check your logs directory (generally /var/log/) generally from their you'll be able to pinpoint what's failing.
来源:https://stackoverflow.com/questions/11875053/paypal-ipn-integration-into-php