feat: add payment intent handling in PurchaseController with validation and QR generation

This commit is contained in:
2026-07-06 11:47:04 -03:00
parent 3731cd67fe
commit 6bda3bf013
6 changed files with 89 additions and 2 deletions

View File

@@ -50,7 +50,67 @@ class PurchaseController extends Controller
);
}
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') {
$bankAccount = $tenant->selectedBankAccount;
return response()->json([
'payment_method' => 'transfer',
'transfer_data' => $bankAccount ? [
'titular' => $bankAccount->titular,
'entidad' => $bankAccount->entidad,
'alias' => $bankAccount->alias,
'cvu' => $bankAccount->cvu,
] : null,
]);
}
if ($method === 'qr') {
$telepagosQr = $compra->telepagosQr;
if (!$telepagosQr) {
$telepagosService = new \App\Domains\Integration\Services\TelepagosIntegrationService();
$telepagosService->forTenant($tenant->codigo);
try {
$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
{