This is my ipn.php that is sending a mail to the user when PayPal transaction is completed.
The problem is that I also need to change the state of the order in the WP database and not just send the email.
I know the unique $code
variable (integer) of that transaction (I have saved it when the form was sent and the user redirected to finish the process on PayPal page) and PayPal is sending it back to ipn.php file inside custom field $_POST['custom']
.
So, the code in ipn.php (accessed by PayPal as notify_url when the transaction is successful) looks like this:
<?php
$req = 'cmd=_notify-validate';
foreach ($_POST as $key => $value) {
$value = urlencode(stripslashes($value));
$req .= "&$key=$value";
}
// post back to PayPal system to validate
$header .= "POST /cgi-bin/webscr HTTP/1.0rn";
$header .= "Content-Type: application/x-www-form-urlencodedrn";
$header .= "Content-Length: " . strlen($req) . "rnrn";
$fp = fsockopen ('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30);
// CUSTOM VARIABLE
$code = $_POST['custom'];
if (!$fp) {
// ERROR
} else {
$email = "emailtouser@email.com" ;
$subject = "Test" ;
$message = "Your code is {$code}" ;
mail($email, $subject, $message, "From:" . $email);
fclose ($fp);
}
So, the email is send OK with the code value as it supposed to be.
However, I also need to get the user email from the WP database based on the $code that is unique for every user (post type) and then change the state of the order in the db.
I know how to do that, but the problem is that WP is not loaded and I cannot access the database to perform these acts.
How to do that?
I mean, how to access WP db here ?:
} else {
// BEFORE SENDING I NEED TO ACCESS WP DB HERE...
// AND THEN SEND THE EMAIL
$email = "emailtouser@email.com" ;
$subject = "Test" ;
$message = "Your code is {$code}" ;
mail($email, $subject, $message, "From:" . $email);
fclose ($fp);
}
Try adding this to the top of your script:
Then you can run queries using: