arok
arok

Reputation: 1845

Paypal - encrypt buynow dynamic checkout button with php

I have spent the last week trying to make an encrypted 'checkout with paypal' button.

finally i found Encrypted web payments with php in this website "https://ppmts.custhelp.com/app/answers/detail/a_id/502"

i m trying to implement in that example according to my site in the localhost .but show an error"Received error #3"

this is my code Example.php

         <?php
         $config = array("cert_id" => "PHCVJS6GDV6TY", "business" => "[email protected]","basedir" => "./",
          "certstore" => "./", "tempdir" => "./", "my_private" => "my-prvkey.pem",          "my_public" => "my.pubcert.pem",
         "paypal_public" => "paypal_cert_pem.txt");
          $paypal = new PayPal_EncryptedButtons($config);

      // You can add parameters to the button one by one (name, value) or you can pass two
       // arrays of names and values, as documented below.
    $names = array("amount", "currency_code", "item_name", "item_number");
        $values = array("2.56", "USD", "Dave's secret encryption sauce (encryption via PHP)", "123456");
     $paypal->addButtonParam($names, $values);

     // We can now generate our code
     $result = $paypal->encryptButtonData();
     if ($result !== PP_ERROR_OK) {
     // Something went wrong
      echo "Received Error #".$result."\n";
       exit;
         }

   // Get the string of encrypted button data

           echo '<form action="https://www.sandbox.paypal.com/cgi-bin/webscr"       method="post">';
     echo '<input type="hidden" name="cmd" value="_s-xclick">';
         echo '<input type="image" src="https://www.sandbox.paypal.com/en_US/i/btn/x-click-but23.gif" border="0" name="submit" alt="Make payments with PayPal - it\'s fast, free and secure!">';             
     echo '<input type="hidden" name="encrypted" value="';
        echo $paypal->getButton();
         echo '">';
        echo "</form>\n";
         ?>
      </body>
         </html>

EncryptedButtons.php

          <?php

        define("PP_ERROR_OK", 0);
        define("PP_ERROR_FILE", 1);
        define("PP_ERROR_OPENSSL", 2);
        define("PP_ERROR_DATA", 3);
        define("PP_ERROR_PARAMS", 4);
        define("PP_ERROR_NOTFOUND", 5);
        define("PP_ERROR_UNKNOWN", 6);


       require_once "/wamp/www/var/Php/Config.php";


       class PayPal_EncryptedButtons {



        /** Button Data Array */
          var $buttonData = array();

        /** Config Pointer */
          var $config;

           /** Internal Data Handler */
               var $_data;

               /** Random Transaction ID */
            var $_rnd;

                var $debug = true;

          function PayPal_EncryptedButtons($config = array()) {
          $this->config = new PayPal_EncryptedButtons_Config;
               /*
               foreach ($config as $name => $val) {
            if ($this->config->$name) {
          $this->config->$name = $val;
              }
              else {
           return PP_ERROR_UNKNOWN;
             }
          }
              */
             return;
            }


              function changeConfig($name, $value = "") {
           if ($this->config->$name) {
           $this->config->$name = $value;
              return true;
                }
           else {
            return false;
             }
                   }


           function addButtonParam($names = "", $values = "") {
            if ((is_array($names) && !is_array($values)) || (!is_array($names) &&       is_array($values))) {
                  return PP_ERROR_PARAMS;
            }
            elseif (is_array($names) && is_array($values)) {
            if (count($names) !== count($values)) {
             return PP_ERROR_PARAMS;
               }
                $i = 0;
           while ($i < count($names)) {
              $this->buttonData[$names[$i]] = $values[$i];
           $i++;
         }
           return PP_ERROR_OK;
              }
          else {
         if ($names == "" || $values == "") {
        return PP_ERROR_PARAMS;
      }
         else {
         $this->buttonData[$names] = $values;
         return PP_ERROR_OK;
           }
         }
              }


            function delButtonParam($name = "") {
       if ($name == "") {
       return PP_ERROR_PARAMS;
     }
        if ($this->buttonData[$name] !== null) {
          $this->buttonData[$name] = null;
        return PP_ERROR_OK;
        }
           return PP_ERROR_NOTFOUND + 10;
              }


          function encryptButtonData() {
       $this->encryptedButton = null;
            if (!is_dir($this->config->basedir)) {
      if (!mkdir($this->config->basedir)) {
     return PP_ERROR_NOTFOUND + 20;
       }
           }
             @chdir($this->config->basedir);
          $this->_data = "cmd=_xclick\n";
        $this->_data .= "business=".$this->config->business."\n";
            $this->_data .= "receiver_email=".$this->config->receiver_email."\n";
           foreach ($this->buttonData as $name => $val) {
       if ($val == null) { continue; }
        $this->_data .= $name."=".$val."\n";
            }
         $this->_data .= "cert_id=".$this->config->cert_id;
          $this->_rnd = rand(100000, 999999);
       if (!is_dir($this->config->tempdir)) {
        if (!@mkdir($this->config->tempdir)) {
     return PP_ERROR_NOTFOUND + 30;
            }
       }
              $f = @fopen($this->config->tempdir.$this->_rnd.".1", "w");
           //if (!$f) { return PP_ERROR_FILE; }
              if (!$f) { return 41; }
                    fwrite($f, trim($this->_data), strlen(trim($this->_data)));
                fclose($f);
                    if (!file_exists($this->config->my_private)) {
                 return 145;
                     }
                  if (!file_exists($this->config->my_public)) {
                 return 245;
                  }
           if (!file_exists($this->config->paypal_public)) {
       return 345;
           }
            if (!file_exists($this->config->my_private) || !file_exists($this->config->my_public) || !file_exists($this->config->paypal_public)) {
             return PP_ERROR_NOTFOUND + 40;
            }
              $exec = $this->config->openssl." smime -sign -in ".$this->config->tempdir.$this->_rnd.".1 -signer ".$this->config->my_public." -inkey ".$this->config->my_private." -outform der -nodetach -binary > ".$this->config->tempdir.$this->_rnd.".2";
       $status1 = `$exec`;
           $exec = $this->config->openssl." smime -encrypt -des3 -binary -outform pem ".$this->config->paypal_public." < ".$this->config->tempdir.$this->_rnd.".2 > ".$this-    >config->tempdir.$this->_rnd.".3";
             $status2 = `$exec`;
             $this->encryptedButton = trim(file_get_contents($this->config->tempdir.$this->_rnd.".3"));
             @unlink($this->config->tempdir.$this->_rnd.".1");
           @unlink($this->config->tempdir.$this->_rnd.".2");
            @unlink($this->config->tempdir.$this->_rnd.".3");
            if (strpos($status1, "No such file or directory") !== false ||        strpos($status2, "No such file or directory") !== false) {
           return PP_ERROR_OPENSSL;
         }
          if (!$this->encryptedButton) {
         return PP_ERROR_DATA;
        }
            return PP_ERROR_OK;
       }


           function getButton() {
       if (!$this->encryptedButton) {
      return "";
          }
       return $this->encryptedButton;
        }

config.php

       <?php
      class PayPal_EncryptedButtons_Config {
      var $cert_id = "PHCVJS6GDV6TY";
       var $business = "[email protected]";

       var $receiver_email = "";

        var $basedir = "/wamp/www/var/";

        var $certstore = "/wamp/www/key/cert/";

        var $tempdir = "/wamp/www/temp/";

        var $openssl = "openssl";

       var $my_private = "/wamp/www/key/cert/my-prvkey.pem";

       /* Your Public Certificate Filename */
       var $my_public = "/wamp/www/key/cert/my.pubcert.pem";

       /* PayPal's Public Certificate Filename */
        var $paypal_public = "/wamp/www/key/cert/paypal_cert_pem.txt";

      }

        ?>

Error

       Received Error #3

can anyone know what error is this any one help me how to fix this really i m help less.thanks

Upvotes: 1

Views: 1242

Answers (2)

Volomike
Volomike

Reputation: 24886

Setting this up can be a real bear or impossible on some hosting plans. Another suggestion is not to encrypt, but to use a tamper detection hash trick, and then check this when the IPN is processed. I document this here:

How do I make a PayPal encrypted buy now button with custom fields?

Upvotes: 1

David
David

Reputation: 2065

The issue is for the script to work you need to have OpenSSL. In the comments of the script it does specify it uses it to encrypt the PayPal buttons.

This line is what is causing the issue, as it is unable to create the file as OpenSSL has not be specified.

$exec = $this->config->openssl." smime -encrypt -des3 -binary -outform pem ".$this->config->paypal_public." < ".$this->config->tempdir.$this->_rnd.".2 > ".$this->config->tempdir.$this->_rnd.".3";

You need to specify the path to OpenSSL in order for it to work.

The below is taken from the site you linked.

Config:

/* OpenSSL Path - Path to the OpenSSL Binary */
/* If openssl isn't in your PATH, then change this to where it's located, otherwise leave it as it is */
/* No trailing slash */
var $openssl = "openssl";

Information on using OpenSSL in your localhost can be found with the following google query: https://www.google.co.uk/search?q=openssl+for+localhost

OpenSSL: http://www.openssl.org/

Hope that helps.

Upvotes: 1

Related Questions