I am trying to submit a payment against Paypal Sandbox, using the Omnipay Paypal module.
I think that I have everything set up in terms of sandbox account, and my transaction is working if I don't specify a card as payment method. I am correctly redirected to the Paypal sandbox, where I need to enter my test credentials before I can submit my payment. Then I am redirected back to my original web site, where I can store the confirmed payment details.
However, when I want to use a credit card, I get an error message saying : Payee account is invalid.
Here is my controller code :
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Omnipay\Omnipay;
use Omnipay\Common\CreditCard;
use App\Payment;
class PaymentController extends Controller
{
private $gateway;
public function __construct()
{
$this->gateway = Omnipay::create('PayPal_Rest');
$this->gateway->setClientId(env('PAYPAL_CLIENT_ID'));
$this->gateway->setSecret(env('PAYPAL_CLIENT_SECRET'));
$this->gateway->setTestMode(env('PAYPAL_SANDBOX_MODE'));;
}
/**
* Call a view.
*/
public function index()
{
return view('payment');
}
/**
* Initiate a payment on PayPal.
*
* #param \Illuminate\Http\Request $request
*/
public function charge(Request $request)
{
if($request->input('submit'))
{
$card = new CreditCard(array(
'number' => '<my test credit card number>',
'expiryMonth' => '03',
'expiryYear' => '2024',
'cvv' => '<my ccv>',
));
try {
$response = $this->gateway->purchase(array(
'amount' => $request->input('amount'),
'currency' => env('PAYPAL_CURRENCY'),
'returnUrl' => url('success'),
'cancelUrl' => url('error'),
'card' => $card
))->send();
if ($response->isRedirect()) {
$response->redirect(); // this will automatically forward the customer
} else {
// not successful
return $response->getMessage();
}
} catch(Exception $e) {
return $e->getMessage();
}
}
}
/**
* Charge a payment and store the transaction.
*
* #param \Illuminate\Http\Request $request
*/
public function success(Request $request)
{
// Once the transaction has been approved, we need to complete it.
if ($request->input('paymentId') && $request->input('PayerID'))
{
$transaction = $this->gateway->completePurchase(array(
'payer_id' => $request->input('PayerID'),
'transactionReference' => $request->input('paymentId'),
));
$response = $transaction->send();
if ($response->isSuccessful())
{
// The customer has successfully paid.
$arr_body = $response->getData();
// Insert transaction data into the database
$payment = new Payment;
$payment->payment_id = $arr_body['id'];
$payment->payer_id = $arr_body['payer']['payer_info']['payer_id'];
$payment->payer_email = $arr_body['payer']['payer_info']['email'];
$payment->amount = $arr_body['transactions'][0]['amount']['total'];
$payment->currency = env('PAYPAL_CURRENCY');
$payment->payment_status = $arr_body['state'];
$payment->save();
return "Payment is successful. Your transaction id is: ". $arr_body['id'];
} else {
return $response->getMessage();
}
} else {
return 'Transaction is declined';
}
}
/**
* Error Handling.
*/
public function error()
{
return 'User cancelled the payment.';
}
}
Can you see where my problem could be ?
Related
I am working on Laravel passport api in which i am using spatie package for user role's and permission's. I have to perform certain operation ('store','view','update','delete') based on user permission's.
For this purpose i have created a trait and used in controller but it is not working correctly.
On every api request it throw's an exception "This action is unauthorized" either the user has permission or not.
Authorize Trait :
<?php
namespace App;
/*
* A trait to handle authorization based on users permissions for given controller
*/
trait Authorizable
{
/**
* Abilities
*
* #var array
*/
private $abilities = [
'index' => 'view',
'edit' => 'edit',
'show' => 'view',
'update' => 'edit',
'create' => 'add',
'store' => 'add',
'destroy' => 'delete'
];
/**
* Override of callAction to perform the authorization before it calls the action
*
* #param $method
* #param $parameters
* #return mixed
*/
public function callAction($method, $parameters)
{
if( $ability = $this->getAbility($method) ) {
$this->authorize($ability);
}
return parent::callAction($method, $parameters);
}
/**
* Get ability
*
* #param $method
* #return null|string
*/
public function getAbility($method)
{
$routeName = explode('.', \Request::route()->getName());
$action = array_get($this->getAbilities(), $method);
return $action ? $action . '_' . $routeName[0] : null;
}
/**
* #return array
*/
private function getAbilities()
{
return $this->abilities;
}
/**
* #param array $abilities
*/
public function setAbilities($abilities)
{
$this->abilities = $abilities;
}
}
Routes:
Route::middleware('auth:api')->group(function () {
Route::post('user', 'ApiController#user');
Route::post('view_department', 'DepartmentController#index');
Route::post('add_department', 'DepartmentController#store');
Route::post('edit_department', 'DepartmentController#update');
Route::post('delete_department', 'DepartmentController#destroy');
Route::post('/logout', 'ApiController#logout');
}); // auth middleware ends
Controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Carbon\Carbon;
use App\User;
use App\Authorizable;
use Illuminate\Support\Facades\Validator;
use App\Department;
use Spatie\Permission\Models\Role;
use Spatie\Permission\Models\Permission;
class DepartmentController extends Controller
{
use Authorizable;
//
public function index(Request $request) {
// return response
return response()->json([
'success' => 'You have the permission to view departments!']);
}
//
public function store(Request $request) {
// validate the posted data
$validator = Validator::make($request->all(), [
'name' => 'required|string|unique:departments',
]);
// return errors
if ($validator->fails())
{
return response(['errors'=>$validator->errors()->all()]);
}
$department = new Department;
$department->name = $request->name;
$department->save();
// return response
return response()->json([
'success' => 'Successfully created department!']);
}
}
I am badly stack at it, don't know where i am going wrong. I would highly appreciate if anyone guide me through this.
Thanks,
I have a login functionality from Web and also from API. I have created one Request file where I have written rules for this. For web, it is working perfectly and given output as expected but when I use it in API controller it redirects me to the login page, it should return JSON response.
One more thing, I want to add extra parameter "status" as it is failed or succeeds.
Here is my code
Request File
public function rules()
{
return [
'username' => 'required',
'password' => 'required'
];
}
API controller
public function login(Request $request)
{
$response = array();
$validator = Validator::make($request->all(), [
'username' => 'required',
'password' => 'required'
]);
if ($validator->fails()) {
$response['status'] = false;
$response['message'] = $validator->messages();
return Response::json($response);
}
try {
$username = trim($request->username);
$password = trim($request->password);
$isAuth = $this->userRepository->login($username, $password);
if ($isAuth) {
$user = Auth::user();
$response['status'] = true;
$response['message'] = Lang::get('custom.auth_success');
$response['user_detail'] = $user;
} else {
$response['status'] = false;
$response['message'] = Lang::get('auth.failed');
}
} catch (\Exception $e) {
$response = array();
$response['status'] = false;
$response['message'] = Lang::get('custom.something_wrong');
}
return Response::json($response);
}
and Web controller
public function checkAuth(UserAuthenticate $request)
{
try {
$username = trim($request->username);
$password = trim($request->password);
$isRemember = false;
if (isset($request->remember) && $request->remember == 'on') {
$isRemember = true;
}
$isAuth = $this->userRepository->login($username, $password, $isRemember);
if ($isAuth) {
return redirect('programs');
} else {
return redirect('login')->withInput()->withErrors(['username' => Lang::get('auth.failed')]);
}
} catch (\Exception $e) {
return redirect('login')->withInput()->withErrors(['error' => Lang::get('custom.something_wrong')]);
}
}
Route/web.php
Route::group(['middleware' => ['guest']], function () {
Route::get('login', ['as' => 'login', 'uses' => 'Front\UserController#login']);
});
Route/api.php
Route::post('user/authenticate', 'API\UserController#login');
I looked for the solutions but didn't found anything
Edit: if you want to use extends Request for both validators do the web validation through ajax
Since you're using passport you already have the token so you can skip login
For api your validator needs to extend Request instead of FormRequest
You can't use the same validator, because the web validator extends FormRequest and that return html. Two validators are necessary, there is no way around it.
use App\Http\Requests\Request;
class YourApiRequest extends Request
{
/**
* Determine if the user is authorized to make this request.
*
* #return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules()
{}....
In your normal web request you would have
use Illuminate\Foundation\Http\FormRequest;
class YourWebRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* #return bool
*/
public function authorize()
{
// only allow updates if the user is logged in
return \Auth::check();
}
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules()
{....
In Handler.php you need to add something to the render method (if you return json from your api)
If you do not prefix your routes with api/ then figure out how to check if you are in api
if (strpos($prefix, 'api') !== false) {
if ($exception instanceof ValidationException) {
return response()->json(['success' => false, 'error' => $exception->errors(), 'data' => null], 200);
}
return response()->json(['success' => false, 'error' => $exception->getMessage(), 'data' => null], 200);
}
You may try to over ride the Laravel form request validation's failedValidation() method.
public function failedValidation(Validator $validator)
{
//wantsJson() that checks Accept header of the request and returns TRUE if JSON was requested.
if ($this->wantsJson()) {
throw new HttpResponseException(response()->json(["response" => [
'msg' => $validator->errors()->all(),
]]));
}
}
[Not tested on api calls]
I am very new to testing, but have now found it essential to automate my testing.
I have a test that is working fine up until it gets to the link '/cart' it gets to the link '/cart' no problem, but any other link I try to click afterwards always ends up back at the cart.
here is my error after trying to navigate away from the cart.
Failed asserting that two strings are equal.
--- Expected
+++ Actual
## ##
-'http://ngwenya-mtb.dev/events'
+'http://ngwenya-mtb.dev/cart'
And here is my test script
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class ExampleTest extends TestCase {
//use Illuminate\Foundation\Testing\WithoutMiddleware;
//use DatabaseTransactions;
//use withoutMiddleware;
//use DatabaseMigrations;
/**
*
* A basic functional test example.
* Please choose a unique email address for your new participant
* #return void
*/
public function testNewUserRegistration() {
$this->visit('http://ngwenya-mtb.dev/')
// View Event
->click('View event details')
->seePageIs('/event?id=30')
->click('#enter-race47')
->press('Enter yourself to this race')
->seePageIs('/events/courses/register/addtocart')
//->withSession(['This email is already registered' => 'alert-danger'])
/////////////////////////////////////////////
// Fill the register for for new user
/////////////////////////////////////////////
->type('Bingo', 'first_name')
->type('11111111', 'password')
->type('11111111', 'password_confirmation')
->type(''.substr(md5(time()), 0, 12).'#tesing.com', 'email')
//->check('terms')
->select('Male', 'gender')
->select('1985', 'year')
->select('07', 'month')
->select('21', 'day')
->select('Small', 'shirt_size')
->select('Swaziland ID', 'id_type')
->type('badassnumber', 'id_number')
->select('Swazi', 'nationality')
//Contact details Physical
->type('Dawlish', 'town_physical')
->select('Swaziland', 'country_physical')
->type('864741', 'phone_cell')
//Emergency contact details 1
->type('Simon', 'emergency_contact_1')
->type('Brother', 'emergency_relationship_1')
->type('864741', 'emergency_phone_1');
$this->press('Register');
$this->seePageIs('/cart');
/////////////////////////////////////////////
// Add a new user
/////////////////////////////////////////////
$this->visit('http://ngwenya-mtb.dev/');
$this->click('#events-link')
->seePageIs('/events');
dd($this->response->getContent());exit;
$this->click('#event-30');
$this->seePageIs('/event?id=30');
$this->click('#enter-race48');
$this->press('Enter someone else to this race');
$this->seePageIs('/events/courses/register/addtocart');
}
}
Everything is working fine up until this comment
/////////////////////////////////////////////
// Add a new user
/////////////////////////////////////////////
Here is my Registration controller
<?php
namespace App\Http\Controllers;
use Vinkla\Hashids\HashidsManager;
use Illuminate\Routing\Controller as BaseController;
use Sentinel\FormRequests\RegisterRequest;
use Sentinel\FormRequests\EmailRequest;
use Sentinel\FormRequests\ResetPasswordRequest;
use Sentinel\Repositories\Group\SentinelGroupRepositoryInterface;
use Sentinel\Repositories\User\SentinelUserRepositoryInterface;
use Sentinel\Traits\SentinelRedirectionTrait;
use Sentinel\Traits\SentinelViewfinderTrait;
use Sentry;
use View;
use Request;
use Event;
use Redirect;
use Session;
use Config;
use App\Models\Users;
use Illuminate\Support\Facades\Input;
use Gloudemans\Shoppingcart\Facades\Cart;
class RegistrationController extends BaseController
{
/**
* Traits
*/
use SentinelRedirectionTrait;
use SentinelViewfinderTrait;
/**
* Constructor
*/
public function __construct(
SentinelUserRepositoryInterface $userRepository,
SentinelGroupRepositoryInterface $groupRepository,
HashidsManager $hashids
) {
$this->userRepository = $userRepository;
$this->groupRepository = $groupRepository;
$this->hashids = $hashids;
}
/**
* Show the registration form, if registration is allowed
*
* #return Response
*/
public function registration()
{
// Is this user already signed in? If so redirect to the post login route
if (Sentry::check()) {
return $this->redirectTo('session_store');
}
//If registration is currently disabled, show a message and redirect home.
if (! config('sentinel.registration', false)) {
return $this->redirectTo(['route' => 'home'], ['error' => trans('Sentinel::users.inactive_reg')]);
}
// All clear - show the registration form.
return $this->viewFinder(config('sentinel.view.user_register', 'Sentinel::users.register'));
}
/**
* Process a registration request
*
* #return Response
*/
public function register(RegisterRequest $request)
{
// Gather input
$data = $request->all();
// collect cart items
$email = Input::get('email');
$course_id = Input::get('course_id');
$event_name = Input::get('event_name');
$entry_fee = Input::get('entry_fee');
// check user exists
if (Users::where('email', '=', $email)->exists()) {
// user found
$request->session()->flash('alert-danger', 'Warning: This email is already registered.');
Input::flash();
return View::make('sentinel.users.register')
->with('course_id',$course_id)
->with('event_name',$event_name)
->with('entry_fee',$entry_fee);
}
// Add user and course to cart
if ($course_id) {
$firstUserRowId = Cart::add($course_id, $event_name , 1, $entry_fee, [
'first_name' => Input::get('first_name'),
'last_name' => Input::get('last_name'),
'email' => Input::get('email'),
'no_email' => 0,
'master_user' => 1,
'gender' => Input::get('gender'),
'dob' => Input::get('dob'),
'shirt_size' => Input::get('shirt_size'),
'id_type' => Input::get('id_type'),
'id_number' => Input::get('id_number'),
'nationality' => Input::get('nationality'),
'phone_cell' => Input::get('phone_cell'),
'town_physical' => Input::get('town_physical'),
'country_physical' => Input::get('country_physical'),
'emergency_contact_1' => Input::get('emergency_contact_1'),
'emergency_relationship_1' => Input::get('emergency_relationship_1'),
'emergency_phone_1' => Input::get('emergency_phone_1'),
]);
}
// get email from request
$email = $request->only('email');
foreach ($email as $userModel) {}
// Edit date of birth from request
$year = Input::get('year');
$month = Input::get('month');
$day = Input::get('day');
$dob = $year.'-'.$month.'-'.$day;
$data['dob'] = $dob;
// Attempt Registration
$result = $this->userRepository->store($data);
// Log user in
FunctionsController::loginUser($userModel);
// It worked! Use config to determine where we should go.
return $this->redirectViaResponse('registration_complete', $result);
}
/**
* Activate a new user
*
* #param int $id
* #param string $code
*
* #return Response
*/
public function activate($hash, $code)
{
// Decode the hashid
$id = $this->hashids->decode($hash)[0];
// Attempt the activation
$result = $this->userRepository->activate($id, $code);
// It worked! Use config to determine where we should go.
return $this->redirectViaResponse('registration_activated', $result);
}
/**
* Show the 'Resend Activation' form
*
* #return View
*/
public function resendActivationForm()
{
return $this->viewFinder('Sentinel::users.resend');
}
/**
* Process resend activation request
* #return Response
*/
public function resendActivation(EmailRequest $request)
{
// Resend the activation email
$result = $this->userRepository->resend(['email' => e($request->get('email'))]);
// It worked! Use config to determine where we should go.
return $this->redirectViaResponse('registration_resend', $result);
}
/**
* Display the "Forgot Password" form
*
* #return \Illuminate\View\View
*/
public function forgotPasswordForm()
{
return $this->viewFinder('Sentinel::users.forgot');
}
/**
* Process Forgot Password request
* #return Response
*/
public function sendResetPasswordEmail(EmailRequest $request)
{
// Send Password Reset Email
$result = $this->userRepository->triggerPasswordReset(e($request->get('email')));
// It worked! Use config to determine where we should go.
return $this->redirectViaResponse('registration_reset_triggered', $result);
}
/**
* A user is attempting to reset their password
*
* #param $id
* #param $code
*
* #return Redirect|View
*/
public function passwordResetForm($hash, $code)
{
// Decode the hashid
$id = $this->hashids->decode($hash)[0];
// Validate Reset Code
$result = $this->userRepository->validateResetCode($id, $code);
if (! $result->isSuccessful()) {
return $this->redirectViaResponse('registration_reset_invalid', $result);
}
return $this->viewFinder('Sentinel::users.reset', [
'hash' => $hash,
'code' => $code
]);
}
/**
* Process a password reset form submission
*
* #param $hash
* #param $code
* #return Response
*/
public function resetPassword(ResetPasswordRequest $request, $hash, $code)
{
// Decode the hashid
$id = $this->hashids->decode($hash)[0];
// Gather input data
$data = $request->only('password', 'password_confirmation');
// Change the user's password
$result = $this->userRepository->resetPassword($id, $code, e($data['password']));
// It worked! Use config to determine where we should go.
return $this->redirectViaResponse('registration_reset_complete', $result);
}
}
It seems that when you click and a your link "Register" your redirection fail, so check if you have multiple "Register" links/buttons, and if they are pointing to the right URL
And for easiest debugging, you should make less assertions per Test, you will gain in visibility :)
I am trying to use form request in my REST API built using laravel 5.2. My controller is
public function save(SbcEntityFormRequest $request)
{
$requestData = Input::all();
try {
list($success, $message) = $this->sbcService->saveSbcEntity($requestData);
if ($success) {
return $this->successJsonResponse($request, ['id' => $message]);
}
return $this->errorJsonResponse($request, Response::HTTP_BAD_REQUEST, [$message]);
} catch (Exception $e) {
AppLog::write($e);
$message = [config('messages.save_failed')];
return $this->errorJsonResponse($request, Response::HTTP_BAD_REQUEST, $message);
}
}
My form request is
namespace App\Http\Requests;
use Illuminate\Http\Request;
class SbcEntityFormRequest extends Request
{
/**
* Determine if the user is authorized to make this request.
*
* #return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules()
{
return [
'logo' => 'Required',
'bio' => 'Required|Max:150'
];
}
}
My validation rules are never called. I put a die statement in authorize() function and it is neither called. When I printed $request->all() in the controller it shows empty array. Any Idea on what is wrong here?
I am trying to integrate Paypal into my Laravel 5 site using this package:
http://packalyst.com/packages/package/netshell/paypal
When I go to: /paypal/checkout though, I get this error:
InvalidArgumentException in UrlGenerator.php line 561: Action
App\Http\Controllers\PayPalController#getDone not defined.
This is my route:
Route::get('/paypal/checkout', [
'as' => 'get-paypal-checkout', 'uses' => 'PayPalController#getCheckout'
]);
And this is the PayPalController:
<?php namespace App\Http\Controllers;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Paypal;
use Redirect;
use Illuminate\Http\Request;
class PayPalController extends Controller {
private $_apiContext;
public function __construct() {
$this->_apiContext = PayPal::ApiContext(
config('services.paypal.client_id'),
config('services.paypal.secret'));
$this->_apiContext->setConfig(array(
'mode' => 'sandbox',
'service.EndPoint' => 'https://api.sandbox.paypal.com',
'http.ConnectionTimeOut' => 30,
'log.LogEnabled' => true,
'log.FileName' => storage_path('logs/paypal.log'),
'log.LogLevel' => 'FINE'
));
}
public function getCheckout() {
$payer = PayPal::Payer();
$payer->setPaymentMethod('paypal');
$amount = PayPal:: Amount();
$amount->setCurrency('EUR');
$amount->setTotal(42); // This is the simple way,
// you can alternatively describe everything in the order separately;
// Reference the PayPal PHP REST SDK for details.
$transaction = PayPal::Transaction();
$transaction->setAmount($amount);
$transaction->setDescription('What are you selling?');
$redirectUrls = PayPal:: RedirectUrls();
$redirectUrls->setReturnUrl(action('PayPalController#getDone'));
$redirectUrls->setCancelUrl(action('PayPalController#getCancel'));
$payment = PayPal::Payment();
$payment->setIntent('sale');
$payment->setPayer($payer);
$payment->setRedirectUrls($redirectUrls);
$payment->setTransactions(array($transaction));
$response = $payment->create($this->_apiContext);
$redirectUrl = $response->links[1]->href;
return Redirect::to( $redirectUrl );
}
public function getDone(Request $request) {
$id = $request->get('paymentId');
$token = $request->get('token');
$payer_id = $request->get('PayerID');
$payment = PayPal::getById($id, $this->_apiContext);
$paymentExecution = PayPal::PaymentExecution();
$paymentExecution->setPayerId($payer_id);
$executePayment = $payment->execute($paymentExecution, $this->_apiContext);
// Clear the shopping cart, write to database, send notifications, etc.
// Thank the user for the purchase
return view('checkout.done');
}
public function getCancel() {
// Curse and humiliate the user for cancelling this most sacred payment (yours)
return view('checkout.cancel');
}
}
Any help would be appreciated.
Thanks
Although laravel allows you to generate url's to actions. I researched what happened in the code.
In the UrlGenerator class:
/**
* Get the URL to a controller action.
*
* #param string $action
* #param mixed $parameters
* #param bool $absolute
* #return string
*
* #throws \InvalidArgumentException
*/
public function action($action, $parameters = [], $absolute = true)
{
if ($this->rootNamespace && !(strpos($action, '\\') === 0)) {
$action = $this->rootNamespace.'\\'.$action;
} else {
$action = trim($action, '\\');
}
if (!is_null($route = $this->routes->getByAction($action))) {
return $this->toRoute($route, $parameters, $absolute);
}
throw new InvalidArgumentException("Action {$action} not defined.");
}
It will search for a defined route with the specified action and in fact returns a link based on that route. So you would have to define the route to PayPayController#getDone for it to work.