Object of class Illuminate\Routing\Redirector could not be converted to string. srmklive/laravel-paypal - laravel

I am currently working on a paypal checkout using paypal and https://github.com/srmklive/laravel-paypal. I'm using the express checkout which I modified it a little bit to fit the requirements of the my project. During testing it is working in a couple of tries, paypal show and payment executes properly but when I tried to run the exact same code. I get this error I don't know what it means.
I tried to check my routes if it all of the errors happens to my routes but all of it are working properly. I also tried dump and die like dd("check") just to check if its really going to my controller and it does. I did this in the method "payCommission" (this where the I think the error happens)
This is my route for the controller
api.php
Route::get('service/commissionfee/payment' , 'api\service\ExpressPaymentController#payCommission');
Route::get('paypal/ec-checkout-success', 'api\service\ExpressPaymentController#payCommissionSuccess');
ExpressPaymentController.php
<?php
namespace App\Http\Controllers\api\service;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Srmklive\PayPal\Services\ExpressCheckout;
class ExpressPaymentController extends Controller
{
protected $provider;
public function __construct()
{
try {
$this->provider = new ExpressCheckout();
}
catch(\Exception $e){
dd($e);
}
}
public function payCommission(Request $request)
{
$recurring = false;
$cart = $this->getCheckoutData($recurring);
try {
$response = $this->provider->setExpressCheckout($cart, $recurring);
return redirect($response['paypal_link']);
} catch (\Exception $e) {
dd($e);
return response()->json(['code' => 'danger', 'message' => "Error processing PayPal payment"]);
}
}
public function payCommissionSuccess(Request $request)
{
$recurring = false;
$token = $request->get('token');
$PayerID = $request->get('PayerID');
$cart = $this->getCheckoutData($recurring);
// ? Verify Express Checkout Token
$response = $this->provider->getExpressCheckoutDetails($token);
if (in_array(strtoupper($response['ACK']), ['SUCCESS', 'SUCCESSWITHWARNING'])) {
if ($recurring === true) {
$response = $this->provider->createMonthlySubscription($response['TOKEN'], 9.99, $cart['subscription_desc']);
if (!empty($response['PROFILESTATUS']) && in_array($response['PROFILESTATUS'], ['ActiveProfile', 'PendingProfile'])) {
$status = 'Processed';
} else {
$status = 'Invalid';
}
} else {
// ? Perform transaction on PayPal
$payment_status = $this->provider->doExpressCheckoutPayment($cart, $token, $PayerID);
$status = $payment_status['PAYMENTINFO_0_PAYMENTSTATUS'];
}
return response()->json(['success' => "payment complete"]);
}
}
private function getCheckoutData($recurring = false)
{
$data = [];
$order_id = 1;
$data['items'] = [
[
'name' => 'Product 1',
'price' => 9.99,
'qty' => 1,
],
];
$data['return_url'] = url('api/paypal/ec-checkout-success');
// !
$data['invoice_id'] = config('paypal.invoice_prefix').'_'.$order_id;
$data['invoice_description'] = "Commission Fee payment";
$data['cancel_url'] = url('/');
$total = 0;
foreach ($data['items'] as $item) {
$total += $item['price'] * $item['qty'];
}
$data['total'] = $total;
return $data;
}
}
Error I am getting
Object of class Illuminate\Routing\Redirector could not be converted to string
Thank you in advance

you may just go to the config/paypal.php and edit
'invoice_prefix' => env('PAYPAL_INVOICE_PREFIX', 'Life_saver_'),
you may use _ underline in this like Life_saver_, dont forget use underline at the end too.

Related

How to make all scripts not run when api response error?

I use laravel and I use command laravel to synchronize my database
My command laravel to call api to synchronize like this :
<?php
namespace App\Console\Commands;
...
class MySynchronize extends Command
{
...
public function handle()
{
DB::statement("SET foreign_key_checks=0");
Vendor::truncate();
Location::truncate();
Item::truncate();
DB::statement("SET foreign_key_checks=1");
$requestContent = [
'auth' => ['Administrator', 'www.Secret.com', 'ntlm']
];
//call api vendor
try {
$client = new GuzzleHttpClient();
$apiRequest = $client->request('GET', "http://www.secret.com:1234/ODATA/ODataV4/Company('secret2018')/Vendor", $requestContent);
$jsonResponse = json_decode($apiRequest->getBody(), true);
$data = [];
foreach ($jsonResponse['value'] as $value) {
$created_at = Carbon::now();
$last_modified_at = Carbon::parse($value['Last_Date_Modified']);
$data[] = [
'id' => $value['Code'],
'name' => $value['Name'],
'last_modified_at' => $last_modified_at,
'created_at'=> $created_at,
'updated_at'=> $created_at
];
}
DB::table('vendors')->insert($data);
} catch (RequestException $re) {
// For handling exception.
}
//call api location
try {
$client = new GuzzleHttpClient();
$apiRequest = $client->request('GET', "http://www.secret.com:1234/ODATA/ODataV4/Company('secret2018')/Location", $requestContent);
$jsonResponse = json_decode($apiRequest->getBody(), true);
$data = [];
foreach ($jsonResponse['value'] as $value) {
$created_at = Carbon::now();
$data[] = [
'id' => $value['Code'],
'description' => $value['Name'],
'created_at'=> $created_at,
'updated_at'=> $created_at
];
}
DB::table('locations')->insert($data);
} catch (RequestException $re) {
// For handling exception.
}
//call api item
try {
$client = new GuzzleHttpClient();
$apiRequest = $client->request('GET', "http://www.secret.com:1234/ODATA/ODataV4/Company('secret2018')/Item", $requestContent);
$jsonResponse = json_decode($apiRequest->getBody(), true);
$data = [];
foreach ($jsonResponse['value'] as $value) {
$last_modified_at = Carbon::parse($value['Last_Date_Modified']);
$created_at = Carbon::now();
$data[] = [
'id' => $value['Code'],
'description' => $value['Description'],
'vendor_code' => $value['Vendor_Code']?$value['Vendor_Code']:null,
'last_modified_at' => $last_modified_at,
'created_at'=> $created_at,
'updated_at'=> $created_at
];
}
\DB::table('items')->insert($data);
} catch (RequestException $re) {
// For handling exception.
}
// send output
echo 'synchronize success';
}
}
First I delete all data in the table using truncate. If it is deleted then it will call the api for the insert process to table
My problem is when an error occurs on the api server. For example, server is died or another error. It makes the data in my database empty because I've run truncate
How do I improve the script so that when an error occurs on the api, truncate is not executed?
What is the best way? Do you use try catch in the try catch to handle it?
You should take a look at DB transactions: https://laravel.com/docs/5.7/database#database-transactions
If an exception is thrown within the transaction Closure, the transaction will automatically be rolled back.
So something like that could work:
public function handle()
{
DB::transaction(function () {
// your DB statements
try {
// external API calls
} catch (SomeException $e) {
// If for any reasons you want to catch the exception inside the closure
// (logging maybe?), make sure to rethrow it.
// Otherwise, Laravel won't know it needs to rollback the DB changes
throw $e;
}
});
}

How to call shell function to another Controller - Cakephp

I'm using this function in my shell to send email
Edit :
UsersShell
<?php
namespace App\Shell;
use Cake\Console\Shell;
use Cake\Log\Log;
use Cake\Controller\Component;
use Cake\Controller\ComponentRegistry;
use App\Controller\Component\EmailComponent;
class UsersShell extends Shell
{
public function initialize()
{
parent::initialize();
$this->loadModel('Users');
//Load Component
$this->Email = new EmailComponent(new ComponentRegistry());
}
public function mail()
{
$to = 'exemple#gmail.com';
$subject = 'Hi buddy, i got a message for you.';
$message = 'User created new event';
try {
$mail = $this->Email->send_mail($to, $subject, $message);
print_r($mail);
} catch (Exception $e) {
echo 'Message could not be sent. Mailer Error: ', $mail-
>ErrorInfo;
}
exit;
}
I would like to know how can I call it in my controller ? here
Edit : Events is located in the plugins folder
EventsController
<?php
namespace FullCalendar\Controller;
use FullCalendar\Controller\FullCalendarAppController;
use Cake\Routing\Router;
use Cake\Event\Event;
use Cake\Console\ShellDispatcher;
class EventsController extends FullCalendarAppController
{
public $name = 'Events';
public function add()
{
$event = $this->Events->newEntity();
if ($this->request->is('post')) {
$event = $this->Events->patchEntity($event, $this->request->data);
if ($this->Events->save($event)) {
/* $shell = new ShellDispatcher();
$output = $shell->run(['cake', 'users'], ['plugin' =>
'Events']);
if (0 === $output) {
$this->Flash->success('Success from shell command.');
} else {
$this->Flash->error('Failure from shell command.'); */
$this->Flash->success(__('The event has been saved.'));
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error(__('The event could not be saved. Please,
try again.'));
}
}
$this->set('eventTypes', $this->Events->EventTypes->find('list'));
$this->set(compact('event'));
$this->set('_serialize', ['event']);
$this->set('user_session', $this->request->session()-
>read('Auth.User'));
$this->viewBuilder()->setLayout('user');
}
As you can see i used the shell dispatched i'm not sure if it's correct
but i'm getting failure
Thanks !
Edit :
EmailComponent
<?php
namespace App\Controller\Component;
use Cake\Controller\Component;
use Cake\Core\App;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require ROOT. '/vendor/phpmailer/phpmailer/src/Exception.php';
require ROOT. '/vendor/phpmailer/phpmailer/src/PHPMailer.php';
require ROOT. '/vendor/phpmailer/phpmailer/src/SMTP.php';
class EmailComponent extends Component {
public function send_mail($to, $subject, $message)
{
// date_default_timezone_set('Asia/Calcutta');
$sender = "exemple#gmail.com"; // this will be overwritten by GMail
$header = "X-Mailer: PHP/".phpversion() . "Return-Path: $sender";
$mail = new PHPMailer();
$mail->SMTPDebug = 2; // turn it off in production
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = "exemple#gmail.com";
$mail->Password = "xxxx";
$mail->SMTPSecure = "tls"; // ssl and tls
$mail->Port = 587; // 465 and 587
$mail->SMTPOptions = array (
'tls' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
),
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
$mail->From = $sender;
$mail->FromName = "From Me";
$mail->AddAddress($to);
$mail->isHTML(true);
$mail->CreateHeader($header);
$mail->Subject = $subject;
$mail->Body = nl2br($message);
$mail->AltBody = nl2br($message);
// return an array with two keys: error & message
if(!$mail->Send()) {
return array('error' => true, 'message' => 'Mailer Error: ' . $mail->ErrorInfo);
} else {
return array('error' => false, 'message' => "Message sent!");
}
}
}
Correct me if I'm wrong. First your shell must be started something like this.
class UsersShell extends AppShell {
public function main(){ //change name here to main
$to = 'exemple#gmail.com';
$subject = 'Hi buddy, i got a message for you.';
$message = 'User created new event';
try {
$mail = $this->Email->send_mail($to, $subject, $message);
print_r($mail);
} catch (Exception $e) {
echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
exit;
}
}
By the way, if you want to check output, you must return something like true or false. Otherwise, there is no point to check output after execute the shell.
First Check Shell Command Run in CakePHP-CLI. Like this
bin/cake users mail
if shell command successfully running. Shell Class Fine.
Next Use Shell in Controller
<?php
namespace App\Controller;
use App\Controller\AppController;
use Cake\Console\ShellDispatcher;
class PagesController extends AppController
{
/**
* Run shell command
*/
public function run()
{
$shell = new ShellDispatcher();
$output = $shell->run(['cake', 'users', 'mail']);
// $output = $shell->run(['cake', 'users', 'mail', 'email']); // [pass arguments]
// debug($output);
if ($output === 0) {
echo "Shell Command execute";
} else {
echo "Failure form shell command";
}
exit;
}
}
Change Shell Function : if mail not sent run $this->abort() function and return (int) 1 and mail sent successfully run $this->out() function and return (int) 0
/**
* Send Mail with shell command
*/
public function mail()
{
$to = 'mail#gmail.com';
$subject = 'Hi buddy, i got a message for you.';
$message = 'Nothing much. Just test out my Email Component using PHPMailer.';
$mail = $this->Email->send_mail($to, $subject, $message);
// debug($mail);
if ($mail['error'] === false) {
$this->out("Mail Successfully Sent For :: ". $to);
} else {
$this->abort("Mail Error.");
}
}

Laravel : API response with pagination parameter

I want to pass pagination parameters through POSTMAN and pass sort,order,limits in my model to get query with paginate.? how can i do this? Currently it return error.
Currently my route :
http://localhost:8000/api/allpost
My PostController function :
public function index(Request $request)
{
try {
$allPost = Post::allUserPost();
if($allPost !="" && count($allPost)>0) {
return [
'status_code' => 200,
'message' => "Post retrieved successfully",
'PostDetails' => $allPost,
];
} else {
return response()->json([
'message' => "Post data not found",
'status_code' => 403,
]);
}
} catch (\Exception $ex) {
return response()->json([
'message' => "Internal server error",
'status_code' => 500,
]);
}
}
And my POST model function :
public static function allUserPost(Request $request){
$sort = $this->parameters->sort();
$order = $this->parameters->order();
$limit = $this->parameters->limit();
$userPost = Post::with(['product','categories','user.userDetails'])->whereStatus("Active")->orderBy($sort, $order)->paginate($limit)->get();
$userPost_array = $userPost->toArray();
foreach ($userPost_array as $key => $value) {
# code...
$attributes_arr = array_column($userPost_array[$key]['categories'], 'attribute_id');
$category_ids = Attribute::whereIn("id",$attributes_arr)->pluck('category_id');
$category_ids = array_unique($category_ids->toArray());
$category_details_with_att = Post::getCategoryWithAttributeData($attributes_arr,$category_ids);
unset($userPost_array[$key]["categories"]);
$userPost_array[$key]["categories"] = $category_details_with_att->toArray();
}
return $userPost_array;
}
Currently it returns error
Type error: Too few arguments to function App\Post::allUserPost(), 0 passed in D:\xampp\htdocs\IDM\app\Api\V1\Controllers\Front\PostController.php on line 30 and exactly 1 expected
So how can i pass parameters in postmen and whats the solution for this error?
First change this line to $allPost = Post::allUserPost();
$allPost = Post::allUserPost($request);
and then change this code
$sort = $this->parameters->sort();
$order = $this->parameters->order();
$limit = $this->parameters->limit();
To
$sort = $request->sort;
$order = $request->order;
$limit = $request->limit;
and then you can pass these paramets in a query string like
http://localhost:8000/api/allpost?sort=somesort&order=asc&limit=10
Also chage this line
$userPost = Post::with(['product','categories','user.userDetails'])->whereStatus("Active")->orderBy($sort, $order)->paginate($limit)->get();
to
$userPost = Post::with(['product','categories','user.userDetails'])->whereStatus("Active")->orderBy($sort, $order)->paginate($limit);
You are missing an argument when calling the allUserPost function inside the try block.
It should be
$allPost = Post::allUserPost($request);
and then you can retrieve the parameters from the $request variable.
Just change this line in your code
$allPost = Post::allUserPost($request);
And then in your function, you have to change your request type. And after that you have to do one more change only use paginate() method not with get() method.
public static function allUserPost(Request $request){
$sort = $request->sort;
$order = $request->order;
$limit = $request->limit;
$userPost = Post::with(['product','categories','user.userDetails'])->whereStatus("Active")->orderBy($sort, $order)->paginate($limit);
$userPost_array = $userPost->toArray();
foreach ($userPost_array as $key => $value) {
$attributes_arr = array_column($userPost_array[$key]['categories'], 'attribute_id');
$category_ids = Attribute::whereIn("id",$attributes_arr)->pluck('category_id');
$category_ids = array_unique($category_ids->toArray());
$category_details_with_att = Post::getCategoryWithAttributeData($attributes_arr,$category_ids);
unset($userPost_array[$key]["categories"]);
$userPost_array[$key]["categories"] = $category_details_with_att->toArray();
}
return $userPost_array;
}
I hope this will help you.

how to i print session on view page in zend framework

I am very new to zend framework, and going to add session in my small application but i dont know how to print session variable to my header.phtml page.
UsersTable.php
public function fetchbyWhere($where) {
$rowset = $this->tableGateway->select($where);
$row = $rowset->current();
if (!$row) {
return;
}
return $row;
}
IndexController.php
<?php
namespace Application\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Application\Model\Users; // <-- Add this import
use Zend\Session\Container; // We need this when using sessions
class IndexController extends AbstractActionController {
protected $usersTable;
public function getUsersTable() {
if (!$this->usersTable) {
$sm = $this->getServiceLocator();
$this->usersTable = $sm->get('Application\Model\UsersTable');
}
return $this->usersTable;
}
public function indexAction() {
$request = $this->getRequest();
if ($request->isPost()) {
$user = $request->getPost('txtuser');
$pass = $request->getPost('txtpassword');
$wher = array('username' => $user, 'password' => $pass);
$resultSet = $this->getUsersTable()->fetchbyWhere($wher);
//var_dump($resultSet);
if($resultSet)
{
$user_session = new Container('user');
$user_session->ses_user = $resultSet->username;
return new ViewModel(array(
'msg' => 'valid user',
'sesuser' => $user_session->ses_user,
));
}
else {
return new ViewModel(array(
'msg' => 'not a valid user',
));
}
} else {
return new ViewModel();
}
}
}
now i dont know how to print this session on header.phtml page.
You need to add session container in your header file as well.
Add following line in your header file.
<?php
use Zend\Session\Container; // We need this when using sessions
$user_session = new Container('user');
if(isset($user_session->ses_user))
echo "user:".$user_session->ses_user;
?>

How to auto fill shipping method Magento for onepagechekout

I want to auto fill the shipping method automatically and not show it on the onepage checkout. I was able to hide the shipping method on my Onepagechekout by changing this on app/code/local/Mage/Checkout/controllers/OnepageController.php:
/**
* save checkout billing address
*/
public function saveBillingAction()
{
if ($this->_expireAjax()) {
return;
}
if ($this->getRequest()->isPost()) {
// $postData = $this->getRequest()->getPost('billing', array());
// $data = $this->_filterPostData($postData);
$data = $this->getRequest()->getPost('billing', array());
$customerAddressId = $this->getRequest()->getPost('billing_address_id', false);
if (isset($data['email'])) {
$data['email'] = trim($data['email']);
}
$result = $this->getOnepage()->saveBilling($data, $customerAddressId);
if (!isset($result['error'])) {
/* check quote for virtual */
if ($this->getOnepage()->getQuote()->isVirtual())
{
$result['goto_section'] = 'payment';
$result['update_section'] = array(
'name' => 'payment-method',
'html' => $this->_getPaymentMethodsHtml()
);
} elseif (isset($data['use_for_shipping']) && $data['use_for_shipping'] == 1)
{
$result['goto_section'] = 'payment';
$result['update_section'] = array(
'name' => 'payment-method',
'html' => $this->_getPaymentMethodsHtml()
);
$result['allow_sections'] = array('shipping');
$result['duplicateBillingInfo'] = 'true';
} else {
$result['goto_section'] = 'shipping';
}
}
$this->saveShippingMethodAction();
$this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
}
}
As you can see I changed the link where I redirect the next step after billing action to the payment step.
In order to auto-save a shipping method, I added
$this->saveShippingMethodAction();
at the end of the function, and this method looks like here:
public function saveShippingMethodAction()
{
$this->_expireAjax();
if ($this->getRequest()->isPost()) {
/* $this->savePaymentAction(); */
$data = $this->getRequest()->getPost('shipping_method', 'flatrate_flatrate');
$result = $this->getOnepage()->saveShippingMethod($data);
$this->getResponse()->setBody(Zend_Json::encode($result));
}
}
So what I did is try to include automatically flatrate_flatrate method as the default one.
But when I try to finish a sale, it says I didn't specify a shipping method. Any idea on why it doesn't work?
Now you need to force the shipping_method in the quote and in the session :
$forcedMethod = "your_method_code";
$this->getOnePage()->getQuote()
->getShippingAddress()
->setShippingMethod($forcedMethod)
->save();
Mage::getSingleton('checkout/session')->setShippingMethod($forcedMethod);
And just before you call :
$this->saveShippingMethodAction();
add a :
$this->getRequest()->setPost('shipping_method', $forcedMethod);
It should work ;)

Resources