Getting values to pass in Payfast - laravel

I'm trying to intergrate Payfast with my laravel project. The problem I'm having is that the data isn't being passed to Payfast so Payfast keeps giving me an error saying The supplied variables are not according to specification:
Here is my code in my controller
public function payPayfast(Request $request)
{
$data = [
'merchant_id' => $request->merchant_id,
'merchant_key' => $request->merchant_key,
'return_url' => $request->return_url,
'cancel_url' => $request->cancel_url,
'm_payment_id' => $request->m_payment_id,
'amount' => $request->amount,
'item_name' => 'Test Item From Controller',
'item_description' => 'This is a test product',
'email_confirmation' => '1',
'confirmation_address' => '',
'payment_method' => $request->payment_method,
'signature' => $request->signature,
];
return redirect()->to('https://sandbox.payfast.co.za/eng/process')->with('data', $data);
}
here is my code in my confirmation.blade.php
<div class="content_wrapper">
<h1>Order Confrimation</h1>
<div class="row">
<div class="col-lg-12">
<form action="{{ route('payfast.payPayfast') }}" method="POST">
#csrf
<input type="hidden" name="merchant_id" value="11111111">
<input type="hidden" name="merchant_key" value="2222222222222">
<input type="hidden" name="return_url" value="{{ route('payfast.success') }}">
<input type="hidden" name="cancel_url" value="{{ route('payfast.cancel') }}">
<input type="hidden" name="notify_url" value="{{ route('payfast.notify') }}">
<input type="hidden" name="m_payment_id" value="01AB">
<input type="hidden" name="amount" class="completePrice" value="{{ $total }}">
<input type="hidden" name="item_name" value="Test Item">
<input type="hidden" name="item_description" value="A test product">
<input type="hidden" name="email_confirmation" value="1">
<input type="hidden" name="confirmation_address" value="test#test.com">
<input type="hidden" name="payment_method" value="eft">
<?php
$success = url('payfast-success');
$cancel = url('payfast-cancel');
$notify = url('payfast-notify');
$original_str = getAscii('merchant_id=11111111&merchant_key=2222222222222&return_url='.$success.'&cancel_url='.$cancel.'&notify_url='.$notify.'&m_payment_id=01AB&amount='.$totalPrice.'&item_name=Test Item&item_description=A test product&email_confirmation=1&confirmation_address=test#test.com&payment_method=eft');
$hash_str = hash('MD5', $original_str);
$hash = strtolower($hash_str);
?>
<input type="hidden" name="signature" value="{{ $hash }}">
<button class="btn btn-dark" type="submit"><img src="{{ asset('img/eft-payfast.jpg') }}"></button>
</form>
</div>
</div>
</div>

The with function adds the data to the session, but PayFast expects the data to be in the url, e.g. https://sandbox.payfast.co.za/eng/process?amount=100&merchant_id=12&merchant_key=23&item_name=cool.
It doesn't seem like the URLGenerator provides an easy way to turn an array into query parameters. You could simply construct the query using string concatenation or have a look at this answer which includes a function to construct such a path.

Related

ErrorException Undefined array key 0 (View: C:\Users\Teacher\CNHSSystem\resources\views\section\index.blade.php)

Hello everyone I was trying to pass value from my resource section blade
<form action="POST" action="{{ ('/import-students') }}">
{{ csrf_field() }}
<div class="form-group">
#foreach($programs as $program)
#if($section->program_id == $program->id)
<input type="hidden" name="strand" value="{{ $program->strand }}">
#endif
#endforeach
<input type="hidden" name="section" value="{{ $section->section_name }}">
<input type="hidden" name="grade_level" value="{{ $section->grade_level }}">
<input type="hidden" name="semester" value="{{ $section->semester }}">
<input type="hidden" name="school_year" value="{{ $section->school_year }}">
</div>
<button type="submit" name="submit">
add
</button>
<!-- <input type="submit" class="text-primary" name="submit"> -->
</form>
to an import blade from another resource
<form method="POST" action="{{ ('/upload-students') }}">
{{ csrf_field() }}
<div class="form-group">
<input type="hidden" name="strand" value="{{ request('strand') }}">
<input type="hidden" name="section" value="{{ request('section') }}">
<input type="hidden" name="grade_level" value="{{ request('grade_level') }}">
<input type="hidden" name="semester" value="{{ request('semester') }}">
<input type="hidden" name="school_year" value="{{ request('school_year') }}">
<input type="file" name="file" class="form-control" />
<button type="submit" class="btn btn-primary mt-1">Save</button>
</div>
</form>
the problem is that the action from the section blade doesn't seem to read the controller
public function importStudents(Request $request)
{
// dd($request);
$strand = $request->input('strand');
$section = $request->input('section');
$grade_level = $request->input('grade_level');
$semester = $request->input('semester');
$school_year = $request->input('school_year');
return view('student.import', $strand, $section, $grade_level, $semester, $school_year);
}
Here's my web route
Route::post('/import-students', [StudentController::class, 'importStudents']);
I'am hoping for your help maam/sirs
and a very big THANK YOU in advance for the help that I can receive maam/sirs.
First, correct forms:
<form action="POST" action="{{ ('/import-students') }}">
this form has two action (method="post" not action) etc. Open this page: https://laravel.com/docs/9.x/blade#forms and see how to write it correctly.
Second, in view('view', $data) the second parameter should contain data (https://laravel.com/docs/5.0/views). So:
return view('student.import', [
'strand' => $strand,
'section' => $section,
'grade_level' => $grade_level,
'semester' => $semester,
'school_year' => $school_year
]);

Laravel $request->all() is empty

I start study laravel.
The route to contacts page
Route::match(['get', 'post'], '/contacts', [ 'uses' => 'Admin\ContactController#show', 'as' => 'contacts' ] );
class ContactController extends Controller {
public function show( Request $request ) {
print_r( $request->all() );
return view( 'default.contacts', [ 'title' => 'Contacts' ] );
}
}
Form
<form method="post" action="{{ route('contacts') }}">
{{ csrf_field() }}
<div class="form-group">
<label for="inputEmail4">Name</label>
<input type="text" class="form-control" id="inputEmail4" placeholder="Name">
</div>
<div class="form-group">
<label for="inputAddress">Address</label>
<input type="text" class="form-control" id="inputAddress" placeholder="1234 Main St">
</div>
<button type="submit" class="btn btn-primary">Sign in</button>
</form>
When i submit the form, i get an array with token.
Array
(
[_token] => JMTxTwh5Cb4sPeDjGcVetgTt2yGy6mDsFs6jW3Tx
)
What can be a problem?
Thanks for answer.
You are missing the name in your inputs. Please add to them.
<form method="post" action="{{ route('contacts') }}">
{{ csrf_field() }}
<div class="form-group">
<label for="inputEmail4">Name</label>
<input type="text" class="form-control" name="name" id="inputEmail4" placeholder="Name">
</div>
<div class="form-group">
<label for="inputAddress">Address</label>
<input type="text" name="address" class="form-control" id="inputAddress" placeholder="1234 Main St">
</div>
<button type="submit" class="btn btn-primary">Sign in</button>
</form>

fifth parameter is not displaying in the cart.index page

I'm sending five parameter to add to cart function,Here is the code
<form action="{{route('cart.store')}}" method="post"> {{ csrf_field() }}
<input type="hidden" name="id" value="{{$product->id}}">
<input type="hidden" name="name" value="{{$product->name}}">
<input type="hidden" name="price" value="{{$product->price}}">
<input type="hidden" name="destination" value="{{$product->destination}}">
value="">
<button type="submit">click me</button>
</form>
The code of cartController is
public function store(Request $request)
{
Cart::add($request->id, $request->name, 1 , $request->price , ['destination' => $request->destination]);
return redirect()->route('cart.index')->with('success_message','Item was added to your cart');
} // i have also tried
Cart::add(array('id' => $request->id, 'name' => $request->name, 'qty' => 1,
'price' => $request->price, 'destination' => $request->destination));
And the code for cart.index is
<tbody>
#foreach(Cart::content() as $row)
<tr>
<td><img src="img/detailsquare.jpg" alt="White Blouse Armani" class="img-fluid"></td>
<td>{{$row->id}}</td>
<td>{{$row->name}}</td>
<td>{{$row->qty}}</td>
<td>{{$row->price}}</td>
<td>{{$row->destination}}</td>
<td><i class="fa fa-trash-o"></i></td>
</tr>
#endforeach
</tbody>
This is not giving me any error and display the product id,name,quantity,price but did not displays the destination. Anyone have a solution
Remove extra line after input type :
<form action="{{route('cart.store')}}" method="post"> {{ csrf_field() }}
<input type="hidden" name="id" value="{{$product->id}}">
<input type="hidden" name="name" value="{{$product->name}}">
<input type="hidden" name="price" value="{{$product->price}}">
<input type="hidden" name="destination[]" value="{{$product->destination}}">
/*value=""> */ // **remove this line**
<button type="submit">click me</button>
</form>
If not working then from cartController try var_dump($request) and see result.
Try this :
public function store(Request $request)
{
Cart::add($request->id, $request->name, 1 , $request->price , $request->destination);
return redirect()->route('cart.index')->with('success_message','Item was added to your cart');
}
Also check your model and db.

Contact Form error MethodNotAllowedHttpException

I'm trying to make a Contact form in Laravel
when i submit the message i got this error
i'm mostly new to laravel so i don't know why it's appearing
Routes.php
Route::post('/contact/sendmail', [
'uses' => 'ContactMessageController#postSendMessage',
'as' => 'contact.send'
]);
ContactMessageController.php
class ContactMessageController extends Controller
{
public function postSendMessage(Request $request)
{
$this->validate($request, [
'email' => 'required|email',
'name' => 'required|max:100',
'subject' => 'required|max:140',
'message' => 'required|min:10'
]);
$message = new ContactMessage();
$message->email = $request['email'];
$message->sender = $request['name'];
$message->subject = $request['subject'];
$message->body = $request['message'];
$message->save();
return redirect()->route('contact')->with(['success' => 'Message Succesfully sent']);
}
}
and
contact.blade.php
#extends ('layouts.master')
#section('title')
Contact
#endsection
#section('styles')
<link rel="stylesheet" href="{{ URL::secure('src/css/form.css') }}" />
#endsection
#section('content')
#include('includes.info-box')
<form action="{{ route('contact.send') }}" mathod="post" id="contact-form">
<div class="input-group">
<label for="name">Your Name</label>
<input type="text" name="name" id="name" value="{{ Request::old('name') }}" />
</div>
<div class="input-group">
<label for="email">Your E-Mail</label>
<input type="text" name="email" id="email" value="{{ Request::old('email') }}" />
</div>
<div class="input-group">
<label for="subject">Subject</label>
<input type="text" name="subject" id="subject" value="{{ Request::old('subject') }}" />
</div>
<div class="input-group">
<label for="message">Your Message</label>
<textarea name="message" id="message" rows="10">{{ Request::old('message') }}</textarea>
</div>
<button type="submit" class="btn">Submit Message</button>
<input type="hidden" value="{{ Session::token() }}" name="_token" />
</form>
#endsection
I cant find why i got this error
MethodNotAllowedHttpException in RouteCollection.php line 218:
Just a typo. You need to change mathod="post" to method="post".

How to upload picture and store to database in Laravel 5

I need to upload picture and store to database in Laravel 5.
My current code is:
Form:
<form method="POST" enctype="multipart/form-data" action="{{ url('products/new) }}">
{!! csrf_field() !!}
<div class="form-group">
<label for="name">Name</label>
<input type="text" name="name" id="name" class="form-control" required>
</div>
<div class="form-group">
<label for="image">Image</label>
<input type="file" id="image">
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Save</button>
</div>
</form>
Controller:
public function store(Request $request)
{
$this->validate($request, [
'name' => 'required|max:100|unique:products',
]);
$input = $request->all();
Product::create($input);
return redirect('products');
}
The function $request->hasFile('image')) returns false.
hasFile('image') returns false because there is not an input with name 'image', only the id is 'image'
You should give it a name property, since that's the way the inputs are sent through http.
Instead of this:
<input type="file" id="image">
use this
<input name="image" type="file" id="image">
and in your validator use this:
$this->validate($request, [
'image' => 'required|max:100|unique:products',
]);

Resources