Check payment by Paypal

1

I have just done an e-commerce for a client, and I am implementing the code provided by Paypal to make the payments. Until now when you click on the paypal button, it redirects the user to the official paypal page so you can make the payment.

My question is: is there any way to verify that the user made the payment? Since I need to print a receipt or a receipt just when the payment has been made.

Thank you.

    
asked by StevePHP 01.09.2016 в 12:06
source

1 answer

4

PayPal uses an IPN (Instant Payment Notification) system to verify that the transaction was successful. You have to define a return url in the paypal configuration, that's where you should host the script that will communicate with the paypal services to check the status of the transaction.

With this we collect the paypal response and do the check using curl.

<?php

$raw_post_data = file_get_contents('php://input');
$raw_post_array = explode('&', $raw_post_data);
$myPost = array();
foreach ($raw_post_array as $keyval) {
$keyval = explode ('=', $keyval);
if (count($keyval) == 2)
$myPost[$keyval[0]] = urldecode($keyval[1]);
}

$req = 'cmd=_notify-validate';
if (function_exists('get_magic_quotes_gpc')) {
 $get_magic_quotes_exists = true;
 }
 foreach ($myPost as $key => $value) {
 if ($get_magic_quotes_exists == true && get_magic_quotes_gpc() == 1) {
 $value = urlencode(stripslashes($value));
} else {
$value = urlencode($value);
  }
 $req .= "&$key=$value";
}


$ch = curl_init('https://www.paypal.com/cgi-bin/webscr');
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
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_FORBID_REUSE, 1);
 curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: Close'));


if ( !($res = curl_exec($ch)) ) {

 curl_close($ch);
  exit;
}
curl_close($ch);

The PayPal response is in the variable $ res, so we checked its value:

if (strcmp ($res, "VERIFIED") == 0) {
  // Acción si PayPal verifica el pago
} else if (strcmp ($res, "INVALID") == 0) {
  // Acción si PayPal NO verifica el pago
}
    
answered by 01.09.2016 / 13:35
source