File: /var/www/vhost/disk-apps/demo.sports-crowd.com/app/Http/Controllers/PlaceToPayController.php
<?php
namespace App\Http\Controllers;
use App\Core\Order\Application\OrderService;
use App\Core\Order\OrderStatusEnum;
use App\Core\Payment\Application\PaymentTransactionService;
use App\Core\Payment\PaymentMethodContext;
use App\Core\Payment\PaymentStatusEnum;
use App\Core\Ticket\Application\TicketService;
use App\Http\Controllers\Interfaces\PaymentGatewayControllerInterface;
use App\TicketMain;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
class PlaceToPayController 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);
$orderPaymentTransaction = $this->orderService->getByPaymentGatewayTxId($request->requestId);
$ticketPaymentTransaction = $this->ticketService->getByPaymentGatewayTxId($request->requestId);
$genericPaymentTransaction = $this->paymentTransactionService->getByPaymentGatewayTxId($request->requestId);
$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('OK', Response::HTTP_OK);
}
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 'CONFIRMED':
$this->updatePayment(
$retrieveResponse->id(),
'CONFIRMED',
2,
'Venta exitosa',
$retrieveResponse->id()
);
break;
case 'REJECTED':
$this->updatePayment(
$retrieveResponse->id(),
$retrieveResponse->status(),
7,
$retrieveResponse->status(),
$retrieveResponse->id()
);
break;
}
}
public function responseTransaction(Request $request)
{
// no implemented
}
private function confirmPayment($paymentTransaction, $placetopayTransactionId, $message)
{
$this->updatePayment(
$paymentTransaction->reference,
'CONFIRMED',
2,
$message,
$placetopayTransactionId
);
}
private function voidPayment($paymentTransaction, $placetopayTransactionId, $message)
{
$this->updatePayment(
$paymentTransaction->reference,
'VOIDED',
7,
$message,
$placetopayTransactionId
);
}
private function setTicketAsConfirmed($ticket)
{
$currentTicket = TicketMain::where('id', $ticket->id)
->with(['ticket_user_blocks' => function ($q) {
$q->where('ticket_user_blocks.is_social_distancing', false);
}])
->first();
if (!$currentTicket->ticket_user_blocks->isEmpty()) {
$tickets = new TicketsController();
$paymentState = $tickets->generateTickets($currentTicket->ticket_user_blocks, $currentTicket->id);
}
if ($paymentState) {
$currentTicket->payment_state = PaymentStatusEnum::CONFIRMED;
$currentTicket->update();
}
$ticket->gw_state = OrderStatusEnum::CONFIRMED;
$ticket->update();
}
public function getAuthorizationCode($gatewayResponse)
{
return $this->getApprovedPayment($gatewayResponse)->authorization ?? null;
}
public function getPaymentMethod($gatewayResponse){
return $this->getApprovedPayment($gatewayResponse)->paymentMethod ?? null;
}
private function getApprovedPayment($gatewayResponse){
if (json_decode($gatewayResponse)->status->status != 'APPROVED') {
return null;
}
$approved = array_filter(json_decode($gatewayResponse)->payment, function ($item) {
return $item->status->status == 'APPROVED';
}, ARRAY_FILTER_USE_BOTH);
return end($approved);
}
}