File: /var/www/vhost/disk-apps/demo.sports-crowd.com/app/Http/Controllers/PayUController.php
<?php
namespace App\Http\Controllers;
use App\Core\Order\Application\OrderService;
use App\Core\Payment\Application\PaymentTransactionService;
use App\Core\Ticket\Application\TicketService;
use App\Http\Controllers\Interfaces\PaymentGatewayControllerInterface;
use Illuminate\Http\Request;
class PayUController 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);
$rawData = $request->getContent();
$data = [];
parse_str(urldecode($rawData), $data);
$orderPaymentTransaction = $this->orderService->getByReference($data['reference_sale']);
$ticketPaymentTransaction = $this->ticketService->getByReference($data['reference_sale']);
$genericPaymentTransaction = $this->paymentTransactionService->getByReference($data['reference_sale']);
$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);
}
}
}
public function getTransactionByReference(Request $request)
{
// no implemented
}
public function validatePayment($transactionId, $reference)
{
// no implemented
}
public function responseTransaction(Request $request)
{
// no implemented
}
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);
}
}