I am currently working on payments using PayPal. Everything was running smoothly until recently. I have a subscription button. I've set the return URL to handle getting data from the transaction then mailing it to the user. All the data I needed are being retrieved successfully, however, the problem is my functions runs twice upon return. I get the emails I'm supposed to receive twice. This was working fine a couple of weeks back so now I'm wondering what happened why it suddenly got this bug.
I've read here : Paypal adaptive payment return url is calling twice that it could be because I tapped the 'click here' button even tho it hasn't been 10 seconds yet. I already tried clicking and not clicking it but I still get the same result.
I've also read that this could be because it gets called upon payment and then for the start of the subscription. Any idea how I could handle this?
$pp_hostname = "www.sandbox.paypal.com"; // Change to www.sandbox.paypal.com to test against sandbox
$req = 'cmd=_notify-synch';
$tx_token = $_GET['tx'];
$auth_token = "my_token_from_paypal";
$req .= "&tx=$tx_token&at=$auth_token";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://$pp_hostname/cgi-bin/webscr");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
//set cacert.pem verisign certificate path in curl using 'CURLOPT_CAINFO' field here,
//if your server does not bundled with default verisign certificates.
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Host: $pp_hostname"));
$res = curl_exec($ch);
curl_close($ch);
if(!$res){
//HTTP ERROR
}else{
// parse the data
$lines = explode("\n", trim($res));
$keyarray = array();
if (strcmp ($lines[0], "SUCCESS") == 0)
{
for ($i = 1; $i < count($lines); $i++) {
$temp = explode("=", $lines[$i],2);
$keyarray[urldecode($temp[0])] = urldecode($temp[1]);
}
// code for mail handling
}
else if (strcmp ($lines[0], "FAIL") == 0) {
echo 'there has been an error';
}
}