I am using Laravel and Moltin/Cart to build the cart system for an online store. So far, I have managed to integrate the Cart system and continue with paypal. Though, in my controller I have a constructor that prevents the user from viewing, adding or removing items from the cart unless he is authenticated.
public function __construct() {
parent::__construct();
$this->beforeFilter('csrf', array('on'=>'post'));
$this->beforeFilter('auth', array('only'=>array('postAddtocart', 'getCart', 'getRemoveitem')));
}
In order to make paypal to work I had to add several hidden inputs in my view that get the necessary values and passing them to paypal's paying page, like so:
<input type="hidden" name="cmd" value="_xclick">
<input type="hidden" name="business" value="office@shop.com">
<input type="hidden" name="item_name" value="eCommerce Store Purchase">
<input type="hidden" name="currency_code" value="EUR">
<input type="hidden" name="amount" value="{{ Cart::total() }}">
<input type="hidden" name="first_name" value="{{ Auth::user()->firstname }}">
<input type="hidden" name="last_name" value="{{ Auth::user()->lastname }}">
<input type="hidden" name="email" value="{{ Auth::user()->email }}">
{{ HTML::link('/', 'Continue Shopping', array('class'=>'btn btn-default')) }}
<input type="submit" value="Checkout with Paypal" class="btn btn-primary">
The problem is that the user shouldn't be logged in to view, add or remove items from the cart, but to be asked for a login when the submit button is clicked. If I remove the filters from my constructor then, I get a "Trying to get property of non-object" error because of the hidden inputs that make use of the Auth class. I have tried to add a blade if auth::check condition but that wasn't the solution. Any suggestions?
Answer
This did the trick:
@if(!Auth::check())
{{ HTML::link('users/signin', 'Sign in to pay', array('class'=>'btn btn-primary')) }}
@else
<input type="hidden" name="first_name" value="{{ Auth::user()->firstname }}">
<input type="hidden" name="last_name" value="{{ Auth::user()->lastname }}">
<input type="hidden" name="email" value="{{ Auth::user()->email }}">
{{ HTML::link('/', 'Continue Shopping', array('class'=>'btn btn-default')) }}
<input type="submit" value="Checkout with Paypal" class="btn btn-primary">
@endif
Also, I removed the filters from the constructor and changed the redirection in the sign in function, so that it redirects to the previously accessed page.
Using a session variable to hold the previously visited url
In order to go back to the previously visited page you need to use session::put
.
So, in the getSignin function add:
Session::put('previous_url', URL::previous());
And in the postSignin function you need to retrieve the previous_url variable, which is stored in the session, like so:
if ( Session::has('previous_url') )
{
$url = Session::get('previous_url');
Session::forget('previous_url');
return Redirect::to($url);
}
如何将签出按钮替换为登录按钮,然后再次重定向到购物车签出呢?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.