File: /var/www/vhost/disk-apps/qas.sports-crowd.com/app/Http/Controllers/PaymentTransactionController.php
<?php
namespace App\Http\Controllers;
use App\Core\Payment\Application\PaymentTransactionService;
use App\PaymentTransaction;
use Carbon\Carbon;
class PaymentTransactionController extends Controller
{
private $paymentTransactionService;
public function __construct()
{
$this->paymentTransactionService = new PaymentTransactionService();
}
public function updatePaymentTransactionByReference($paymentReference, $paymentState, $paymentComment, $paymentTransactionId)
{
$paymentTransaction = PaymentTransaction::when($paymentReference, function ($query) use ($paymentReference) {
return $query->where('reference', $paymentReference);
})
->when($paymentTransactionId, function ($query) use ($paymentTransactionId) {
return $query->orWhere('gateway_transaction_id', $paymentTransactionId);
})
->first();
if ($paymentTransaction == null) {
return false;
}
$paymentTransaction->state = $paymentState;
$paymentTransaction->comment = $paymentComment;
$paymentTransaction->gateway_transaction_id = $paymentTransactionId;
$paymentTransaction->payment_date = new \DateTime();
$paymentTransaction->reference = $paymentReference;
$paymentTransaction->update();
if ($paymentState == 'CONFIRMED') {
if ($paymentTransaction->origin_class && !$paymentTransaction->finish_payment) {
$paymentTransaction->update(['finish_payment' => true]);
$originClass = new $paymentTransaction->origin_class;
$originClass->finishPayment($paymentTransaction);
}
}
}
public function updatePendingStatusTransaction($paymentTransaction, $add_time = false)
{
$add_time_text = $add_time ? '_' . microtime(true) : '';
if ($paymentTransaction->reference) {
$reference = explode('_', $paymentTransaction->reference);
array_pop($reference);
$paymentTransaction->reference = (implode('_', $reference) . $add_time_text);
} else
$paymentTransaction->reference = strtoupper(hash("md5", $paymentTransaction->id)) . '_' . $paymentTransaction->pin . $add_time_text;
$paymentTransaction->update();
}
public function validatePayments($validateHistoricalPayments = false)
{
$paymentTransactions = PaymentTransaction::where('state', '!=', 'CONFIRMED')->whereNotNull('gateway_payments_id')
->where(function ($query) use ($validateHistoricalPayments) {
if ($validateHistoricalPayments) {
$query->where('payment_transactions.created_at', '>', Carbon::now()->subDays(3)->startOfDay());
} else {
$query->where('payment_transactions.created_at', '>', Carbon::now()->subHours(3));
}
})
->get();
foreach ($paymentTransactions as $paymentTransaction) {
try {
$this->paymentTransactionService->validatePayment($paymentTransaction);
} catch (\Throwable $th) {
$error = [
'paymentTransaction' => $paymentTransaction,
'message' => $th->getMessage(),
'getFile' => $th->getFile(),
'getLine' => $th->getLine(),
];
$util = new UtilController;
$util->logFile(json_encode($error));
}
}
return array(
'r' => true,
'data' => null,
'm' => 'Se validaron ' . count($paymentTransactions) . ' pagos pendientes.'
);
}
public function validatePayment($paymentTransaction)
{
$this->paymentTransactionService->validatePayment($paymentTransaction);
}
}