Skip to content

Commit

Permalink
Merge pull request #95 from Crudzaso/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
DiegoAndresRamirez authored Dec 9, 2024
2 parents 90f13d8 + 13567f3 commit d74d482
Show file tree
Hide file tree
Showing 8 changed files with 433 additions and 262 deletions.
93 changes: 50 additions & 43 deletions app/Http/Controllers/PaymentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,58 +2,65 @@

namespace App\Http\Controllers;

use Carbon\Carbon;
use App\Services\MPService;
use Illuminate\Http\Request;
use Modules\Payment\Models\Payment;
use Modules\Raffle\Models\Raffle;
use Modules\Ticket\Models\Ticket;

class PaymentController extends Controller
{
public function create(Request $request)
protected $mercadoPagoService;

public function __construct(MPService $mercadoPagoService)
{
$this->mercadoPagoService = $mercadoPagoService;
}

// Mostrar el formulario de pago
public function showPaymentForm()
{
return view('mercadopago.payment');
}

// Crear la preferencia de pago
public function createPayment(Request $request)
{
$raffle = Raffle::find($request->raffle_id);
$user = auth()->user();

// Datos del pago
$amount = $raffle->ticket_price;
$nequiNumber = config('services.nequi.number'); // Número de Nequi de la plataforma

// Generar enlace de pago
$paymentLink = "https://recarga.nequi.com/$nequiNumber?amount=$amount";
$items = [
[
"id" => "1234567890",
"title" => "Producto 1",
"description" => "Descripción del producto 1",
"currency_id" => "COP",
"quantity" => 1,
"unit_price" => 1000.00
]
];

return response()->json([
'paymentLink' => $paymentLink,
'qrCode' => "https://api.qrserver.com/v1/create-qr-code/?data=$paymentLink",
]);
$payer = [
"name" => $user->name,
"surname" => $user->lastname,
"email" => $user->email,
];

$preference = $this->mercadoPagoService->createPaymentPreference($items, $payer);

if ($preference) {
return response()->json(['id' => $preference->id]); // Devolver el ID de la preferencia
} else {
return response()->json(['error' => 'No se pudo crear la preferencia de pago.'], 500);
}
}

public function store(Request $request)
// Página de éxito del pago
public function success()
{
$user = auth()->user();
$raffle = Raffle::find($request->raffle_id);

// Crear ticket
$ticket = Ticket::create([
'raffle_id' => $raffle->id,
'user_id' => $user->id,
'ticket_number' => $request->ticket_number,
'purchase_date' => Carbon::now(),
'verification_code' => uniqid(),
]);

// Registrar pago
$payment = Payment::create([
'user_id' => $user->id,
'raffle_id' => $raffle->id,
'amount' => $raffle->ticket_price,
'payment_method' => 'Nequi',
'payment_date' => Carbon::now(),
]);

return response()->json([
'message' => 'Pago registrado exitosamente',
'ticket' => $ticket,
'payment' => $payment,
]);
}}
return redirect()->route('mercadopago.payment');
}

// Página de fallo en el pago
public function failure()
{
return redirect()->route('mercadopago.payment');
}
}
67 changes: 67 additions & 0 deletions app/Services/MPService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace App\Services;

use MercadoPago\MercadoPagoConfig;
use MercadoPago\Client\Preference\PreferenceClient;
use MercadoPago\Exceptions\MPApiException;
use App\Events\ErrorOccurred;
use Illuminate\Support\Facades\Log;

class MPService
{
public function __construct()
{
$this->authenticate();
}

protected function authenticate()
{
$mpAccessToken = env('MERCADOPAGO_ACCESS_TOKEN');
MercadoPagoConfig::setAccessToken($mpAccessToken);
}

public function createPaymentPreference($items, $payer)
{
$paymentMethods = [
"excluded_payment_methods" => [], // Métodos de pago excluidos, si es necesario
"installments" => 12, // Número de cuotas disponibles
"default_installments" => 1, // Cuotas predeterminadas
];

$backUrls = [
'success' => route('mercadopago.success'),
'failure' => route('mercadopago.failed'),
];

$request = [
"items" => $items,
"payer" => $payer,
"payment_methods" => $paymentMethods,
"back_urls" => $backUrls,
"statement_descriptor" => "NOMBRE_EN_FACTURA", // Nombre que aparecerá en la factura
"external_reference" => "1234567890",
"expires" => false,
"auto_return" => 'approved', // Automáticamente regresa cuando el pago es aprobado
];

$client = new PreferenceClient();

try {
// Crear la preferencia de pago
$preference = $client->create($request);
return $preference;
} catch (MPApiException $e) {
// Log de error más detallado
Log::error('Mercado Pago error:', [
'message' => $e->getMessage(),
'code' => $e->getCode(),
'trace' => $e->getTraceAsString(),
]);

// Aquí puedes lanzar un evento para manejar el error si lo deseas
event(new ErrorOccurred('Error al crear la preferencia de pago con Mercado Pago', $e->getMessage()));
return null; // Si hay error, devuelve null
}
}
}
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"laravel/socialite": "^5.16",
"laravel/telescope": "^5.2",
"laravel/tinker": "^2.9",
"mercadopago/dx-php": "3.0.8",
"nwidart/laravel-modules": "^11.1",
"php-imap/php-imap": "^2.0",
"spatie/laravel-permission": "^6.9",
Expand Down
Loading

0 comments on commit d74d482

Please sign in to comment.