home *** CD-ROM | disk | FTP | other *** search
/ 79.96.193.101 / 79.96.193.101.tar / 79.96.193.101 / www / paypal_callback.php < prev    next >
PHP Script  |  2014-10-20  |  6KB  |  159 lines

  1. <?php
  2. //============================================
  3. // Easy 2.0
  4. // Copyright     : .exe Evolution
  5. // Wersja        : 2.0
  6. // Uwagi         : Brak
  7. //============================================
  8. define("BASEDIR", dirname(__FILE__));
  9. include(BASEDIR . "/inc/init.php");
  10.  
  11. $tmpl = new Easy("templates/$THEME/");
  12. include(BASEDIR . "/system/shop/shopinc.php");
  13.  
  14. $sql_pp = $db->Query("SELECT inst_id FROM ".PREFIX."_shop_payment_methods WHERE id='5'");
  15. $row_pp = $sql_pp->fetchrow();
  16. define("PEMAIL", $row_pp->inst_id);
  17.  
  18.     $paypal_mail = PEMAIL;
  19.         $req = 'cmd=_notify-validate';
  20.  
  21.     foreach($_POST as $key => $value){
  22.         $value = urlencode(stripslashes($value));
  23.         $req .= "&$key=$value";
  24.     }
  25.  
  26.     $header .= "POST /cgi-bin/webscr HTTP/1.0\r\n";
  27.     $header .= "Content-Type: application/x-www-form-urlencoded\r\n";
  28.     $header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
  29.     $fp = fsockopen('www.paypal.com', 80, $errno, $errstr, 30);
  30.  
  31.         $item_name = $_POST['item_name'];
  32.     $item_number = $_POST['item_number'];
  33.     $payment_status = $_POST['payment_status'];
  34.     $payment_amount = str_replace(',','.',$_POST['mc_gross']);
  35.     $payment_currency = $_POST['mc_currency'];
  36.     $txn_id = $_POST['txn_id'];
  37.     $receiver_email = $_POST['receiver_email'];
  38.     $payer_email = $_POST['payer_email'];
  39.  
  40. if(!$fp)
  41. {
  42.         PutLog("PayPal payment: could not connect to www.paypal.com:80", PRIO_ERROR, __FILE__, __LINE__);
  43. }
  44. else
  45. {
  46.     fputs ($fp, $header . $req);
  47.     while (!feof($fp))
  48.     {
  49.         $res = fgets ($fp, 1024);
  50.         if(strcmp($res, 'VERIFIED') == 0)
  51.         {
  52.                         if(strcmp($_REQUEST['payment_status'], 'Completed') == 0)
  53.             {
  54.                                         if(trim(strtolower($_REQUEST['receiver_email'])) == trim(strtolower($paypal_mail)))
  55.                     {
  56.                                                 $cartId = $item_number;
  57.  
  58.                         $q = "
  59.                             SELECT
  60.                                 a.*,
  61.                                 b.email
  62.                             FROM
  63.                                 " . PREFIX . "_shop_orders as a,
  64.                                 " . PREFIX . "_user as b
  65.                             WHERE
  66.                                 a.control = '$cartId' AND
  67.                                 a.ovall = ".$payment_amount." AND
  68.                                 a.status_o != 'failed' AND
  69.                                 a.status_o != 'ok' AND
  70.                                 b.uid = a.uid
  71.                             ";
  72.  
  73.                             $sql = $db->Query($q);
  74.                             $row_u = $sql->fetchrow();
  75.                             $uemail = $row_u->email;
  76.  
  77.                             if($sql->numrows() == 1){
  78.                                                                                                                                 $sql_first = $db->Query("UPDATE ". PREFIX ."_shop_orders SET status_o = 'progress' WHERE control = '$cartId' ");
  79.                                 $items = explode(',', $row_u->articles);
  80.                                                                 $download_files = '';
  81.                                 foreach($items as $item){
  82.                                     $sql_esd = $db->Query("SELECT id,artnumber,esd_download,esd_dltimes,esd_timespan FROM " . PREFIX . "_shop_articles WHERE artnumber='$item'");
  83.                                     $row_esd = $sql_esd->fetchrow();
  84.                                     $esd_timespan = ($row_esd->esd_timespan=='') ? 0 : $row_esd->esd_timespan ;
  85.  
  86.                                     $time_end = mktime(date("H")+$esd_timespan, date("i"), date("s"),date("m"), date("d"), date("Y"));
  87.                                     if($row_esd->esd_download != '') {
  88.                                         $itemkey = makepass(10);
  89.                                         $sql_newesd = $db->Query("INSERT INTO " . PREFIX . "_shop_esd (id,uid,dltimes,dlid,dltimespan,itemkey) VALUES ('','$userId','','$row_esd->id','$time_end','$itemkey')");
  90.  
  91.                                         $download_files .= "\n---------------------------------------\n$row_esd->esd_download ($row_esd->esd_dltimes ".$lang['shop_max_downloads_esd'].")\nhttp://" .$_SERVER['HTTP_HOST'] . str_replace('/paypal_callback.php','',$_SERVER['PHP_SELF']) . "/index.php?p=shop&action=downloadfile&file_id=$row_esd->id&itemkey=$itemkey\n";
  92.                                         $download_files_copy .= "\n$row_esd->esd_download";
  93.                                     }
  94.  
  95.                                 }
  96.                                 
  97.                                                                 if($download_files!=''){
  98.                                     $body_first = str_replace("__TRANSID__", $cartId, $lang['shop_subject_cc_transaction_ok_header']);
  99.                                     $mails = $body_first . "\n" . $download_files . "\n" . $lang['shop_footer_ccokesd_2'] . "\n\n" . SHOPADRESS;
  100.  
  101.                                     $thismail = new MIMEMail();
  102.                                     $thismail->from_email = EMAILFROM;
  103.                                     $thismail->subject = $lang['shop_subject_cc_transaction_ok'];
  104.                                     $thismail->to = $uemail;
  105.                                     $thismail->headers();
  106.                                     $thismail->addtextpart( false, $mails);
  107.                                     $thismail->finish();
  108.                                     $thismail->send();
  109.                                 } else {
  110.                                                                     $body_first = str_replace("__TRANSID__", $cartId, $lang['shop_subject_cc_transaction_ok_header2']);
  111.                                     $mails = $body_first . "\n\n" . SHOPADRESS;
  112.  
  113.                                     $thismail = new MIMEMail();
  114.                                     $thismail->from_email = EMAILFROM;
  115.                                     $thismail->subject = $lang['shop_subject_cc_transaction_ok'];
  116.                                     $thismail->to = $uemail;
  117.                                     $thismail->headers();
  118.                                     $thismail->addtextpart( false, $mails);
  119.                                     $thismail->finish();
  120.                                     $thismail->send();
  121.                                 }
  122.  
  123.                                                                 $copybody = $lang['shop_subject_cc_transaction_ok_header_copy'];
  124.                                 $copybody  = str_replace("__OVALL__", $payment_amount,$copybody);
  125.                                 $copybody  = str_replace("__TRANSID__", $cartId ,$copybody);
  126.                                 $copybody  = str_replace("__USERID__", $row_u->uid,$copybody);
  127.                                 $copybody  = str_replace("__UEMAIL__", $uemail,$copybody);
  128.                                 if($download_files_copy!='') $copybody .= "\n\n" . $lang['shop_copy_esdfoot_admin'] . "\n" . $download_files_copy;
  129.  
  130.                                 $thismail = new MIMEMail();
  131.                                 $thismail->from_email = EMAILFROM;
  132.                                 $thismail->subject = $lang['shop_subject_cc_transaction_ok'];
  133.                                 $thismail->to = EMAILTO;
  134.                                 $thismail->headers();
  135.                                 $thismail->addtextpart( false, $copybody);
  136.                                 $thismail->finish();
  137.                                 $thismail->send();
  138.                                                             }
  139.                             else
  140.                             {
  141.                                                             }
  142.  
  143.  
  144.  
  145.                     }
  146.                     else
  147.                     {
  148.                         PutLog("PayPal payment: payment receiver is not \"" . $paypal_mail . "\" " . $_REQUEST['txn_id'] . ")", PRIO_WARNING, __FILE__, __LINE__);
  149.                     }
  150.             }
  151.         }
  152.         else if(strcmp($res, 'INVALID') == 0)
  153.         {
  154.                         PutLog("PayPal payment: got INVALID while trying to validate payment (" . (isset($_REQUEST['txn_id']) ? $_REQUEST['txn_id'] : 'n/a') . ")", PRIO_WARNING, __FILE__, __LINE__);
  155.         }
  156.     }
  157.     fclose ($fp);
  158. }
  159. ?>