refactor: remove PaymentProviderInterface and TelepagosProvider, simplify CheckoutService and related tests
This commit is contained in:
@@ -1,15 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace App\Domains\Integration\Services\Contracts;
|
|
||||||
|
|
||||||
interface PaymentProviderInterface
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* Procesa un pago con las credenciales específicas del Tenant.
|
|
||||||
*
|
|
||||||
* @param array $paymentData Datos del pago, incluyendo el método (ej. qr, transferencia).
|
|
||||||
* @param array $credentials Credenciales encriptadas previamente configuradas por el Tenant.
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function process(array $paymentData, array $credentials): array;
|
|
||||||
}
|
|
||||||
@@ -1,18 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace App\Domains\Integration\Services;
|
|
||||||
|
|
||||||
use App\Domains\Integration\Services\Contracts\PaymentProviderInterface;
|
|
||||||
use App\Domains\Integration\Services\Providers\TelepagosProvider;
|
|
||||||
use InvalidArgumentException;
|
|
||||||
|
|
||||||
class PaymentProviderFactory
|
|
||||||
{
|
|
||||||
public function make(string $integrationCode): PaymentProviderInterface
|
|
||||||
{
|
|
||||||
return match ($integrationCode) {
|
|
||||||
'telepagos' => new TelepagosProvider(),
|
|
||||||
default => throw new InvalidArgumentException("Integración de pago no soportada: {$integrationCode}"),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,19 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace App\Domains\Integration\Services\Providers;
|
|
||||||
|
|
||||||
use App\Domains\Integration\Services\Contracts\PaymentProviderInterface;
|
|
||||||
use BadMethodCallException;
|
|
||||||
|
|
||||||
class TelepagosProvider implements PaymentProviderInterface
|
|
||||||
{
|
|
||||||
public function process(array $paymentData, array $credentials): array
|
|
||||||
{
|
|
||||||
// Mocked response for development and local testing
|
|
||||||
return [
|
|
||||||
'status' => 'approved',
|
|
||||||
'transaction_id' => 'mock_tp_' . uniqid(),
|
|
||||||
'payment_method' => $paymentData['payment_method'] ?? 'qr',
|
|
||||||
];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -32,17 +32,10 @@ class PurchaseController extends Controller
|
|||||||
{
|
{
|
||||||
$data = $request->validated();
|
$data = $request->validated();
|
||||||
|
|
||||||
$integrationCode = $data['integration_code'];
|
|
||||||
$paymentData = $data['payment_data'];
|
|
||||||
|
|
||||||
unset($data['integration_code'], $data['payment_data']);
|
|
||||||
|
|
||||||
$purchase = $checkoutService->processCheckout(
|
$purchase = $checkoutService->processCheckout(
|
||||||
$tenant,
|
$tenant,
|
||||||
$request->user()->id,
|
$request->user()->id,
|
||||||
$data,
|
$data
|
||||||
$integrationCode,
|
|
||||||
$paymentData
|
|
||||||
);
|
);
|
||||||
|
|
||||||
return PurchaseResource::make($purchase)->response()->setStatusCode(201);
|
return PurchaseResource::make($purchase)->response()->setStatusCode(201);
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ use App\Domains\Purchase\Models\Purchase;
|
|||||||
use App\Domains\Tenant\Models\Tenant;
|
use App\Domains\Tenant\Models\Tenant;
|
||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
use Illuminate\Validation\ValidationException;
|
use Illuminate\Validation\ValidationException;
|
||||||
use Exception;
|
|
||||||
|
|
||||||
class CheckoutService
|
class CheckoutService
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ class TelepagosIntegrationSeeder extends Seeder
|
|||||||
*/
|
*/
|
||||||
public function run(): void
|
public function run(): void
|
||||||
{
|
{
|
||||||
$integration = Integration::updateOrCreate(
|
Integration::updateOrCreate(
|
||||||
['integration_code' => 'telepagos'],
|
['integration_code' => 'telepagos'],
|
||||||
[
|
[
|
||||||
'name' => 'Telepagos',
|
'name' => 'Telepagos',
|
||||||
@@ -24,7 +24,7 @@ class TelepagosIntegrationSeeder extends Seeder
|
|||||||
]
|
]
|
||||||
);
|
);
|
||||||
|
|
||||||
$integrationHomo = Integration::updateOrCreate(
|
Integration::updateOrCreate(
|
||||||
['integration_code' => 'telepagos_homo'],
|
['integration_code' => 'telepagos_homo'],
|
||||||
[
|
[
|
||||||
'name' => 'Telepagos Homologación',
|
'name' => 'Telepagos Homologación',
|
||||||
@@ -35,16 +35,5 @@ class TelepagosIntegrationSeeder extends Seeder
|
|||||||
]
|
]
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
|
|
||||||
// Seed association for sonder tenant
|
|
||||||
$tenantIntegrationService = app(\App\Domains\Integration\Services\TenantIntegrationService::class);
|
|
||||||
$tenantIntegrationService->updateOrCreateIntegration(
|
|
||||||
'sonder',
|
|
||||||
$integration,
|
|
||||||
[
|
|
||||||
'username' => 'sonder_telepagos',
|
|
||||||
'password' => 'secret123',
|
|
||||||
]
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ namespace Tests\Feature\Purchase;
|
|||||||
use App\Domains\Auth\Models\User;
|
use App\Domains\Auth\Models\User;
|
||||||
use App\Domains\Catalog\Models\Product;
|
use App\Domains\Catalog\Models\Product;
|
||||||
use App\Domains\Catalog\Models\ProductVariant;
|
use App\Domains\Catalog\Models\ProductVariant;
|
||||||
use App\Domains\Integration\Models\Integration;
|
|
||||||
use App\Domains\Tenant\Models\Tenant;
|
use App\Domains\Tenant\Models\Tenant;
|
||||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||||
use Tests\TestCase;
|
use Tests\TestCase;
|
||||||
@@ -19,33 +18,12 @@ class StorePurchaseTest extends TestCase
|
|||||||
// 1. Setup Tenant
|
// 1. Setup Tenant
|
||||||
$tenant = $this->createTenant('sonder', 'Sonder', 'sonder.com.ar');
|
$tenant = $this->createTenant('sonder', 'Sonder', 'sonder.com.ar');
|
||||||
|
|
||||||
// 2. Setup Integration
|
// 2. Setup User
|
||||||
$integration = Integration::create([
|
|
||||||
'integration_code' => 'telepagos',
|
|
||||||
'name' => 'Telepagos',
|
|
||||||
'url' => 'https://api.telepagos.com.ar',
|
|
||||||
'integration_data_schema' => [
|
|
||||||
'username' => 'required|string',
|
|
||||||
'password' => 'required|string',
|
|
||||||
]
|
|
||||||
]);
|
|
||||||
|
|
||||||
$tenantIntegrationService = app(\App\Domains\Integration\Services\TenantIntegrationService::class);
|
|
||||||
$tenantIntegrationService->updateOrCreateIntegration(
|
|
||||||
'sonder',
|
|
||||||
$integration,
|
|
||||||
[
|
|
||||||
'username' => 'sonder_telepagos',
|
|
||||||
'password' => 'secret123',
|
|
||||||
]
|
|
||||||
);
|
|
||||||
|
|
||||||
// 3. Setup User
|
|
||||||
$user = User::factory()->create([
|
$user = User::factory()->create([
|
||||||
'email' => 'buyer@example.com'
|
'email' => 'buyer@example.com'
|
||||||
]);
|
]);
|
||||||
|
|
||||||
// 4. Setup Product & Variant
|
// 3. Setup Product & Variant
|
||||||
$category = \App\Domains\Catalog\Models\Category::query()->create([
|
$category = \App\Domains\Catalog\Models\Category::query()->create([
|
||||||
'tenant_code' => 'sonder',
|
'tenant_code' => 'sonder',
|
||||||
'nombre' => 'Test Category',
|
'nombre' => 'Test Category',
|
||||||
@@ -68,7 +46,7 @@ class StorePurchaseTest extends TestCase
|
|||||||
'precio' => '50.00',
|
'precio' => '50.00',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
// 5. Add item to user's cart
|
// 4. Add item to user's cart
|
||||||
$this->actingAs($user, 'sanctum')
|
$this->actingAs($user, 'sanctum')
|
||||||
->postJson('/api/tenants/sonder/cart/items', [
|
->postJson('/api/tenants/sonder/cart/items', [
|
||||||
'product_variant_id' => $variant->id,
|
'product_variant_id' => $variant->id,
|
||||||
@@ -82,17 +60,14 @@ class StorePurchaseTest extends TestCase
|
|||||||
'user_id' => $user->id,
|
'user_id' => $user->id,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
// 6. Submit Purchase
|
// 5. Submit Purchase with payment_method at root
|
||||||
$response = $this->actingAs($user, 'sanctum')
|
$response = $this->actingAs($user, 'sanctum')
|
||||||
->postJson('/api/tenants/sonder/compras', [
|
->postJson('/api/tenants/sonder/compras', [
|
||||||
'dni' => '987654321',
|
'dni' => '987654321',
|
||||||
'telefono' => '+54 9 341 555-4321',
|
'telefono' => '+54 9 341 555-4321',
|
||||||
'nombre_apellido' => 'Juan Perez',
|
'nombre_apellido' => 'Juan Perez',
|
||||||
'email' => 'juan.perez@example.com',
|
'email' => 'juan.perez@example.com',
|
||||||
'integration_code' => 'telepagos',
|
'payment_method' => 'transferencia',
|
||||||
'payment_data' => [
|
|
||||||
'payment_method' => 'transferencia',
|
|
||||||
],
|
|
||||||
'items' => [
|
'items' => [
|
||||||
[
|
[
|
||||||
'producto_variante_id' => $variant->id,
|
'producto_variante_id' => $variant->id,
|
||||||
@@ -101,7 +76,7 @@ class StorePurchaseTest extends TestCase
|
|||||||
]
|
]
|
||||||
]);
|
]);
|
||||||
|
|
||||||
// 7. Assertions
|
// 6. Assertions
|
||||||
$response->assertCreated();
|
$response->assertCreated();
|
||||||
$response->assertJsonPath('data.dni', '987654321');
|
$response->assertJsonPath('data.dni', '987654321');
|
||||||
$response->assertJsonPath('data.telefono', '+54 9 341 555-4321');
|
$response->assertJsonPath('data.telefono', '+54 9 341 555-4321');
|
||||||
|
|||||||
Reference in New Issue
Block a user