File: /var/www/vhost/disk-apps/demo.sports-crowd.com/app/Http/Controllers/YappyController.php
<?php
namespace App\Http\Controllers;
use App\Order;
use App\BgFirma;
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 App\TicketMain;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redirect;
use App\Http\Controllers\PaymentGatewayBridgeController;
class YappyController extends PaymentGatewayBridgeController implements PaymentGatewayControllerInterface
{
private $util;
private $orderService;
private $ticketService;
private $paymentTransactionService;
public function __construct()
{
$this->determinateWebService('Yappy', 'Yappy');
$this->util = new UtilController();
$this->orderService = new OrderService();
$this->ticketService = new TicketService();
$this->paymentTransactionService = new PaymentTransactionService();
}
public function index($order)
{
if ($order->gw_state == 'CONFIRMED') {
return $this->webcheckoutDone($order);
}
$this->updatePendingStatus($order, true);
$order->string_price = $this->fmt->formatCurrency($order->total_price, $this->currency, $this->minimumFractionDigits, $this->maximumFractionDigits);
return view('yappy.webcheckout')
->with('order', $order)
->with('currency', $this->currency)
->with('gatewayData', $this->gatewayData)
->with('team', $this->team);
}
public function ticketIndex($ticket)
{
if ($ticket->payment_state == 'CONFIRMED') {
return $this->webcheckoutDone($ticket);
}
$this->updatePendingStatus($ticket, true);
$userController = new UserController();
$user = $userController->getById($ticket->user_id_log);
$ticket->string_price = $this->fmt->formatCurrency($ticket->total, $this->currency, $this->minimumFractionDigits, $this->maximumFractionDigits);
return view('yappy.webcheckout')
->with('ticket', $ticket)
->with('currency', $this->currency)
->with('gatewayData', $this->gatewayData)
->with('team', $this->team)
->with('phone', $user->phone);
}
public function genericIndex($paymentTransaction, $price, $origin, $clientId, $description) {}
public function payment(Request $request)
{
// Obtener el dominio del servidor
$protocol = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
$domain = $protocol . $_SERVER['HTTP_HOST'];
// verificar credenciales
$response = json_decode(BgFirma::checkCredentials(
$this->gatewayData->merchant_id,
$this->gatewayData->client_secret,
$domain
), true);
if (!$response['success']) {
return $this->returnErrorResponse($request, 'Error al verificar las credenciales');
}
$confirmUrl = $this->getConfirmUrl('/store/confirmtransaction', $request['origin'], $request['orderId']);
//Inicializar objeto para poder generar el url de exito
$bg = new BgFirma(
$request['subtotal'] + $request['taxes'],
$this->gatewayData->merchant_id,
$this->currency,
$request['subtotal'],
$request['taxes'],
$response['unixTimestamp'],
'YAP',
'VEN',
$request['orderId'],
$confirmUrl, //Url de confirmación
$confirmUrl, //Url de error
$domain,
$this->gatewayData->client_secret,
!$this->is_production, //Si es pruebas o prod
$response['accessToken'],
$request['phone']
);
$response = $bg->createHash();
if (!$response['success']) {
return $this->returnErrorResponse($request, $response['msg']);
}
return Redirect::to($response['url']);
}
private function returnErrorResponse(Request $request, $message)
{
$order = null;
$ticket = null;
switch ($request['origin']) {
case 'order':
$order = Order::where('id', $request['orderId'])->first();
$order->gw_state = 'ERROR';
$order->payment_comment = $message;
$order->gw_code_transaction = $message;
$order->update();
break;
case 'ticket':
$ticket = TicketMain::where('id', $request['orderId'])->with(['ticket_user_blocks' => function ($q) {
$q->where('ticket_user_blocks.is_social_distancing', false);
}])->first();
$ticket->payment_state = 'ERROR';
$ticket->payment_comment = $message;
$ticket->update();
break;
}
return $this->webcheckoutConfirm($order, $ticket);
}
public function webhooksListener(Request $request)
{
$this->util->logFile($request);
$orderPaymentTransaction = $this->orderService->getByPin($request->orderId);
$ticketPaymentTransaction = $this->ticketService->getByPin($request->orderId);
$genericPaymentTransaction = $this->paymentTransactionService->getByPin($request->orderId);
$paymentTransaction = $orderPaymentTransaction ?? $ticketPaymentTransaction ?? $genericPaymentTransaction;
if (!is_null($paymentTransaction)) {
if ($paymentTransaction === $orderPaymentTransaction) {
$this->orderService->validatePayment($paymentTransaction);
$paymentReference = $paymentTransaction->gw_code_transaction;
}
if ($paymentTransaction === $ticketPaymentTransaction) {
$this->ticketService->validatePayment($paymentTransaction);
$paymentReference = $paymentTransaction->payment_reference;
}
if ($paymentTransaction === $genericPaymentTransaction) {
$this->paymentTransactionService->validatePayment($paymentTransaction);
$paymentReference = $paymentTransaction->reference;
}
}
$status = $request->status;
if ($status == 'E') {
$this->updatePayment($paymentReference, 'CONFIRMED', 2, 'Venta exitosa', $request['confirmationNumber']);
} else {
$this->updatePayment($paymentReference, 'REJECTED', 7, 'Venta fallida', $paymentReference);
}
return response(array('r' => true, 'm' => "Recibido", 'd' => null));
}
public function getTransactionByReference(Request $request) {}
function validateHash(Request $request)
{
try {
$orderId = $request['orderId'];
$status = $request['status'];
$hash = $request['hash'];
$domain = $request['domain'];
$values = base64_decode($this->gatewayData->client_secret);
$secrete = explode('.', $values);
$signature = hash_hmac('sha256', $orderId . $status . $domain, $secrete[0]);
$success = strcmp($hash, $signature) === 0;
} catch (\Throwable $th) {
$success = false;
}
return $success;
}
public function validatePayment($transactionId, $reference) {}
/**
* @param Request $request
* @return mixed
*/
public function responseTransaction(Request $request) {}
public function getAuthorizationCode($gatewayResponse)
{
return null;
}
public function getPaymentMethod($gatewayResponse)
{
return null;
}
}