File: /var/www/vhost/disk-apps/agile-selling-wpb/app/Console/Commands/OrderPayments.php
<?php
namespace App\Console\Commands;
use App\Order;
use App\Parameter;
use App\GatewayPayment;
use Illuminate\Console\Command;
class OrderPayments extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'dim:orderPayments';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Valida cierto numero de veces si un pago de un pedido 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();
$ordersPending = Order::where('gw_state', 'PENDING')
->where('payment_attempts' , '<', $this->__PAYMENT_ATTEMPTS)
->get();
foreach($ordersPending 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->gw_code_transaction, [
'headers' => ['Authorization' => 'Bearer ' . $this->private_key]
]);
$body = $response->getBody()->getContents();
$body = json_decode($body);
if($body->data){
$o->gw_state = $body->data[0]->status == "APPROVED" ? "CONFIRMED" : "PENDING";
$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;
}
}
}