How to use authorize API in cakephp? - cakephp

I am using cakephp 2.6.7. I want to integrates Authorize Api(http://www.authorize.net/) to my cakephp app. I set up api by composer in my localhost. It works fine. Here is the file structure:
Here charge-credit-card.php is the final script to run. Inside this file I include some pre requisite files as follows:
require 'authorize/autoload.php';
use net\authorize\api\contract\v1 as AnetAPI;
use net\authorize\api\controller as AnetController;
Now I put the authorize folder inside Vendor folder of cakephp:
And I add autoload.php in paymentsController.php as follows:
require_once(APP . 'Vendor' . DS . 'authorize' . DS . 'autoload.php');
But I am confused about
use net\authorize\api\contract\v1 as AnetAPI;
use net\authorize\api\controller as AnetController;
Replacement. How should I replace these two lines inside cakephp controller? Here is my full code:
<?php
require_once(APP . 'Vendor' . DS . 'authorize' . DS . 'autoload.php');
use net\authorize\api\contract\v1 as AnetAPI;
use net\authorize\api\controller as AnetController;
define("AUTHORIZENET_LOG_FILE", "phplog");
class PaymentsController extends AppController {
var $layout = 'admin';
// public $components = array('Auth');
public function isAuthorized($user = null) {
$sidebar = $user['Role']['name'];
$this->set(compact('sidebar'));
return true;
}
public function beforeFilter() {
parent::beforeFilter();
// Allow users to register and logout.
$this->Auth->allow('process');
}
}
?>
Thanks for your time.

I guess your question is specific to php aliases and not the ANet sdk itself?
Here's a question about making globally accessible aliases. There's no straight forward way do it, it seems.
PHP Global namespace aliases
Related:
http://grokbase.com/t/php/php-general/10a38hape9/is-it-possible-to-create-a-global-namespace-alias
The default approach is to define ...
use net\authorize\api\contract\v1 as AnetAPI;
use net\authorize\api\controller as AnetController;
... in each of the files where you need to use the aliases. (As you must know, once autoloaded, the namespaces are globally accessible)
That should have your application working.

Related

How to make cakephp cacheAction work with multilanguage

I'm working with cakephp 2.x
I've got a site with static pages (Multilanguage) and cacheAction.
The cache does not recognize the language and caches the page in the firest language....
Any idea how to solve (a part from disabling the cache?)
Thanks,
Massimo
class PagesController extends AppController {
/**
* This controller does not use a model
*
* #var array
*/
public $uses = array();
public $helpers = ['Cache','AbTest.AbTest'];
public $cacheAction = '1 month';
public $components = array('AbTest.AbTest');
....
if ($locale && file_exists(APP . 'View' . $theme_path . DS . $this->viewPath . DS . $locale .DS. implode('/', $path) . $this->ext ))
{
array_unshift($path,$locale);
}
try {
$this->render(implode('/', $path));
} catch (MissingViewException $e) {
if (Configure::read('debug')) {
throw $e;
}
throw new NotFoundException();
}
I expect the cache to present me ita/pages/who and eng/pages/who as different pages, while it outputs alway ita/pages/who
There is an option for this called Cache.viewPrefix. You didn't mention how you manage the languages in your site, but the basic logic is that you can set the cache file prefix with the language and this way you will have separate cache files for every language. For example, you can do it in your PagesController or AppController like
Configure::write('Cache.viewPrefix', /* place the language here */);

How to add new MongoClient in cakephp 3x

How to add
new MongoClient('mongodb://localhost');
in cakephp 3x __construct . I am trying to setup mongodb in cakephp-3.2, and also is there any plugin for cakephp3.2 to have mongodb
I found solution in https://stackoverflow.com/a/31274207/4244742 . To bring 3rd party class we need to add class file in vendor folder and then we need to use
require_once(ROOT . DS . 'vendor' . DS . "my_library_folder" . DS . "my_library_base_class.php") in bootstrap.php. Now you can use it AppController under parent::initialize();

Multiple JS Engine Helpers cakephp 2.x

I have two plugins that both need to extend the JqueryEngineHelper.
I know that you can only specify one engine in AppController Helpers.
How can I extend the JqueryEngineHelper in both plugins? I have them both working but I cannot get them to work at the same time.
$helpers = ['Js'=>['MyPlugin.MyPluginJquery', 'MyPlugin2.MyPlugin2Jquery']];
I would like both to work, but unfortunately they do not. Only the first one is used.
Code from one of the engines
App::uses('AppHelper', 'View/Helper');
App::uses('JqueryEngineHelper', 'View/Helper');
class MrgCustomSelectJqueryEngineHelper extends JqueryEngineHelper{
function __construct(View $view, $settings = array()){
parent::__construct($view, $settings = array());
$this->_init_callbacks();
}
protected function _init_callbacks(){
$callbacks = [
'selectBoxIt'=>[]
];
$this->_callbackArguments = array_merge($this->_callbackArguments, $callbacks);
}
public function selectBoxIt($options = []){
$template = '%s.selectBoxIt({%s});';
return $this->_methodTemplate('selectBoxIt', $template, $options);
}
}
I think you're going to have to alter the plugins so that they don't conflict. Depending on how the plugins work, there may be different ways you can go about this:
Only load the version Js helper you need at the moment. Only works if you don't need both for a single controller action.
Change the plugins to refer to the different versions of Js helper by different names. This could possibly take a lot of search and replace.
If there are no conflicts between how the plugins extended Js helper, you can merge the two helpers.

CakePHP won't read my default.po file

I've got a CakePHP application that works with multiple languages (on database level), and in the end, I generated a .po file with PoEdit and translated the keywords, but cake doesn't want to read it.
This is my code in AppController:
Configure::write('Config.language', 'mkd');
class AppController extends Controller {
// ... some code here ...
public function beforeFilter () {
parent::beforeFilter();
$this->params['language'] =
!$this->params['language'] ? 'mkd' : $this->params['language'];
Configure::write('Config.language', $this->params['language']);
// more code...
}
I know that declaring the language before AppController class is obsolete, but it was an act of desperation. Both parameters, $this->params['language'] and Configure::read('Config.language'), are set to 'mkd', however it only displays the original values.
This is my localization folder structure:
C:\wamp\www\animalmedica\app\Locale\mkd\LC_MESSAGES\default.po
I am using CakePHP 2.3. What am I doing wrong here?

Admin Panel/Interface in a CakePHP project

I want to create an "Admin Panel/Interface" in one of my CakePHP projects. You know its very common in modern web sites. At first, I planned to create a plugin for this, and tried to do it. It didn't work, no idea why, I'll ask for help for it later. But, next I saw that CakePHP already provides this feature, using "Scafolding". I am now trying this, but don't know why its not working as I expected. Here is what I did :
app/config/core.php :
---------------------
.
.
.
Configure::write('Routing.prefixes',array('admin'));
.
.
.
app/Controller/AppController.php :
----------------------------------
.
.
.
public $components=array(
'Session',
'Auth'=>array(
'loginRedirect'=>array('admin'=>true,'controller'=>'home','action'=>'index'),
'logoutRedirect'=>array('controller'=>'home','action'=>'index'),
'authorize'=>array('Controller')
)
);
.
.
.
I thought, there should be a seperate controller for Admin Panel, that's why I created it :
app/Controller/AdminsController.php :
-------------------------------------
<?php
App::uses('AppController','Controller');
class AdminsController extends AppController{
public $name='Admins';
public $scaffold='admin';
}
But it didn't work. So, I thought CakePHP provided this feature for all individual controller; I mean, I thought I am supposed to have the Admin Panel for all individual controller, not as a different module/controller/sub-system. So, I changed a little one of my existing controllers "Controllers1" :
<?php
App::uses('AppController','Controller');
class Controllers1Controller extends AppController{
public $name='Controllers1';
public $scaffold='admin';
}
then tried to go to this URL : my_site/admin/jobs/view
but still same result.
Please give me a suggestion, what should I do ? Should I create a new plugin for the "Admin Panel", or Scafolding is better ? And what is my fault ?
Thanks
The AdminsController is not necessary to use the admin prefix, all you need to do is define the Routing.Prefixes like you already did.
Configure::write('Routing.prefixes',array('admin'));
For the JobsController example that you mentioned to us what all you need to do to make it work is:
<?php
App::uses('AppController', 'Controller');
class JobsController extends AppController {
public $scaffold = 'admin';
}
Because the way to make Routing Prefixes work is to declare methods with the prefixes, not use an additional controller:
<?php
App::uses('AppController', 'Controller');
class ArticlesController extends AppController {
function admin_index() {
//This method can be found under /admin/articles
}
}

Resources