File: /var/www/api.ayokah.co.uk/app/Http/Controllers/Seller/PaymentController.php
<?php
namespace App\Http\Controllers\Seller;
use PDO;
use Stripe\Stripe;
use App\Models\Payment;
use Stripe\PaymentIntent;
use Illuminate\Http\Request;
use App\Models\PaymentHistory;
use Illuminate\Support\Facades\Log;
use App\Http\Controllers\Controller;
use App\Models\SellerPaymentHistory;
use Illuminate\Validation\ValidationException;
class PaymentController extends Controller
{
public function save(Request $request)
{
try {
$validatedData = $this->validatePaymentRequest($request);
} catch (ValidationException $e) {
return response()->json(['error' => $e->errors()], 422);
}
$validatedData['due_date'] = now()->addDays(30);
$payment = $this->findOrCreatePayment($request, $validatedData);
return response()->json(['message' => 'Subscription Payment made successfully', 'payment' => $payment], 200);
}
protected function validatePaymentRequest(Request $request)
{
return $request->validate([
'seller_plans_id' => ['required', 'exists:seller_plans,id'],
'seller_id' => ['required', 'exists:sellers,id'],
'due_date' => ['required', 'date'],
]);
}
protected function findOrCreatePayment(Request $request, array $validatedData)
{
$sellerId = $validatedData['seller_id'];
// Check if payment record exists for the seller
$payment = PaymentHistory::where('seller_id', $sellerId)->first();
if ($payment) {
// If payment record exists, update it
$payment->fill($validatedData);
$payment->save();
} else {
// If payment record doesn't exist, create a new one
$payment = new PaymentHistory();
$payment->fill($validatedData);
$payment->save();
}
// Create payment history record
$paymentHistory = new PaymentHistory();
$paymentHistory->fill($validatedData);
$paymentHistory->save();
return $payment;
}
public function list()
{
try {
$payment = Payment::all();
return response()->json(['message' => 'success', 'payment' => $payment], 200);
} catch (\Throwable $th) {
Log::error($th->getMessage());
return response()->json(['error' => 'failed'], 500);
}
}
public function history(Request $request)
{
try {
$histories = PaymentHistory::where('seller_id', $request->seller_id)->paginate(10);
return response()->json(['message' => 'success', 'payment' => $histories], 200);
} catch (\Throwable $th) {
Log::error($th->getMessage());
return response()->json(['error' => 'failed'], 500);
}
}
public function createPaymentIntent(Request $request)
{
$validatedData = $request->validate([
'plan_id' => 'required',
'user_id' => 'required',
'amount' => 'required|numeric',
'plan_name' => 'required|string',
'user_name' => 'required|string',
'user_email' => 'required|exists:users,email',
'user_shop_id' => 'required|exists:sellers,id',
]);
Stripe::setApiKey(env('STRIPE_SECRET'));
$paymentIntent = PaymentIntent::create([
'amount' => $validatedData['amount'] * 100, // amount in cents
'currency' => 'eur',
'product_data' => [
'name' => $validatedData['user_name'],
'email' => $validatedData['user_email'],
'plan' => $validatedData['plan_name'],
],
'payment_method_types' => ['card'],
]);
return response()->json(['clientSecret' => $paymentIntent->client_secret]);
}
}