132 lines
4.9 KiB
PHP
132 lines
4.9 KiB
PHP
<?php
|
|
|
|
namespace App\Domains\Purchase\Controllers;
|
|
|
|
use App\Domains\Catalog\Models\ProductVariant;
|
|
use App\Domains\Purchase\Models\Purchase;
|
|
use App\Domains\Purchase\Requests\StorePurchaseRequest;
|
|
use App\Domains\Purchase\Resources\PurchaseResource;
|
|
use App\Domains\Tenant\Models\Tenant;
|
|
use App\Http\Controllers\Controller;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use App\Domains\Purchase\Services\CheckoutService;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Validation\ValidationException;
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
|
|
|
class PurchaseController extends Controller
|
|
{
|
|
public function index(Request $request, Tenant $tenant): JsonResponse
|
|
{
|
|
return PurchaseResource::collection(
|
|
Purchase::query()
|
|
->with(['items.variant.product', 'items.variant.definitions.productAttribute.attribute'])
|
|
->where('tenant_codigo', $tenant->codigo)
|
|
->where('user_id', $request->user()->id)
|
|
->latest()
|
|
->paginateFromRequest()
|
|
)->response();
|
|
}
|
|
|
|
public function store(StorePurchaseRequest $request, Tenant $tenant, CheckoutService $checkoutService): JsonResponse
|
|
{
|
|
$data = $request->validated();
|
|
|
|
$purchase = $checkoutService->startCheckout(
|
|
$tenant,
|
|
$request->user()->id,
|
|
$data
|
|
);
|
|
|
|
return PurchaseResource::make($purchase)->response()->setStatusCode(201);
|
|
}
|
|
|
|
public function show(Request $request, Tenant $tenant, Purchase $compra): PurchaseResource
|
|
{
|
|
$compra = $this->resolveScopedPurchase($tenant, $request->user()->id, $compra);
|
|
|
|
return PurchaseResource::make(
|
|
$compra->loadMissing(['items.variant.product', 'items.variant.definitions.productAttribute.attribute'])
|
|
);
|
|
}
|
|
|
|
public function paymentIntent(\App\Domains\Purchase\Requests\PaymentIntentRequest $request, Tenant $tenant, Purchase $compra): JsonResponse
|
|
{
|
|
$compra = $this->resolveScopedPurchase($tenant, $request->user()->id, $compra);
|
|
$method = $request->validated('method');
|
|
|
|
$compra->update([
|
|
'payment_method' => $method,
|
|
]);
|
|
|
|
if ($method === 'transfer') {
|
|
$telepagosService = new \App\Domains\Integration\Services\TelepagosIntegrationService();
|
|
$telepagosService->forTenant($tenant->codigo);
|
|
|
|
try {
|
|
$accountInfo = $telepagosService->getAccountInfo();
|
|
|
|
return response()->json([
|
|
'payment_method' => 'transfer',
|
|
'transfer_data' => [
|
|
'titular' => $accountInfo['holder'] ?? null,
|
|
'cvu' => $accountInfo['cvu'] ?? null,
|
|
'alias' => $accountInfo['alias'] ?? null,
|
|
'cuit' => $accountInfo['cuit'] ?? null,
|
|
'entidad' => $accountInfo['entity'] ?? 'Telepagos S.A.',
|
|
],
|
|
]);
|
|
} catch (\Exception $e) {
|
|
return response()->json([
|
|
'message' => 'Error getting account info: ' . $e->getMessage()
|
|
], 500);
|
|
}
|
|
}
|
|
|
|
if ($method === 'qr') {
|
|
$telepagosService = new \App\Domains\Integration\Services\TelepagosIntegrationService();
|
|
$telepagosService->forTenant($tenant->codigo);
|
|
|
|
try {
|
|
Log::info("Generating QR for purchase ID: {$compra->id}, amount: {$compra->getTotalAmount()}");
|
|
$qrResponse = $telepagosService->generateQr(
|
|
$compra->getTotalAmount(),
|
|
'Compra',
|
|
"Compra #{$compra->id}"
|
|
);
|
|
|
|
$telepagosQr = $compra->telepagosQr()->create([
|
|
'qr_order_id' => (string) ($qrResponse['id'] ?? $qrResponse['order_id'] ?? $qrResponse['cashin_id'] ?? ''),
|
|
'qr_code' => $qrResponse['qr'] ?? $qrResponse['qr_code'] ?? $qrResponse['qr_data'] ?? '',
|
|
]);
|
|
} catch (\Exception $e) {
|
|
return response()->json([
|
|
'message' => 'Error generating QR: ' . $e->getMessage()
|
|
], 500);
|
|
}
|
|
|
|
return response()->json([
|
|
'payment_method' => 'qr',
|
|
'qr_data' => [
|
|
'qr_code' => $telepagosQr->qr_code,
|
|
'qr_order_id' => $telepagosQr->qr_order_id,
|
|
],
|
|
]);
|
|
}
|
|
|
|
return response()->json([
|
|
'message' => 'Invalid payment method.',
|
|
], 400);
|
|
}
|
|
|
|
protected function resolveScopedPurchase(Tenant $tenant, int $userId, Purchase $purchase): Purchase
|
|
{
|
|
if ($purchase->tenant_codigo !== $tenant->codigo || $purchase->user_id !== $userId) {
|
|
throw new NotFoundHttpException('Purchase not found for tenant.');
|
|
}
|
|
|
|
return $purchase;
|
|
}
|
|
}
|