0

I have created a file in my template folder called ipn.php:

<?php

$req = 'cmd=_notify-validate';
foreach($_POST as $key => $value) :
  $value = urlencode(stripslashes($value));
  $req .= "&$key=$value";
endforeach;

$header .= "POST /cgi-bin/webscr HTTP/1.0\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
$fp = fsockopen ('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30);

if(!$fp) :
// HTTP ERROR
else :
    fputs ($fp, $header . $req);
    while(!feof($fp)) :

        $res = fgets ($fp, 1024);

        $fh = fopen('result.txt', 'w');
            fwrite($fh, $res);
            fclose($fh);

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

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

            $headers = 'From: Tester 1 <testmail@something.com>' . "\r\n";
            $subject = 'Subject test' . "\r\n";
            $message = 'Just a test stuff.';
            wp_mail('myemail@gmail.com', $subject, $message, $headers );

        elseif(strcmp ($res, "INVALID") == 0) :

            // error

        endif;

    endwhile;
fclose ($fp);
endif;

and notify_url I have set to:

<input type="hidden" name="notify_url" value="<?php bloginfo('stylesheet_directory'); ?>/ipn.php">

But after I have paid and the process on PyaPal page is done and I am being redirected back to my thank you page (this part works ok), for some reason the ipn.php didn't send the email to me. Why is that?

Is there some error in the code? I have removed all if conditions of txn_id and email etc. to eliminate errors.

If I use IPN simulator in developer paypal website and put the notify_url address there and select Web Accept the result is: IPN sent successfully So, I guess my hosting is not blocking PayPal transactions. Or?

I even tried to put the email code in the top of the ipn.php script:

 <?php

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

                $headers = 'From: Tester 1 <testmail@something.com>' . "\r\n";
                $subject = 'Subject test' . "\r\n";
                $message = 'Just a test stuff.';
                wp_mail('myemail@gmail.com', $subject, $message, $headers );

    $req = 'cmd=_notify-validate';
    foreach($_POST as $key => $value) :
      $value = urlencode(stripslashes($value));
      $req .= "&$key=$value";
    endforeach;

    $header .= "POST /cgi-bin/webscr HTTP/1.0\r\n";
    $header .= "Content-Type: application/x-www-form-urlencoded\r\n";
    $header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
    $fp = fsockopen ('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30);

    if(!$fp) :
    // HTTP ERROR
    else :
        fputs ($fp, $header . $req);
        while(!feof($fp)) :

            $res = fgets ($fp, 1024);

            $fh = fopen('result.txt', 'w');
                fwrite($fh, $res);
                fclose($fh);

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



            elseif(strcmp ($res, "INVALID") == 0) :

                // error

            endif;

        endwhile;
    fclose ($fp);
    endif;

But it seems that its not triggered at all ;( (no email sent).

Could be Wordpress blocking this address or something? I don't know maybe the mod_rewrite is blocking it or something?

I am using nice url so mod_rewrite is working, but maybe there is some other problem.

Any idea what could be wrong?

I have checked the email and even spam folder but nothing is coming to me. If I use wp_mail in function.php it works as expected, but not in ipn.php file.

Any idea what could be wrong and the mail is not being sent?

1

First of all, is better use get_template_directory_uri() instead of bloginfo(...

<input type="hidden" name="notify_url" value="<?php echo get_template_directory_uri(); ?>/ipn.php">

but you are right if you think the problem is not this one.

I cannot be sure, but I bet your problem is here:

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

This is a problematic way to include wp-load.php, but ...

Why load the entire wordpress environment for just sending an email?

Be aware that loading wp-load.php you load all WP core files, get options from database, load all active plugins, load the active theme functions.php and the parent theme functions.php if you are using a child theme. You also trigger some actions and filters... a lot of unnecessary stuff.

You don't need them at all: just send the email using php mail() function, (or at least include a class like this).

Your process will be a lot faster and I bet... successfull.

| improve this answer | |
  • Hmm, I will try mail, we will see if it works. – Derfder Sep 7 '13 at 8:06
  • Wow, if I used just mail() and delete everything from that file it was sent via paypal which is nice, now I need to find out how to save the custom value from paypal to the Wp database without loading wordpress ;(. Anyway, thanks for the help. – Derfder Sep 7 '13 at 8:21
  • Glad it works. Normally headers should not be sended when you end to send email. So, after sending, you can use php header func to redirect to another url handled by wordpress where you can do everithing with $wpdb. If you need to pass variables you can meke use of query string... @Derfder – gmazzap Sep 7 '13 at 8:27
  • @Derfder Turns out that this is a VERY old script (should be expected by tutsplus) The new PayPal IPN scripts are found here github.com/paypal/ipn-code-samples – Ben Racicot Feb 20 '14 at 16:54

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.