How do call a method inside a component in OctoberCMS? - php

I don't know if this is possible or if it's a complete madness but I'm trying to execute a PHP method from AJAX call using OctoberCMS Ajax Framework(I assume that this uses jQuery behind it) and is not working because I never get redirect to PayPal site. The PHP code I'm trying to get working is this one:
protected function onExecutePurchaseMethod()
{
Omnipay::gateway('PayPal_Express');
$params = [
'username' => $this->username,
'password' => $this->password,
'signature' => $this->signature,
'testMode' => $this->sandboxMode,
'amount' => Session::get('amountToReload'),
'cancelUrl' => url( 'payment/step4', "", $secure = null ),
'returnUrl' => url( 'payment/step2', "", $secure = null ),
'currency' => 'USD'
];
$response = Omnipay::purchase($params)->send();
if ($response->isSuccessful()) {
var_dump($response);
} else {
var_dump($response->getMessage());
}
}
What is happening since none redirect to PayPal is executed and page is getting stuck many times forcing me to close the browser and reopen again, no method is executed and no visible errors. It's possible to do what I'm trying to do? Is not a madness? If it's possible where is my error?
As extra info I'm using Barryvdh Laravel-omnipay package for handle Omnipay from within Laravel.

After looking briefly through the documentation, my best guess is that you're missing a required field for the purchase() method. I believe you need a card parameter (even if it's an invalid one) to get it to process.

Related

php function gives a loop - hostfact API

I'm using hostfact https://www.hostfact.nl for our billing. Now I want to extend the code and get a loop on executing the internalAPI in hooks.php. I tried to find an answer in the documentation https://www.hostfact.nl/developer/api/ but without success.
// When a client is edited, the bankParameters are checked and validated.
// That is only an example
function action_debtor_is_edited($parameters){
$params = array(
'DebtorCode' => $parameters['DebtorCode']
);
$hostfactResult = internalAPI('debtor', 'show', $params);
$bankParams = array(
'Identifier' => '2',
'AccountNumber' => 'AT6734080000000000',
'AccountBank' => 'test bank',
'AccountCity' => 'city',
'AccountBIC' => 'RLNODASD'
);
$bankResult = internalAPI('debtor', 'edit', $bankParams);
}
I've already asked the support and got follwoing answer:
One of the option is keeping track of the situation. For example, use
a static or global variable. When you call an (internal)API call from
a webhook, first flag that variable, so in the newly called webhook
you know that is not needed to execute the webhook again (at least not
the part which causes the loop). In the original call, after receiving
the result, the flag can be unset again.
My Problem is, I'm new to PHP and don't understand what I should do to avoid the loop. The $bankParams variable is intentionally hardcoded.
Can you help?

Why WooCommerce REST API seems not working?

I'm currently working on my admin website using WooCommerce REST API, It works very well since last week but now it shows error like
Error: Sorry, you cannot list resources. [woocommerce_rest_cannot_view]
I don't change anything in my code, The authentication credentials are all fine. Thanks for any advice.
Edited:
protected $url = 'https://mywebsite.com';
protected const CONSUMER_KEY = 'ck_b7144d17091aa01a7a096154a445180c603d****';
protected const CONSUMER_SECRET = 'cs_cdb7705d4ad5bf29aa2b6366c55ac98397e4****';
function __construct(){
$this->woocommerce = new Woo(
$this->url,
self::CONSUMER_KEY,
self::CONSUMER_SECRET,
[
'wp_api' => true,
'version' => 'wc/v2',
]
);
}
This is often as a result of an setup issue, specifically with what values you've passed to verifySsl and queryStringAuth. You didn't post your code, so I can't tell you specifically, but these two resources might help:
https://github.com/woocommerce/wc-api-node/issues/43
https://robotninja.com/blog/fix-common-woocommerce-rest-api-issues/

Using Zend_Form_Element_Captcha - session has already been started

I am trying to add a Captcha as part of a big application form.
Getting the following error:
session has already been started by session.auto-start or session_start()
How can I get around this as behind the scenes it uses:
// Process metadata specific only to this namespace.
Zend_Session::start(true); // attempt auto-start (throws exception if strict option set)
I instantiate it in the Controller:
$this->view->captcha = new Zend_Form_Element_Captcha('captcha', array(
'captcha' => array(
'captcha' => 'Figlet',
'wordLen' => 6,
'width' => 300,
'height' => 100,
)
)
);
In the View:
<?php echo $this->captcha; ?>
I can't tamper with current session as it holds a lot of information. Is there a workaround?
Help would be appreciated.
You can do one thing in this case, try starting your Zend session in bootstrap file in below way and set that session object in registry:
protected function _initSession() {
$userSession = new Zend_Session_Namespace('user_session');
Zend_Registry::set('userSession', $userSession);
}
After this you will be able to get session object anywhere from registry.
$userSession = Zend_Registry::get('userSession');
Since you will start your Zend Session before session_start() so it might not produce any error.

PHP Yii: Database connect in runtime

I would like to connect to a second database with Yii at runtime. The database name would come from a database table after the user to login.
I saw in a tutorial I should do this:
$db2 = Yii::createComponent(array(
'class' => 'EMongoClient',
'server' => 'mongodb://127.0.0.1:27017',
'db' => $emp['database']
));
Yii::app()->setComponent('db2',$db2);
But in my controler when I access Yii::app()->db2 get the error:
Property "CWebApplication.db2" is not defined
What am I doing wrong?
The following works for me:
Yii::app()->mongodb->setActive(false);
Yii::app()->mongodb->setServer('mongodb://localhost:27017');
Yii::app()->mongodb->setDb('db1');
Yii::app()->mongodb->setActive(true);
UPDATED: Try, instead instance, pass configurations:
Yii::app()->setComponent( 'db2', array(
'class' => 'EMongoClient',
'server' => 'mongodb://127.0.0.1:27017',
'db' => $emp['database']
)
);
Or, you may create special index on params in configurations, such as:
...
'params' => array(
'db2' => null,
),
And the use Yii::app()->params['db2'] = $db2
From this comment:
My problem is not with the creation of the component. Soon after
creating if I access Yii::app()->db2 its works, but when I try to
access via another model or controller I get the error
I think you are setting this component only once somewhere, and then making subsequent requests to different controllers.
You need to put the code, somewhere it is being called EVERYTIME, on every Request. thats how PHP works, there is no "global application state"
by default Yii comes with protected/components/controller.php has base controller for the rest of the app.
my suggestion would be to put your code on the init() method of that controller, so that it always gets called.
You mentioned the database name comes from a table once the user logs in, so you need to save that value in the session, in other to be able to access it in the other requests:
<?php
// After login in
Yii::app()->user->setState('db_name', $db_name);
// in protected/components/controller.php
public function init()
{
if (!Yii::app()->user->isGuest) {
$db2 = Yii::createComponent(array(
'class' => 'EMongoClient',
'server' => 'mongodb://127.0.0.1:27017',
'db' => Yii::app()->user->getState('db_name')
));
Yii::app()->setComponent('db2',$db2);
}
}
Hope it helps, I am assuming many things here :)

CodeIgniter - Variable scope

I have a controller which I use for a login form. In the view, I have a {error} variable which I want to fill in by using the parser lib, when there is an error. I have a function index() in my controller, controlled by array $init which sets some base variables and the error message to '':
function index()
{
$init = array(
'base_url' => base_url(),
'title' => 'Login',
'error' => ''
);
$this->parser->parse('include/header', $init);
$this->parser->parse('login/index', $init);
$this->parser->parse('include/footer', $init);
}
At the end of my login script, I have the following:
if { // query successful }
else
{
$init['error'] = "fail";
$this->parser->parse('login/index', $init);
}
Now, of course this doesn't work. First of all, it only loads the index view, without header and footer, and it fails at setting the original $init['error'] to (in this case) "fail". I was trying to just call $this->index() with perhaps the array as argument, but I can't seem to figure out how I can pass a new $init['error'] which overrides the original one. Actually, while typing this, it seems to impossible to do what I want to do, as the original value will always override anything new.. since I declare it as nothing ('').
So, is there a way to get my error message in there, or not? And if so, how. If not, how would I go about getting my error message in the right spot? (my view: {error}. I've tried stuff with 'global' to bypass the variable scope but alas, this failed. Thanks a lot in advance.
$init musst be modified before generating your view.
To load your header and footer you can include the following command and the footer's equivalent into your view.
<?php $this->load->view('_header'); ?>
to display errors, you can as well use validation_errors()
if you are using the codeigniter form validation.
if you are using the datamapper orm for codeigniter you can write model validations, and if a query fails due to validation rule violation, you get a proper error message in the ->error property of your model.
Code for your model:
var $validation = array(
'user_name' => array(
'rules' => array('required', 'max_length' => 120),
'label' => 'Name'
)
);
You might try this:
function index() {
$init = array(
'base_url' => base_url(),
'title' => 'Login',
'error' => ''
);
$string = $this->parser->parse('include/header', $init, TRUE);
$string .= $this->parser->parse('login/index', $init, TRUE);
$string .= $this->parser->parse('include/footer', $init, TRUE);
$this->parser->parse_string(string);
}
In parse()you can pass TRUE (boolean) to the third parameter, when you want data returned instead of being sent (immediately) to the output class. By the other hand, the method parse_string works exactly like `parse(), only accepts a string as the first parameter in place of a view file, thus it works in conjunction.

Categories