File: /var/www/vhost/disk-apps/agile-selling-wpb/app/Http/Controllers/Api/SymbolicTicketApiController.php
<?php
namespace App\Http\Controllers\Api;
use App\GatewayPayment;
use App\Http\Controllers\Controller;
use App\Parameter;
use App\SymbolicTicket;
use App\SymbolicTicketPrice;
use App\SymbolicTicketUser;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Storage;
class SymbolicTicketApiController extends Controller{
private $url_ws;
private $private_key;
private $gateway_data;
public function confirmFromWompiSymbolic(Request $request){
$this->determinateWebService();
$id = $request->input("id");
$this->updateStatusOrderFromID($id);
return view("wompi.webcheckout-confirm")->with("id", $id);
}
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 = SymbolicTicketUser::where('payment_reference', $body->data->reference)->first();
$o->payment_state = $body->data->status == "APPROVED" ? "CONFIRMED" : "PENDING";
$o->payment_comment = $body->data->status;
$o->update();
}
public function updateStatusOrderFromReference($order_id){
$o = SymbolicTicketUser::where('id', $order_id)->first();
$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){
$o->payment_state = $body->data[0]->status == "APPROVED" ? "CONFIRMED" : "PENDING";
$o->payment_comment = $body->data[0]->status;
$o->update();
}
}
public function getStatusTransaction($order_id){
$o = SymbolicTicketUser::where('id', $order_id)->first();
$client = new \GuzzleHttp\Client();
$response = $client->post($this->url_ws . "/transactions?reference=" . $o->payment_reference, [
'headers' => ['Authorization' => $this->private_key]
]);
$body = $response->getBody()->getContents();
return json_decode($body);
}
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 get(){
$data = SymbolicTicket::with('match_event')
->where('active', true)
// ->where('start_date','<=', Carbon::now())
// ->where('end_date', '>=', Carbon::now())
->orderBy('created_at','desc')
->get();
return response()->json($data, 200);
}
public function getPrices($id){
$data = SymbolicTicketPrice::where('symbolic_ticket_id', $id)
->orderBy('price', 'ASC')
->get();
return response()->json($data, 200);
}
public function getMyTickets($take, $offset){
$simbolics = SymbolicTicketUser::where([['user_id', Auth::user()->id],['payment_state','CONFIRMED']])
->with(['symbolic_ticket','symbolic_ticket_price','user'])
->take($take)
->offset($offset)
->orderBy('created_at', 'desc')
->get();
foreach ($simbolics as $item) {
$current_url = env('S3_SYMBOLIC_TICKETS_PATH') . $item->symbolic_ticket->path_image;
$item->path_image = $this->temporaryUrlAws($current_url,60);
}
return response()->json($simbolics, 200);
}
public function temporaryUrlAws($url,$minutes)
{
return Storage::disk('s3')->temporaryUrl($url, Carbon::now()->addMinutes($minutes));
}
public function createSymbolicTicketPending(Request $request){
if(Auth::user()->id != 2){
$price = SymbolicTicketPrice::where('id', $request['symbolic_ticket_price_id'])->first();
$total = $price->price * $request['quantity'];
$o = new SymbolicTicketUser;
$o->symbolic_ticket_id = $request['symbolic_ticket_id'];
$o->symbolic_ticket_price_id = $request['symbolic_ticket_price_id'];
$o->user_id = Auth::user()->id;
$o->price = $price->price;
$o->quantity = $request['quantity'];
$o->total = $total;
$o->payment_reference = "PENDING";
$o->payment_state = "PENDING";
$o->pin = $this->gen_uid();
$o->save();
return response()->json($o, 200);
}
return response()->json([], 500);
}
public function purchase(Request $request){
$this->determinateWebService();
$id = $request->input("uid");
// Verifico sin consultar a Wompi.
$o = SymbolicTicketUser::where('id', $id)->first();
if($o->payment_state == 'CONFIRMED'){
return view("wompi.webcheckout-done");
}
// Consulto a Wompi el estado
$this->updateStatusOrderFromReference($id);
$o = SymbolicTicketUser::where('id', $id)->first();
if($o->payment_state == 'CONFIRMED'){
return view("wompi.webcheckout-done");
}
$o->payment_reference = time() . "_" . $o->id . '_' . $o->pin;
$o->payment_state = "PENDING";
$o->update();
return view("wompi.webcheckout")
->with('gateway_data', $this->gateway_data)
->with('total_price', $o->total)
->with('reference', $o->payment_reference)
->with('confirm_url', env('APP_URL') . '/store/wompi/confirm_symbolic')
->with("order_id", $id);
}
}