Abul Hasnat
Abul Hasnat

Reputation: 1631

PayPal transaction verification IPN in php returns INVALID

I know this question have been asked here over and over. But none of the solution is working out for me.

I am trying to Verify a PayPal transaction with Sandbox. I have researched and found different codes but none of them is responding to my code or mostly PayPal returns INVALID


Here is my code

header("HTTP/1.1 200 OK");
  // parse the paypal URL


$req = 'cmd=' . urlencode('_notify-validate');

foreach ($_GET as $key => $value) {
if (!is_array($value)) {
    $value = urlencode(stripslashes($value));
    $req .= "&$key=$value";
}else if (is_array($value)) {
    $paymentArray = explode(' ', $value[0]);
    $paymentCurrency = urlencode(stripslashes($paymentArray[0]));
    $paymentGross = urlencode(stripslashes($paymentArray[1]));
    $req .= '&mc_currency=' . $paymentCurrency . '&mc_gross=' . $paymentGross;
}
}

For some reason I can't get any info with _POST so I tried with _GET. Any ideas?

echo $req;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.sandbox.paypal.com/cgi-bin/webscr');

curl_setopt($ch, CURLOPT_CAINFO, dirname(__FILE__)."/api_cert_chain.crt");

curl_setopt($ch, CURLOPT_HEADER, 0);
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);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Host: www.sandbox.paypal.com'));
$res = curl_exec($ch);
curl_close($ch);




if (strcmp ($res, "VERIFIED") == 0) {
echo "valid";
// check the payment_status is Completed
// check that txn_id has not been previously processed
// check that receiver_email is your Primary PayPal email
// check that payment_amount/payment_currency are correct
// process payment
}
else if (strcmp ($res, "INVALID") == 0) {
echo "invalid";
// log for manual investigation
}

And after all that either I get INVALID or nothing at all..
I have also put the cert in root directory of my server.

Upvotes: 0

Views: 134

Answers (2)

Abul Hasnat
Abul Hasnat

Reputation: 1631

My bad..turns out PayPal does send the signal in the background..I was actually trying this with redirection(after transaction completed PayPal takes back to original source)..which does not get much info from PayPal. So overall the code is working fine in case anyone wondering. Also need to remove 'sandbox' for real transaction.

Upvotes: 0

Andrew Walker
Andrew Walker

Reputation: 492

Have you tried using this:

$data = file_get_contents('php://input');

?

Upvotes: 1

Related Questions