2016-07-24 3 views
1

Я не могу использовать это внутри другой функции. Поэтому у меня есть функция user_authnet ($ user), где я передаю массив из моего БД объекта пользователя. Внутри этого объекта я сохранил идентификатор профиля клиента authorize.net. Поэтому я пытаюсь вызвать API Auth.net, чтобы получить способы оплаты и подписки.AuthnetJson Namespace John Conde

Если я включаю

namespace JohnConde\Authnet; 

в верхней части моего сценария, то я получаю

function 'user_authnet' not found or invalid function name 

как ошибка. Я думаю, потому что это не часть любого из ваших классов.

Если я не ставлю объявление пространства имен, я получаю

Class 'AuthnetApiFactory' not found 

даже несмотря на то, autoload.php работает.

Я пытаюсь создать плагин для Wordpress. Вот мой полный код:

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']); 
} 

ответ

1

Размещая это пространство имен в верхней части страницы, вы, по сути размещения всей страницы в этом пространстве имен. Вы не хотите этого делать.

Вместо этого просто добавьте пространство имен к вашему вызову AuthnetApiFactory::getJsonApiHandler(), чтобы вы все еще могли работать в глобальном пространстве имен.

// 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);     

Вы также можете использовать use заявление, чтобы сократить этот синтаксис немного:

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); 
Смежные вопросы