Skip to content

Commit

Permalink
Implement Order Web Hosting Package Functionalit
Browse files Browse the repository at this point in the history
  • Loading branch information
sweep-ai[bot] authored Aug 23, 2024
1 parent c96cd21 commit a1a39f2
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 0 deletions.
103 changes: 103 additions & 0 deletions app/Http/Controllers/OrderController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php

namespace App\Http\Controllers;

use App\Models\Products_Service;
use App\Models\Customer;
use App\Models\HostingAccount;
use App\Models\Subscription;
use App\Models\Invoice;
use App\Models\Invoice_Item;
use App\Http\Controllers\PaymentController;
use Illuminate\Http\Request;

class OrderController extends Controller
{
public function create()
{
$packages = Products_Service::where('type', 'hosting')->get();
return view('orders.create', compact('packages'));
}

public function store(Request $request)
{
$request->validate([
'package_id' => 'required|exists:products_services,id',
'name' => 'required|string|max:255',
'email' => 'required|email|max:255',
'domain' => 'required|string|max:255',
]);

// Create or update customer
$customer = Customer::firstOrCreate(
['email' => $request->email],
['name' => $request->name]
);

// Get the selected package
$package = Products_Service::findOrFail($request->package_id);

// Create subscription
$subscription = Subscription::create([
'customer_id' => $customer->id,
'product_service_id' => $package->id,
'start_date' => now(),
'end_date' => now()->addYear(),
'renewal_period' => 'yearly',
'status' => 'active',
]);

// Create hosting account
$hostingAccount = HostingAccount::create([
'customer_id' => $customer->id,
'subscription_id' => $subscription->id,
'domain' => $request->domain,
'package' => $package->name,
'status' => 'pending',
]);

// Create invoice
$invoice = Invoice::create([
'customer_id' => $customer->id,
'total_amount' => $package->price,
'status' => 'pending',
]);

// Create invoice item
Invoice_Item::create([
'invoice_id' => $invoice->id,
'product_service_id' => $package->id,
'quantity' => 1,
'unit_price' => $package->price,
'total_price' => $package->price,
]);

// Process payment
$paymentController = new PaymentController();
$paymentResult = $paymentController->processPayment(new Request([
'invoice_id' => $invoice->id,
'payment_gateway_id' => 1, // Assuming 1 is a valid payment gateway ID
'amount' => $package->price,
'payment_method' => 'credit_card', // This should be dynamically set based on user input
]));

if ($paymentResult->status() === 200) {
// Payment successful, update statuses
$invoice->update(['status' => 'paid']);
$hostingAccount->update(['status' => 'active']);
return redirect()->route('orders.confirmation', $invoice->id)->with('success', 'Your order has been placed successfully!');
} else {
// Payment failed, rollback changes
$subscription->delete();
$hostingAccount->delete();
$invoice->delete();
return back()->withErrors(['payment' => 'Payment processing failed. Please try again.']);
}
}

public function confirmation($invoiceId)
{
$invoice = Invoice::findOrFail($invoiceId);
return view('orders.confirmation', compact('invoice'));
}
}
49 changes: 49 additions & 0 deletions resources/views/orders/create.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<x-app-layout>
<x-slot name="header">
<h2 class="font-semibold text-xl text-gray-800 leading-tight">
{{ __('Order Web Hosting Package') }}
</h2>
</x-slot>

<div class="py-12">
<div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
<div class="bg-white overflow-hidden shadow-xl sm:rounded-lg">
<div class="p-6 sm:px-20 bg-white border-b border-gray-200">
<form method="POST" action="{{ route('orders.store') }}">
@csrf

<div class="mb-4">
<x-label for="package" value="{{ __('Select Package') }}" />
<select id="package" class="block mt-1 w-full" name="package_id" required>
@foreach ($packages as $package)
<option value="{{ $package->id }}">{{ $package->name }} - ${{ number_format($package->price, 2) }}</option>
@endforeach
</select>
</div>

<div class="mb-4">
<x-label for="name" value="{{ __('Full Name') }}" />
<x-input id="name" class="block mt-1 w-full" type="text" name="name" :value="old('name')" required autofocus />
</div>

<div class="mb-4">
<x-label for="email" value="{{ __('Email') }}" />
<x-input id="email" class="block mt-1 w-full" type="email" name="email" :value="old('email')" required />
</div>

<div class="mb-4">
<x-label for="domain" value="{{ __('Domain Name') }}" />
<x-input id="domain" class="block mt-1 w-full" type="text" name="domain" :value="old('domain')" required />
</div>

<div class="flex items-center justify-end mt-4">
<x-button class="ml-4">
{{ __('Place Order') }}
</x-button>
</div>
</form>
</div>
</div>
</div>
</div>
</x-app-layout>

0 comments on commit a1a39f2

Please sign in to comment.