i try to send data after pay Approve from paypal , want save this data about customer and create order after paying ,
here i check if paypal to send data
}elseif ($request->payment == "paypal"){
$data = array();
$data['user_name'] = $request->name;
$data['user_email'] = $request->email;
$data['city'] = $request->city;
$data['address'] = $request->address;
$data['phone'] = $request->phone;
if (Auth::user()){
$cart = Cart::where('user_id',Auth::user()->id)->get();
$subtotal = 0;
foreach($cart as $product){
$subtotal += $product->quantity * $product->product_price;
}
return view('web.paypal_pay',compact('data','subtotal'));
}else{
return view('web.paypal_pay',compact('data'));
}
}
} // END METHOD
and here this page i called paypal gate
want after paying send this data to creat order and save customer info
after approve i redirect to method to save this
@extends('layouts.main')
@section('content')
<section class="container mt-2 my-3 py-5" style="margin: 50px auto">
<div class="container mt-2 text-center">
<form type="hidden" id="form_data">
<input type="hidden" name="user_name" value="{{ $data['user_name'] }} ">
<input type="hidden" name="phone" value="{{ $data['phone'] }} ">
<input type="hidden" name="user_email" value="{{ $data['user_email'] }} ">
<input type="hidden" name="address" value="{{ $data['address'] }} ">
<input type="hidden" name="city" value="{{ $data['city'] }} ">
</form>
<!-- Set up a container element for the button -->
<div id="paypal-button-container"></div>
</div>
</section>
<body>
<!-- Include the PayPal JavaScript SDK; replace "test" with your own sandbox Business account app client ID -->
<script src="https://www.paypal.com/sdk/js?client-id=AVI_GBOauOlLoghPnbfBB-AxqbMWaoZgl6lWG5jzRz6AMw7wZqox7nDXgjw9XXCWeCGsSgfjeyejBsRx¤cy=USD"></script>
<script>
paypal.Buttons({
// Sets up the transaction when a payment button is clicked
createOrder: function(data, actions) {
return actions.order.create({
purchase_units: [{
amount: {
value: @if(Session::has('subtotal'))
{{Session::get('subtotal')}}
@elseif(Auth::user())
{{$subtotal}}
@endif
// Can reference variables or functions. Example: `value: document.getElementById('...').value`
}
}]
});
},
// Finalize the transaction after payer approval
onApprove: function(data, actions) {
return actions.order.capture().then(function(orderData) {
// Successful capture! For dev/demo purposes:
console.log('Capture result', orderData, JSON.stringify(orderData, null, 2));
var transaction = orderData.purchase_units[0].payments.captures[0];
alert('Transaction '+ transaction.status + ': ' + transaction.id + '\n\nSee console for all available details');
// Access the form element...
const form = document.getElementById( "form_data" );
//هنا نحوله بعد ما يتم الدفع لاضافه الاوردر ف الداتا بيز
window.location.href = "/paypal/payment/"+transaction.id;
// When ready to go live, remove the alert and show a success message within this page. For example:
// var element = document.getElementById('paypal-button-container');
// element.innerHTML = '';
// element.innerHTML = '<h3>Thank you for your payment!</h3>';
// Or go to another URL: actions.redirect('thank_you.html');
});
}
}).render('#paypal-button-container');
</script>
</body>
@endsection
this method i redirect
<?php
namespace App\Http\Controllers\Web;
use App\Http\Controllers\Controller;
use App\Models\Cart;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Session;
class PaymentPaypalController extends Controller
{
public function paypalPayment(Request $request,$transaction_id){
if (Session::has('cart')){
$cart = Session::get('cart');
$data = array();
$data['cost'] = Session::get('subtotal');
$data['user_name'] = $request->name;
$data['user_email'] = $request->email;
$data['status'] = $transaction_id;
$data['city'] = $request->city;
$data['address'] = $request->address;
$data['phone'] = $request->phone;
$data['date'] = date('Y-m-d');
$order_id = DB::table('orders')->insertGetId($data);
foreach ($cart as $id => $product){
// $product = $cart[$id];
DB::table('order_items')->insert([
'product_id' => $product['product_id'],
'order_id' => $order_id ,
'product_name' => $product['product_name'],
'product_price' => $product['product_price'],
'product_image' => $product['image'],
'product_quantity' => $product['quantity'],
'order_date' => $data['date'],
]);
$alert = array(
'alert-type' => "success",
'message' => "order done wait Delivery",
);
}
//save order_id to show for gust to can track his order
Session::push('order_id',$order_id);
Session::forget('cart');
return redirect()->route('delivery_details')->with($alert);
}elseif (Auth::user()){
$cart = Cart::where('user_id',Auth::user()->id)->get();
$subtotal = 0;
foreach ($cart as $product){
$subtotal += $product->product_price * $product->quantity;
}
$data = array();
$data['user_id'] = Auth::id();
$data['cost'] = $subtotal;
$data['user_name'] = $request->name;
$data['user_email'] = $request->email;
$data['status'] = $transaction_id;
$data['city'] = $request->city;
$data['address'] = $request->address;
$data['phone'] = $request->phone;
$data['date'] = date('Y-m-d');
$order_id = DB::table('orders')->insertGetId($data);
foreach ($cart as $product){
DB::table('order_items')->insert([
'product_id' => $product->product_id,
'order_id' => $order_id ,
'product_name' => $product->product_name,
'product_price' => $product->product_price,
'product_image' => $product->image,
'product_quantity' => $product->quantity,
'order_date' => $data['date'],
]);
$alert = array(
'alert-type' => "success",
'message' => "order done wait Delivery",
);
}
Cart::where('user_id',Auth::user()->id)->delete();
return redirect()->route('delivery_details')->with($alert);
}
}
}
give me this error
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'user_name' cannot be null (SQL: insert into `orders` (`cost`, `user_name`, `user_email`, `status`, `city`, `address`, `phone`, `date`) values (5, ?, ?, 4TL92186F3031115A, ?, ?, ?, 2022-02-26))
`