0

i'm sorry for my english.

I have a problem with the function wp_insert_post in a script.

I'm trying to create a new post when i receive a notification from paypal. Everything works fine untill i try to create a new post in the database.

This is my code:

<?php include_once($_SERVER['DOCUMENT_ROOT'].'/wp-load.php');


            // Send an email announcing "received IPN"
            $mail_From = "xxxxxx@gmail.com";
            $mail_To = "xxxxxx@gmail.com";
            $mail_Subject = "received IPN";
            $mail_Body = $req;
            mail($mail_To, $mail_Subject, $mail_Body, $mail_From);

?>
<?php

// STEP 1: read POST data

// Reading POSTed data directly from $_POST causes serialization issues with array data in the POST.
// Instead, read raw POST data from the input stream. 
$raw_post_data = file_get_contents('php://input');
$raw_post_array = explode('&', $raw_post_data);
$myPost = array();
foreach ($raw_post_array as $keyval) {
  $keyval = explode ('=', $keyval);
  if (count($keyval) == 2)
     $myPost[$keyval[0]] = urldecode($keyval[1]);
}
// read the IPN message sent from PayPal and prepend 'cmd=_notify-validate'
$req = 'cmd=_notify-validate';
if(function_exists('get_magic_quotes_gpc')) {
   $get_magic_quotes_exists = true;
} 
foreach ($myPost as $key => $value) {        
   if($get_magic_quotes_exists == true && get_magic_quotes_gpc() == 1) { 
        $value = urlencode(stripslashes($value)); 
   } else {
        $value = urlencode($value);
   }
   $req .= "&$key=$value";
}


// STEP 2: POST IPN data back to PayPal to validate

$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, $req);
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'));

// In wamp-like environments that do not come bundled with root authority certificates,
// please download 'cacert.pem' from "http://curl.haxx.se/docs/caextract.html" and set 
// the directory path of the certificate as shown below:
// curl_setopt($ch, CURLOPT_CAINFO, dirname(__FILE__) . '/cacert.pem');
if( !($res = curl_exec($ch)) ) {
    // error_log("Got " . curl_error($ch) . " when processing IPN data");
    curl_close($ch);
    exit;
}
curl_close($ch);

// STEP 3: Inspect IPN validation result and act accordingly

if (strcmp ($res, "VERIFIED") == 0) {


            // Send an email announcing "enter in verified IF"
            $mail_From = "xxxxxxx@gmail.com";
            $mail_To = "xxxxxxx@gmail.com";
            $mail_Subject = "enter in verified IF";
            $mail_Body = $req;
            mail($mail_To, $mail_Subject, $mail_Body, $mail_From);

    // You should validate against these values.
            $donCause       = $_POST['item_number'];
            $txnID          = $_POST['txn_id'];
            $firstName      = $_POST['first_name'];
            $lastName       = $_POST['last_name'];
            $addressCountry = $_POST['address_country'];
            $addressCity    = $_POST['address_city'];
            $addressStreet  = $_POST['address_street'];
            $addressZip     = $_POST['address_zip'];
            $payerEmail     = $_POST['payer_email'];
            $payment_gross  = $_POST['mc_gross'];
            $payment_status = $_POST['payment_status'];

            if ($payment_status == 'Completed') {

                // Send an email announcing "enter in payment_status==completed"
                $mail_From = "xxxxxx@gmail.com";
                $mail_To = "xxxxxx@gmail.com";
                $mail_Subject = "enter in payment_status==completed";
                $mail_Body = $req;
                mail($mail_To, $mail_Subject, $mail_Body, $mail_From);

                // Create post object
                $my_post = array(
                    'post_title'    => $txnID,
                    'post_status'   => 'publish',
                    'post_author'   => 1,
                    'comment_status' => 'closed',
                    'ping_status' => 'closed',
                    'post_type'      => 'post_pledges',
                );
                $post_id = wp_insert_post( $my_post, true );

                // Send an email announcing "after post_pledges creation"
                $mail_From = "ragazzin@gmail.com";
                $mail_To = "ragazzin@gmail.com";
                $mail_Subject = "after post_pledges creation";
                $mail_Body = $post_id;
                mail($mail_To, $mail_Subject, $mail_Body, $mail_From);

                add_post_meta($post_id, "wpl_pledge_cause", $donCause);
                add_post_meta($post_id, "wpl_pledge_transaction_id", $txnID);
                add_post_meta($post_id, "wpl_pledge_first_name", $firstName);
                add_post_meta($post_id, "wpl_pledge_last_name", $lastName);
                add_post_meta($post_id, "wpl_pledge_country", $addressCountry);
                add_post_meta($post_id, "wpl_pledge_city", $addressCity);
                add_post_meta($post_id, "wpl_pledge_address", $addressStreet);
                add_post_meta($post_id, "wpl_pledge_postal_code", $addressZip);
                add_post_meta($post_id, "wpl_pledge_email", $payerEmail);
                add_post_meta($post_id, "wpl_pledge_donation_amount", $payment_gross);
                add_post_meta($post_id, "wpl_pledge_payment_source", 'paypal');
                add_post_meta($post_id, "wpl_pledge_payment_Status", $payment_status);

            }

            // Response is VERIFIED
            // Send an email announcing the IPN message is VERIFIED
            $mail_From = "xxxxxx@gmail.com";
            $mail_To = "xxxxxxx@gmail.com";
            $mail_Subject = "VERIFIED IPN";
            $mail_Body = $req;
            mail($mail_To, $mail_Subject, $mail_Body, $mail_From);

} else if (strcmp ($res, "INVALID") == 0) {
    // IPN invalid, log for manual investigation

            // Notification protocol is NOT complete, begin error handling

            // Send an email announcing the IPN message is INVALID
             $mail_From = "xxxxxxx@gmail.com";
            $mail_To = "xxxxxxx@gmail.com";
            $mail_Subject = "INVALID IPN";
            $mail_Body = $req;
            mail($mail_To, $mail_Subject, $mail_Body, $mail_From);
}
?>

I read about a problem with an infinite loop and maybe it's also for me because the script doesn't go on when try to execute the function.

Have you any ideas?

  • Have you added a custom post type of post_pledges using http://codex.wordpress.org/Function_Reference/register_post_type? After confirming that can you post what errors or symptoms you are seeing to indicate that your create post method didn't work? – Luke Gedeon May 12 '14 at 23:11
  • @LukeGedeon thanks for your reply. Yes, post_pledges is already registered. I checked the method didn't work from the database and also with some scripts like mail($mail_To, $mail_Subject, $mail_Body, $mail_From) that send me an email after each step of the script. I haven't received the mail defined after wp_insert_post. – Manuel Ragazzini May 12 '14 at 23:28
  • have you tried determining the value of $post_id after you attempt to insert the post? if wp_insert_post fails, that will be an error object, you should log it to the log file and see what it contains. – Milo May 13 '14 at 0:07
  • i'm sorry for this reply delate. Debug.log doesn't report any error associated to ipn.php (the file that i report in the question). – Manuel Ragazzini May 20 '14 at 22:39
1

Because you mention an infinite I am guessing that you are hooked into a hook like save_post that fires for wp_insert_post() so if you hook your code into that your code will run then fire the same hook over and over.

To avoid that remove the action from the top of your callback:

remove_action('save_post','yourcallbackfunctionname');
$my_post = array(
    // ...
 );
 $post_id = wp_insert_post( $my_post, true );
  • Thanks for your reply. No, the script isn't a function but a IPN listener that catches and process the IPN messages sent by PayPal. I wrote about an infinite loop because the script doesn't execute any code after wp_insert_post function. – Manuel Ragazzini May 12 '14 at 23:33
  • hmmm... there is code that hooks into WordPress somehow. That is going to be a function or a class, or you have a stand-alone script loading WordPress (which is almost certainly the wrong way). Post your code in context. – s_ha_dum May 12 '14 at 23:36
  • i edited my question with the code – Manuel Ragazzini May 12 '14 at 23:45
  • I have found here link the first part of the code. I never received the mail "after post_pledges creation". – Manuel Ragazzini May 12 '14 at 23:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.