Implement new API controllers for wallet management and discount code validation. This includes adding an Artisan command for exchange rate updates and refining the API routing structure. Additionally, perform a significant cleanup of the repository by removing obsolete migrations, debug scripts, and temporary test files. Security and configuration improvements include restricting CORS origins via environment variables and updating the system settings schema. - Add `WalletController` and `DiscountCodeController` APIs - Add `UpdateExchangeRates` command - Remove redundant migrations and debug/test scripts - Secure CORS configuration using `CORS_ALLOWED_ORIGINS` - Update `STATUS.md` to reflect current phase progress
98 lines
3.2 KiB
PHP
98 lines
3.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
use App\Models\DiscountCode;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
class DiscountCodeController
|
|
{
|
|
public function index(): JsonResponse
|
|
{
|
|
$codes = DiscountCode::where('is_active', true)
|
|
->where(function ($q) {
|
|
$q->whereNull('expires_at')->orWhere('expires_at', '>=', now());
|
|
})
|
|
->where(function ($q) {
|
|
$q->whereNull('usage_limit')->orWhereColumn('used_count', '<', 'usage_limit');
|
|
})
|
|
->get(['code', 'type', 'value', 'min_order_amount', 'expires_at']);
|
|
|
|
return response()->json(['codes' => $codes]);
|
|
}
|
|
|
|
public function validate(Request $request): JsonResponse
|
|
{
|
|
try {
|
|
$validated = $request->validate([
|
|
'code' => ['required', 'string'],
|
|
'total_price' => ['required', 'numeric', 'min:0'],
|
|
]);
|
|
} catch (ValidationException $e) {
|
|
return response()->json([
|
|
'message' => 'Validation failed',
|
|
'errors' => $e->errors(),
|
|
], 422);
|
|
}
|
|
|
|
$code = DiscountCode::where('code', $validated['code'])->first();
|
|
|
|
if (!$code) {
|
|
return response()->json([
|
|
'valid' => false,
|
|
'message' => 'کد تخفیف نامعتبر است.',
|
|
]);
|
|
}
|
|
|
|
if (!$code->is_active) {
|
|
return response()->json([
|
|
'valid' => false,
|
|
'message' => 'این کد تخفیف غیرفعال شده است.',
|
|
]);
|
|
}
|
|
|
|
if ($code->expires_at && $code->expires_at->isPast()) {
|
|
return response()->json([
|
|
'valid' => false,
|
|
'message' => 'این کد تخفیف منقضی شده است.',
|
|
]);
|
|
}
|
|
|
|
if ($code->usage_limit && $code->used_count >= $code->usage_limit) {
|
|
return response()->json([
|
|
'valid' => false,
|
|
'message' => 'ظرفیت استفاده از این کد تخفیف به پایان رسیده است.',
|
|
]);
|
|
}
|
|
|
|
if ($code->min_order_amount > 0 && $validated['total_price'] < $code->min_order_amount) {
|
|
return response()->json([
|
|
'valid' => false,
|
|
'message' => "حداقل مبلغ سفارش برای استفاده از این کد " . number_format($code->min_order_amount) . " ریال است.",
|
|
]);
|
|
}
|
|
|
|
$discountAmount = 0;
|
|
|
|
if ($code->type === 'percentage') {
|
|
$discountAmount = ($validated['total_price'] * $code->value) / 100;
|
|
} elseif ($code->type === 'fixed') {
|
|
$discountAmount = $code->value;
|
|
}
|
|
|
|
$discountAmount = min($discountAmount, $validated['total_price']);
|
|
$finalPrice = $validated['total_price'] - $discountAmount;
|
|
|
|
return response()->json([
|
|
'valid' => true,
|
|
'code' => $code->code,
|
|
'type' => $code->type,
|
|
'value' => $code->value,
|
|
'discount_amount' => $discountAmount,
|
|
'final_price' => $finalPrice,
|
|
]);
|
|
}
|
|
} |