laravelEcomm/app/Http/Controllers/StripePaymentController.php

76 lines
2.3 KiB
PHP
Raw Normal View History

<?php
namespace App\Http\Controllers;
use App\Models\Order;
use App\Models\Cart;
use App\Models\Products;
use App\Models\OrderItem;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Session;
use Stripe;
class StripePaymentController extends Controller
{
/**
* success response method.
*
* @return \Illuminate\Http\Response */
public function stripe(Request $data)
{
$bill = $data->input('bill');
$name = $data->input('name');
$phone = $data->input('phone');
$address = $data->input('address');
return view(
'stripe',
compact('bill', 'name', 'phone', 'address')
);
}
/**
* success response method.
*
* @return \Illuminate\Http\Response */
public function stripePost(Request $request)
{
Stripe\Stripe::setApiKey(env('STRIPE_SECRET'));
Stripe\Charge::create([
"amount" => $request->input('bill') * 100,
"currency" => "usd",
"source" => $request->stripeToken,
"description" => "Payment Successfull!"
]);
Session::flash('success', 'Payment successful!');
if (session()->has('id')) {
$order = new Order();
$order->status = "Pending";
$order->customerId = session()->get('id');
$order->name = $request->input('name');
$order->phone = $request->input('phone');
$order->address = $request->input('address');
$order->bill = $request->input('bill');
if ($order->save()) {
$cartItems = Cart::where('customerId', session()->get('id'))->get();
foreach ($cartItems as $item) {
$product = Products::find($item->productId);
$orderItem = new OrderItem();
$orderItem->orderId = $order->id;
$orderItem->productId = $item->productId;
$orderItem->quantity = $item->quantity;
$orderItem->price = $product->price;
$orderItem->save();
$item->delete();
}
return redirect('/cart')->back()->with('success', 'Order placed successfully!');
}
}
return back();
}
}