swg1cor14
swg1cor14

Reputation: 1722

AuthnetJson Namespace John Conde

I can't seem to use this inside another function. So I have a function called user_authnet($user) where I pass an array from my DB the user object. Inside that object I have stored the authorize.net customer profile ID. So I try to call Auth.net API to get the payment methods and subscriptions.

If I include

namespace JohnConde\Authnet;

at the top of my script, then I get

function 'user_authnet' not found or invalid function name

as an error. I guess because it is not part of any of your classes.

If I don't put the namespace declaration, I get

Class 'AuthnetApiFactory' not found

even though the autoload.php is running.

I'm trying to make a plugin for Wordpress. Here's my full code:

namespace JohnConde\Authnet;
include get_template_directory() . '/assets/vendor/authnetjson/config.inc.php'; 
include get_template_directory() . '/assets/vendor/authnetjson/src/autoload.php';

$request = AuthnetApiFactory::getJsonApiHandler(AUTHNET_LOGIN, AUTHNET_TRANSKEY, AuthnetApiFactory::USE_DEVELOPMENT_SERVER);                

add_action( 'show_user_profile', 'user_authnet' );
add_action( 'edit_user_profile', 'user_authnet' );  


function user_authnet( $user )
{   
    global $request;
    ?>
        <h3>Stored Payment Methods</h3>
        <input type="text" name="authnet_customerProfileId" value="<?php echo esc_attr(get_the_author_meta( 'authnet_customerProfileId', $user->ID )); ?>">
    <?php
        if(get_the_author_meta( 'authnet_customerProfileId', $user->ID )) {

            $response = $request->getCustomerProfileRequest(array(
                "customerProfileId" => get_the_author_meta('authnet_customerProfileId', $user->ID)
            ));

            print_r($response);
        }
}


add_action( 'personal_options_update', 'save_authnet' );
add_action( 'edit_user_profile_update', 'save_authnet' );

function save_authnet( $user_id )
{
    update_user_meta($user_id, 'authnet_customerProfileId', $_POST['authnet_customerProfileId']);
}

Upvotes: 1

Views: 99

Answers (1)

John Conde
John Conde

Reputation: 219794

By placing that namespace at the top of your page you are essentially placing the entire page in that namespace. You do not want to do that.

Instead, just prepend the namespace to your call to AuthnetApiFactory::getJsonApiHandler() so you can still work in the global namespace.

// Remove the line below
//namespace JohnConde\Authnet;
include get_template_directory() . '/assets/vendor/authnetjson/config.inc.php'; 
include get_template_directory() . '/assets/vendor/authnetjson/src/autoload.php';

// Add the namespace to your call to AuthnetApiFactory::getJsonApiHandler()
$request = \JohnConde\Authnet\AuthnetApiFactory::getJsonApiHandler(AUTHNET_LOGIN, AUTHNET_TRANSKEY, \JohnConde\Authnet\AuthnetApiFactory::USE_DEVELOPMENT_SERVER);                

You can also use the use statement to shorten this syntax a bit:

use JohnConde\Authnet\AuthnetApiFactory;
include get_template_directory() . '/assets/vendor/authnetjson/config.inc.php'; 
include get_template_directory() . '/assets/vendor/authnetjson/src/autoload.php';

$request = \JohnConde\Authnet\AuthnetApiFactory::getJsonApiHandler(AUTHNET_LOGIN, AUTHNET_TRANSKEY, AuthnetApiFactory::USE_DEVELOPMENT_SERVER);  

Upvotes: 1

Related Questions