title: Knet Payment Gateway
body: <p>I have integrated the "Knet" payment gateway in PHP but it always goes to an error page. While I have checked for the same I found that:</p>
  
  <p><code>$Pipe-&gt;performPaymentInitialization()</code> this variable returns value <code>-1</code>. I don't know what is the issue behind it. Please suggest the solution to me.</p>
  
  <p>Please check the below code which I have used.</p>
  
  <h1>-------------------------------knetpayment.php-------------------------------</h1>
  
  <pre><code>&lt;?php
  $transId = "KWC".time();
  require_once "knet/e24PaymentPipe.inc.php" ;
  
  $Pipe = new e24PaymentPipe;
  
  $respURL = SITE_URL."/payment/success";
  $errorURL = SITE_URL."/payment/fail";
  
  $res = DOC_ROOT."/controller/payment/knet/resource.cgn";
  $Pipe-&gt;setAction(1);
  $Pipe-&gt;setCurrency(414);
  $Pipe-&gt;setLanguage("ENG"); //change it to "ARA" for arabic language
  $Pipe-&gt;setResponseURL($respURL); // set your respone page URL
  $Pipe-&gt;setErrorURL($errorURL); //set your error page URL
  $Pipe-&gt;setAmt("10"); //set the amount for the transaction
  $Pipe-&gt;setResourcePath($res); //change the path where your resource file is
  $Pipe-&gt;setAlias("abcd"); //set your alias name here
  $randId = time();
  $Pipe-&gt;setTrackId($randId);//generate the random number here
  
  $Pipe-&gt;setUdf1("UDF 1"); //set User defined value
  $Pipe-&gt;setUdf2("UDF 2"); //set User defined value
  $Pipe-&gt;setUdf3("UDF 3"); //set User defined value
  $Pipe-&gt;setUdf4("UDF 4"); //set User defined value
  $Pipe-&gt;setUdf5("UDF 5"); //set User defined value
  
  //get results
  if($Pipe-&gt;performPaymentInitialization()!=$Pipe-&gt;SUCCESS)
  {
      header("location: ".$errorURL."");
      exit;
  }else {
      $payID = $Pipe-&gt;getPaymentId();
      $payURL = $Pipe-&gt;getPaymentPage();
      $transId = "KWC".time();    
      header("location:".$payURL."?PaymentID=".$payID); 
       exit;
  }
  ?&gt;
  </code></pre>
  
  <h1>---------------------e24PaymentPipe.inc.php---------------------------</h1>
  
  <pre><code>&lt;?php
  
  class e24PaymentPipe {
      var $SUCCESS = 0;
      var $FAILURE = -1;
      var $BUFFER = 2320;
      var $strIDOpen = "&lt;id&gt;";
      var $strPasswordOpen = "&lt;password&gt;";
      var $strWebAddressOpen = "&lt;webaddress&gt;";
      var $strPortOpen = "&lt;port&gt;";
      var $strContextOpen = "&lt;context&gt;";
      var $strIDClose = "&lt;/id&gt;";
      var $strPasswordClose = "&lt;/password&gt;";
      var $strWebAddressClose = "&lt;/webaddress&gt;";
      var $strPortClose = "&lt;/port&gt;";
      var $strContextClose = "&lt;/context&gt;";
      var $webAddress;
      var $port;
      var $id;
      var $password;
      var $passwordHash;
      var $action;
      var $transId;
      var $amt;
      var $responseURL;
      var $trackId;
      var $udf1;
      var $udf2;
      var $udf3;
      var $udf4;
      var $udf5;
      var $paymentPage;
      var $paymentId;
      var $result;
      var $auth;
      var $ref;
      var $avr;
      var $date;
      var $currency;
      var $errorURL;
      var $language;
      var $context;
      var $resourcePath;
      var $alias;
      var $error;
      var $rawResponse;
      var $debugMsg;
      var $arr = array();
  
      function e24PaymentPipe() {
          $this-&gt;webAddress = "";
          $this-&gt;port = "";
          $this-&gt;id = "";
          $this-&gt;password = "";
          $this-&gt;action = ""; // 1 = purchase
          $this-&gt;transId = "";
          $this-&gt;amt = "";
          $this-&gt;responseURL = "";
          $this-&gt;trackId = "";
          $this-&gt;udf1 = "";
          $this-&gt;udf2 = "";
          $this-&gt;udf3 = "";
          $this-&gt;udf4 = "";
          $this-&gt;udf5 = "";
          $this-&gt;paymentPage = "";
          $this-&gt;paymentId = "";
          $this-&gt;result = 0;
          $this-&gt;auth = "";
          $this-&gt;ref = "";
          $this-&gt;avr = "";
          $this-&gt;date = "";
          $this-&gt;currency = "";
          $this-&gt;errorURL = "";
          $this-&gt;language = "";
          $this-&gt;context = "";
          $this-&gt;resourcePath = "";
          $this-&gt;alias = "";
          $this-&gt;error = "";
          $this-&gt;rawResponse = "";
          $this-&gt;debugMsg = "";
      }
  
      function getWebAddress() {
          return webAddress;
      }
  
      function setWebAddress($s) {
          $this-&gt;webAddress = $s;
      }
  
      function getPort() {
          return $this-&gt;port;
      }
  
      function setPort($s) {
          $this-&gt;port = $s;
      }
  
      function set($k, $v) {
          $this-&gt;arr[$k] = $v;
      }
  
      function get($k) {
          return $this-&gt;arr[$k];
      }
  
      function setId($s) {
          $this-&gt;id = $s;
      }
  
      function getId() {
          return $this-&gt;id;
      }
  
      function setPassword($s) {
          $this-&gt;password = $s;
      }
  
      function setPasswordHash($s) {
          $this-&gt;passwordHash = $s;
      }
  
      function getPassword() {
          return $this-&gt;password;
      }
  
      function getPasswordHash() {
          return $this-&gt;passwordHash;
      }
  
      function setAction($s) {
          $this-&gt;action = $s;
      }
  
      function getAction() {
          return $this-&gt;action;
      }
  
      function setTransId($s) {
          $this-&gt;transId = $s;
      }
  
      function getTransId() {
          return $this-&gt;transId;
      }
  
      function setAmt($s) {
          $this-&gt;amt = $s;
      }
  
      function getAmt() {
          return $this-&gt;amt;
      }
  
      function setResponseURL($s) {
          $this-&gt;responseURL = $s;
      }
  
      function getResponseURL() {
          return $this-&gt;responseURL;
      }
  
      function setTrackId($s) {
          $this-&gt;trackId = $s;
      }
  
      function getTrackId() {
          return $this-&gt;trackId;
      }
  
      function setUdf1($s) {
          $this-&gt;udf1 = $s;
      }
  
      function getUdf1() {
          return $this-&gt;udf1;
      }
  
      function setUdf2($s) {
          $this-&gt;udf2 = $s;
      }
  
      function getUdf2() {
          return $this-&gt;udf2;
      }
  
      function setUdf3($s) {
          $this-&gt;udf3 = $s;
      }
  
      function getUdf3() {
          return $this-&gt;udf3;
      }
  
      function setUdf4($s) {
          $this-&gt;udf4 = $s;
      }
  
      function getUdf4() {
          return $this-&gt;udf4;
      }
  
      function setUdf5($s) {
          $this-&gt;udf5 = $s;
      }
  
      function getUdf5() {
          return $this-&gt;udf5;
      }
  
      function getPaymentPage() {
          return $this-&gt;paymentPage;
      }
  
      function getPaymentId() {
          return $this-&gt;paymentId;
      }
  
      function setPaymentId($s) {
          $this-&gt;paymentId = $s;
      }
  
      function setPaymentPage($s) {
          $this-&gt;paymentPage = $s;
      }
  
      function getRedirectContent() {
          return ($this-&gt;paymentPage . "&amp;PaymentID=" . $this-&gt;paymentId);
      }
  
      function getResult() {
          return $this-&gt;result;
      }
  
      function getAuth() {
          return $this-&gt;auth;
      }
  
      function getAvr() {
          return $this-&gt;avr;
      }
  
      function getDate() {
          return $this-&gt;date;
      }
  
      function getRef() {
          return $this-&gt;ref;
      }
  
      function getCurrency() {
          return $this-&gt;currency;
      }
  
      function setCurrency($s) {
          $this-&gt;currency = $s;
      }
  
      function getLanguage() {
          return $this-&gt;language;
      }
  
      function setLanguage($s) {
          $this-&gt;language = $s;
      }
  
      function getErrorURL() {
          return $this-&gt;errorURL;
      }
  
      function setErrorURL($s) {
          $this-&gt;errorURL = $s;
      }
  
      function setContext($s) {
          $this-&gt;context = $s;
      }
  
      function getResourcePath() {
          return $this-&gt;resourcePath;
      }
  
      function setResourcePath($s) {
          $this-&gt;resourcePath = $s;
      }
  
      function getAlias() {
          return $this-&gt;alias;
      }
  
      function setAlias($s) {
          $this-&gt;alias = $s;
      }
  
      function getErrorMsg() {
          return $this-&gt;error;
      }
  
      function getRawResponse() {
          return $this-&gt;rawResponse;
      }
  
      function getDebugMsg() {
          return $this-&gt;debugMsg;
      }
  
      function performPaymentInitialization() {
          $stringbuffer = "";
          if (!$this-&gt;getSecureSettings())
              return -1;
          if (strlen($this-&gt;id) &gt; 0)
              $stringbuffer .= ("id=" . $this-&gt;id . "&amp;");
          if (strlen($this-&gt;password) &gt; 0)
              $stringbuffer .= ("password=" . $this-&gt;password . "&amp;");
          if (strlen($this-&gt;passwordHash) &gt; 0)
              $stringbuffer.=("passwordhash=" . $this-&gt;passwordHash . "&amp;");
          if (strlen($this-&gt;amt) &gt; 0)
              $stringbuffer.=("amt=" . $this-&gt;amt . "&amp;");
          if (strlen($this-&gt;currency) &gt; 0)
              $stringbuffer.=("currencycode=" . $this-&gt;currency . "&amp;");
          if (strlen($this-&gt;action) &gt; 0)
              $stringbuffer.=("action=" . $this-&gt;action . "&amp;");
          if (strlen($this-&gt;language) &gt; 0)
              $stringbuffer.=("langid=" . $this-&gt;language . "&amp;");
          if (strlen($this-&gt;responseURL) &gt; 0)
              $stringbuffer.=("responseURL=" . $this-&gt;responseURL . "&amp;");
          if (strlen($this-&gt;errorURL) &gt; 0)
              $stringbuffer.=("errorURL=" . $this-&gt;errorURL . "&amp;");
          if (strlen($this-&gt;trackId) &gt; 0)
              $stringbuffer.=("trackid=" . $this-&gt;trackId . "&amp;");
          if (strlen($this-&gt;udf1) &gt; 0)
              $stringbuffer.=("udf1=" . $this-&gt;udf1 . "&amp;");
          if (strlen($this-&gt;udf2) &gt; 0)
              $stringbuffer.=("udf2=" . $this-&gt;udf2 . "&amp;");
          if (strlen($this-&gt;udf3) &gt; 0)
              $stringbuffer.=("udf3=" . $this-&gt;udf3 . "&amp;");
          if (strlen($this-&gt;udf4) &gt; 0)
              $stringbuffer.=("udf4=" . $this-&gt;udf4 . "&amp;");
          if (strlen($this-&gt;udf5) &gt; 0)
              $stringbuffer.=("udf5=" . $this-&gt;udf5 . "&amp;");
          $s = $this-&gt;sendMessage($stringbuffer, "PaymentInitHTTPServlet");
          if ($s == null)
              return -1;
          $i = strpos($s, ":");
          if ($i == -1) {
              $this-&gt;error = "Payment Initialization returned an invalid response: " . $s;
              return -1;
          } else {
              $this-&gt;paymentId = substr($s, 0, $i);
              $this-&gt;paymentPage = substr($s, $i + 1);
              return 0;
          }
      }
  
      function performTransaction() {
          $stringbuffer = "";
          if (!$this-&gt;getSecureSettings())
              return -1;
          if (strlen($this-&gt;id) &gt; 0)
              $stringbuffer.=("id=" . $this-&gt;id . "&amp;");
          if (strlen($this-&gt;password) &gt; 0)
              $stringbuffer.=("password=" . $this-&gt;password . "&amp;");
          if (strlen($this-&gt;passwordHash) &gt; 0)
              $stringbuffer.=("passwordhash=" . $this-&gt;passwordHash . "&amp;");
          if (strlen($this-&gt;currency) &gt; 0)
              $stringbuffer.=("currencycode=" . $this-&gt;currency . "&amp;");
          if (strlen($this-&gt;amt) &gt; 0)
              $stringbuffer.=("amt=" . $this-&gt;amt . "&amp;");
          if (strlen($this-&gt;action) &gt; 0)
              $stringbuffer.=("action=" . $this-&gt;action . "&amp;");
          if (strlen($this-&gt;paymentId) &gt; 0)
              $stringbuffer.=("paymentid=" . $this-&gt;paymentId . "&amp;");
          if (strlen($this-&gt;transId) &gt; 0)
              $stringbuffer.=("transid=" . $this-&gt;transId . "&amp;");
          if (strlen($this-&gt;trackId) &gt; 0)
              $stringbuffer.=("trackid=" . $this-&gt;trackId . "&amp;");
          if (strlen($this-&gt;udf1) &gt; 0)
              $stringbuffer.=("udf1=" . $this-&gt;udf1 . "&amp;");
          if (strlen($this-&gt;udf2) &gt; 0)
              $stringbuffer.=("udf2=" . $this-&gt;udf2 . "&amp;");
          if (strlen($this-&gt;udf3) &gt; 0)
              $stringbuffer.=("udf3=" . $this-&gt;udf3 . "&amp;");
          if (strlen($this-&gt;udf4) &gt; 0)
              $stringbuffer.=("udf4=" . $this-&gt;udf4 . "&amp;");
          if (strlen($this-&gt;udf5) &gt; 0)
              $stringbuffer.=("udf5=" . $this-&gt;udf5 . "&amp;");
  
          if (is_array($this-&gt;arr) &amp;&amp; count($this-&gt;arr)) {
              foreach ($this-&gt;arr as $key =&gt; $var) {
                  $stringbuffer .= ($key . "=" . $var . "&amp;");
              }
          }
          $stringbuffer = substr($stringbuffer, 0, strlen($stringbuffer) - 1);
          echo $stringbuffer;
          $s = $this-&gt;sendMessage($stringbuffer, "PaymentTranHTTPServlet");
          if ($s == null)
              return -1;
  
          $arraylist = $this-&gt;parseResults($s);
          if ($arraylist == null) {
              return -1;
          } else {
              $this-&gt;result = $arraylist[0];
              $this-&gt;auth = $arraylist[1];
              $this-&gt;ref = $arraylist[2];
              $this-&gt;avr = $arraylist[3];
              $this-&gt;date = $arraylist[4];
              $this-&gt;transId = $arraylist[5];
              $this-&gt;trackId = $arraylist[6];
              $this-&gt;udf1 = $arraylist[7];
              $this-&gt;udf2 = $arraylist[8];
              $this-&gt;udf3 = $arraylist[9];
              $this-&gt;udf4 = $arraylist[10];
              $this-&gt;udf5 = $arraylist[11];
              return 0;
          }
      }
  
      function sendMessage($s, $s1) {
          $stringbuffer = "";
          $error = "";
          $this-&gt;debugMsg .= ("&lt;br&gt;---------- " . $s1 . ": " . time() . " ---------- &lt;br&gt;");
          if ($this-&gt;port == "443") {
              if (strlen($this-&gt;webAddress) &lt;= 0) {
                  $error = "No URL specified.";
                  return null;
              }
              if ($this-&gt;port == "443")
                  $stringbuffer.=("https://");
              else
                  $stringbuffer.=("http://");
              $stringbuffer.=($this-&gt;webAddress);
              if (strlen($this-&gt;port) &gt; 0) {
                  $stringbuffer.=(":");
                  $stringbuffer.=($this-&gt;port);
              }
              if (strlen($this-&gt;context) &gt; 0) {
                  if (!$this-&gt;StartsWith($this-&gt;context, "/"))
                      $stringbuffer.=("/");
                  $stringbuffer.=($this-&gt;context);
                  if (!$this-&gt;EndsWith($this-&gt;context, "/"))
                      $stringbuffer.=("/");
              } else {
                  $stringbuffer.=("/");
              }
              $stringbuffer.=("servlet/");
              $stringbuffer.=($s1);
              $this-&gt;debugMsg.=("&lt;br&gt;About to create the URL to: " . $stringbuffer);
              $url = $stringbuffer;
              //echo '&lt;br&gt;'. $stringbuffer . '&lt;br&gt;';
              $this-&gt;debugMsg.=("&lt;br&gt;About to create http connection....");
  
              $this-&gt;debugMsg.=("&lt;br&gt;Created connection.!!");
              if (strlen($s) &gt; 0) {
                  $c = curl_init();
                  curl_setopt($c, CURLOPT_HEADER, 0);
                  curl_setopt($c, CURLOPT_URL, $stringbuffer);
                  curl_setopt($c, CURLOPT_POST, true);
                  curl_setopt($c, CURLOPT_POSTFIELDS, $s);
                  curl_setopt($c, CURLOPT_SSL_VERIFYHOST, true);
                  curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);
                  $this-&gt;debugMsg.=("&lt;br&gt;about to write DataOutputSteam....");
                  curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
                  $this-&gt;debugMsg.=("&lt;br&gt;after DataOutputStream.!!");
                  $tmp = curl_exec($c);
                  if (curl_error($c)) {
                      echo 'CURL ERROR: ' . curl_errno($c) . '::' . curl_error($c);
                  } elseif ($tmp) {
  
                      curl_close($c);
                      $this-&gt;rawResponse = $tmp;
                      $this-&gt;debugMsg.=("&lt;br&gt;Received RESPONSE: " . $this-&gt;rawResponse);
                      return $this-&gt;rawResponse;
                  } else {
                      $error = "No Data To Post!";
                  }
              } else {
                  $this-&gt;clearFields();
                  $this-&gt;error = "Failed to make connection:\n" . $error;  //. $exception;
                  return null;
              }
          }
      }
  
      function parseResults($s) {
  
          $arraylist = array(); {
              if ($this-&gt;StartsWith($s, "!ERROR!")) {
                  $this-&gt;error = $s;
                  return null;
              }
  
              $tokens = strtok($s, ":\r\n");
              print_r($tokens);
              $s1;
              $flag = false;
              foreach ($tokens as $token) {
                  $s2 = $token;
                  if (!$s2 . $this-&gt;startsWith(":")) {
                      $arraylist[] = ($s2);
                      $flag = false;
                  } else {
                      if ($flag)
                          $arraylist[] = ("");
                      $flag = true;
                  }
              }
              return $arraylist;
          }
  
          $this-&gt;error = "Internal Error!";
          return null;
      }
  
      function clearFields() {
          $this-&gt;error = "";
          $this-&gt;paymentPage = "";
          $this-&gt;paymentId = "";
      }
  
      function getSecureSettings() {
          $s = "";
          if (!$this-&gt;createReadableZip())
              return false;
          $s = $this-&gt;readZip();
          if ($s == "")
              return false;
  
          unlink($this-&gt;getResourcePath() . "resource.cgz");
          return $this-&gt;parseSettings($s);
      }
  
      function createReadableZip() { {
  
              $filenameInput = $this-&gt;getResourcePath() . "resource.cgn";
              $handleInput = fopen($filenameInput, "r");
              $contentsInput = fread($handleInput, filesize($filenameInput));
  
              $filenameOutput = $this-&gt;getResourcePath() . "resource.cgz";
              @unlink($filenameOutput);
              $handleOutput = fopen($filenameOutput, "w");
  
              $inByteArray = $this-&gt;getBytes($contentsInput);
              $outByteArray = $this-&gt;simpleXOR($inByteArray);
  
              fwrite($handleOutput, $this-&gt;getString($outByteArray));
              fclose($handleInput);
              fclose($handleOutput);
          }
  
          return true;
      }
  
      function readZip() {
  
          $s = ""; {
  
              $filenameInput = $this-&gt;getResourcePath() . "resource.cgz";
  
              $zipentry;
              $i = 0;
  
              $zip = new ZipArchive;
              if ($zip-&gt;open($filenameInput) === TRUE) {
                  $zip-&gt;extractTo($this-&gt;resourcePath);
                  $zip-&gt;close();
              } else {
                  echo 'failed';
                  $this-&gt;error = "Failed to unzip file";
              }
  
              if (strlen($this-&gt;error) === 0) {
                  $xmlNameInput = $this-&gt;resourcePath . $this-&gt;getAlias() . ".xml";
                  $xmlHandleInput = fopen($xmlNameInput, "r");
                  $xmlContentsInput = fread($xmlHandleInput, filesize($xmlNameInput));
                  fclose($xmlHandleInput);
                  unlink($xmlNameInput);
                  $s = $xmlContentsInput;
  
                  $s = $this-&gt;getString($this-&gt;simpleXOR($this-&gt;getBytes($s)));
              } else {
                  $this-&gt;error = "Unable to open resource";
              }
              return $s;
          }
      }
  
      function parseSettings($s) {
          $i = 0;
          $j = 0;
          $i = strpos($s, "&lt;id&gt;") + strlen("&lt;id&gt;");
          $j = strpos($s, "&lt;/id&gt;");
  
          $this-&gt;setId(substr($s, $i, $j - $i));
          $i = strpos($s, "&lt;password&gt;") + strlen("&lt;password&gt;");
          $j = strpos($s, "&lt;/password&gt;");
          $this-&gt;setPassword(substr($s, $i, $j - $i));
  
          $i = strpos($s, "&lt;passwordhash&gt;") + strlen("&lt;passwordhash&gt;");
          $j = strpos($s, "&lt;/passwordhash&gt;");
          $this-&gt;setPasswordHash(substr($s, $i, $j - $i));
  
          $i = strpos($s, "&lt;webaddress&gt;") + strlen("&lt;webaddress&gt;");
          $j = strpos($s, "&lt;/webaddress&gt;");
          $this-&gt;setWebAddress(substr($s, $i, $j - $i));
          $i = strpos($s, "&lt;port&gt;") + strlen("&lt;port&gt;");
          $j = strpos($s, "&lt;/port&gt;");
          $this-&gt;setPort(substr($s, $i, $j - $i));
          $i = strpos($s, "&lt;context&gt;") + strlen("&lt;context&gt;");
          $j = strpos($s, "&lt;/context&gt;");
          $this-&gt;setContext(substr($s, $i, $j - $i));
          return true;
      }
  
      function simpleXOR($abyte0) {
          $key = "Those who profess to favour freedom and yet depreciate agitation are men who want rain without thunder and lightning";
          $abyte1 = $this-&gt;getBytes($key);
  
          for ($i = 0; $i &lt; sizeof($abyte0);) {
              for ($j = 0; $j &lt; sizeof($abyte1); $j++) {
                  $abyte2[$i] = ($abyte0[$i] ^ $abyte1[$j]);
                  if (++$i == sizeof($abyte0))
                      break;
              }
          }
  
          return $abyte2;
      }
  
      function getBytes($s) {
          $hex_ary = array();
          $size = strlen($s);
          for ($i = 0; $i &lt; $size; $i++)
              $hex_ary[] = chr(ord($s[$i]));
          return $hex_ary;
      }
  
      function getString($byteArray) {
          $s = "";
          foreach ($byteArray as $byte) {
              $s .=$byte;
          }
          return $s;
      }
  
      function StartsWith($Haystack, $Needle) {
          // Recommended version, using strpos
          return strpos($Haystack, $Needle) === 0;
      }
  
      function EndsWith($Haystack, $Needle) {
          // Recommended version, using strpos
          return strrpos($Haystack, $Needle) === strlen($Haystack) - strlen($Needle);
      }
  
  }
  
  function xor_string($string) {
      $buf = '';
      $size = strlen($string);
      for ($i = 0; $i &lt; $size; $i++)
          $buf .= chr(ord($string[$i]) ^ 255);
      return $buf;
  }
  ?&gt;
  </code></pre>