What is the role of PayPal IPN in automating payment processes on a website?

PayPal IPN (Instant Payment Notification) plays a crucial role in automating payment processes on a website by allowing real-time communication between PayPal and the website. This ensures that payment notifications are sent to the website immediately after a transaction is completed, enabling automated processing of orders, updating of databases, and triggering of other necessary actions.

// Sample PHP code snippet to handle PayPal IPN
// Verify IPN request
$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';
foreach ($myPost as $key => $value) {
  $value = urlencode(stripslashes($value));
  $req .= "&$key=$value";
}

// Post IPN data back to PayPal to validate
$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)) ) {
    // HTTP error
} else {
    $info = curl_getinfo($ch);
    if ($info['http_code'] != 200) {
        // HTTP error
    } else {
        // IPN verified
        // Process the payment and update the database
    }
}
curl_close($ch);