Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New Coder
    Join Date
    Mar 2013
    Posts
    39
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Help with variable scope

    I have the build() method here and also the process () method. I can't retrieve values in process() method.
    such as echo "Code: " . $xml->messages->resultCode

    PHP Code:
    error_reporting(E_ALL);

    class 
    AuthorizeNet extends GatewayFramework implements GatewayModule {

        var 
    $cards = array("visa""mc");
        var 
    $status = array("1" => "CHARGED","4" => "PENDING");
        
        
        public 
    $xml;
        public 
    $splitted;
        public 
    $txnstatus;
        function 
    AuthorizeNet () {
            
    parent::__construct();
            
    $this->setup('login','password','testmode');
        }

        function 
    actions () {
            
    add_action('mycart_process_order',array(&$this,'process'));
        }

        
            
    //function process () {
            
    function process () {
            global 
    $Mycart;
            
    $message $this->build();
            
    $Response $this->send($message);
            
    $status $splitted[0];
            
            
            if (
    $status == "2" || $status == "4"){ 
        
                
    $message $xml->messages->message->code;
                
                
    $code $splitted[0];
                
                if (empty(
    $message)) { 
                    new 
    MycartError(__("A configuration error occurred while processing this transaction. Please contact the site administrator.","Mycart"),'authnet_error',MYCART_TRXN_ERR);
                    
                    return;
                }
                new 
    MycartError($xml->messages->message->code,'authnet_error',MYCART_TRXN_ERR);
                    
    var_dump($xml->messages->message->code);
                    
            }
            
            
    var_dump($xml->directResponse[6]);echo "\n<br />\n";
    $splitted explode(','$xml->directResponse[0]); 
    //var_dump($this->$splitted);echo "\n<br />\n";
    //echo var_dump($this->$splitted[6]);echo "\n<br />\n";
    $txnid $splitted[6]; 
    var_dump($txnid);echo "\n<br />\n"

            
    $txnstatus $xml->messages->resultCode;
            
            
    $Mycart->Order->transaction($txnid,$txnstatus);

        }
        

        

        function 
    build () {
            
    $Order $this->Order;
        

     
        
    $xml = new AuthnetXML(AUTHNET_LOGINAUTHNET_TRANSKEYAuthnetXML::USE_DEVELOPMENT_SERVER);
        
    $xml->createCustomerProfileTransactionRequest(array(
          
    'transaction' => array(
                
    'profileTransAuthCapture' => array(
                    
    'amount' => '10.95',
                    
    'tax' => array(
                        
    'amount' => '1.00',
                        
    'name' => 'your state sales tax',
                        
    'description' => 'description state sales tax'
                    
    ),
                    
    'shipping' => array(
                        
    'amount' => '2.00',
                        
    'name' => 'ground based shipping',
                        
    'description' => 'Ground based 5 to 10 day shipping'
                    
    ),
                    
    'lineItems' => array(
                        
    => array(
                            
    'itemId' => '1',
                            
    'name' => 'widget',
                            
    'description' => 'description of widget',
                            
    'quantity' => '18',
                            
    'unitPrice' => '45.00'
                        
    ),
                        
    => array(
                            
    'itemId' => '2',
                            
    'name' => 'widget2',
                            
    'description' => 'description widget 2',
                            
    'quantity' => '10',
                            
    'unitPrice' => '85.00'
                        
    )
                    ),
                    
    'customerProfileId' => '129',
                    
    'customerPaymentProfileId' => '119',
                    
    'customerShippingAddressId' => '120',
                    
    'order' => array(
                        
    'invoiceNumber' => 'INV56',
                        
    'description' => 'description of transaction',
                        
    'purchaseOrderNumber' => 'PON56'
                    
    ),
                    
    'taxExempt' => 'false',
                    
    'recurringBilling' => 'false',
                    
    'cardCode' => '000'
                
    )
            ),
         
            
        ));

    echo 
    "Response: " $xml->messages->message->code  "<br><br>";//Ok
    echo "Code: " $xml->messages->resultCode .  "<br><br>";// number
    echo "Text: " $xml->messages->message->text .  "<br><br>"//Successful
    $splitted explode(','$xml->directResponse[0]);
    echo 
    "Direct Code: " $splitted[0] . "<br><br>"//Successful
    echo "Dir Response: " $xml->directResponse.  "<br><br>"//Successful
    echo "TXID: " $splitted[6] .  "<br><br>"//Successful
    //echo "Direct Response: " . $xml->directResponse[0] .  "<br><br>"; //Successful
    echo "Transaction ID: " $splitted[6] .  "<br><br>";
    var_dump($splitted[6]);
    echo 
    "Raw request: " $xml "<br><br>";


     }    


        
        
    function 
    send ($message) {
         
    //  $url = $this->url();//added
            
    $url 'https://apitest.authorize.net/xml/v1/request.api';
            
    $url apply_filters('mycart_authorize_net_url',$url);
            
        }





    // END class AuthorizeNet

    ?> 

  • #2
    Regular Coder patryk's Avatar
    Join Date
    Oct 2012
    Location
    /dev/couch
    Posts
    398
    Thanks
    2
    Thanked 64 Times in 64 Posts
    use $GLOBALS array
    whatever you put in it, will become global variable (like $_post or $_get)

    http://php.net/manual/en/reserved.variables.globals.php
    Last edited by patryk; 04-01-2013 at 08:16 PM.

    -------------------------------------------------------------------------------
    "Real Programmers can write assembly code in any language" - Larry Wall

  • #3
    New Coder
    Join Date
    Mar 2013
    Posts
    39
    Thanks
    3
    Thanked 0 Times in 0 Posts
    I thought using Globals was not good practice? If I were to use it would I put it under both build() and process()

  • #4
    New Coder
    Join Date
    Mar 2013
    Posts
    39
    Thanks
    3
    Thanked 0 Times in 0 Posts
    This is how I tried it.
    PHP Code:
    GLOBALS $xml
    GLOBALS $splitted

    $splitted 
    explode(','$xml->directResponse[0]);//this is a string where I get 6th element of the string
    echo "TXID: " $splitted[6] . 


    echo 
    GLOBALS[xml]; //all the info
    echo GLOBALS[splitted]; // returned "ARRAY'

    echo $GLOBALS[splitted][6]; 
    Last edited by mallen2; 04-01-2013 at 09:32 PM.

  • #5
    Regular Coder patryk's Avatar
    Join Date
    Oct 2012
    Location
    /dev/couch
    Posts
    398
    Thanks
    2
    Thanked 64 Times in 64 Posts
    what you want to do is this:
    $GLOBALS['xml'] = $xml; to "copy" contents of $xml to $GLOBALS['xml']
    and then when you want to retrieve information you just use $GLOBALS['xml'] instead of $xml

    and you are right. it's not a good practice, but if you want have variables accessable inside functions and also outsie, then global variables is your only way. you just have to be careful not to override other globals you might have in your code

    -------------------------------------------------------------------------------
    "Real Programmers can write assembly code in any language" - Larry Wall

  • Users who have thanked patryk for this post:

    mallen2 (04-01-2013)

  • #6
    New Coder
    Join Date
    Mar 2013
    Posts
    39
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Thanks. So how would I write the echo statement for this value: $xml->messages->message->text

  • #7
    Regular Coder patryk's Avatar
    Join Date
    Oct 2012
    Location
    /dev/couch
    Posts
    398
    Thanks
    2
    Thanked 64 Times in 64 Posts
    try echo $GLOBALS['xml']->messages->message->text
    but i'm not sure if i'm right with that. i'm almost asleep already

    -------------------------------------------------------------------------------
    "Real Programmers can write assembly code in any language" - Larry Wall

  • #8
    New Coder
    Join Date
    Mar 2013
    Posts
    39
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Perfect. That worked great. I was able to follow the structure to print other values also.

  • #9
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    : squint :
    why? Globals are almost never needed to start with, and they are NEVER needed when using a class (with the exception of predefined methods in something like SessionHandlerInterface for example for which you *may* want to use globalization, but is probably not required as you can pass in during construction). Globals are less about bad practice, and more about a debugging nightmare. They should be reserved for callbacks that have defined signatures which you cannot force additional information in through the argument passing (such as a registered error handler).
    In a class you have two options. The first is to return from one into another. If the datatype returned is an object, it is returned by object pointer reference so changes in one reflect changes in another (with the exception of destruction since destruction would require an actual return by reference). Scope is cloned in the sense that if you return from a function, that object is preserved, but once destroyed by the caller, than it is now gone.
    The other option is to simply make use of your member properties and store and retrieve from the object members. That's a simple matter of using $this->xml = $xml; in build, and accessing it using $this->xml in any other class method. If it makes sense to store it, than use the member properties. If it is used once and disposed of, return the results.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #10
    New Coder
    Join Date
    Mar 2013
    Posts
    39
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Thanks Fou-Lu. Can you explain what you explain what you mean by storing it?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •