File: /var/www/vhost/disk-apps/demo.sports-crowd.com/app/Console/Commands/SymbolicTicketPayments.php
<?php
namespace App\Console\Commands;
use App\Parameter;
use App\GatewayPayment;
use App\SymbolicTicketUser;
use Illuminate\Console\Command;
class SymbolicTicketPayments extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'sportscrowd:symbolicTicketPayments';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Valida cierto numero de veces si un pago de una boleta simbolica fue aprobado';
private $url_ws, $private_key, $gateway_data;
private $__PAYMENT_ATTEMPTS = 3; // Cuantas veces se intenta establecer el estado del pago.
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$this->determinateWebService();
$symbolicTicketsPending = SymbolicTicketUser::where('payment_state', 'PENDING')
->where('payment_attempts', '<', $this->__PAYMENT_ATTEMPTS)
->get();
foreach ($symbolicTicketsPending as $s) {
$s->payment_attempts = $s->payment_attempts + 1;
$s->update();
$this->updateStatusTicketFromReference($s);
}
}
public function updateStatusTicketFromReference($o)
{
$client = new \GuzzleHttp\Client();
$response = $client->get($this->url_ws . "/transactions?reference=" . $o->payment_reference, [
'headers' => ['Authorization' => 'Bearer ' . $this->private_key]
]);
$body = $response->getBody()->getContents();
$body = json_decode($body);
if ($body->data && count($body->data)) {
$ticket = end($body->data);
$isApproved = collect($body->data)->where('status', "APPROVED")->first();
if ($isApproved) {
$ticket = $isApproved;
}
if ($ticket->status == "APPROVED") {
$o->payment_state = "CONFIRMED";
$o->payment_comment = $ticket->status;
$o->update();
}
}
}
private function determinateWebService()
{
$parameters = Parameter::first();
$is_production = $parameters->is_production_gateway; // TRUE: Production ; FALSE: Test
$this->gateway_data = GatewayPayment::where("name", "wompiTicketOffice")
->where("is_productive", $is_production)
->first();
$this->private_key = $this->gateway_data->client_secret;
$this->public_key = $this->gateway_data->client_public;
if ($is_production) { // Si se encuentra en producción el gateway.
$this->url_ws = $this->gateway_data->gw_url_prd;
} else {
$this->url_ws = $this->gateway_data->gw_url_sandbox;
}
}
}