📅  最后修改于: 2023-12-03 14:42:10.115000             🧑  作者: Mango
IPN (Instant Payment Notification) is a feature provided by PayPal that allows your application to be notified about various events related to payments. By setting up an IPN listener, you can receive and process these notifications in real-time.
In this guide, we will explore how to implement an IPN listener using PHP for PayPal integration. We will cover the necessary steps to receive IPN notifications, verify the authenticity of the notifications, process the received data, and respond back to PayPal.
Before starting the implementation of the IPN listener, make sure you have the following prerequisites in place:
ipn_listener.php
, which will serve as your IPN listener script.<?php
// Handle IPN notification
// ...
// Verify IPN authenticity
// ...
// Process received data
// ...
// Respond back to PayPal
// ...
?>
<?php
// Handle IPN notification
$raw_post_data = file_get_contents('php://input');
$decoded_data = urldecode($raw_post_data);
// Extract relevant information
// ...
?>
<?php
// Verify IPN authenticity
$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, $decoded_data . '&cmd=_notify-validate');
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'));
$response = curl_exec($ch);
curl_close($ch);
// Verify response from PayPal
// ...
?>
<?php
// Process received data
if ($response === 'VERIFIED') {
// Update payment status
// Store transaction details
// Trigger subsequent actions
} else if ($response === 'INVALID') {
// Log or handle invalid IPN notification
// ...
} else {
// Unexpected response, handle accordingly
// ...
}
?>
<?php
// Respond back to PayPal
header('HTTP/1.1 200 OK');
?>
Implementing an IPN listener using PHP for PayPal integration allows you to receive real-time notifications about payment-related events. By following the steps outlined in this guide and customizing the processing logic to fit your application's needs, you can seamlessly integrate PayPal's IPN feature into your PHP application.