File: /var/www/vhost/disk-apps/alq-cali.bikenow.co/app/Http/Controllers/MercadoPagoMKTController.php
<?php
namespace App\Http\Controllers;
use App\Core\Order\Application\OrderService;
use App\Core\Payment\Application\PaymentGatewayService;
use App\Core\Payment\Application\PaymentTransactionService;
use App\Core\Payment\Methods\MercadoPagoMKT\MercadoPagoMKTStrategy;
use App\Core\Payment\PaymentMethodContext;
use App\Core\Payment\PaymentStatusEnum;
use App\Core\Ticket\Application\TicketService;
use App\Http\Controllers\Interfaces\PaymentGatewayControllerInterface;
use App\Models\Payment\PaymentGateway;
use Exception;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
class MercadoPagoMKTController extends PaymentGatewayBridgeController implements PaymentGatewayControllerInterface
{
private $util;
private $orderService;
private $ticketService;
private $paymentTransactionService;
public function __construct()
{
$this->util = new UtilController();
$this->orderService = new OrderService();
$this->ticketService = new TicketService();
$this->paymentTransactionService = new PaymentTransactionService();
}
public function payment(Request $request)
{
// no implemented
}
public function webhooksListener(Request $request)
{
$this->util->logFile($request);
$listenedEvents = [
"payment.created",
"payment.updated"
];
if (in_array($request->action, $listenedEvents)) {
$orderPaymentTransaction = $this->orderService->getByReference($request->reference);
$ticketPaymentTransaction = $this->ticketService->getByReference($request->reference);
$genericPaymentTransaction = $this->paymentTransactionService->getByReference($request->reference);
$paymentTransaction = $orderPaymentTransaction ?? $ticketPaymentTransaction ?? $genericPaymentTransaction;
if (!is_null($paymentTransaction)) {
if ($paymentTransaction === $orderPaymentTransaction) {
$this->orderService->validatePayment($paymentTransaction);
}
if ($paymentTransaction === $ticketPaymentTransaction) {
$this->ticketService->validatePayment($paymentTransaction);
}
if ($paymentTransaction === $genericPaymentTransaction) {
$this->paymentTransactionService->validatePayment($paymentTransaction);
}
}
}
return response('', Response::HTTP_NO_CONTENT);
}
public function getTransactionByReference(Request $request)
{
// no implemented
}
public function validatePayment($transactionId, $reference)
{
$paymentContext = new PaymentMethodContext($this->gatewayData->toArray());
$retrieveResponse = $paymentContext->retrieve($transactionId);
switch ($retrieveResponse->status()) {
case PaymentStatusEnum::CONFIRMED:
$this->updatePayment(
$retrieveResponse->id(),
'CONFIRMED',
2,
'Venta exitosa',
$retrieveResponse->id()
);
break;
case PaymentStatusEnum::DECLINED:
$this->updatePayment(
$retrieveResponse->id(),
$retrieveResponse->status(),
7,
$retrieveResponse->status(),
$retrieveResponse->id()
);
break;
}
}
public function responseTransaction(Request $request)
{
// no implemented
}
public function getAuthorizationCode($gatewayResponse)
{
return null;
}
public function getPaymentMethod($gatewayResponse){
return null;
}
public function configURL(Request $request, PaymentGateway $paymentProcessor)
{
try {
if ($paymentProcessor->page_response_for_gateway !== 'mercadopagoMKT') {
throw new Exception('Payment processor is not valid', 500);
}
$configURL = MercadoPagoMKTStrategy::getAuthorizationURL(
$paymentProcessor->client_id,
$paymentProcessor->id
);
return $configURL;
} catch (\Throwable $th) {
//throw $th;
new Exception('There was a problem with the MercadoPago configuration', 500);
}
}
public function config(Request $request)
{
try {
$paymentService = new PaymentGatewayService();
$paymentProcessor = $paymentService->find($request->state);
if ($paymentProcessor->page_response_for_gateway !== 'mercadopagoMKT') {
throw new Exception('Payment processor is not valid', 500);
}
$config = MercadoPagoMKTStrategy::getOAuthCredentials(
$paymentProcessor->client_id,
$paymentProcessor->client_secret,
$request->code
);
$paymentProcessor->api_key = $config->access_token;
$paymentProcessor->client_signature = $config->refresh_token;
$paymentProcessor->save();
return "Payment processor configured successfully";
} catch (\Throwable $th) {
//throw $th;
new Exception('There was a problem with the MercadoPago configuration', 500);
// return request()->all();
}
}
public function refreshToken(Request $request, PaymentGateway $paymentProcessor)
{
try {
if ($paymentProcessor->page_response_for_gateway !== 'mercadopagoMKT') {
throw new Exception('Payment processor is not valid', 500);
}
$config = MercadoPagoMKTStrategy::refreshOAuthCredentials(
$paymentProcessor->client_id,
$paymentProcessor->client_secret,
$paymentProcessor->client_signature
);
$paymentProcessor->api_key = $config->access_token;
$paymentProcessor->client_signature = $config->refresh_token;
$paymentProcessor->save();
return $paymentProcessor;
} catch (\Throwable $th) {
//throw $th;
new Exception('There was a problem with the MercadoPago configuration', 500);
// return request()->all();
}
}
}