【问题标题】:Paypal - encrypt buynow dynamic checkout button with phpPaypal - 使用 php 加密 buynow 动态结帐按钮
【发布时间】:2012-09-23 11:51:19
【问题描述】:

上周我一直在尝试制作一个加密的“使用贝宝结帐”按钮。

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

我正在尝试根据我在本地主机中的站点在该示例中实现。但显示错误“收到错误 #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

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

【问题讨论】:

  • 您确定所有目录/路径在您的配置等中都正确吗?尤其是临时目录。
  • 是的,我检查所有目录是否正确
  • 如果我删除,在 example.php 中!这个 $result = $paypal->encryptButtonData(); if ($result == PP_ERROR_OK) { // 出了点问题 echo "Received Error #".$result."\n";出口; }它转到贝宝页面并显示一些必需的信息丢失或不完整。请更正您的输入,然后重试。
  • 我不能肯定地说,但我在测试后有一种感觉,它试图执行它不能在本地执行的代码(openssl)。无法创建扩展名为 .3 的文件
  • 但是它将文件写入临时目录中。那么你知道任何解决方案兄弟

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


【解决方案1】:

问题是要让脚本工作,您需要有 OpenSSL。在脚本的 cmets 中,它确实指定使用它来加密 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 查询找到有关在本地主机中使用 OpenSSL 的信息: https://www.google.co.uk/search?q=openssl+for+localhost

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

希望对您有所帮助。

【讨论】:

  • 我按照您为本地主机的 openssl 提供的链接进行操作。但它仍然显示相同的错误
  • 您确定 OpenSSL 已安装且工作正常吗? phpjoel.com/2011/04/07/… - 尝试执行“生成密钥”部分,如果可行,请执行之后的所有部分,包括“修改 HTTPD-SSL.CONF”
  • 它的生成密钥正确。但是在 localhost 中设置时,我们是否需要更改路由器中的任何内容
  • 否 - localhost 留在机器内,它甚至不会到达路由器(除非尝试从外部访问,如果已设置)
  • 好吧兄弟,它不工作。现在我要在现场检查它。我正在使用 debian linux。你能提供任何类似如何在 linux 中进行 paypal 集成的方法
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 2011-05-21
    • 1970-01-01
    • 2011-05-05
    • 2015-01-06
    • 2021-06-11
    • 2017-07-15
    • 2014-01-03
    • 2020-10-06
    • 2016-07-09
    相关资源
    最近更新 更多