File: /var/www/vhost/disk-apps/agile-selling-wpb/app/Http/Controllers/WompiController.php
<?php
namespace App\Http\Controllers;
use App\GatewayPayment;
use App\Order;
use App\Parameter;
use Illuminate\Http\Request;
class WompiController extends Controller
{
private $url_ws;
private $private_key;
private $gateway_data;
public function __construct(){
$this->determinateWebService();
}
public function webcheckout(Request $request){
$id = $request->input("uid");
// Verifico sin consultar a Wompi.
$o = Order::where('id', $id)->first();
if($o->gw_state == 'CONFIRMED'){
return view("wompi.webcheckout-done");
}
// Consulto a Wompi el estado
$this->updateStatusOrderFromReference($id);
$o = Order::where('id', $id)->first();
if($o->gw_state == 'CONFIRMED'){
return view("wompi.webcheckout-done");
}
$o->gw_code_transaction = time() . "_" . $o->code;
$o->gw_state = "PENDING";
$o->update();
return view("wompi.webcheckout")
->with('gateway_data', $this->gateway_data)
->with('total_price', $o->total_price)
->with('reference', $o->gw_code_transaction)
->with('confirm_url', env('APP_URL') . '/store/wompi/confirm')
->with("order_id", $id);
}
public function confirmFromWompi(Request $request){
$id = $request->input("id");
$this->updateStatusOrderFromID($id);
return view("wompi.webcheckout-confirm")
->with("id", $id);
}
public 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;
}
}
public function updateStatusOrderFromID($wompi_id){
$client = new \GuzzleHttp\Client();
$response = $client->get($this->url_ws . "/transactions/" . $wompi_id);
$body = $response->getBody()->getContents();
$body = json_decode($body);
$o = Order::where('gw_code_transaction', $body->data->reference)->first();
$o->gw_state = $body->data->status == "APPROVED" ? "CONFIRMED" : "PENDING";
$o->update();
}
public function updateStatusOrderFromReference($order_id){
$o = Order::where('id', $order_id)->first();
$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();
}
}
public function getStatusTransaction($order_id){
$o = Order::where('id', $order_id)->first();
$client = new \GuzzleHttp\Client();
$response = $client->post($this->url_ws . "/transactions?reference=" . $o->gw_code_transaction, [
'headers' => ['Authorization' => $this->private_key]
]);
$body = $response->getBody()->getContents();
return json_decode($body);
}
}