#1
  1. A Change of Season
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    3,189
    Rep Power
    220

    How to define instance of class globally


    Hi hello;

    In Codeigniter, how can I do this only once (properly) and use it in different locations in the class or through application?
    PHP Code:
    $client = new Ontraport("********","********"); 
    PHP Code:
    class Jvzoo_api extends CI_Controller {

    public function 
    first_name()
        {
            
    $first_name "";
            if(
    strlen($_POST['ccustname'])>0)
                {
                    
    $first_name explode(" "$_POST['ccustname']);
                    
    $first_name $first_name[0];
                }  
            return 
    $first_name;      
        }
    public function 
    index() 
        { 
          if(
    $this->jvzipn_verification())
                {
                    
    $this->inster_transaction_into_database();
                    
    $client = new Ontraport("****","****");  
                  
    //Do some OP task  
                 
    }
    }   
    public function 
    load() 
        { 
                  
    $client = new Ontraport("****","****");    
                  
    //Do some OP task
            

    Thanks
  2. #2
  3. Lazy Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,326
    Rep Power
    9645
    The first question is whether it's suitable as a controller or not. Is it responsible for some aspect of a request or page? Does it include some sort of outputted response? If it's not a controller then just make a regular class - it's not like everything that you write has to fit into some piece of CI.

    So, how is that code supposed to be used?
  4. #3
  5. A Change of Season
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    3,189
    Rep Power
    220
    We receive api messages from Clickbank, Jvzoo etc...

    Then call Ontraport and update the records there.

    Here is some code:
    PHP Code:
    <?php

    defined
    ('BASEPATH') OR exit('No direct script access allowed');

    require_once(
    'SDK-PHP/src/Ontraport.php');

    use 
    OntraportAPI\Ontraport;

    class 
    Jvzoo_api extends CI_Controller {

    public function 
    first_name()
        {
            
    $first_name "";
            if(
    strlen($_POST['ccustname'])>0)
                {
                    
    $first_name explode(" "$_POST['ccustname']);
                    
    $first_name $first_name[0];
                }  
            return 
    $first_name;      
        }
    public function 
    index() 
        { 
          if(
    $this->jvzipn_verification())
                {
                    
    $this->inster_transaction_into_database();
                    
    $this->ontraport();
                }           
        }       
    public function 
    inster_transaction_into_database()
        {
            if(
    $_POST['cprodtype']=='STANDARD')
                {
                    
    $recurring 0;
                }
            else 
                {
                    
    $recurring 1;
                } 
            
            
    $data = array(
                    
    'transaction_time'=>date('c'),
                    
    'date'=>date('Y-m-d'),
                    
    'transaction_type'=> $_POST['ctransaction'],
                    
    'email' => $_POST['ccustemail'],
                    
    'receipt' => $_POST['ctransreceipt'],
                    
    'first_name' => $this->first_name(),
                    
    'jvzoo_cprodtitle' => $_POST['cprodtitle'],
                    
    'jvzoo_cproditem' => $_POST['cproditem'],
                    
    'recurring' => $recurring,
                    
    'jvzoo_ctransamount' => $_POST['ctransamount'],
                    
    'jvzoo_ctranspaymentmethod' => $_POST['ctranspaymentmethod'],
                    
    'jvzoo_ctransvendor' => $_POST['ctransvendor'],
                    
    'jvzoo_caffitid' => $_POST['caffitid'],
                    
    'jvzoo_cupsellreceipt' => $_POST['caffitid'],
                    
    'jvzoo_ctransvendor' => $_POST['ctransvendor'],
                    
    'payment_method' => $_POST['ctranspaymentmethod']

            );
            
    $this->db->insert('transactions'$data);
        }    
    public function 
    jvzipn_verification() 
        {
            
    $secretKey "********";
                
    $pop "";
                
    $ipnFields = array();
                foreach (
    $_POST AS $key => $value) {
                    if (
    $key == "cverify") {
                        continue;
                    }
                    
    $ipnFields[] = $key;
                }
                
    sort($ipnFields);
                foreach (
    $ipnFields as $field) {
                    
    // if Magic Quotes are enabled $_POST[$field] will need to be
                    // un-escaped before being appended to $pop
                    
    $pop $pop $_POST[$field] . "|";
                }
                
    $pop $pop $secretKey;
                if (
    'UTF-8' != mb_detect_encoding($pop))
                {
                    
    $pop mb_convert_encoding($pop"UTF-8");
                }
                
    $calcedVerify sha1($pop);
                
    $calcedVerify strtoupper(substr($calcedVerify,0,8));
                return 
    $calcedVerify == $_POST["cverify"];    
        } 

    public function 
    get_op_tag_id()
        {
            
    $tag_id 0;
            
    $query $this->db->get_where('ontraport_tags', array('jvzoo_product_id' => $_POST['cproditem']));

            if(
    $query->num_rows()==1)
                {
                    return 
    $query->result_array()[0]['ontraport_tag_id'];
                }
            if(
    $tag_id==0)    
                {
                    return 
    false;
                }        
        }
    public function 
    get_op_contact_id()
        {
            
    $client = new Ontraport(OP_SECRET,OP_PASS);    
            
    $contact_id='';
            
    $requestParams = [ 
                
    'condition' => json_encode([[
                    
    'field' => ['field' => 'email']
                    , 
    'op' => '='
                    
    'value' => ['value' => $_POST['ccustemail']]
                ]])
            ];
            
    $response $client->contact()->retrieveMultiple($requestParams);
            
    $response json_decode($response);
                    foreach(
    $response as $val=>$row)
                       {
                            if(isset(
    $row[0]->id))
                                {
                                    
    $contact_id $row[0]->id;
                                }
                       }
            if(
    is_numeric($contact_id))
                {
                    return 
    $contact_id;
                }
            else 
                {
                    
    $requestParams = array(
                        
    "firstname" => $this->first_name(),
                        
    "email"     => $_POST['ccustemail']
                    );
                    
    $response $client->contact()->saveOrUpdate($requestParams);
                    
    $response json_decode($response,true);
                    return 
    $response['data']['id'];
                } 
        }   
    public function 
    add_tag($contact_id,$tag_id)
        {
            if(isset(
    $contact_id) && (is_numeric($contact_id)))
                {
                    
    $client = new Ontraport(OP_SECRET,OP_PASS);
                    
    $requestParams = array(
                        
    "objectID" => 0
                        
    "ids"      => $contact_id,
                        
    "add_list" => $tag_id
                    
    );
                    
    $response $client->object()->addTag($requestParams);
                }
        } 
    public function 
    ontraport()
        {
            
    $contact_id $this->get_op_contact_id();

            if(
    $this->get_op_tag_id())
                {
                    
    $tag_id $this->get_op_tag_id();
                    
    $this->add_tag($contact_id,$tag_id);
                }    
            
    //$this->add_transaction_to_op();
                     
        
    }    
    public function 
    add_transaction_to_op()
        {
            
    $client = new Ontraport(OP_SECRET,OP_PASS);
            
    $requestParams = array(
              
    "contact_id"       => $this->get_op_contact_id(),
              
    "chargeNow"        => "chargeNow",
              
    "trans_date"       => round(microtime(true) * 1000),
              
    "invoice_template" => 0,
              
    "gateway_id"       => 3,
              
    "offer"            => array(
                
    "products"          => array(
                                         array(
                                            
    "quantity"           => 1,
                                            
    "shipping"           => false,
                                            
    "tax"                => false,
                                            
    "price"              => array(
                                              array(
                                                
    "price"             => $_POST['ctransamount'],
                                                
    "payment_count"     => '',
                                                
    "unit"              => "month",
                                                
    "id"                => $_POST['ctransreceipt']
                                                )
                                            ),
                                            
    "type"                => $_POST['cprodtype'],
                                            
    "owner"               => 1,
                                            
    "offer_to_affiliates" => true,
                                            
    "trial_period_unit"   => "",
                                            
    "trial_period_count"  => 0,
                                            
    "setup_fee_when"      => "",
                                            
    "setup_fee_date"      => "",
                                            
    "delay_start"         => 0,
                                            
    "subscription_count"  => 0,
                                            
    "subscription_unit"   => "month",
                                            
    "taxable"             => false,
                                            
    "id"                  => 5
                                         
    ),
                )
              ),
              
    "billing_address"     => array(
                                        
    "address"     => "",
                                        
    "city"        => "",
                                        
    "state"       => "",
                                        
    "zip"         => "",
                                        
    "country"     => ""
              
    ),
              
    "payer"              => array(
                                        
    "ccnumber"     => "",
                                        
    "code"         => "",
                                        
    "expire_month" => '',
                                        
    "expire_year"  => ''
              
    )
            );

            
    $response $client->transaction()->processManual($requestParams);

        }    
    }           
    ?>
  6. #4
  7. Lazy Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,326
    Rep Power
    9645
    Ohhh, you're talking about the new Ontraport(...) stuff. I thought you meant the whole class.

    Variety of options. The best would be whatever CodeIgniter offers in the way of dependency injection.

IMN logo majestic logo threadwatch logo seochat tools logo