Paga.php
Codice PHP:
<?php


include('config.php');

$paypal_email "giogio993@gmail.com"#  Email de paypal

require_once('includes//class.Payments.php');  # classe php
$p = new class_payments;
$p->paypal_url =  'https://www.paypal.com/cgi-bin/webscr';# paypal url

$this_script  "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];

function  
logerror_mr($time,$error,$sql,$file){
   
$newrow =  $time.",".$error.",".$file.",".$sql."\n";
   
$csv =  fopen('paypal_error.csv''a');
   
fwrite($csv$newrow);
    
fclose($csv);
}

foreach (
$_REQUEST['formDoor'] as $checkbox)
//Costo
#  Aqui ponemos los costos y la cantidad
$amount=$checkbox;
if  (
$amount == 10000){  # cantidad del recurso
   
$cost='3.00'# precio  real
   
}
if (
$amount == 80000){
   
$cost='7.50';
   }
if  (
$amount == 110000){
   
$cost='10.00';
}
if (
$amount ==  250000){
   
$cost='20.00';
}
if (
$amount == 350000){
    
$cost='25.00';
}
if (
$amount == 1000000){
   
$cost='40.00';
}

switch  (
$_GET['action']) {
case 
'success':
   
// Order was successful...

    
if(intval($_GET['id']) == $user['id']){
      
$x =  $user['username'];
   }else{
      
$x 'id  '.intval($_GET['id']);
   }

   echo  
"<html><head><title>ok</title></head><body><h3>Grazie.</h3>";
   
//foreach ($_POST as $key =>  $value) { echo "$key: $value
"; }
    echo (
$_GET['amount'])." qui scrivi un piccolo messaggio ".$x." hai comprato tot " ;
   //echo "
<a  href=\"./game.php?page=".overview."\">Continue</a>";
   echo  
"</body></html>";

   break;

case 
'cancel':
    
// Order was canceled...

  
echo "<html><head><title>Cancella</title></head><body><h3>Il contratto viene annullato.</h3>
è ora possibile chiudere questa finestra.
"


   
//echo "<a  href=\"./game.php?page=".overview."\">Continuar</a>";
    
echo "</body></html>";

   break;

   if  (
$p->validate_ipn()) {
      
// Payment has been recieved and IPN  is verified.  This is where you

      // For this example, we'll  just email ourselves ALL the data.
   
$subject 'Notifica immediata del pagamento - Pagamento ricevuto';
      
$to $paypal_email;   //   your email
      
$body =  "Notifica inviata del pagamento\n";
      
$body  .= "from ".$p->ipn_data['payer_email']." (GET =  ".$_GET['id'].")(USER = ".$user['id'].") on ".date('m/d/Y');
       
$body .= " at ".date('g:i A')."\n\nDetagli:\n";

      foreach  (
$p->ipn_data as $key => $value) { $body .= "\n$key$value"; }
       
mail($to$subject$body);

      
$payer_email =  $p->ipn_data['payer_email'];
      
$newrow // qui crei la query che deve dare qualcosa;
       
doquery($newrow,'tabella_utente o quella che sia') or  logerror_mr(time(),mysql_error(),$newrow,__FILE__.",".__LINE__);
   }
    break;

default:
define('Uni' 'Delta');
   
$p->add_field('business',  $paypal_email);
   
$p->add_field('return'$this_script.'  &action=success&id='.$user['id'].'&amount='.$amount);
    
$p->add_field('cancel_return'$this_script.'&action=cancel');
    
$p->add_field('notify_url',  $this_script.'&action=ipn&id='.$user['id'].'&amount='.$amount);
    
$p->add_field('item_name', ($amount).' Materia oscura user: '.$user['username']);
    
$p->add_field('item_number'$amount.'_Materia oscura');
    
$p->add_field('amount'$cost);
    
$p->add_field  ('action',$action);
   
$p->add_field ('currency_code''EUR'); ///  put here your country payment euro, bp, dollars, enz enz 'EURO'
    
$p->submit_paypal_post(); // submit the fields to paypal
    //$p->dump_fields();     // for debugging, output a table of all the  fields
   
break;

}
?>
Classe paypal

Codice PHP:
<?php

class class_payments {
    
    var 
$last_error;
   var 
$ipn_log;
   var 
$ipn_log_file;
    var 
$ipn_response;
   var 
$ipn_data = array();
   var 
$fields =  array();

   function 
class_payments() {
       
$this->paypal_url 'https://www.paypal.com/cgi-bin/webscr';
       
$this->last_error '';
      
      
$this->ipn_log_file =  'paypal_error.log';
      
$this->ipn_log true
       
$this->ipn_response '';
      
       
$this->add_field('rm','2');
       
$this->add_field('cmd','_xclick'); 
      
   }
   
    function 
add_field($field$value) {
       
$this->fields["$field"] = $value;
   }

   function  
submit_paypal_post() {

      echo 
"<html>\n";
      echo  
"<head><link rel=\"stylesheet\"  href=\"skins/SpaceWars/formate.css\"  type=\"text/css\"><title>Caricando....</title></head>\n";
      echo 
"<body  onLoad=\"document.forms['paypal_form'].submit();\">\n";
      echo 
"<center><h2>Attendere prego, il tuo ordine è in fase di elaborazione e";
      echo 
" sarete reindirizzati al sito web di PayPal.</h2></center>\n";
      echo 
"<form  method=\"post\" name=\"paypal_form\" ";
      echo  
"action=\"".$this->paypal_url."\">\n";

      foreach  (
$this->fields as $name => $value) {
         echo 
"<input  type=\"hidden\" name=\"$name\" value=\"$value\"/>\n";
      }
      echo 
"<center>

Se non sei automaticamente reindirizzato a paypal entro 5 secondi 

\n"
;
      echo 
"<input type=\"submit\" value=\"Clicca qui\"></center>\n";
      
       echo 
"</form>\n";
      echo  
"</body></html>\n";
    
   }
   
   function  
validate_ipn() {

       
$url_parsed=parse_url($this->paypal_url);        

       
$post_string '';    
      foreach (
$_POST as $field=>$value) { 
          
$this->ipn_data["$field"] = $value;
         
$post_string  .= $field.'='.urlencode(stripslashes($value)).'&'
      }
       
$post_string.="cmd=_notify-validate";
      
$fp =  fsockopen($url_parsed[host],"80",$err_num,$err_str,30);

       if(!
$fp) {
         
$this->last_error "fsockopen error no.  $errnum$errstr";
         
$this->log_ipn_results(false);       
          return 
false;
      } else { 
         
fputs($fp"POST  $url_parsed[path] HTTP/1.1\r\n");
         
fputs($fp"Host:  $url_parsed[host]\r\n"); 
         
fputs($fp"Content-type:  application/x-www-form-urlencoded\r\n"); 
         
fputs($fp,  "Content-length: ".strlen($post_string)."\r\n"); 
         
fputs($fp,  "Connection: close\r\n\r\n"); 
         
fputs($fp$post_string .  "\r\n\r\n"); 

         while(!
feof($fp)) {
             
$this->ipn_response .= fgets($fp1024); 
         }
          
fclose($fp);

      }
      
      if  (
eregi("VERIFIED",$this->ipn_response)) {
  
          
$this->log_ipn_results(true);
         return 
true;       
       } else {
         
$this->last_error 'IPN Validation Failed.';
          
$this->log_ipn_results(false);   
         return 
false;
       }
    }
   
   function 
log_ipn_results($success) {
        
      if (!
$this->ipn_log) return;
      
      
$text =  '['.date('m/d/Y g:i A').'] - ';

      if (
$success$text .=  "SUCCESS!\n";
      else 
$text .= 'FAIL: '.$this->last_error."\n";

       
$text .= "IPN POST Vars from Paypal:\n";
      foreach  (
$this->ipn_data as $key=>$value) {
         
$text .=  "$key=$value, ";
      }
 
     
$text .= "\nIPN Response from  Paypal Server:\n ".$this->ipn_response;
       
$fp=fopen($this->ipn_log_file,'a');
      
fwrite($fp$text .  "\n\n"); 

      
fclose($fp);  // close file
   
}

    function 
dump_fields() {

      echo  
"<h3>paypal_class->dump_fields() Output:</h3>";
       echo 
"<table width=\"95%\" border=\"1\" cellpadding=\"2\"  cellspacing=\"0\">
            <tr>
               <td  bgcolor=\"black\">[b]<font color=\"white\">Field  Name</font>[/b]</td>
               <td  bgcolor=\"black\">[b]<font  color=\"white\">Value</font>[/b]</td>
             </tr>"

      
      
ksort($this->fields);
       foreach (
$this->fields as $key => $value) {
         echo  
"<tr><td>$key</td><td>".urldecode($value)."</td></tr>";
       }
 
      echo 
"</table>
"

   }
}          

?>

Html :
Codice PHP:
 <tr>
    <
form action="paga.php" method="POST">
    <
td class="c">Que cantidad de creditos desea comprar?


    <
input type="radio" name="formDoor[]" value="10000" CHECKED />10.000 X   (3,00€)

    <
input type="radio" name="formDoor[]" value="80000" />80.000 X  7,50€

    <
input type="radio" name="formDoor[]" value="110000" />110.000 X (10,00€

    <
input type="radio" name="formDoor[]" value="250000" />250.000 X (20,00€

    <
input type="radio" name="formDoor[]" value="350000" />350.000 X (25,00€

    <
input type="radio" name="formDoor[]" value="1000000" />1.000.000 X (40,00€


 <
center><input type="submit" name="formSubmit" value="Compra" /></center>
 </
td>
 </
form