I need to integrate Authorize.net payment gateway to my Zf2 application.
Downloaded and copied the PHP SDK in php include path. The sdk has the following files
+authoriznet
+doc
+lib
+shared
+ssl
-AuthorizeNetSIM.php (and more)
-AuthorizeNet.php
My code (Zend controller) is
require_once 'path/AuthorizeNet.php'; /*This require_once AuthorizeNetSIM.php and other files)*/
class PurchaseController extends AbstractActionController{
public purchaseAction(){
$works = new AuthorizeNetException("test"); //A class defined in AuthorizeNet.php
$notFound = new AuthorizeNetSIM;
}
}
$works -> works fine, where as $notFound throws class not found exception
If i add require_once in view file (.phtml) then all classes in sdk is available to use.
Can someone explain this behaviour? How to properly use this 3rd party library in Zf2?
Related
I'm having some hard time wrapping around namespaces in PHP, especially when you code needs to interact with scripts residing in another namespace. I downloaded a Shopify API toolkit and trying to get it working. Everything was fine before I started adding namespaces to my code (which is required or threre is script collisition with other Wordpress plugins on my site). Also, the weird namespace {} bit at the top is because in this same file I want a globally accessible function for making the class a singleton.
Looking forward to learning more about how this works.
#### FILE BEING CALLED
namespace {
function SomeFunctionToBeAccessedGlobally() {
return 'Hello';
}
}
namespace MySpecialApp {
class ShopifyImport {
public function __construct() {
// Do Whatever
$this->doImport();
}
public function doImport() {
require __DIR__ . '/vendor/autoload.php';
$credential = new Shopify\PrivateAppCredential('standard_api_key', 'secret_api_key', 'shared_api_key');
$client = new Shopify\Client($credential, 'shop_url', [ 'metaCacheDir' => './tmp' ]);
}
}
}
#### FILE '/vendor/autoload.php'
require_once __DIR__ . '/composer/autoload_real.php';
return ComposerAutoloaderInit73503f8de5d68cdd40a9c0dfd8a25b44::getLoader();
I do noice that some of the files which where part of the repository cloned into vendor have namespace Slince\Shopify; declarations. I tried to do a use with that namespace within my original namespace but it also didn't work.
The PHP Error being reported is:
Fatal error: Uncaught Error: Class
'MySpecialApp\Shopify\PrivateAppCredential' not found in
/.../ShopifyImporter.php:139 Stack trace: #0 (Blah Blah Blah)
Your code tries to create a new Shopify\PrivateAppCredential() object in the current namespace. However, this class does not exist in your namespace since it is part of the "vendor" namespace.
You can "reset" (read fallback) to the global namespace for your object creations by adding a \ in front of them, as described in the documentation:
$credential = new \Shopify\PrivateAppCredential('standard_api_key', 'secret_api_key', 'shared_api_key');
You can check the difference here without \ and with \.
How can I integrate the Mailjet API PHP wrapper into my Codeigniter installation as a library?
Is it as simple as placing the contents of the repository into application/libraries/Mailjet and then creating a Mailjet.php file in application/libraries which initializes Mailjet like shown below?
require 'Mailjet/vendor/autoload.php';
use \Mailjet\Resources;
$mj = new \Mailjet\Client(getenv('MJ_APIKEY_PUBLIC'), getenv('MJ_APIKEY_PRIVATE'));
Please let me know if I'm on the right track. Thanks.
Yes, you are on right track. But you don't need to create CI library. Use Mailjet repository library in controller as well. Just use composer as stated in CI docs.
If you want CodeIgniter to use a Composer auto-loader, just set $config['composer_autoload'] to TRUE or a custom path in application/config/config.php.
Step by step instruction for using github repository in CodeIgniter
Set $config['composer_autoload'] = TRUE; in APPPATH.'config/config.php' file
Put composer.json file with wanted repositories/projects in APPPATH location
Do the job with composer install command through console which will make vendor and other related files and folders inside
Use it when needed in controller or in other code as shown in example bellow
example controller Mailman.php
<?php defined('BASEPATH') OR exit('No direct script access allowed');
use \Mailjet\Resources;
class Mailman extends CI_Controller
{
private $apikey = 'apy__key__here';
private $secretkey = 'apy__secret__here';
protected $mj = NULL;
public function __construct()
{
// $this->mj variable is becoming available to controller's methods
$this->mj = new \Mailjet\Client($this->apikey, $this->apisecret);
}
public function index()
{
$response = $this->mj->get(Resources::$Contact);
/*
* Read the response
*/
if ($response->success())
var_dump($response->getData());
else
var_dump($response->getStatus());
}
}
If you explicitly want to use Mailjet (or any other) repository through CI library, check in docs how to create custom library and merge this code above with it. Personaly I use repositories this way to avoid unnecessarily loading and parsing sufficient libraries.
I installed the Bed Edmund's Ion Auth login script and would like to make the functions global throughout my application.
I installed the code and it works great, but I'm having issues extending it. The ion Auth controller extends CI_Controller, so then in all my other controllers I extend Auth.
//Ion Auth Controller Class
class Auth extends CI_Controller {
and in a separate page:
//Home Page Controller Class, where I will be placing the login form
require_once("auth.php");
class Home extends Auth {
By doing this, I get the following PHP error:
Message: Assigning the return value of new by reference is deprecated
I read in the CodeIgniter docs that extended classes must now begin with MY_, so I tried that as well with no luck.
What am I doing wrong? Is my logic all wrong?
Do a CTRL-F for =& inside the Auth class source code (the one you installed) and replace every instance by =.
$foo =& new Bar();
is a deprecated syntax that was used in PHP4 but no longer in PHP5.
I'm trying to use the php-ga third-party library in my symfony project. I've installed the library in apps/<app>/lib, cleared the cache, and the third-party files are appearing as I would expect in config_autoload.yml.php:
'tracker' => 'C:/wamp/www/apps/api/lib/php-ga/GoogleAnalytics/Tracker.php',
'transaction' => 'C:/wamp/www/apps/api/lib/php-ga/GoogleAnalytics/Transaction.php',
...
However, when I try to use the classes in an action under the same app like so:
use UnitedPrototype\GoogleAnalytics;
public function executeNew(sfWebRequest $request)
{
$tracker = new GoogleAnalytics\Tracker(...);
...
I get an error saying it can't resolve the class:
Fatal error: Class 'UnitedPrototype\GoogleAnalytics\Tracker' not found in C:\wamp\www\apps\api\modules\encoding\actions\actions.class.php
What am I missing?
Symfony's autoloader can not load namespaced classes properly. I think you should move the ga lib to lib/vendor/php-ga and use it's own autoloader in config/ProjectConfiguration.class.php (like require_once __DIR__ . '/../lib/vendor/php-ga/autoload.php').
I'm trying to write some test cases for a ZF application I'm writing but I can't seem get past this point.
I have extended the Zend_Test_PHPUnit_ControllerTestCase, adding the following:
public function setUp() {
$this->bootstrap = new Zend_Application('testing',
APPLICATION_PATH . '/configs/application.ini');
parent::setUp();
}
It works, and it initializes the Zend Autoloader, allowing me to load other classes inside my library as well as Zend classes. So I setup a test:
public function testUserUnauthorized() {
$this->dispatch('/api/user');
//Assertions...
}
Sadly, the test never gets past the dispatch. Instead, it gives me an error:
Fatal error: Class 'App_Model_User' not found in ....Action.php
Action.php is a class extending Zend_Controller_Action. In it, there is a function that uses App_Model_User to authenticate the logged in user. I never had to add a require_once since the Autoloader works. That's the strange part: it works through my browser. Just not in PHPUnit.
So I dumped the Autoloaders registered with the Zend Autoloader:
public function testUserUnauthorized() {
print_r(Zend_Loader_Autoloader::getInstance()->getAutoloaders());
exit;
$this->dispatch('/api/user');
}
It shows all my modules, as well as the namespaces for views, controllers, models, etc. I did the same through my browser and the dumps matched.
Zend Autoloader needs App_ added as an autoload prefix for this to work as you expect, something like this:
$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->registerNamespace("App_");