📜  ipn listener paypel php (1)

📅  最后修改于: 2023-12-03 14:42:10.115000             🧑  作者: Mango

IPN Listener PayPal PHP
Introduction

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.

Prerequisites

Before starting the implementation of the IPN listener, make sure you have the following prerequisites in place:

  1. PayPal Business account: You will need a valid PayPal Business account to access the IPN feature.
  2. Web server with PHP support: Ensure that you have a web server with PHP installed to host your IPN listener script.
Setting Up the IPN Listener
  1. Create a new PHP file, e.g., 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
// ...
?>
  1. Handle IPN Notification: Within the script, you need to write code to handle the IPN notification from PayPal. This includes reading the raw request data and extracting relevant information.
<?php
// Handle IPN notification
$raw_post_data = file_get_contents('php://input');
$decoded_data = urldecode($raw_post_data);

// Extract relevant information
// ...
?>
  1. Verify IPN Authenticity: To ensure that the IPN notification is genuine and originated from PayPal, you need to verify the authenticity using PayPal's verification mechanism.
<?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
// ...
?>
  1. Process Received Data: Once the authenticity of the IPN notification is verified, you can process the received data according to your application's requirements. This may include updating the payment status, storing transaction details, or triggering subsequent actions.
<?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
    // ...
}
?>
  1. Respond Back to PayPal: Lastly, you need to respond back to PayPal with an HTTP 200 status code to acknowledge the IPN notification.
<?php
// Respond back to PayPal
header('HTTP/1.1 200 OK');
?>
Conclusion

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.