Paypal - 使用php加密buynow动态结账按钮

时间:2012-09-23 11:51:19

标签: php paypal localhost paypal-ipn paypal-sandbox

我上周花了一个时间尝试使用paypal加密'结账'按钮。

最后我在这个网站上找到了使用php加密的网络付款“https://ppmts.custhelp.com/app/answers/detail/a_id/502”

我试图根据我在localhost中的网站在该示例中实现。但显示错误“收到错误#3”

这是我的代码 使用example.php

         <?php
         $config = array("cert_id" => "PHCVJS6GDV6TY", "business" => "roki_1347003608_biz@gmail.com","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 = "roki_1347003608_biz@gmail.com";

       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";

      }

        ?>

错误

       Received Error #3

任何人都可以知道这是什么错误任何人帮助我如何解决这个问题我真的帮助少了。谢谢

2 个答案:

答案 0 :(得分:1)

问题是脚本工作需要OpenSSL。它在脚本的注释中指定它使用它来加密PayPal按钮。

这一行导致了这个问题,因为它无法创建文件,因为没有指定OpenSSL。

$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";

您需要指定OpenSSL的路径才能使其正常工作。

以下内容取自您链接的网站。

配置:

/* 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";

可以使用以下google查询找到有关在localhost中使用OpenSSL的信息: https://www.google.co.uk/search?q=openssl+for+localhost

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

希望有所帮助。

答案 1 :(得分:1)

在某些托管计划中设置此项可能是真正的熊或不可能。另一个建议是不加密,而是使用篡改检测哈希技巧,然后在处理IPN时检查这一点。我在这里记录:

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