I have an external paiement application service who take care about the paiements from my shopping application .
I have a form method Post to the service to send the data and the service valide the payment transaction .
<form method="POST" action="https://paiement.systempay.fr/vads-payment/">
...
<input type="submit" name="payer" value="Payer"/></form>
</form>
I would like to post also a request method from my payment controller to update my items who was paid
here my controller wish i would like to be run when the user click on "payer" also
public function postCheckoutCreditCard(Request $request)
{
if(!Session::has('Cart')){
return view('shop.panier');
}
$oldCart = Session::get('Cart') ;
$cart = new Cart($oldCart);
$items = $cart->items;
if(Input::get('payer')) {
// on inject le nouveau statut de la licence
foreach ($items as $item) {
$item['item']->statut_licence_id = LicenceStatut::where('id', '4')->firstOrFail()->id;
$item['item']->valid_licence_id = LicenceValid::where('id', '1')->firstOrFail()->id;
$item['item']->save();
}
$order = new Order;
$prefix = 'F';
$date = Carbon::now();
$saison = Saison::where('dt_deb', '<', $date)->where('dt_fin', '>', $date)->value('lb_saison');
$saison_deb = substr($saison, 2, 2);
$saison_fin = substr($saison, -2);
$num_facture_exist = true;
while ($num_facture_exist) {
$num_facture = $prefix . $saison_deb . $saison_fin . substr(uniqid(rand(), true), 4, 4);
if (!Order::where('num_facture', '=', $num_facture)->exists()) {
$order->num_facture = $num_facture;
$num_facture_exist = false;
}
}
$order->structure_id = Structure::where(['id' => Auth::user()->structure->id])->firstOrFail()->id;
$order->cart = serialize($cart);
$order->date_achat = Carbon::now();
$order->payment_method = 'Carte de Crédit';
$order->etat_paiement = 'Facture Réglée';
$order->save();
Auth::user()->notify(new InvoicePaid($order));
$federation = Structure::where('id', '1')->first();
Mail::to($federation->adresse_email_structure)->send(new NouvelleCommande($order));
Session::forget('Cart');
return redirect('home')->with('status', "Votre paiement à été effectué avec sucess , votre numéro de facture : . $order->num_facture est disponible dans la rubrique Mes cotisation ");
}
}
I'm not sure how to do this . someone could help me ? thanks in advance
I would change the form action to your Controller and then use Guzzle to call the external route. Check here about Guzzle (http://docs.guzzlephp.org/en/latest/quickstart.html#post-form-requests).
Call something like this in your Controller:
$response = $client->request('POST', 'https://paiement.systempay.fr/vads-payment/', [
'form_params' => [
'field_name' => 'abc',
]
]);
Then check the response and return something based on it.
Related
Hope you all have a good day..
At first i have a form that user should fill before redirect to PayTabs page for payment
But i don't need the form to be inserted in database before success payment
public function index(Request $request): RedirectResponse
{
$response = $this->request(
url: 'https://secure-global.paytabs.com/payment/request',
payload: $this->transactionPayload(
amount: 0
)
);
Transaction::create([
'paytabs_transaction_reference' => $response->json()['tran_ref'] ?? null
]);
Cart::where('opened', '=', 0)->where('user_id', Auth::id())->update(['paytabs_transaction_reference' => $response->json()['tran_ref'] ?? null]);
$cart_id = Cart::latest()->first()->id;
$payment = new Payment;
$payment->cart_id = $cart_id;
$payment->country_id = $request->input('country_id');
$payment->delivery_id = $request->input('delivery_id');
$payment->address = $request->input('address');
$payment->street = $request->input('street');
$payment->home = $request->input('home');
$payment->email = $request->input('email');
$payment->save();
return redirect()->away($response['redirect_url']);
}
public function return(Request $request): string
{
$category = Category::all();
$products = Product::all();
$validSignature = $this->validateSignature($request->all());
if ($validSignature) {
if ($request->respStatus == 'A') {
$transaction = Transaction::where('paytabs_transaction_reference', $request->tranRef)->first();
$transaction->paid = true;
$transaction->save();
DB::table('carts')->where('user_id', Auth::id())->where('opened', 0 )->update(['opened' => 1]);
return view('index', compact('category', 'products'))->with('success', 'Payment has been done successfully. Thank you!') . $request->respMessage;
}
return view('index', compact('category', 'products')) . $request->respMessage;
} else {
return 'Invalid Transaction Signature';
}
}
Here the form will be inserted even if he back from the next page
<form role="form" action="{{ route('index') }}" method="post">
#csrf
.......
</form>
That's my form should be to INDEX function to redirect to payment page
You Need To Make A Validation To Your Form
blade edit
<form action="/files" method="POST" enctype="multipart/form-data">
{{csrf_field()}}
<label for="dokumen">Dokumen Awal : ({{$aktivitas_list->dokumen}}) <br><i>Upload Ulang Dokumen</i></label>
<input type="file" id="dokumen" name="dokumen" accept=".pdf" class="form-control" value="{{$aktivitas_list->dokumen}}">
#if ($errors->has('dokumen'))
<span class="text-danger">{{ $errors->first('dokumen') }}</span>
#endif
controller store
$aktivitas_list = new Aktivitas;
$aktivitas_list->pt_id = $request->get('pt_id');
$aktivitas_list->nama_aktivitas = $request->get('nama_aktivitas');
$aktivitas_list->tgl_aktivitas = $request->get('tgl_aktivitas');
$aktivitas_list->tempat = $request->get('tempat');
$aktivitas_list->jenis_aktivitas = $request->get('jenis_aktivitas');
$aktivitas_list->dokumen = $request->file('dokumen');
$aktivitas_list->tenggat_waktu = $request->get('tenggat_waktu');
$aktivitas_list->deskripsi = $request->get('deskripsi');
$aktivitas_list->status = $request->get('status');
$aktivitas_list->user = $request->get('user');
$rules = array(
'nama_aktivitas' => 'required',
'dokumen' => 'required|mimes:pdf'
);
$dokumen = $request->file('dokumen');
$tujuan_upload = 'document-upload';
$dokumen->move($tujuan_upload, $dokumen->getClientOriginalName());
$aktivitas_list->dokumen = $dokumen->getClientOriginalName();
if ($aktivitas_list->save()) {
return redirect('pt')->with('success', 'Data Berhasil Ditambahkan');
} else {
return redirect('pt')->with('error', 'error message');
}
}
controller update
$rules = array(
'nama_aktivitas' => 'required',
'dokumen' => 'required|mimes:pdf'
);
$dokumen = $request->file('dokumen');
$tujuan_upload = 'document-upload';
$dokumen->move($tujuan_upload, $dokumen->getClientOriginalName());
$aktivitas_list->dokumen = $dokumen->getClientOriginalName();
$aktivitas_list->status = $status=1;
$aktivitas_list->user = Auth::user()->name;
$user = Auth::user()->name;
if ($aktivitas_list->save()) {
return redirect('pt')->with('success', 'Data Berhasil Ditambahkan');
} else {
return redirect('pt')->with('error', 'error message');
}
The problem is, if I don't select again the file when I update the data so the data is null. for example i just want to edit field nama aktivitas and not change the files. and another example i want to update all field. how can i get the files beside the browse button? how can i solve them? please guys help me
In Controller on update method
Make file as optional.
Check if request has file then make process of upload.
in view you can't set file input value for security reason.
$rules = array(
'nama_aktivitas' => 'required',
'dokumen' => 'nullable|mimes:pdf'//make file as optional
);
if($request->file('dokumen')){//check if file are exists on request
$dokumen = $request->file('dokumen');
$tujuan_upload = 'document-upload';
$dokumen->move($tujuan_upload, $dokumen->getClientOriginalName());
$aktivitas_list->dokumen = $dokumen->getClientOriginalName();
}
$aktivitas_list->status = $status=1;
$aktivitas_list->user = Auth::user()->name;
$user = Auth::user()->name;
if ($aktivitas_list->save()) {
return redirect('pt')->with('success', 'Data Berhasil Ditambahkan');
} else {
return redirect('pt')->with('error', 'error message');
}
you can check if file existed then upload it and save file's new name in database and if file didn't exist just escape this field so old value remain in database.
if($request->hasFile()) {
}
See docs here
public function update(Request $request)
{
$rules = array(
'nama_aktivitas' => 'required',
'dokumen' => 'sometimes|mimes:pdf'
);
//Validation of request data
if($request->hasFile('dokumen') && $request->file('dokumen')->isValid()){
$dokumen = $request->file('dokumen');
$tujuan_upload = 'document-upload';
$dokumen->move($tujuan_upload, $dokumen->getClientOriginalName());
//Since your provided code snippet for update is truncated
// don't know how $aktivitas_list is instantiated
$aktivitas_list->dokumen = $dokumen->getClientOriginalName();
}
$aktivitas_list->status = $status=1;
$aktivitas_list->user = Auth::user()->name;
$user = Auth::user()->name;
if ($aktivitas_list->save()) {
return redirect('pt')->with('success', 'Data Berhasil Ditambahkan');
} else {
return redirect('pt')->with('error', 'error message');
}
}
I get problem.
This is my controller
public function finish(Request $request)
{
$result = $request->input('data');
//$data = json_decode($result, true);
return $this->InvoiceBayar($result);
}
public function InvoiceBayar($result)
{
$data = json_decode($result, true);
$transaction = $data['transaction_status'];
$type = $data['payment_type'];
$order_id = $data['order_id'];
$fraud = $data['fraud_status'];
Fee::where('invoice',$order_id)
->update([
'status' => 'Paid',
]);
echo "Transaction order_id: " . $order_id ." successfully transfered using " . $type;
}
This is my Route
Route::POST('/notification', 'SnapController#finish');
When Payment gateway, send a parameter to me, I cannot update DB.
But when I use POSTMAN. I success update DB
You need to use $request->all() as it will contain all payment gateway data.
public function finish(Request $request)
{
$result = $request->all();
return $this->InvoiceBayar($result);
}
Alternately you can do this
$update = Fee::where('invoice',$order_id)->first();
$update->status = 'Paid';
$update->save();
You should try this:
public function InvoiceBayar($result)
{
$data = json_decode($result, true);
$transaction = $data->transaction_status;
$type = $data->payment_type;
$order_id = $data->order_id;
$fraud = $data->fraud_status;
Fee::where('invoice',$order_id)
->update([
'status' => 'Paid',
]);
echo "Transaction order_id: " . $order_id ." successfully transfered using " . $type;
}
I have a little problem about update avatar pic of my user.
I have a polymorph relation table for Image and when i update info of my user profile and upload new avatar in my DB he create a new entry and not updated the current id of my table Images.
Table Images Id|path|Imageable_id|imageable_type|created_at
My UsersController method update
public function update($id){
$rules =[
/*'lastname' => 'min:3|string',
'firstname' => 'min:3|string',
'username'=> 'min:4|unique:users',
'mail' => ' email|unique:users',
'birthday' => 'date_format:d-m-Y|before:today',
'country'=>'min:3',
'type_street'=>'min:3',
'number'=>'min:1|numeric',
'street'=>'min:4|string',
'complementary_street'=>'min:2|string',
'town'=>'min:2|string',
'zip'=>'min:4|numeric',
'phone_home'=>'min:10|numeric',
'phone_mobile'=>'min:10|numeric',
'image_path'=>'image|max:1000|mimes:jpeg,jpg,png',*/
];
$validator = Validator::make(Input::all(),$rules);
if($validator->fails()){
return Redirect::to('/profil/'.$id)
->with('alert_error','Merci de corriger les erreurs');
}else{
$user = User::find($id);
$user->lastname = Input::get('lastname');
$user->firstname = Input::get('firstname');
$user->username = Input::get('username');
$user->mail = Input::get('mail');
$user->birthday = Input::get('birthday');
$user->adresse->type_street = Input::get('type_street');
$user->adresse->number = Input::get('number');
$user->adresse->street = Input::get('street');
$user->adresse->complementary_street = Input::get('complementary_street');
$user->adresse->town = Input::get('town');
$user->adresse->zip = Input::get('zip');
$user->adresse->country = Input::get('country');
$user->adresse->phone_home = Input::get('phone_home');
$user->adresse->phone_mobile = Input::get('phone_mobile');
if(Input::hasFile('avatar')){
$avatar = Image::find($id);
$file = Input::file('avatar');
$name = time().'-'.$file->getClientOriginalName();
$file = $file->move('img/avatar/', $name);
$input['path'] = 'img/avatar/'.$name;
$input['imageable_id'] = $user->id;
$input['imageable_type'] = 'User';
$avatar = new Image($input);
$avatar->save();
}
$user->adresse->save();
return Redirect::to('/profil/'.$id)
->with('alert_success','Modification sauvegardé avec succès');
}
}
Can you help me for this feature i don't understand why no updated of current id of my entry and create new One .
Thank's
You're overwriting your find $avatar = Image::find($id); with a new instance $avatar = new Image($input);
I have a problem with Sentry. The problem is probably that under $user = Sentry::getUserProvider()->findById($id);it is not only finding one user, but many users. Thus it cannot recognize the method user->save.
How can I solve this problem?
I am trying to build a form to edit my user details.
Thank you.
My form looks like this:
{{ Form::open(array('url' => 'profile/useredit')) }}
{{ Form::text('address',null) }}
<br>
{{Form::submit('Submit', array('class' => 'btn btn-default'))}}
{{ Form::close() }}
/**
* Edit the user profile under profile/user
*
* #return View
*/
public function postUseredit(){
try {
$id= Session::get(Config::get('sentry::sentry.session.user'));
// Get the user information
$user = Sentry::getUserProvider()->findById($id);
} catch (UserNotFoundException $e) {
// Prepare the error message
$error = Lang::get('users/message.user_not_found', compact('id'));
// Redirect to the user management page
return Redirect::route('users')->with('error', $error);
}
try {
// Update the user
$user->first_name = Input::get('first_name');
$user->last_name = Input::get('last_name');
$user->email = Input::get('email');
$user->dob = Input::get('dob');
$user->bio = Input::get('bio');
$user->gender = Input::get('gender');
$user->country = Input::get('country');
$user->state = Input::get('state');
$user->city = Input::get('city');
$user->address = Input::get('address');
$user->postal = Input::get('postal');
$user->activated = Input::get('activate')?1:0;
/*
// is new image uploaded?
if ($file = Input::file('pic'))
{
$fileName = $file->getClientOriginalName();
$extension = $file->getClientOriginalExtension() ?: 'png';
$folderName = '/uploads/users/';
$destinationPath = public_path() . $folderName;
$safeName = str_random(10).'.'.$extension;
$file->move($destinationPath, $safeName);
//delete old pic if exists
if(File::exists(public_path() . $folderName.$user->pic))
{
File::delete(public_path() . $folderName.$user->pic);
}
//save new file path into db
$user->pic = $safeName;
}
*/
/*
// Get the current user groups
$userGroups = $user->groups()->lists('group_id', 'group_id');
// Get the selected groups
$selectedGroups = Input::get('groups', array());
// Groups comparison between the groups the user currently
// have and the groups the user wish to have.
$groupsToAdd = array_diff($selectedGroups, $userGroups);
$groupsToRemove = array_diff($userGroups, $selectedGroups);
// Assign the user to groups
foreach ($groupsToAdd as $groupId) {
$group = Sentry::getGroupProvider()->findById($groupId);
$user->addGroup($group);
}
// Remove the user from groups
foreach ($groupsToRemove as $groupId) {
$group = Sentry::getGroupProvider()->findById($groupId);
$user->removeGroup($group);
}
*/
// Was the user updated?
if ($user->save()) {
// Prepare the success message
$success = Lang::get('users/message.success.update');
// Redirect to the user page
return Redirect::route('profile/user', $id)->with('success', $success);
}
// Prepare the error message
$error = Lang::get('users/message.error.update');
} catch (LoginRequiredException $e) {
$error = Lang::get('users/message.user_login_required');
}
// Redirect to the user page
return Redirect::route('profile/user', $id)->withInput()->with('error', $error);
}
Instead of
$user = Sentry::getUserProvider()->findById($id);
try this
$user = Sentry::findUserById($id);