Advertisement
Guest User

Untitled

a guest
Jul 31st, 2015
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 11.42 KB | None | 0 0
  1. <?php
  2. /**************************************************************************************************
  3. * Sage Pay Server PHP Kit Includes File
  4. ***************************************************************************************************
  5.  
  6. ***************************************************************************************************
  7. * Change history
  8. * ==============
  9. *
  10. * 02/04/2009 - Simon Wolfe - Updated UI for re-brand
  11. * 11/02/2009 - Simon Wolfe - Updated for VSP protocol 2.23
  12. * 18/12/2007 - Nick Selby - New PHP version adapted from ASP
  13. ***************************************************************************************************
  14. * Description
  15. * ===========
  16. *
  17. * Page with no visible content, but defines the constants and functions used in other pages in the
  18. * kit.  It also opens connections to the database and defines record sets for later use.  It is
  19. * included at the top of every other page in the kit and is paried with the closedown scipt.
  20. ***************************************************************************************************/
  21.  
  22. ini_set('session.bug_compat_warn', 0);
  23. ini_set('session.bug_compat_42', 0);
  24.  
  25. ob_start(); //start output buffering
  26. session_start(); //enable sessions
  27.  
  28. /***************************************************************************************************
  29. * Values for you to update
  30. ***************************************************************************************************/
  31.  
  32. $strConnectTo="LIVE";   /** Set to SIMULATOR for the Sage Pay Simulator expert system, TEST for the Test Server **
  33.                             *** and LIVE in the live environment **/
  34. //if ($_SESSION['Mobile'] == 'Yes') { $strConnectTo="TEST"; }
  35.  
  36.  
  37. if(strpos($_SERVER['SERVER_NAME'], 'dev.') !== false){
  38.     $strHostName="xxx";
  39.     $strDatabaseUser="xxx"; // Change this if you created a different user name to access the database
  40.     $strDatabasePassword="xxx"; // Set the password for the above user here
  41.     $strDatabase="xxx"; // Change this if you created a different database name
  42.     $strVirtualDir="xxx"; // Change if you've created a Virtual Directory in IIS with a different name
  43.     $strYourSiteFQDN="xxx";
  44.     $strYourSiteInternalFQDN="xxx";
  45. }
  46. else {
  47.     $strHostName="xxx";
  48.     $strDatabaseUser="xxx"; // Change this if you created a different user name to access the database
  49.     $strDatabasePassword="xxx"; // Set the password for the above user here
  50.     $strDatabase="xxx"; // Change this if you created a different database name
  51.  
  52.     $strVirtualDir="booking"; // Change if you've created a Virtual Directory in IIS with a different name
  53.  
  54.     /** IMPORTANT.  Set the strYourSiteFQDN value to the Fully Qualified Domain Name of your server. **
  55.     ** This should start http:// or https:// and should be the name by which our servers can call back to yours **
  56.     ** i.e. it MUST be resolvable externally, and have access granted to the Sage Pay servers **
  57.     ** examples would be https://www.mysite.com or http://212.111.32.22/ **
  58.     ** NOTE: You should leave the final / in place. **/
  59.     $strYourSiteFQDN="xxx";
  60.  
  61.     /** At the end of a Sage Pay Server transaction, the customer is redirected back to the completion page **
  62.     ** on your site using a client-side browser redirect. On live systems, this page will always be **
  63.     ** referenced using the strYourSiteFQDN value above.  During development and testing, however, it **
  64.     ** is often the case that the development machine sits behind the same firewall as the server **
  65.     ** hosting the kit, so your browser might not be able resolve external IPs or dns names. **
  66.     ** e.g. Externally your server might have the IP 212.111.32.22, but behind the firewall it **
  67.     ** may have the IP 192.168.0.99.  If your test machine is also on the 192.168.0.n network **
  68.     ** it may not be able to resolve 212.111.32.22. **
  69.     ** Set the strYourSiteInternalFQDN to the internal Fully Qualified Domain Name by which **
  70.     ** your test machine can reach the server (in the example above you'd use http://192.168.0.99/) **
  71.     ** If you are not on the same network as the test server, set this value to the same value **
  72.     ** as strYourSiteFQDN above. **
  73.     ** NOTE: You should leave the final / in place. **/
  74.     $strYourSiteInternalFQDN="xxx";
  75. }
  76.  
  77. $strVendorName="xxx"; // Set this value to the Vendor Name assigned to you by Sage Pay or chosen when you applied
  78. $strCurrency="xxx"; // Set this to indicate the currency in which you wish to trade. You will need a merchant number in this currency
  79. $strTransactionType="xxx"; // This can be DEFERRED or AUTHENTICATE if your Sage Pay account supports those payment types
  80. $strPartnerID=""; /** Optional setting. If you are a Sage Pay Partner and wish to flag the transactions with your unique partner id set it here. **/
  81.  
  82. /**************************************************************************************************
  83. * Global Definitions for this site
  84. ***************************************************************************************************/
  85.  
  86. //Open the VPS database
  87. mysql_connect($strHostName,$strDatabaseUser,$strDatabasePassword); //Change 'localhost' if your database is hosted externally
  88. @mysql_select_db($strDatabase) or die("Unable to select database");
  89.  
  90. $strProtocol="3.00";
  91.  
  92. if ($strConnectTo=="LIVE")
  93. {
  94.   $strAbortURL="https://live.sagepay.com/gateway/service/abort.vsp";
  95.   $strAuthoriseURL="https://live.sagepay.com/gateway/service/authorise.vsp";
  96.   $strCancelURL="https://live.sagepay.com/gateway/service/cancel.vsp";
  97.   $strPurchaseURL="https://live.sagepay.com/gateway/service/vspserver-register.vsp";
  98.   $strRefundURL="https://live.sagepay.com/gateway/service/refund.vsp";
  99.   $strReleaseURL="https://live.sagepay.com/gateway/service/release.vsp";
  100.   $strRepeatURL="https://live.sagepay.com/gateway/service/repeat.vsp";
  101.   $strVoidURL="https://live.sagepay.com/gateway/service/void.vsp";
  102. }
  103. elseif ($strConnectTo=="TEST")
  104. {
  105.   $strAbortURL="https://test.sagepay.com/gateway/service/abort.vsp";
  106.   $strAuthoriseURL="https://test.sagepay.com/gateway/service/authorise.vsp";
  107.   $strCancelURL="https://test.sagepay.com/gateway/service/cancel.vsp";
  108.   $strPurchaseURL="https://test.sagepay.com/gateway/service/vspserver-register.vsp";
  109.   $strRefundURL="https://test.sagepay.com/gateway/service/refund.vsp";
  110.   $strReleaseURL="https://test.sagepay.com/gateway/service/abort.vsp";
  111.   $strRepeatURL="https://test.sagepay.com/gateway/service/repeat.vsp";
  112.   $strVoidURL="https://test.sagepay.com/gateway/service/void.vsp";
  113. }
  114. else
  115. {
  116.   $strAbortURL="https://test.sagepay.com/simulator/VSPServerGateway.asp?Service=VendorAbortTx";
  117.   $strAuthoriseURL="https://test.sagepay.com/simulator/VSPServerGateway.asp?Service=VendorAuthoriseTx";
  118.   $strCancelURL="https://test.sagepay.com/simulator/VSPServerGateway.asp?Service=VendorCancelTx";
  119.   $strPurchaseURL="https://test.sagepay.com/simulator/VSPServerGateway.asp?Service=VendorRegisterTx";
  120.   $strRefundURL="https://test.sagepay.com/simulator/VSPServerGateway.asp?Service=VendorRefundTx";
  121.   $strReleaseURL="https://test.sagepay.com/simulator/VSPServerGateway.asp?Service=VendorReleaseTx";
  122.   $strRepeatURL="https://test.sagepay.com/simulator/VSPServerGateway.asp?Service=VendorRepeatTx";
  123.   $strVoidURL="https://test.sagepay.com/simulator/VSPServerGateway.asp?Service=VendorVoidTx";
  124. }
  125.  
  126. /**************************************************************************************************
  127. * Useful functions for all pages in this kit
  128. **************************************************************************************************/
  129.  
  130. //Function to redirect browser
  131. function redirect($url)
  132. {
  133.    if (!headers_sent())
  134.         header('Location: '.$url);
  135.    else
  136.    {
  137.         echo '<script type="text/javascript">';
  138.         echo 'window.location.href="'.$url.'";';
  139.         echo '</script>';
  140.         echo '<noscript>';
  141.         echo '<meta http-equiv="refresh" content="0;url='.$url.'" />';
  142.         echo '</noscript>';
  143.    }
  144. }
  145.  
  146. // Filters unwanted characters out of an input string.  Useful for tidying up FORM field inputs
  147. function cleanInput($strRawText,$strType)
  148. {
  149.  
  150.     if ($strType=="Number") {
  151.         $strClean="0123456789.";
  152.         $bolHighOrder=false;
  153.     }
  154.     else if ($strType=="VendorTxCode") {
  155.         $strClean="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.";
  156.         $bolHighOrder=false;
  157.     }
  158.     else {
  159.         $strClean=" ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.,'/{}@():?-_&£$=%~<>*+\"";
  160.         $bolHighOrder=true;
  161.     }
  162.  
  163.     $strCleanedText="";
  164.     $iCharPos = 0;
  165.  
  166.     do
  167.     {
  168.         // Only include valid characters
  169.         $chrThisChar=substr($strRawText,$iCharPos,1);
  170.  
  171.         if (strspn($chrThisChar,$strClean,0,strlen($strClean))>0) {
  172.             $strCleanedText=$strCleanedText . $chrThisChar;
  173.         }
  174.         else if ($bolHighOrder==true) {
  175.                 // Fix to allow accented characters and most high order bit chars which are harmless
  176.                 if (bin2hex($chrThisChar)>=191) {
  177.                     $strCleanedText=$strCleanedText . $chrThisChar;
  178.                 }
  179.             }
  180.  
  181.         $iCharPos=$iCharPos+1;
  182.         }
  183.     while ($iCharPos<strlen($strRawText));
  184.  
  185.     $cleanInput = ltrim($strCleanedText);
  186.     return $cleanInput;
  187.  
  188. }
  189.  
  190.  
  191. // Function to check validity of email address entered in form fields
  192. function is_valid_email($email) {
  193.   $result = TRUE;
  194.   if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$", $email)) {
  195.     $result = FALSE;
  196.   }
  197.   return $result;
  198. }
  199.  
  200. /*************************************************************
  201.     Send a post request with cURL
  202.         $url = URL to send request to
  203.         $data = POST data to send (in URL encoded Key=value pairs)
  204. *************************************************************/
  205. function requestPost($url, $data){
  206.     // Set a one-minute timeout for this script
  207.     set_time_limit(60);
  208.  
  209.     // Initialise output variable
  210.     $output = array();
  211.  
  212.     // Open the cURL session
  213.     $curlSession = curl_init();
  214.  
  215.     // Set the URL
  216.     curl_setopt ($curlSession, CURLOPT_URL, $url);
  217.     // No headers, please
  218.     curl_setopt ($curlSession, CURLOPT_HEADER, 0);
  219.     // It's a POST request
  220.     curl_setopt ($curlSession, CURLOPT_POST, 1);
  221.     // Set the fields for the POST
  222.     curl_setopt ($curlSession, CURLOPT_POSTFIELDS, $data);
  223.     // Return it direct, don't print it out
  224.     curl_setopt($curlSession, CURLOPT_RETURNTRANSFER,1);
  225.     // This connection will timeout in 30 seconds
  226.     curl_setopt($curlSession, CURLOPT_TIMEOUT,30);
  227.     //The next two lines must be present for the kit to work with newer version of cURL
  228.     //You should remove them if you have any problems in earlier versions of cURL
  229.     curl_setopt($curlSession, CURLOPT_SSL_VERIFYPEER, FALSE);
  230.     curl_setopt($curlSession, CURLOPT_SSL_VERIFYHOST, 1);
  231.  
  232.     //Send the request and store the result in an array
  233.  
  234.     $rawresponse = curl_exec($curlSession);
  235.     //Store the raw response for later as it's useful to see for integration and understanding
  236.     $_SESSION["rawresponse"]=$rawresponse;
  237.     //Split response into name=value pairs
  238.     $response = explode(chr(10), $rawresponse);
  239.     // Check that a connection was made
  240.     if (curl_error($curlSession)){
  241.         // If it wasn't...
  242.         $output['Status'] = "FAIL";
  243.         $output['StatusDetail'] = curl_error($curlSession);
  244.     }
  245.  
  246.     // Close the cURL session
  247.     curl_close ($curlSession);
  248.  
  249.     // Tokenise the response
  250.     for ($i=0; $i<count($response); $i++){
  251.         // Find position of first "=" character
  252.         $splitAt = strpos($response[$i], "=");
  253.         // Create an associative (hash) array with key/value pairs ('trim' strips excess whitespace)
  254.         $output[trim(substr($response[$i], 0, $splitAt))] = trim(substr($response[$i], ($splitAt+1)));
  255.     } // END for ($i=0; $i<count($response); $i++)
  256.  
  257.     // Return the output
  258.     return $output;
  259. } // END function requestPost()
  260.  
  261. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement