This is a Laravel-based API for managing orders and payments. It supports extensible payment gateways and adheres to RESTful principles.
- Order Management: Create, update, delete, and view orders.
- Payment Management: Process payments for orders using multiple gateways.
- Authentication: Secure API endpoints using JWT authentication.
- Extensibility: Easily add new payment gateways using the strategy pattern.
git clone https://github.com/ahmedalmory/order-payment-api.git
cd order-payment-apicomposer install-
Update the
.envfile with your database credentials:DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=order_payment_api DB_USERNAME=root DB_PASSWORD=
-
Run migrations to create the required tables:
php artisan migrate
php artisan jwt:secretphp artisan serveThe API will be available at http://localhost:8000.
-
Register User:
POST /api/register{ "name": "Ahmed Almory", "email": "ahmedalmory99@gmail.com", "password": "password" } -
Login User:
POST /api/login{ "email": "ahmedalmory99@gmail.com", "password": "password" }
-
Create Order:
POST /api/orders{ "user_id": 1, "items": [ { "product_name": "Product 1", "quantity": 2, "price": 10.00 }, { "product_name": "Product 2", "quantity": 1, "price": 20.00 } ] } -
Delete Order:
DELETE /api/orders/{id} -
View Orders:
GET /api/orders -
View Single Order:
GET /api/orders/{id}
-
Process Payment:
POST /api/payments{ "order_id": 1, "payment_method": "credit_card" } -
View Payments:
GET /api/payments
The system is designed to allow easy addition of new payment gateways using the Strategy Pattern. Here’s how to add a new payment gateway:
Create a new class in the app/Services/PaymentGateways directory that implements the PaymentGateway interface. For example, create NewGateway.php:
namespace App\Services\PaymentGateways;
use App\Interfaces\PaymentGateway;
class NewGateway implements PaymentGateway {
public function processPayment($amount, $orderId) {
// Implement payment processing logic here
return 'successful';
}
}Add the new gateway to the PaymentGatewayFactory:
namespace App\Services;
use App\Interfaces\PaymentGateway;
use App\Services\PaymentGateways\CreditCardGateway;
use App\Services\PaymentGateways\PayPalGateway;
use App\Services\PaymentGateways\NewGateway;
class PaymentGatewayFactory {
public static function create($gateway): PaymentGateway {
return match ($gateway) {
'credit_card' => new CreditCardGateway(),
'paypal' => new PayPalGateway(),
'new_gateway' => new NewGateway(),
default => throw new \InvalidArgumentException('Invalid payment gateway'),
};
}
}When processing a payment, specify the new gateway in the request:
{
"order_id": 1,
"payment_method": "new_gateway"
}- Payments can only be processed for orders in the
confirmedstatus. - Orders cannot be deleted if they have associated payments.
- Run the tests using:
php artisan test
- Import the Postman collection from
OrderPaymentAPI.postman_collection.jsonto test the API endpoints.
- Ensure the following environment variables are set in the
.envfile:JWT_SECRET=your-jwt-secret-key APP_URL=http://localhost:8000
This project is open-source and available under the MIT License.
Your repository should look like this:
order-payment-api/
├── app/
├── config/
├── database/
├── public/
├── resources/
├── routes/
├── tests/
├── .env.example
├── .gitignore
├── README.md
├── OrderPaymentAPI.postman_collection.json
├── composer.json
└── composer.lock