feat: add customer details fields to Purchase model and request, implement checkout process with cart clearing
This commit is contained in:
@@ -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