Advertisement
Guest User

Untitled

a guest
Nov 10th, 2011
1,086
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.29 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Předpokladem je PHP verze 5.1.2 a vyšší s moduly mhash, mcrypt.
  4.  *
  5.  * Obsahuje pomocne funkcionality pro komunikaci s platebnim rozh-
  6.  * ranim GoPay.
  7.  */
  8. class ButtonHelper{
  9.  
  10.     function iconRychloplatba(){
  11.         return "https://www.gopay.cz/download/PT_rychloplatba.png";    
  12.     }
  13.     function iconDaruj(){
  14.         return "https://www.gopay.cz/download/PT_daruj.png";       
  15.     }
  16.     function iconBuynow(){
  17.         return "https://www.gopay.cz/download/PT_buynow.png";      
  18.     }
  19.     function iconDonate(){
  20.         return "https://www.gopay.cz/download/PT_donate.png";      
  21.     }
  22.     /**
  23.      * URL platebni brany pro platebni tlacitko
  24.      *
  25.      * @return URL
  26.      */
  27.     function buttonURL(){
  28.         return 'https://testgw.gopay.cz/zaplatit-tlacitko';    
  29.     }
  30.  
  31.  
  32.     /**
  33.      * Sestaveni retezce pro podpis platebniho prikazu
  34.      *
  35.      * @param long $buyerGoId
  36.      * @param string $productName
  37.      * @param long $totalPriceInCents
  38.      * @param string $variableSymbol
  39.      * @param string $failedURL
  40.      * @param string $successURL
  41.      * @param string $key
  42.      * @return retezec pro podpis
  43.      */
  44.     function concatPaymentCommand(
  45.         $buyerGoId,
  46.         $productName,
  47.         $totalPriceInCents,
  48.         $variableSymbol,
  49.         $failedURL,
  50.         $successURL,
  51.         $key){
  52.  
  53.         return $buyerGoId."|".$productName."|".$totalPriceInCents."|".$variableSymbol."|".$failedURL."|".$successURL."|".$key;
  54.     }
  55.  
  56.  
  57.     /**
  58.      * Sestaveni retezce pro podpis popisu platby (paymentIdentity)
  59.      *
  60.      * @param long $buyerGoId
  61.      * @param long $paymentSessionId
  62.      * @param string $variableSymbol
  63.      * @param string $key
  64.      * @return retezec pro podpis
  65.      */
  66.     function concatPaymentIdentity(
  67.         $buyerGoId,
  68.         $paymentSessionId,
  69.         $variableSymbol,
  70.         $key){
  71.         return $buyerGoId."|".$paymentSessionId."|".$variableSymbol."|".$key;
  72.     }
  73.  
  74.     /**
  75.      * Sifrovani dat 3DES
  76.      *
  77.      * @param string $data
  78.      * @param string $key
  79.      * @return sifrovany obsah v HEX forme
  80.      */
  81.     function encrypt($data, $key){
  82.         $td = mcrypt_module_open (MCRYPT_3DES, '', MCRYPT_MODE_ECB, '');
  83.         $mcrypt_iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
  84.         mcrypt_generic_init ($td, substr($key, 0, mcrypt_enc_get_key_size($td)), $mcrypt_iv);
  85.         $encrypted_data = mcrypt_generic ($td, $data);
  86.         mcrypt_generic_deinit ($td);
  87.         mcrypt_module_close ($td);
  88.  
  89.         return bin2hex($encrypted_data);
  90.     }
  91.  
  92.     /**
  93.      * desifrovani
  94.      *
  95.      * @param string $data
  96.      * @param string $key
  97.      * @return desifrovany retezec
  98.      */
  99.     function decrypt($data, $key){
  100.         $td = mcrypt_module_open (MCRYPT_3DES, '', MCRYPT_MODE_ECB, '');
  101.         $mcrypt_iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
  102.         mcrypt_generic_init ($td, substr($key, 0, mcrypt_enc_get_key_size($td)), $mcrypt_iv);
  103.  
  104.         $decrypted_data = mdecrypt_generic($td, ButtonHelper::convert($data));
  105.         mcrypt_generic_deinit ($td);
  106.         mcrypt_module_close ($td);
  107.  
  108.         return Trim($decrypted_data);
  109.  
  110.     }  
  111.  
  112.     /**
  113.      * hash SHA1 dat
  114.      *
  115.      * @param string $data
  116.      * @return otisk dat SHA1
  117.      */
  118.     function hash($data){
  119.         $hash = mhash(MHASH_SHA1,$data);
  120.         return bin2hex($hash);         
  121.     }
  122.  
  123.     /**
  124.      * konverze z HEX -> string
  125.      *
  126.      * @param string $hexString
  127.      * @return konverze z HEX -> string
  128.      */
  129.     function convert($hexString){
  130.  
  131.         $hexLenght = strlen($hexString);
  132.         // only hex numbers is allowed                
  133.         if ($hexLenght % 2 != 0 || preg_match("/[^\da-fA-F]/",$hexString)) return FALSE;
  134.         $binString = "";
  135.         for ($x = 1; $x <= $hexLenght/2; $x++)                
  136.         {                        
  137.             $binString .= chr(hexdec(substr($hexString,2 * $x - 2,2)));
  138.  
  139.         }
  140.  
  141.         return $binString;
  142.  
  143.     }
  144.  
  145.  
  146.     /**
  147.      * Kontrola parametru predavanych ve zpetnem volani
  148.      * po potvrzeni/zruseni platby
  149.      *
  150.      * - verifikace podpisu
  151.      *
  152.      *
  153.      * @param long $returnedGoId - buyerGoId vracene v redirectu
  154.      * @param long $returnedPaymentSessionId - paymentSessionId vracene v redirectu
  155.      * @param string $returnedVariableSymbol - variableSymbol vracene v redirectu
  156.      * @param string $returnedEncryptedSignature - encryptedSignature vracene v redirectu
  157.      * @param long $buyerGoId - identifikace uzivatele
  158.      * @param string $variableSymbol - oznaceni objednavky
  159.      * @param string $secret - klic urceny k podepisovani komunikace
  160.      *
  161.      * @return true
  162.      * @return false
  163.      */
  164.     function checkPaymentIdentity(
  165.                 $returnedGoId,
  166.                 $returnedPaymentSessionId,                 
  167.                 $returnedVariableSymbol,
  168.                 $returnedEncryptedSignature,
  169.                 $buyerGoId,
  170.                 $variableSymbol,               
  171.                 $secret                
  172.                 ){
  173.  
  174.         $valid = true;
  175.         if($returnedVariableSymbol != $variableSymbol){
  176.             $valid = false;
  177.             echo "PI invalid VS <br>";
  178.         }
  179.  
  180.         if($returnedGoId != $goId){
  181.             $valid = false;
  182.             echo "PI invalid EID<br>";
  183.         }
  184.  
  185.         $hashedSignature=ButtonHelper::hash(
  186.                 ButtonHelper::concatPaymentIdentity(
  187.                     $returnedGoId,
  188.                     $returnedPaymentSessionId,                     
  189.                     $returnedVariableSymbol,
  190.                     $secret)
  191.             );
  192.         $decryptedHash = ButtonHelper::decrypt($returnedEncryptedSignature, $secret);
  193.  
  194.         if($decryptedHash != $hashedSignature){
  195.             $valid = false;
  196.             echo "PI invalid signature <br>";
  197.         }
  198.  
  199.         return $valid;
  200.     }
  201.  
  202.  
  203.     /**
  204.      * Sestaveni formulare platebniho tl.
  205.      *
  206.      *
  207.      * @param long $buyerGoId - identifikace uzivatele
  208.      * @param long $totalPrice - cena objednavky v centech
  209.      * @param string $productName - nazev zbozi
  210.      * @param string $variableSymbol - identifikace akt. objednavky
  211.      * @param string $successURL - URL, kam se ma prejit po uspesnem zaplaceni
  212.      * @param string $failedURL - URL, kam se ma prejit po neuspesnem zaplaceni
  213.      * @param string $secret - klic urceny k podepisovani komunikace
  214.      * @param string $iconUrl - URL obrazku tlacitka
  215.      *
  216.      * @return HTML kod platebniho tlacitka
  217.      */
  218.     function createForm(
  219.             $buyerGoId,
  220.             $totalPrice,
  221.             $productName,
  222.             $variableSymbol,
  223.             $successURL,
  224.             $failedURL,
  225.             $secret,
  226.             $iconUrl
  227.             ){
  228.  
  229.         $encryptedSignature = ButtonHelper::encrypt(
  230.                 ButtonHelper::hash(
  231.                         ButtonHelper::concatPaymentCommand(
  232.                         $buyerGoId,
  233.                         $productName,
  234.                         $totalPrice,
  235.                         $variableSymbol,
  236.                         $failedURL,
  237.                         $successURL,
  238.                         $secret)
  239.                     ), $secret);
  240.  
  241.         $formBuffer = "";
  242.         $formBuffer .= "<form method='post' action='" . ButtonHelper::buttonURL() . "' target='_blank'>\n";
  243.             $formBuffer .= "<input type='hidden' name='paymentCommand.buyerGoId' value='" . $buyerGoId . "' />\n";
  244.             $formBuffer .= "<input type='hidden' name='paymentCommand.totalPrice' value='" . $totalPrice . "' />\n";
  245.             $formBuffer .= "<input type='hidden' name='paymentCommand.productName' value='" . $productName . "' />\n";
  246.             $formBuffer .= "<input type='hidden' name='paymentCommand.variableSymbol' value='" . $variableSymbol . "' />\n";
  247.             $formBuffer .= "<input type='hidden' name='paymentCommand.successURL' value='" . $successURL . "' />\n";
  248.             $formBuffer .= "<input type='hidden' name='paymentCommand.failedURL' value='" . $failedURL . "' />\n";
  249.             $formBuffer .= "<input type='hidden' name='paymentCommand.encryptedSignature' value='" . $encryptedSignature . "' />\n";
  250.             $formBuffer .= "<input type='submit' name='submit' value='' style='background:url(" . $iconUrl . ") no-repeat;width:100px;height:30px;border:none;'>\n";
  251.         $formBuffer .= "</form>\n";
  252.  
  253.         return $formBuffer;
  254.     }
  255.  
  256.     /**
  257.      * Sestaveni platebniho tl. jako odkazu
  258.      *
  259.      *
  260.      * @param long $buyerGoId - identifikace uzivatele
  261.      * @param long $totalPrice - cena objednavky v centech
  262.      * @param string $productName - nazev zbozi
  263.      * @param string $variableSymbol - identifikace akt. objednavky
  264.      * @param string $successURL - URL, kam se ma prejit po uspesnem zaplaceni
  265.      * @param string $failedURL - URL, kam se ma prejit po neuspesnem zaplaceni
  266.      * @param string $secret - klic urceny k podepisovani komunikace
  267.      * @param string $iconUrl - URL obrazku tlacitka
  268.      *
  269.      * @return HTML kod platebniho tlacitka
  270.      */
  271.     function createHref(
  272.             $buyerGoId,
  273.             $totalPrice,
  274.             $productName,
  275.             $variableSymbol,
  276.             $successURL,
  277.             $failedURL,
  278.             $secret,
  279.             $iconUrl
  280.             ){
  281.  
  282.         $encryptedSignature = ButtonHelper::encrypt(
  283.                 ButtonHelper::hash(
  284.                         ButtonHelper::concatPaymentCommand(
  285.                         $buyerGoId,
  286.                         $productName,
  287.                         $totalPrice,
  288.                         $variableSymbol,
  289.                         $failedURL,
  290.                         $successURL,
  291.                         $secret)
  292.                     ), $secret);
  293.  
  294.         $params = "";
  295.         $params .= "paymentCommand.buyerGoId=" . $buyerGoId;
  296.         $params .= "&paymentCommand.totalPrice=" . $totalPrice;
  297.         $params .= "&paymentCommand.productName=" . urlencode($productName);
  298.         $params .= "&paymentCommand.variableSymbol=" . urlencode($variableSymbol);
  299.         $params .= "&paymentCommand.successURL=" . urlencode($successURL);
  300.         $params .= "&paymentCommand.failedURL=" . urlencode($failedURL);
  301.         $params .= "&paymentCommand.encryptedSignature=" . urlencode($encryptedSignature);
  302.  
  303.         $formBuffer = "";
  304.         $formBuffer .= "<a target='_blank' href='" . ButtonHelper::buttonURL() . "?" . $params . "' >";
  305.         $formBuffer .= " <img src='" . $iconUrl . "' border='0' style='border:none;'/> ";
  306.         $formBuffer .= "</a>";
  307.  
  308.         return $formBuffer;
  309.     }
  310. }
  311. ?>
  312.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement