Paypal IPN

pippuccio76
Contributor
Contributor

HI , sorry for  english , i have a problem with Paypal IPN , I test it on sandbox , the payment work fine , i have the retun to my site . When Paypal return message to me (IPN)   the response is VERIFIED but it send me some message instead only one and if i see the cronology the message is faled .
This is  the code :

[php]public function ipn()
   {
       $this->paypal_lib->log_ipn_test('########################## NUOVO ACCESSO IPN ############################## ');


       if ( ! $this->input->post()) {
           
           $this->paypal_lib->log_ipn_test('Missing POST Data');

           throw new Exception("Missing POST Data");
       }

 

       $raw_post_data = file_get_contents('php://input');
       $raw_post_array = explode('&', $raw_post_data);
       $myPost = array();
       foreach ($raw_post_array as $keyval) {
           $keyval = explode('=', $keyval);
           if (count($keyval) == 2) {
               // Since we do not want the plus in the datetime string to be encoded to a space, we manually encode it.
               if ($keyval[0] === 'payment_date') {
                   if (substr_count($keyval[1], '+') === 1) {
                       $keyval[1] = str_replace('+', '%2B', $keyval[1]);
                   }
               }
               $myPost[$keyval[0]] = urldecode($keyval[1]);
           }
       }
       // Build the body of the verification post request, adding the _notify-validate command.
       $req = 'cmd=_notify-validate';
       $get_magic_quotes_exists = false;
       if (function_exists('get_magic_quotes_gpc')) {
           $get_magic_quotes_exists = true;
       }
       foreach ($myPost as $key => $value) {
           if ($get_magic_quotes_exists == true && get_magic_quotes_gpc() == 1) {
               $value = urlencode(stripslashes($value));
           } else {
               $value = urlencode($value);
           }
           $req .= "&$key=$value";

       }

       $this->paypal_lib->log_ipn_test($req);
       
       // Post the data back to PayPal, using curl. Throw exceptions if errors occur.
       $ch = curl_init($this->ipn_paypal_url);
       
       $this->paypal_lib->log_ipn_test('CH:'.$ch);
       
       curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
       curl_setopt($ch, CURLOPT_POST, 1);
       curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
       curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
       curl_setopt($ch, CURLOPT_SSLVERSION, 6);
       curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
       curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
       // This is often required if the server is missing a global cert bundle, or is using an outdated one.
       if ($this->use_local_certs) {
           curl_setopt($ch, CURLOPT_CAINFO, __DIR__ . "/cert/cacert.pem");
       }
       curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
       curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
       curl_setopt($ch, CURLOPT_HTTPHEADER, array(
           'User-Agent: PHP-IPN-Verification-Script',
           'Connection: Close',
       ));

       $res = curl_exec($ch);
       $this->paypal_lib->log_ipn_test('RES:'.$res);

       if ( ! ($res)) {
           $errno = curl_errno($ch);
           $errstr = curl_error($ch);
           curl_close($ch);

           $this->paypal_lib->log_ipn_test("cURL error: [$errno] $errstr");

           throw new Exception("cURL error: [$errno] $errstr");
       }

       $info = curl_getinfo($ch);

       $this->paypal_lib->log_ipn_test("INFO: ".print_r($info));

       $http_code = $info['http_code'];

       $this->paypal_lib->log_ipn_test("http_code: ".$http_code);

       if ($http_code != 200) {

           $this->paypal_lib->log_ipn_test("PayPal responded with http code $http_code");
           throw new Exception("PayPal responded with http code $http_code");
       }
       


       // Reply with an empty 200 response to indicate to paypal the IPN was received correctly.
       header("HTTP/1.1 200 OK");


       
       // inspect IPN validation result and act accordingly
       if (strcmp ($res, "VERIFIED") == 0) {


               // The IPN is verified, process it


               foreach($this->input->post() as $key => $value) {
                     
                   $this->paypal_lib->log_ipn_test( $key . " = " . $value );
               }


               
               // Paypal return transaction details array
               $paypalInfo = $this->input->post();

               $azienda_id     = $paypalInfo['custom'];
               $preventivo_id  = $paypalInfo["item_number"];
               $txn_id         = $paypalInfo["txn_id"];
               $payment_gross  = $paypalInfo["mc_gross"];
               $currency_code  = $paypalInfo["mc_currency"];
               $payer_email    = $paypalInfo["payer_email"];
               $payment_status = $paypalInfo["payment_status"];

 

 

               $this->paypal_lib->log_ipn_test("RES VERIFIED");


               // Insert the transaction data into the database
               if($this->preventivi_paypal_model->insertTransaction(


                   $azienda_id,
                   $preventivo_id,
                   $txn_id,
                   $payment_gross,
                   $currency_code,
                   $payer_email,
                   $payment_status


               ) ){

                   $this->paypal_lib->log_ipn_test('Inserita transazione nel db ');

 

                   $this->paypal_lib->log_ipn_test('pagamento verificato');
                   $this->paypal_lib->log_ipn_test('azienda_id:'.$paypalInfo['custom']);
                   $this->paypal_lib->log_ipn_test('preventivo_id:'.$paypalInfo['item_number']);
                   $this->paypal_lib->log_ipn_test('txn_id:'.$paypalInfo['txn_id']);
                   $this->paypal_lib->log_ipn_test('mc_gross:'.$paypalInfo['mc_gross']);
                   $this->paypal_lib->log_ipn_test('mc_currency:'.$paypalInfo['mc_currency']);
                   $this->paypal_lib->log_ipn_test('payer_email:'.$paypalInfo['payer_email']);
                   $this->paypal_lib->log_ipn_test('payment_status:'.$paypalInfo['payment_status']);


                   #############################################
                   //Invio mail a cliente
                   #############################################

                   //recupero id del lavoro dal preventivo
                   $preventivo=$this->preventivi_inviati_aziende_model
                                    ->get_Preventivi_inviati_aziende($paypalInfo['item_number']);

                   //recupero id del cliente dal lavoro
                   $lavoro=$this->lavori_model->get_Lavori($preventivo->id_lavori);


                   //recupero i dati del cliente
                   $cliente=$this->clienti_model->get_Clienti($lavoro->id_clienti);

                   //invio mail a cliente

                   if($this->send_mail_cliente_preventivo_pagato_paypal($cliente->email) ){

                       $this->paypal_lib->log_ipn_test('mail inviata al cliente : '.$cliente->email);

                   }

                   

                   ############################################


                   #############################################
                   //Cambio lo stato del preventivo
                   #############################################

                   if($this->preventivi_inviati_aziende_model->set_stato($preventivo->id,4)){

                       $this->paypal_lib->log_ipn_test('Cambio stato preventivo (4) : '.$preventivo->id);

                   
                   }

 

                   #############################################

                   #############################################
                   //cambiare lo stato del lavoro
                   #############################################

                   if($this->lavori_model->set_stato($preventivo->id_lavori,4)){


                       $this->paypal_lib->log_ipn_test('Stato lavoro modificato : '.$preventivo->id_lavori);
                   
                   }


                   #############################################
                   //cambio il tipo di pagamento nel preventivo
                   #############################################


                    if($this->preventivi_inviati_aziende_model->set_metodo_pagamento($preventivo->id,2)){

                       $this->paypal_lib->log_ipn_test('Cambio stato metodo pagamento (2) : '.$preventivo->id);

                   
                   }


                   #############################################

                   //TODO
                   //eliminare pulsanti paypal e bonifico


                   #############################################
                   //Invio mail a admin
                   #############################################

                   if($this->send_mail_admin_preventivo_pagato_paypal($paypalInfo["item_number"]) ) {

                       $this->paypal_lib->log_ipn_test('Mail inviata a admin : '.$preventivo->id_lavori);


                   }

 

                   #############################################
                   //Invio mail a azienda dai del cliente
                   #############################################

                   $azienda=$this->aziende_model->get_Aziende($paypalInfo['custom']);


                   if($this->invia_mail_azienda_abilitato_visualizzazione_cliente($azienda->email,$cliente) ){

                       $this->paypal_lib->log_ipn_test('mail inviata all\'azienda : '.$azienda->email);

                   }

 

                   ############################################
                   // INSERisci transazione in preventivo
                   #############################################

                     if($this->preventivi_inviati_aziende_model->inserisci_transazione_paypal($txn_id)){

                       $this->paypal_lib->log_ipn_test('Transazione inserita in preventivi');

                   
                   }

                   #############################################

 

       
          }    
           
       } else {


           $this->paypal_lib->log_ipn_test('NOT VALID '.$res);
           return false;
       }

 

   } [/php]


[size=small][font=Monaco, Consolas, Courier, monospace]$this->paypal_lib->log_ipn_test($sometxet) only write in a log...[/font][/size]

Login to Me Too
0 REPLIES 0

Haven't Found your Answer?

It happens. Hit the "Login to Ask the community" button to create a question for the PayPal community.