How can developers ensure the integrity and security of payment transactions when integrating PayPal into PHP scripts?

Developers can ensure the integrity and security of payment transactions when integrating PayPal into PHP scripts by using PayPal's IPN (Instant Payment Notification) feature. This feature allows PayPal to notify the website of successful transactions, which can then be verified to ensure the payment was legitimate.

// Verify PayPal IPN
$postData = 'cmd=_notify-validate';
foreach ($_POST as $key => $value) {
    $postData .= "&$key=" . urlencode($value);
}

$ch = curl_init('https://www.sandbox.paypal.com/cgi-bin/webscr');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);
curl_close($ch);

if ($response == 'VERIFIED') {
    // Payment was successful, process the transaction
} else {
    // Payment was not verified, handle accordingly
}