Click here to Skip to main content
15,892,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello CodeProject,
I have some scripts for Paypal IPN scripts, but it's not working at all, but i am sure this script is useful for me.
Here is this scripts
This is main file which display all content
Index.php :-

PHP
<script type="text/javascript">
       $(window).on('load', function(){
              var _defaultContent = $('#paypal-load').html();
              $.ajax({
                     async : true ,
                     cache : false ,
                     data : new Object ( ) ,
                     success : function ( content ) {
                            var _response = $('<center />').html(content);
                            $('#paypal-load').html(_response.hide());
                            $('#paypal-load').find('center').fadeIn();
                     } ,
                     url : '<?php echo BaseURLConcat ( 'Paypal/PaypalObject.php' ) ?>' 
              });
              $('input[type="image"][name="submit"]').live('click',function(e){
                     var src = $(this).attr('src');
                     var account = $('input[name="os1"][type="text"]');
                     if(account.val().toString().length === 0){
                            $('#response').html('<span class="inline-help">The account number cannot be empty.</span>');
                            account.focus();
                     }else{
                            var status = false;
                            $.post('<?php echo BaseURLConcat('check-account.php') ?>',{
                                   'isAjax' : 'true' ,
                                   'account' : account.val()
                            },function(data){
                                   var resp = data ;
                                   if(resp == 'false'){
                                         $('#response').html('<span class="inline-help">Account '+account.val()+' does not exists, please insert your valid account.</span>');
                                          account.val(new String()); 
                                   } else {
                                          $('#response').html(new String());
                                          $('#paypal-load center form').submit();
                                   }
                            }) ;
                     }
                     e.preventDefault();
              });
       });
</script>


PAYPAL_OBJECT.PHP (This is the file which linked on index.php) :-
PHP
<?php
       require_once '../Functions/Database.php' ;
       $sendPayData = array (
           "METHOD" => "BMCreateButton" ,
           "VERSION" => "65.2" ,
           "USER" => "frozentalisman_api1.gmail.com" ,
           "PWD" => "B2PBUNCKASY8GSQZ" ,
           "SIGNATURE" => "AbPDCHpF-e45tePhvejMCawkga.bAXLdJIfnQqAbBxpaOu5FbKjXuY90" ,
           "BUTTONCODE" => "ENCRYPTED" ,
           "BUTTONTYPE" => "BUYNOW" ,
           "BUTTONSUBTYPE" => "SERVICES" ,
           "BUTTONCOUNTRY" => "RO" ,
           "BUTTONIMAGE" => "reg" ,
           "BUYNOWTEXT" => "BUYNOW" ,
           "L_BUTTONVAR1" => "item_name=Frozen Talisman D-Points" ,
           "L_BUTTONVAR2" => "currency_code=USD" ,
           "L_BUTTONVAR3" => "no_shipping=2" ,
           "L_BUTTONVAR4" => "no_note=1" ,
           "L_BUTTONVAR5" => "notify_url=http://frozentalisman.com/Paypal/DonateListener.php" ,
       ) ;
       
       $db = GetDatabaseConnection ( 'db_misc' ) ;
       $points = $db->query ( 'SELECT * FROM `t_point_sale` ORDER BY `points`' )->fetchAll ( PDO::FETCH_ASSOC ) ;
       
       $sendPayData [ "OPTION0NAME" ] = "D-Points" ;
       foreach ( $points as $i => $point ) {
              $sendPayData [ sprintf ( 'L_OPTION0SELECT%d' , $i ) ] = sprintf ( '%d D-Points' , $point [ 'points' ] ) ;
              $sendPayData [ sprintf ( 'L_OPTION0PRICE%d' , $i ) ] = $point [ 'price' ] ;
       }
       $sendPayData [ "L_TEXTBOX0" ] = "Account" ;
       
       $curl = curl_init ( ) ;
       curl_setopt ( $curl , CURLOPT_RETURNTRANSFER , true ) ;
       curl_setopt ( $curl , CURLOPT_SSL_VERIFYPEER , false ) ;
       curl_setopt ( $curl , CURLOPT_URL , 'https://api-3t.paypal.com/nvp?' . http_build_query ( $sendPayData ) ) ;
       $nvpPayReturn = curl_exec ( $curl ) ;
       curl_close ( $curl ) ;
       
       
       $output = array ( ) ;
       parse_str ( $nvpPayReturn , $output ) ;
       
       if ( isset ( $output [ "WEBSITECODE" ] ) ) {
              $html = $output [ "WEBSITECODE" ] ;
              echo $html ; 
       }


DONATELISTENER.PHP :-

PHP
<?php
       
       require_once 'DonateIPNHandler.php' ;
       require_once 'InstantPaymentNotification.php' ;

       $ipn = new InstantPaymentNotification ( true ) ;
       $ipn->setIPNHandler ( new DonateIPNHandler ( ) ) ;
       $ipn->listen ( ) ;


DonateIPNHandler.php :-

PHP
<?php

       require_once 'IPNHandler.php' ;
       require_once '../Functions/Database.php' ;
       require_once 'PaypalPayment.php' ;
       
       /**
        * Manipulador de exemplo de Notificação de Pagamento
        * Instantâneo
        */
       class DonateIPNHandler implements IPNHandler {
              
              private $db , $query ;
              /**
               * @param   boolean $isVerified
               * @param   array $message
               * @see     IPNHandler::handle()
               */
              public function handle ( $isVerified , array $message ) {
                     if ( $isVerified ) { 
                            if ( $message [ 'receiver_email' ] == 'frozentalisman@gmail.com' ) {
                                   //file_put_contents('test.text', print_r($message, true), FILE_APPEND);
                                   $data = array ( 
                                       'first_name' => $message [ 'first_name' ] ,
                                       'last_name' => $message [ 'last_name' ] ,
                                       'address_country' => $message [ 'address_country' ] ,
                                       'address_city' => $message [ 'address_city' ] ,
                                       'quantity' => ( int ) $message [ 'quantity' ] ,
                                       'payment_status' => $message [ 'payment_status' ] ,
                                       'item_name' => $message [ 'item_name' ] ,
                                       'mc_currency' => $message [ 'mc_currency' ] ,
                                       'gross' => $message [ 'payment_gross' ] ,
                                       'acct' => array (
                                           'account' => $message [ 'option_selection2' ] ,
                                           'dpoints' => preg_replace ( '/[^\d]/' , null , $message [ 'option_selection1' ] )
                                       ) ,
                                       'check' => $message [ 'option_selection1' ] ,
                                       'email' => $message [ 'payer_email' ] ,
                                       'ID' => $message [ 'ipn_track_id' ]
                                   ) ;
                                   $paypalPayment = new PaypalPayment ( GetDatabaseConnection ( 'db_misc' ) ) ;
                                   $_validate = $paypalPayment->isValid ( $data ) ;
                                   if ( $_validate [ "isValid" ] === true ) {
                                          $paypalPayment->storePayment ( $data )->dispatch ( ) ;
                                   }
                            }
                     }
              }
              
              
       }


InstantPaymentNotification.php :-

PHP
<?php

       class InstantPaymentNotification {

              private $endpoint = 'https://www.paypal.com' ;

              public function __construct ( $sandbox = false ) {
                     if ( $sandbox === false ) {
                            $this->endpoint = 'https://www.sandbox.paypal.com' ;
                     }
                     $this->endpoint .= '/cgi-bin/webscr?cmd=_notify-validate' ;
              }

              public function listen () {
                     if ( $this->ipnHandler !== null ) {
                            if ( $_SERVER[ 'REQUEST_METHOD' ] == 'POST' ) {
                                   if ( filter_input ( INPUT_POST , 'receiver_email' , FILTER_VALIDATE_EMAIL ) ) {
                                          $curl = curl_init ( ) ;

                                          curl_setopt ( $curl , CURLOPT_URL , $this->endpoint ) ;
                                          curl_setopt ( $curl , CURLOPT_SSL_VERIFYPEER , false ) ;
                                          curl_setopt ( $curl , CURLOPT_RETURNTRANSFER , 1 ) ;
                                          curl_setopt ( $curl , CURLOPT_POST , 1 ) ;
                                          curl_setopt ( $curl , CURLOPT_POSTFIELDS , http_build_query ( $_POST ) ) ;

                                          $response = curl_exec ( $curl ) ;
                                          $error = curl_error ( $curl ) ;
                                          $errno = curl_errno ( $curl ) ;

                                          curl_close ( $curl ) ;

                                          if ( empty ( $error ) && $errno == 0 ) {
                                                 $this->ipnHandler->handle ( $response == 'VERIFIED' , $_POST ) ;
                                          }
                                   }
                            }
                     } else {
                            throw new BadMethodCallException ( 'Nenhum manipulador de mensagem ou email foi definido' ) ;
                     }
              }

              public function setIPNHandler ( IPNHandler $ipnHandler ) {
                     $this->ipnHandler = $ipnHandler ;
              }

       }

DonateIPNHandler.php :-

PHP
<?php

       require_once 'IPNHandler.php' ;
       require_once '../Functions/Database.php' ;
       require_once 'PaypalPayment.php' ;
       
       class DonateIPNHandler implements IPNHandler {
              
              private $db , $query ;
              public function handle ( $isVerified , array $message ) {
                     if ( $isVerified ) { 
                            if ( $message [ 'receiver_email' ] == 'frozentalisman@gmail.com' ) {
                                   //file_put_contents('test.text', print_r($message, true), FILE_APPEND);
                                   $data = array ( 
                                       'first_name' => $message [ 'first_name' ] ,
                                       'last_name' => $message [ 'last_name' ] ,
                                       'address_country' => $message [ 'address_country' ] ,
                                       'address_city' => $message [ 'address_city' ] ,
                                       'quantity' => ( int ) $message [ 'quantity' ] ,
                                       'payment_status' => $message [ 'payment_status' ] ,
                                       'item_name' => $message [ 'item_name' ] ,
                                       'mc_currency' => $message [ 'mc_currency' ] ,
                                       'gross' => $message [ 'payment_gross' ] ,
                                       'acct' => array (
                                           'account' => $message [ 'option_selection2' ] ,
                                           'dpoints' => preg_replace ( '/[^\d]/' , null , $message [ 'option_selection1' ] )
                                       ) ,
                                       'check' => $message [ 'option_selection1' ] ,
                                       'email' => $message [ 'payer_email' ] ,
                                       'ID' => $message [ 'ipn_track_id' ]
                                   ) ;
                                   $paypalPayment = new PaypalPayment ( GetDatabaseConnection ( 'db_misc' ) ) ;
                                   $_validate = $paypalPayment->isValid ( $data ) ;
                                   if ( $_validate [ "isValid" ] === true ) {
                                          $paypalPayment->storePayment ( $data )->dispatch ( ) ;
                                   }
                            }
                     }
              }
              
              
       }


IPNHandler.php :-

PHP
<?php

       interface IPNHandler {

              public function handle ( $isVerified , array $message ) ;
       }



This is all script which contains Paypal things :)

Here is explanation how this all works. :-
User Purchase Points with Paypal and he will get automatically Points. so its just one kind of IPN Buttons, but i am not sure if it is just that.
i can attach files if need please help me to make it work.

hope its not too long :)
and thanks in advance for help..
Posted
Updated 27-Oct-14 5:44am
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900