PHP code example of davidiwezulu / ecommerce
1. Go to this page and download the library: Download davidiwezulu/ecommerce library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
davidiwezulu / ecommerce example snippets
'currency' => [
'symbol' => env('CURRENCY_SYMBOL', '£'),
'code' => env('CURRENCY_CODE', 'GBP'),
],
'tax' => [
'default_rate' => env('TAX_RATE', 0.0),
'
'payment_gateways' => [
'stripe' => [
'class' => \Davidiwezulu\Ecommerce\Payments\StripeGateway::class,
'secret_key' => env('STRIPE_SECRET_KEY'),
],
// ...
],
'payment_gateways' => [
'paypal' => [
'class' => \Davidiwezulu\Ecommerce\Payments\PayPalGateway::class,
'client_id' => env('PAYPAL_CLIENT_ID'),
'secret' => env('PAYPAL_SECRET'),
'mode' => env('PAYPAL_MODE', 'sandbox'),
],
// ...
],
'models' => [
'product' => App\Models\Product::class,
'inventory' => App\Models\Inventory::class,
'order' => App\Models\Order::class,
'order_item' => App\Models\OrderItem::class,
'cart_item' => App\Models\CartItem::class,
],
use Davidiwezulu\Ecommerce\Facades\Cart;
Cart::addOrUpdate($productId, $quantity);
Cart::remove($productId);
$items = Cart::items();
foreach ($items as $item) {
echo 'Product: ' . $item->product->name . PHP_EOL;
echo 'Price: ' . $item->price . PHP_EOL;
echo 'Tax Amount: ' . $item->tax_amount . PHP_EOL;
echo 'Quantity: ' . $item->quantity . PHP_EOL;
echo 'Total Price: ' . $item->total_price . PHP_EOL; // Uses getTotalPriceAttribute()
echo '---' . PHP_EOL;
}
Cart::clear();
use Davidiwezulu\Ecommerce\Facades\Admin;
$productData = [
'name' => 'Product Name',
'price' => 100.00,
'tax_rate' => 0.15, // 15% tax rate
'description' => 'Product Description',
'sku' => 'SKU001',
];
$product = Admin::addProduct($productData);
$productData = [
'name' => 'Updated Product Name',
'price' => 120.00,
'tax_rate' => 0.18, // Updated tax rate
'description' => 'Updated Description',
];
$product = Admin::updateProduct($productId, $productData);
$quantity = 50; // New stock quantity
Admin::updateInventory($productId, $quantity);
use Davidiwezulu\Ecommerce\Facades\Order;
use Illuminate\Support\Facades\Auth;
use Davidiwezulu\Ecommerce\Facades\Cart;
$paymentDetails = [
'gateway' => 'stripe',
'token' => 'stripe-token', // Token from Stripe.js or Checkout
];
$cartItems = Cart::items()->toArray();
try {
$order = Order::create(Auth::id(), $cartItems, $paymentDetails);
Cart::clear();
return redirect()->route('order.success')->with('message', 'Order created successfully!');
} catch (\Exception $e) {
return redirect()->route('order.failed')->with('error', 'Payment failed: ' . $e->getMessage());
}
use Davidiwezulu\Ecommerce\Facades\Order;
use Illuminate\Support\Facades\Auth;
$paymentDetails = [
'gateway' => 'paypal',
'return_url' => route('paypal.return'),
'cancel_url' => route('paypal.cancel'),
];
$cartItems = Cart::items()->toArray();
try {
// This will redirect the user to PayPal for payment approval
return Order::create(Auth::id(), $cartItems, $paymentDetails);
} catch (\Exception $e) {
return redirect()->route('order.failed')->with('error', 'Payment initiation failed: ' . $e->getMessage());
}
Route::get('/paypal/return', [PaymentController::class, 'handlePayPalReturn'])->name('paypal.return');
Route::get('/paypal/cancel', [PaymentController::class, 'handlePayPalCancel'])->name('paypal.cancel');
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Davidiwezulu\Ecommerce\Services\OrderService;
use Davidiwezulu\Ecommerce\Facades\Cart;
class PaymentController extends Controller
{
public function handlePayPalReturn(Request $request)
{
$paymentId = $request->get('paymentId');
$payerId = $request->get('PayerID');
$userId = Auth::id();
$cartItems = Cart::items()->toArray();
try {
$orderService = new OrderService();
$order = $orderService->executePayPalPayment($paymentId, $payerId, $userId, $cartItems);
Cart::clear();
return redirect()->route('order.success')->with('message', 'Order created successfully!');
} catch (\Exception $e) {
return redirect()->route('order.failed')->with('error', 'Payment failed: ' . $e->getMessage());
}
}
public function handlePayPalCancel()
{
return redirect()->route('order.cancelled')->with('message', 'Payment was cancelled.');
}
}
use Davidiwezulu\Ecommerce\Facades\Admin;
$productData = [
'name' => 'Taxed Product',
'price' => 200.00,
'tax_rate' => 0.10, // 10% tax rate
'description' => 'A product with a specific tax rate',
'sku' => 'TP-002',
];
$product = Admin::addProduct($productData);
use Davidiwezulu\Ecommerce\Facades\Cart;
// Add product to cart
Cart::addOrUpdate($productId, $quantity);
// Retrieve cart items with tax details
$items = Cart::items();
foreach ($items as $item) {
echo 'Product: ' . $item->product->name . PHP_EOL;
echo 'Price: ' . $item->price . PHP_EOL;
echo 'Tax Amount: ' . $item->tax_amount . PHP_EOL;
echo 'Total Price (incl. Tax): ' . $item->total_price . PHP_EOL;
}
namespace App\Models;
use Davidiwezulu\Ecommerce\Models\Product as BaseProduct;
class Product extends BaseProduct
{
// Add your customisations here
/**
* Example of adding a new relationship.
*/
public function categories()
{
return $this->belongsToMany(Category::class);
}
/**
* Example of adding a custom method.
*/
public function calculateDiscountedPrice()
{
// Implement your discount logic here
}
}
'models' => [
'product' => App\Models\Product::class,
// Other models...
],
namespace App\Payments;
use Davidiwezulu\Ecommerce\Payments\PaymentGatewayInterface;
class CustomGateway implements PaymentGatewayInterface
{
public function charge($amount, $paymentDetails)
{
// Implement charge logic
}
public function execute($paymentId, $payerId)
{
// Implement execute logic (if needed)
}
public function refund($transactionId)
{
// Implement refund logic
}
}
'payment_gateways' => [
// Existing gateways...
'custom_gateway' => [
'class' => \App\Payments\CustomGateway::class,
// Additional configuration...
],
],
allykeynamelanguage
composer --provider="Davidiwezulu\Ecommerce\EcommerceServiceProvider" --tag=config
php artisan vendor:publish --provider="Davidiwezulu\Ecommerce\EcommerceServiceProvider" --tag=migrations
php artisan migrate
bash
php artisan vendor:publish --provider="Davidiwezulu\Ecommerce\EcommerceServiceProvider"
bash
php artisan migrate