How to integrate citruspay payment gateway in yii2? Citruspay provide library for payment integration. When i am using that library it gives error "Class 'seller\citruspay\lib\Zend_Config_Ini' not found" - any in this will be appreciated
PHP Fatal Error – yii\base\ErrorException
Class 'seller\modules\citruspay\controllers\CitrusPay' not found
1. in C:\wamp\www\haggl\seller\modules\citruspay\controllers\DefaultController.php at line 44
35363738394041424344454647484950515253
"cardHolderName" => Yii::$app->request->post('cardHolderName'),
"cardNumber" => Yii::$app->request->post('cardNumber'),
"expiryMonth" => Yii::$app->request->post('expiryMonth'),
"cardType" => Yii::$app->request->post('cardType'),
"cvvNumber" => Yii::$app->request->post('cvvNumber'),
"expiryYear" => Yii::$app->request->post('expiryYear'),
"returnUrl" => Yii::$app->request->post('returnUrl'),
"amount" => Yii::$app->request->post('amount'),
);
CitrusPay::setApiKey("apikey",'sandbox');
$response = Transaction::create($tarr,CitrusPay::getApiKey());
$redirectUrl = $response->get_redirect_url();
$response_code = $response->get_resp_code();
if($redirectUrl != "" && $response_code == 200)
{
return $this->redirect($redirectUrl);
}else{
$html = "<p>Response Code is ".$response->get_resp_code()."</p>";
$html .= "<p>Response Message is ".$response->get_resp_msg()."</p>";
2. in C:\wamp\www\haggl\seller\vendor\yiisoft\yii2\base\InlineAction.php – call_user_func_array:{C:\wamp\www\haggl\seller\vendor\yiisoft\yii2\base\InlineAction.php:55}() at line 55
3. in C:\wamp\www\haggl\seller\vendor\yiisoft\yii2\base\Controller.php – yii\base\InlineAction::runWithParams() at line 151
4. in C:\wamp\www\haggl\seller\vendor\yiisoft\yii2\base\Module.php – yii\base\Controller::runAction() at line 455
5. in C:\wamp\www\haggl\seller\vendor\yiisoft\yii2\web\Application.php – yii\base\Module::runAction() at line 83
6. in C:\wamp\www\haggl\seller\vendor\yiisoft\yii2\base\Application.php – yii\web\Application::handleRequest() at line 375
7. in C:\wamp\www\haggl\seller\index.php – yii\base\Application::run() at line 22
16171819202122
require(__DIR__ . '/../common/config/main-local.php'),
require(__DIR__ . '/config/main.php'),
require(__DIR__ . '/config/main-local.php')
);
$application = new yii\web\Application($config);
$application->run();
Solution:
In "index.php" file which is present in Yii2 folder make changes as given below,
<?php
defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'dev');
// register CitrusPay path to yii enviornment
set_include_path(realpath(dirname(__FILE__)) . '/components/CitrusPay/lib'.PATH_SEPARATOR.get_include_path());
// register CitrusPay autoloader
require(realpath(dirname(__FILE__)) . '/components/CitrusPay/lib/CitrusPay.php');
// register Composer autoloader
require(__DIR__ . '/vendor/autoload.php');
// include Yii class file
require(__DIR__ . '/vendor/yiisoft/yii2/Yii.php');
Yii::$classMap['CitrusPay'] = (realpath(dirname(__FILE__)) . '/components/CitrusPay/lib/CitrusPay.php');
// load application aliases
require(__DIR__ . '/../common/config/aliases.php');
// load application configuration
$config = yii\helpers\ArrayHelper::merge(
require(__DIR__ . '/../common/config/main.php'),
require(__DIR__ . '/../common/config/main-local.php'),
require(__DIR__ . '/config/main.php'),
require(__DIR__ . '/config/main-local.php')
);
$application = new yii\web\Application($config);
$application->run();
And then call the library classes using the "use" keyword
For Example:
use CitrusPay;
And it will work like a charm..
Related
Someone else has configured my computer and I don't know how. The script to run my application is not in web/index.php but public_html/index.php. I don't know if that makes a difference. The application uses a mysql database and a react front end and is hosted on aws. Here is the code from the public_html/index.php file:
<?php
require(__DIR__ . '/../vendor/autoload.php');
if (file_exists(__DIR__ . '/../.env')) {
(new \Dotenv\Dotenv(__DIR__ . '/../'))->load();
}
$yii_env = 'test';
switch (getenv('ENVIRONMENT')) {
case 'local': $yii_env = 'dev'; break;
case 'qa': $yii_env = 'test'; break;
case 'prod': $yii_env = 'prod'; break;
}
defined('YII_DEBUG') or define('YII_DEBUG', getenv('DEBUG'));
defined('YII_ENV') or define('YII_ENV', $yii_env);
require(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php');
require(__DIR__ . '/../common/config/bootstrap.php');
require(__DIR__ . '/../api/config/bootstrap.php');
$config = yii\helpers\ArrayHelper::merge(
require(__DIR__ . '/../common/config/main.php'),
require(__DIR__ . '/../api/config/main.php')
);
$application = new yii\web\Application($config);
$application->run();
How do I access gii?
If you're using apache2. Please check to make sure you have ".htaccess" file in public_html/ folder.
I am trying to send an email with attachment when I used var_dump($filename)it returns the filename and gettype($filename) it returns string. but when I am trying to send an attachment it still returns Unable to open file for reading [filename.pdf] even if $file_attachment was looped I tried to change UploadedFile::getInstancesByName('file_attachment'); to UploadedFile::getInstanceByName('file_attachment'); but nothing happened. Please help me.
This is my controller
if(Yii::$app->request->isPost){
$email = Yii::$app->request->post('email');
$message = Yii::$app->request->post('message');
$file_attachment = UploadedFile::getInstancesByName('file_attachment');
if($file_attachment){
$mail = Yii::$app->mailer->compose()
->setFrom(['myemail#gmail.com' => 'My Email'])
->setTo($email)
->setSubject('My Subject')
->setHtmlBody($message);
foreach ($file_attachment as $file) {
$filename = $file->baseName. '.' . $file->extension;
$mail->attach($filename);
}
//$mail->send();
//echo gettype($filename);
// var_dump($filename);
$mail->send();
}else{
$mail = Yii::$app->mailer->compose()
->setFrom(['myemail#gmail.com' => 'My Email'])
->setTo($email)
->setSubject('My Subject')
->setHtmlBody($message)
->send();
}
}
This is the view
<?php
echo FileInput::widget([
'name' => 'file_attachment',
'attribute' => 'file_attachment',
'options' => ['multiple' => true]
]);
?>
The baseName property of yii\web\UploadedFile contains the original filename but the file is not present on the server under its original filename. You need to use tempName property that contains path to the uploaded file on server.
Your for each cycle that attaches files to mail should look like:
foreach ($file_attachment as $file) {
$filename = $file->baseName. '.' . $file->extension;
$mail->attach(
$file->tempName,
['fileName' => $filename]
);
}
It might also be a good idea to check hasError property of yii\web\UploadedFile before attempting to attach file to see if the upload was successful.
Also make sure that you've set 'enctype' => 'multipart/form-data' in you form options when you are not using ActiveForm with ActiveField::fileInput() otherwise the files might not be uploaded.
In following code, I'm trying to create a new category (name = 'To be deleted') and then I want to delete it.
first part of the code working fine and I can see new record in the database every time I run the unit test
but I'm having a problem with recode deletion. what's wrong with my code
public function testCategoryDeletion()
{
$user = \App\User::find(1);
//dd($user->name);
$category = \App\Category::create(['name' => 'To be deleted']);
//dd($category->id);
$response = $this->actingAs($user, 'api')
->json('DELETE', "/api/category/{$category->id}")
->assertStatus(200)->assertJson([
'status' => true,
'message' => 'Category Deleted',
]);
}
Test case output
PS C:\xampp\htdocs\Chathura\Vue_Laravel_API> ./vendor/bin/phpunit
PHPUnit 7.5.7 by Sebastian Bergmann and contributors.
. . . . . . . 7 / 7 (100%)
Time: 1.53 minutes, Memory: 18.00 MB
OK (7 tests, 65 assertions)
PS C:\xampp\htdocs\Chathura\Vue_Laravel_API>
In database, recode is created but not deleted,
I fixed the controller as follows and now its working,
public function destroy(Category $category)
{
$status = $category->delete();
return response()->json([
'status' => $status,
'message' => $status ? 'Category Deleted' : 'Error Deleting Category'
]);
}
API routes
Route::group(['middleware' => 'auth:api'], function(){
Route::resource('/task', 'TaskController');
Route::resource('/category', 'CategoryController');
Route::get('/category/{category}/tasks', 'CategoryController#tasks');
});
I've seen similiar questions but haven't found a working solution. Here is my index.php file:
<?php
// comment out the following two lines when deployed to production
defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'dev');
require(__DIR__ . '/../vendor/autoload.php');
require(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php');
$config = require(__DIR__ . '/../config/web.php');
(new yii\web\Application($config))->run();
require(__DIR__ . '/../config/aliases.php');
As you can see, it requires config/aliases.php file, whose contents are:
<?php
Yii::setAlias('message_files_root', '#webroot/message_files');
Yii::setAlias('message_files', '#web/message_files');
CallingYii::getAlias('#message_files_root') works fine within the index.php, but when it is in a controller file, it says Invalid path alias: #message_files_root.
Here is my controller's action (the rest of the code is fully commented):
public function actionIndex()
{
var_dump(Yii::getAlias('#message_files_root'));
$message = new Message();
if you invoke Yii::getAlias() static method from controller, Yii::setAlias(#web) does not get register yet.
you may refer to the web/index file.
web/index.php
require(__DIR__ . '/../../vendor/autoload.php');
require(__DIR__ . '/../../vendor/yiisoft/yii2/Yii.php');
require(__DIR__ . '/../../common/config/bootstrap.php'); --> Yii::setAlias(#web) are not listed here
require(__DIR__ . '/../config/bootstrap.php');
$config = yii\helpers\ArrayHelper::merge(
require(__DIR__ . '/../../common/config/main.php'),
require(__DIR__ . '/../../common/config/main-local.php'),
require(__DIR__ . '/../config/main.php'),
require(__DIR__ . '/../config/main-local.php')
);
$application = new yii\web\Application($config);
//var_dump(Yii::getAlias('#web'));die; --> imitate invoke #web alias fail
$application->run(); --> here both #web alias and aliases from $config got registered.
//var_dump(Yii::getAlias('#web'));die; --> imitate invoke #web alias success
my guess is, the config got registered first before the default #web aliases being register. Thats why invoking #web aliases from bootrap config fail.
thus, u need to use absolute path for the second paramter. use this..
yii2 advanced template
Yii::setAlias('#message_files_root', dirname(dirname(__DIR__)) . '/frontend/web/message_files');
yii2 basic template
Yii::setAlias('#message_files_root', dirname(__DIR__) . '/web/message_files');
I am NOT claiming that is the nicest way to do that, but it works the way I wanted it to work – when an alias can include other aliases.
<?php
// comment out the following two lines when deployed to production
defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'dev');
require(__DIR__ . '/../vendor/autoload.php');
require(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php');
$config = require(__DIR__ . '/../config/web.php');
$app = new class($config) extends yii\web\Application
{
protected function bootstrap()
{
parent::bootstrap();
require(__DIR__ . '/../config/aliases.php');
}
};
$app->run();
I have a console command to generate user report. I want to call the same from my web application. I am using Yii 2.0.0 beta version.I tried to follow answers given in this post How to call a console command in web application action in Yii?
Since Yii 2.0 structure is very different from Yii 1.1 ,I get errors if I try to include command/userReportController.php .Can someone guide me on this?
You should use an extension like https://github.com/vova07/yii2-console-runner-extension
I think this is the simplest solution:
$controller = new YourConsoleController(Yii::$app->controller->id, Yii::$app);
$controller->actionYourConsoleAction();
use this code:
$application = new yii\console\Application($config);
$application->runAction('controller/action');
I'm using this method instead of yii console command, because I'm running Yii on managed VPS where unix commands are not supported in cron, only php scripts.
To run it this way instead of console, the yii configuration must be initialized first, of course:
defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'dev');
require(__DIR__ . '/vendor/autoload.php');
require(__DIR__ . '/vendor/yiisoft/yii2/Yii.php');
require(__DIR__ . '/common/config/bootstrap.php');
require(__DIR__ . '/console/config/bootstrap.php');
$config = yii\helpers\ArrayHelper::merge(
require(__DIR__ . '/common/config/main.php'),
require(__DIR__ . '/common/config/main-local.php'),
require(__DIR__ . '/console/config/main.php'),
require(__DIR__ . '/console/config/main-local.php')
);
Let's say your action is called file/convert, and you want to run it in the background:
$cmd = "cd /home/site.com/public_html/yii && nohup php yii file/convert >/dev/null &";
shell_exec( $cmd );
yii2-console-runner-extension keeps loading.
Try this:
It's yii (without ext, not yii.bat)
Make sure that php folder added to PATH variable (Windows)
$op = shell_exec ( 'absolute/path/to/yii ' . 'cache/flush-all' );
\yii\helpers\VarDumper::dump($op, 10, 1);
Output:
The following cache components were processed:
* cache (yii\\caching\\FileCache)
If you really want to run Console controller by Web controller likes migrate:
public function actionMigrate()
{
// Keep current application
$oldApp = \Yii::$app;
// Load Console Application config
$config = require \Yii::getAlias('#app'). '/config/console.php';
new \yii\console\Application($config);
$result = \Yii::$app->runAction('migrate', ['migrationPath' => '#app/migrations/', 'interactive' => false]);
// Revert application
\Yii::$app = $oldApp;
return;
}
Above sample code is for yii2-app-basic template, you could change path for yii2-app-advanced template.