File: /var/www/vhost/disk-apps/agile-selling-orl/app/Console/Commands/TicketPayments.php
<?php
namespace App\Console\Commands;
use App\Parameter;
use App\TicketMain;
use App\GatewayPayment;
use Illuminate\Console\Command;
use App\Http\Controllers\TicketsController;
class TicketPayments extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'dim:ticketPayments';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Valida cierto numero de veces si un pago de una boleta 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();
$ticketsPending = TicketMain::where('payment_state', 'PENDING')
->with(['ticket_user_blocks' => function ($q){
$q->where('ticket_user_blocks.is_social_distancing', false);
}])
->where('payment_attempts' , '<', $this->__PAYMENT_ATTEMPTS)
->get();
foreach($ticketsPending as $o){
$o->payment_attempts = $o->payment_attempts + 1;
$o->update();
$this->updateStatusOrderFromReference($o);
}
}
public function updateStatusOrderFromReference($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){
if($body->data[0]->status == "APPROVED" && !$o->ticket_user_blocks->isEmpty()){
$tickets = new TicketsController();
$tickets->generateTickets($o->ticket_user_blocks,$o->id);
}
$o->payment_state = $body->data[0]->status == "APPROVED" ? "CONFIRMED" : "PENDING";
$o->payment_comment = $body->data[0]->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", "wompi")
->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;
}
}
}