Confirmed User
Industry Role:
Join Date: Oct 2018
Location: New Orleans, Louisiana. / Newcastle, England.
Posts: 1,123
|
Paypal IPN (PHP) Script Issue
On PayPal's side of things, they're saying that everything is working (IPN was sent and the handshake was verified.) with the following php code however, despite this (and that the DB, table, etc is all correct) the data from the sandbox (and live) transaction does not appear to be saving to SQL.
Can any of you with a little more php knowledge than me see any reason why this would be so, or do you think it may be an issue on PayPal's side of things?
This is a few steps before I finish up a custom affiliate type script for PayPal and its been giving me grief for a few days now :/
Code:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// MySQL database configuration
$host = 'localhost'; // Your MySQL host
$username = 'PayPal'; // Your MySQL username
$password = 'Password'; // Your MySQL password
$database = 'IPNPayPal'; // Your MySQL database name
// Create connection
$conn = new mysqli($host, $username, $password, $database);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Read the IPN notification from PayPal and add 'cmd'
$req = 'cmd=_notify-validate';
foreach ($_POST as $key => $value) {
$value = urlencode(stripslashes($value));
$req .= "&$key=$value";
}
// Set up the PayPal URL to send the IPN verification request to
$paypal_url = 'https://www.sandbox.paypal.com/cgi-bin/webscr';
// For live transactions, use:
// $paypal_url = 'https://www.paypal.com/cgi-bin/webscr';
// Set up cURL to make the request
$ch = curl_init($paypal_url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Execute the request
$response = curl_exec($ch);
curl_close($ch);
// Inspect IPN validation result and act accordingly
if (strcmp($response, "VERIFIED") == 0) {
// IPN is verified, process the payment
$txn_id = $_POST['txn_id'];
$payment_status = $_POST['payment_status'];
$mc_gross = $_POST['mc_gross'];
$mc_currency = $_POST['mc_currency'];
$payment_date = $_POST['payment_date'];
$payer_email = $_POST['payer_email'];
$receiver_email = $_POST['receiver_email'];
$item_name = $_POST['item_name'];
$item_number = $_POST['item_number'];
$quantity = $_POST['quantity'];
$payment_type = $_POST['payment_type'];
$payment_fee = $_POST['payment_fee'];
$payment_gross = $_POST['payment_gross'];
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$address_street = $_POST['address_street'];
$address_city = $_POST['address_city'];
$address_state = $_POST['address_state'];
$address_country = $_POST['address_country'];
$address_zip = $_POST['address_zip'];
$subscr_id = isset($_POST['subscr_id']) ? $_POST['subscr_id'] : "";
$subscr_date = isset($_POST['subscr_date']) ? $_POST['subscr_date'] : "";
$subscr_effective = isset($_POST['subscr_effective']) ? $_POST['subscr_effective'] : "";
$period1 = isset($_POST['period1']) ? $_POST['period1'] : "";
$period3 = isset($_POST['period3']) ? $_POST['period3'] : "";
$custom = isset($_POST['custom']) ? $_POST['custom'] : "";
$invoice = isset($_POST['invoice']) ? $_POST['invoice'] : "";
$notify_version = isset($_POST['notify_version']) ? $_POST['notify_version'] : "";
$verify_sign = isset($_POST['verify_sign']) ? $_POST['verify_sign'] : "";
// Insert IPN data into database
$sql = "INSERT INTO transactions (
txn_id, payment_status, mc_gross, mc_currency, payment_date,
payer_email, receiver_email, item_name, item_number, quantity,
payment_type, payment_fee, payment_gross, first_name, last_name,
address_street, address_city, address_state, address_country,
address_zip, subscr_id, subscr_date, subscr_effective, period1,
period3, custom, invoice, notify_version, verify_sign
) VALUES (
'$txn_id', '$payment_status', '$mc_gross', '$mc_currency', '$payment_date',
'$payer_email', '$receiver_email', '$item_name', '$item_number', '$quantity',
'$payment_type', '$payment_fee', '$payment_gross', '$first_name', '$last_name',
'$address_street', '$address_city', '$address_state', '$address_country',
'$address_zip', '$subscr_id', '$subscr_date', '$subscr_effective', '$period1',
'$period3', '$custom', '$invoice', '$notify_version', '$verify_sign'
)";
// Execute SQL query
if ($conn->query($sql) === TRUE) {
// Record inserted successfully
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
} else if (strcmp($response, "INVALID") == 0) {
// IPN is invalid, log for investigation
// Log the invalid IPN for further investigation
$error = "Invalid IPN: " . print_r($_POST, true);
// Log the error
}
// Close MySQL connection
$conn->close();
?>
Thanks in advance for any advice or solutions you can offer 
__________________
SOMETHING EXTREME IS COMING SOON!
|