Advertisement
Guest User

Untitled

a guest
Jul 19th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.42 KB | None | 0 0
  1. <?
  2. define(PLATNOSCI_POS_ID, $pos[0]);
  3. define(PLATNOSCI_KEY1, $klucz1[0]);
  4. define(PLATNOSCI_KEY2, $klucz2[0]);
  5.  
  6.  
  7. function get_status($parts)
  8. {
  9.     if ($parts[1] != PLATNOSCI_POS_ID) return array('code' => false, 'message' => 'wrong POS number');  //--- bledny numer POS
  10.     $sig = md5($parts[1] . $parts[2] . $parts[3] . $parts[5] . $parts[4] . $parts[6] . $parts[7] . PLATNOSCI_KEY2);
  11.     if ($parts[8] != $sig) return array('code' => false, 'message' => 'incorrect signature');  //--- bledny podpis
  12.     switch ($parts[5]) {
  13.         case 1:
  14.             return array('code' => $parts[5], 'message' => 'new');
  15.             break;
  16.         case 2:
  17.             return array('code' => $parts[5], 'message' => 'cancelled');
  18.             break;
  19.         case 3:
  20.             return array('code' => $parts[5], 'message' => 'rejected');
  21.             break;
  22.         case 4:
  23.             return array('code' => $parts[5], 'message' => 'started');
  24.             break;
  25.         case 5:
  26.             return array('code' => $parts[5], 'message' => 'awaiting receipt');
  27.             break;
  28.         case 6:
  29.             return array('code' => $parts[5], 'message' => 'no authorization');
  30.             break;
  31.  
  32.         case 7:
  33.             return array('code' => $parts[5], 'message' => 'payment rejected');
  34.             break;
  35.         case 99:
  36.             return array('code' => $parts[5], 'message' => 'payment received - ended');
  37.             break;
  38.         case 888:
  39.             return array('code' => $parts[5], 'message' => 'incorrect status');
  40.             break;
  41.         default:
  42.             return array('code' => false, 'message' => 'no status');
  43.             break;
  44.     }
  45. }
  46.  
  47.  
  48. if (!isset($_POST['pos_id']) || !isset($_POST['session_id']) || !isset($_POST['ts']) || !isset($_POST['sig'])) die('ERROR: EMPTY PARAMETERS'); //-- brak wszystkich parametrow
  49.  
  50. if ($_POST['pos_id'] != PLATNOSCI_POS_ID) die('ERROR: WRONG POS ID');   //--- błędny numer POS
  51.  
  52. $sig = md5($_POST['pos_id'] . $_POST['session_id'] . $_POST['ts'] . PLATNOSCI_KEY2);
  53. if ($_POST['sig'] != $sig) die('ERROR: WRONG SIGNATURE');   //--- błędny podpis
  54.  
  55. $ts = time();
  56. $sig = md5(PLATNOSCI_POS_ID . $_POST['session_id'] . $ts . PLATNOSCI_KEY1);
  57. $parameters = "pos_id=" . PLATNOSCI_POS_ID . "&session_id=" . $_POST['session_id'] . "&ts=" . $ts . "&sig=" . $sig;
  58.  
  59. $fsocket = false;
  60. $curl = false;
  61. $result = false;
  62.  
  63. if ((PHP_VERSION >= 4.3) && ($fp = @fsockopen('ssl://' . $server, 443, $errno, $errstr, 30))) {
  64.     $fsocket = true;
  65. } elseif (function_exists('curl_exec')) {
  66.     $curl = true;
  67. }
  68.  
  69. if ($fsocket == true) {
  70.     $header = 'POST ' . $server_script . ' HTTP/1.0' . "\r\n" .
  71.         'Host: ' . $server . "\r\n" .
  72.         'Content-Type: application/x-www-form-urlencoded' . "\r\n" .
  73.         'Content-Length: ' . strlen($parameters) . "\r\n" .
  74.         'Connection: close' . "\r\n\r\n";
  75.     @fputs($fp, $header . $parameters);
  76.     $platnosci_response = '';
  77.     while (!@feof($fp)) {
  78.         $res = @fgets($fp, 1024);
  79.         $platnosci_response .= $res;
  80.     }
  81.     @fclose($fp);
  82.  
  83. } elseif ($curl == true) {
  84.     $ch = curl_init();
  85.     curl_setopt($ch, CURLOPT_URL, "https://" . $server . $server_script);
  86.     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  87.     curl_setopt($ch, CURLOPT_HEADER, 0);
  88.     curl_setopt($ch, CURLOPT_TIMEOUT, 20);
  89.     curl_setopt($ch, CURLOPT_POST, 1);
  90.     curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters);
  91.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  92.     $platnosci_response = curl_exec($ch);
  93.     curl_close($ch);
  94. } else {
  95.     die("ERROR: No connect method ...\n");
  96. }
  97.  
  98. //if (preg_match('/<trans>s\*<pos_id>([0-9]*)<\/pos_id>\s*<session_id>(.*?)<\/session_id>\s*<order_id>(.*?)<\/order_id>\s*<amount>(\d+)<\/amount>\s*<status>(\d+)<\/status>\s*<desc>(.*?)<\/desc>\s*<ts>(\d+)<\/ts>\s*<sig>([a-z0-9]*)<\/sig>\s*<\/trans>/si', $platnosci_response, $parts))  $result = get_status($parts);
  99.  
  100.  
  101. //if (preg_match('~^<trans>.*<pos_id>([0-9]*)</pos_id>.*<session_id>(.*)</session_id>.*<order_id>(.*)</order_id>.*<amount>([0-9]*)</amount>.*<status>([0-9]*)</status>.*<desc>(.*)</desc>.*<ts>([0-9]*)</ts>.*<sig>([a-z0-9]*)</sig>.*</trans>$~', $platnosci_response, $parts))  $result = get_status($parts);
  102.  
  103.  
  104. //if (preg_match("/<trans>.*<pos_id>([0-9]*)</pos_id>.*<session_id>(.*)</session_id>.*<order_id>(.*)</order_id>.*<amount>([0-9]*)</amount>.*<status>([0-9]*)</status>.*<desc>(.*)</desc>.*<ts>([0-9]*)</ts>.*<sig>([a-z0-9]*)</sig>.*</trans>/", $platnosci_response, $parts))  $result = get_status($parts);
  105.  
  106. // if (eregi("<trans>.*<pos_id>([0-9]*)</pos_id>.*<session_id>(.*)</session_id>.*<order_id>(.*)</order_id>.*<amount>([0-9]*)</amount>.*<status>([0-9]*)</status>.*<desc>(.*)</desc>.*<ts>([0-9]*)</ts>.*<sig>([a-z0-9]*)</sig>.*</trans>", $platnosci_response, $parts))  $result = get_status($parts);
  107.  
  108. $parts = array();
  109. if (preg_match_all("/<trans>\s*<pos_id>([0-9]*)<\/pos_id>\s*<session_id>(.*)<\/session_id>\s*<order_id>(.*)<\/order_id>\s*<amount>([0-9]*)<\/amount>\s*<status>([0-9]*)<\/status>\s*<desc>(.*)<\/desc>\s*<ts>([0-9]*)<\/ts>\s*<sig>([a-z0-9]*)<\/sig>\s*<\/trans>/smi", $platnosci_response, $output_array)) {
  110.     foreach ($output_array as $index => $item) {
  111.         if ($index > 0 && isset($item[0])) {
  112.             $parts[$index] = $item[0];
  113.         }
  114.     }
  115. }
  116.  
  117. $result = get_status($parts);
  118.  
  119. if ($result['code']) {  //--- rozpoznany status transakcji
  120.  
  121.     $pos_id = $parts[1];
  122.     $session_id = $parts[2];
  123.     $order_id = $parts[3];
  124.     $amount_grosze = $parts[4];  //-- w groszach
  125.     $status = $parts[5];
  126.     $desc = $parts[6];
  127.     $ts = $parts[7];
  128.     $sig = $parts[8];
  129.  
  130.  
  131.     $transid = $parts[2];
  132.  
  133.  
  134.     $invoiceid = $parts[3];
  135.     $amount = $amount_grosze / 100;
  136.     $fee = 0;
  137.  
  138.     /* TODO: zmiana statusu transakcji w systemie Sklepu */
  139.  
  140.  
  141.     if ($result['code'] == '99') {
  142.  
  143.         if ($licencja["status"] == "Active") {
  144.             # Allow Script to Run
  145.  
  146.             checkCbTransID($transid);
  147.  
  148.             addInvoicePayment($invoiceid, $transid, $amount, $fee, $gatewaymodule);
  149.             logTransaction($GATEWAY["payu"], $_POST, "Successful");
  150.  
  151.  
  152.             if ($licencja["localkey"]) {
  153.                 # Save Updated Local Key to DB or File
  154.                $localkeydata = $licencja["localkey"];
  155.                 $fh = fopen($key_file, 'w') or die("can't find local key!");
  156.  
  157.                 fwrite($fh, $localkeydata);
  158.                 fclose($fh);
  159.  
  160.             }
  161.             echo "OK";
  162.             exit;
  163.         } elseif ($licencja["status"] == "Invalid") {
  164.             # Show Invalid Message
  165.            echo "Invalid";
  166.         } elseif ($licencja["status"] == "Expired") {
  167.             # Show Expired Message
  168.            echo "Expired";
  169.         } elseif ($licencja["status"] == "Suspended") {
  170.             # Show Suspended Message
  171.            echo "Suspended";
  172.         }
  173.  
  174.  
  175.         /*
  176.                 $postfields["username"] = $username;
  177.             $postfields["password"] = md5($password);
  178.             $postfields["action"] = "addinvoicepayment";
  179.             $postfields["invoiceid"] = $faktura;
  180.             $postfields["transid"] = $sig;
  181.             $postfields["gateway"] = "platnosci";
  182.             //$postfields["amount"] = $kwota;
  183.  
  184.             $ch = curl_init();
  185.             curl_setopt($ch, CURLOPT_URL, $url);
  186.             curl_setopt($ch, CURLOPT_POST, 1);
  187.             curl_setopt($ch, CURLOPT_TIMEOUT, 100);
  188.             curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  189.             curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
  190.             $data = curl_exec($ch);
  191.             curl_close($ch);
  192.  
  193.  
  194.             $data = explode(";",$data);
  195.             foreach ($data AS $temp) {
  196.           $temp = explode("=",$temp);
  197.           $results[$temp[0]] = $temp[1];
  198.  
  199.  
  200.             }
  201.         */
  202.  
  203.     } else if ($result['code'] == '2') {
  204.         // transakcja anulowana mozemy również anulować zamowienie
  205.     } else {
  206.         // inne akcje
  207.     }
  208.  
  209.     // jezeli wszytskie operacje wykonane poprawnie wiec odsylamy ok
  210.     // w innym przypadku należy wygenerować błąd
  211.     // if ( wszystko_ok ) {
  212.     echo "OK";
  213.     exit;
  214.     // } else {
  215.     //
  216.     // }
  217.  
  218.  
  219. } else {
  220.     /* TODO: obsługa powiadamiania o błędnych statusach transakcji*/
  221.     echo "ERROR: data error ....\n";
  222.     echo "code=" . $result['code'] . " message=" . $result['message'] . "\n";
  223.     echo $platnosci_response;
  224.     // powiadomienie bedzie wysłane ponownie przez platnosci.pl
  225.     // ewentualnie dodajemy sobie jakis wpis do logow ...
  226. }
  227.  
  228.  
  229. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement