Question

I recently set up an Amazon AWS free tier account. I have an instance running, apache, php5, cURL etc installed, and everything seems to work fine up until this point. I have a Paypal IPJ script running on the server which is as follows:

<?php

error_reporting(E_ALL);

class PaypalIPN {

    private $_url;

    /**
    * @param $mode String
    * Set to live when applicable
    */
    public function __construct($mode = 'sandbox'){
        if($mode == 'live')
            $this->_url = 'https://paypal.com/cgi-bin/webscr';
        else
            $this->_url = "https://sandbox.paypal.com/cgi-bin/webscr";
    }

    /**
    * @param
    * cURL function that will get info from paypal
    */
    public function run(){
        # Post fields
        $fields = 'cmd=_notify-validate';

        foreach($_POST as $key => $value){
            $fields .= "&$key=" . urlencode($value);
        }

        $ch = curl_init();
            curl_setopt_array($ch, array(
                CURLOPT_URL             => $this->_url,
                CURLOPT_SSL_VERIFYPEER  => false,
                CURLOPT_RETURNTRANSFER  => true,
                CURLOPT_POST            => true,
                CURLOPT_POSTFIELDS      => $fields
            ));
        $result = curl_exec($ch);
        echo $result;
        echo '<br>';
        echo 'Result^';
        curl_close($ch);
        $a = strcmp($result, "VERIFIED") == 0;
        # Write to file here, so we can analyze
        $fh = fopen('result.txt', 'w');
        fwrite($fh, $result . ' --' . $fields . ' --' . $a);
        fclose($fh);

        if (strcmp ($result, "VERIFIED") == 0){
              # If the transaction has been verfied
              # Continue...

              // payment info
              $payment_status = $_POST['payment_status'];
              $payment_amount = $_POST['mc_gross'];
              $payment_currency = $_POST['mc_currency'];
              $txn_id = $_POST['txn_id'];

              // product info
              $item_name = $_POST['item_name'];
              $item_number = $_POST['item_number'];

              // buyer info
              $payer_email = $_POST['payer_email'];
              $first_name = $_POST['first_name'];
              $last_name = $_POST['last_name'];
              $address_city = $_POST['address_city'];
              $address_state = $_POST['address_state'];
              $address_country = $_POST['address_country'];

              // receiver_email, that's our email address
              $receiver_email = $_POST['receiver_email'];

              // put your actual email address here
              $our_email = 'email@email.com';

              if (($payment_status == 'Completed') &&
                 ($receiver_email == $our_email) &&
                 ($payment_amount == "20.00" ) &&
                 ($payment_currency == "USD"))
              {
                foreach ($_POST as $key => $value)
                {
                  $emailtext .= $key . " = " .$value ."\n\n";
                }
                # If all is good, mail our customer, and create a tmp/.dat file
                # to be created for our license

                mail($payer_email, "Live-VERIFIED IPN", $emailtext . "\n\n" . $req);

                $dat_filename = $first_name . '-' . $last_name . '-' . $address_city . '.dat';

                $fc = fopen($dat_filename, 'w');
                fwrite($fc, 
                    "first_name = " . $first_name . "\n\n" .
                    "last_name = "  . $last_name . "\n\n" .
                    "email = " . $receiver_email . "\n\n" .
                    "city = " . $address_city . "\n\n" .
                    "state = " . $address_state . "\n\n" .
                    "country = " . $address_country
                );
                fclose($fc);

                $filename = explode('.', $dat_filename);

                # Execute shell through PHP
                $wat = "/var/www/license/";
                $argc[0] = $filename[0];
                $out = exec('sudo java -jar license2.jar '.$wat. ' '.$filename[0]);

                $fd = fopen('log.txt', 'w');
                fwrite($fd, $out);
                fclose($fd);
              }
        } else if (strcmp ($result, "INVALID") == 0) {
            {
              // If 'INVALID', send an email. TODO: Log for manual investigation.
              foreach ($_POST as $key => $value)
              {
                $emailtext .= $key . " = " .$value ."\n\n";
              }
              mail($payer_email, "Live-INVALID IPN", $emailtext . "\n\n" . $req);
            }
        }

        # Functionality needs to be added beyond this point to execute
        # and wait for the result returned by what will be our shell script

    }
}

?>

Now the weird thing here is that i can log my post fields but the script will not return a VALID nor INVALID field. I must point out that this script works perfectly well on a shared hosting account i have. I thought initially it may be something to do with permissions, but as far as i can tell everything is in order. Does anyone know what the problem may be?

Était-ce utile?

La solution

Apparently the problem was simple. The request required www. before the domain called in the constructor.

 public function __construct($mode = 'sandbox'){
    if($mode == 'live')
        $this->_url = 'https://www.paypal.com/cgi-bin/webscr';
    else
        $this->_url = "https://www.sandbox.paypal.com/cgi-bin/webscr";
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top