feat: add customer details fields to Purchase model and request, implement checkout process with cart clearing
This commit is contained in:
@@ -9,7 +9,11 @@ class TelepagosProvider implements PaymentProviderInterface
|
||||
{
|
||||
public function process(array $paymentData, array $credentials): array
|
||||
{
|
||||
// TODO: Implementar lógica de comunicación con Telepagos (usando $paymentData['payment_method'])
|
||||
throw new BadMethodCallException('Método de pago no implementado para Telepagos.');
|
||||
// Mocked response for development and local testing
|
||||
return [
|
||||
'status' => 'approved',
|
||||
'transaction_id' => 'mock_tp_' . uniqid(),
|
||||
'payment_method' => $paymentData['payment_method'] ?? 'qr',
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,6 +16,10 @@ use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
'status',
|
||||
'payment_status',
|
||||
'payment_method',
|
||||
'dni',
|
||||
'telefono',
|
||||
'nombre_apellido',
|
||||
'email',
|
||||
])]
|
||||
class Purchase extends Model
|
||||
{
|
||||
|
||||
@@ -20,9 +20,11 @@ class StorePurchaseRequest extends FormRequest
|
||||
return [
|
||||
'status' => ['sometimes', 'string', Rule::in(['pending', 'paid', 'cancelled'])],
|
||||
'payment_status' => ['sometimes', 'string', Rule::in(['pending', 'approved', 'rejected'])],
|
||||
'integration_code' => ['required', 'string'],
|
||||
'payment_data' => ['required', 'array'],
|
||||
'payment_data.payment_method' => ['required', 'string'],
|
||||
'payment_method' => ['required', 'string'],
|
||||
'dni' => ['required', 'string'],
|
||||
'telefono' => ['required', 'string'],
|
||||
'nombre_apellido' => ['required', 'string'],
|
||||
'email' => ['required', 'string', 'email'],
|
||||
'items' => ['required', 'array', 'min:1'],
|
||||
'items.*.producto_variante_id' => ['required', 'integer', 'exists:productos_variantes,id'],
|
||||
'items.*.cantidad' => ['required', 'integer', 'min:1'],
|
||||
|
||||
@@ -36,6 +36,10 @@ class PurchaseResource extends JsonResource
|
||||
'status' => $this->status,
|
||||
'payment_status' => $this->payment_status,
|
||||
'payment_method' => $this->payment_method,
|
||||
'dni' => $this->dni,
|
||||
'telefono' => $this->telefono,
|
||||
'nombre_apellido' => $this->nombre_apellido,
|
||||
'email' => $this->email,
|
||||
'items' => PurchaseItemResource::collection($items),
|
||||
'subtotal' => $this->formatMoney($subtotal),
|
||||
'total' => $this->formatMoney($total),
|
||||
|
||||
@@ -3,8 +3,6 @@
|
||||
namespace App\Domains\Purchase\Services;
|
||||
|
||||
use App\Domains\Catalog\Models\ProductVariant;
|
||||
use App\Domains\Integration\Services\PaymentProviderFactory;
|
||||
use App\Domains\Integration\Services\TenantIntegrationService;
|
||||
use App\Domains\Purchase\Models\Purchase;
|
||||
use App\Domains\Tenant\Models\Tenant;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
@@ -13,53 +11,36 @@ use Exception;
|
||||
|
||||
class CheckoutService
|
||||
{
|
||||
protected TenantIntegrationService $tenantIntegrationService;
|
||||
protected PaymentProviderFactory $paymentProviderFactory;
|
||||
|
||||
public function __construct(
|
||||
TenantIntegrationService $tenantIntegrationService,
|
||||
PaymentProviderFactory $paymentProviderFactory
|
||||
) {
|
||||
$this->tenantIntegrationService = $tenantIntegrationService;
|
||||
$this->paymentProviderFactory = $paymentProviderFactory;
|
||||
}
|
||||
|
||||
public function processCheckout(Tenant $tenant, int $userId, array $purchaseData, string $integrationCode, array $paymentData): Purchase
|
||||
public function processCheckout(Tenant $tenant, int $userId, array $purchaseData): Purchase
|
||||
{
|
||||
// 1. Obtener configuración del tenant
|
||||
$tenantIntegration = $this->tenantIntegrationService->getTenantIntegration($tenant->codigo, $integrationCode);
|
||||
|
||||
if (!$tenantIntegration) {
|
||||
throw ValidationException::withMessages([
|
||||
'integration_code' => "El tenant no tiene configurada la integración: {$integrationCode}",
|
||||
]);
|
||||
}
|
||||
|
||||
// 2. Instanciar el proveedor de pago
|
||||
$provider = $this->paymentProviderFactory->make($integrationCode);
|
||||
|
||||
// 3. Procesar el pago con las credenciales desencriptadas
|
||||
$paymentResult = $provider->process($paymentData, $tenantIntegration->integration_data);
|
||||
|
||||
// 4. Preparar items de la compra
|
||||
// 1. Preparar items de la compra
|
||||
$items = $purchaseData['items'];
|
||||
unset($purchaseData['items']);
|
||||
|
||||
$variants = $this->resolveTenantVariants($tenant, $items);
|
||||
$purchaseItemsPayload = $this->buildPurchaseItemsPayload($items, $variants);
|
||||
|
||||
// 5. Crear la orden de compra
|
||||
return DB::transaction(function () use ($tenant, $userId, $purchaseData, $purchaseItemsPayload, $paymentResult): Purchase {
|
||||
// 2. Crear la orden de compra y vaciar carrito
|
||||
return DB::transaction(function () use ($tenant, $userId, $purchaseData, $purchaseItemsPayload): Purchase {
|
||||
/** @var Purchase $purchase */
|
||||
$purchase = Purchase::query()->create([
|
||||
...$purchaseData,
|
||||
'tenant_codigo' => $tenant->codigo,
|
||||
'user_id' => $userId,
|
||||
// TODO: En un futuro se puede guardar info de $paymentResult en la base de datos (ej: payment_id, status)
|
||||
]);
|
||||
|
||||
$purchase->items()->createMany($purchaseItemsPayload);
|
||||
|
||||
// Vaciar y eliminar el carrito activo del usuario
|
||||
$cart = \App\Domains\Cart\Models\Cart::query()
|
||||
->where('tenant_codigo', $tenant->codigo)
|
||||
->where('user_id', $userId)
|
||||
->first();
|
||||
if ($cart) {
|
||||
$cart->items()->delete();
|
||||
$cart->delete();
|
||||
}
|
||||
|
||||
return $purchase->load(['items.variant.product', 'items.variant.definitions.productAttribute.attribute']);
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user