200 lines
5.9 KiB
PHP
200 lines
5.9 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Livewire;
|
|
use DateInterval;
|
|
use DateTime;
|
|
use Exception;
|
|
use constants;
|
|
|
|
use App\Models\recarga;
|
|
use App\Models\Saldo;
|
|
use App\Models\User;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Livewire\Component;
|
|
use Jantinnerezo\LivewireAlert\LivewireAlert;
|
|
use WpOrg\Requests\Requests;
|
|
|
|
|
|
|
|
class ShowRecarga extends Component
|
|
{
|
|
use LivewireAlert;
|
|
public $numero_nequi;
|
|
public $valor_recarga;
|
|
public $nequi =false;
|
|
public $cobrar = false;
|
|
public $monto = 0;
|
|
|
|
|
|
public function render()
|
|
{
|
|
// $this->fechaActual = date('d/m/Y');
|
|
if (auth()->user()->rol->nombre == "super" || auth()->user()->rol->nombre == "administrador" || auth()->user()->rol->nombre == "distribuidor") {
|
|
$this->cobrar = true;
|
|
$this->monto = "500";
|
|
}
|
|
$consulta_historial = recarga::where('usuario_id',Auth::user()->id)->orderBy('id','DESC')->limit(20)->get();
|
|
|
|
$consulta_saldo = User::where('id',Auth::user()->id)->with('saldo')->first();
|
|
|
|
return view('livewire.show-recarga',[
|
|
'user' => $consulta_saldo,
|
|
'historial_recargas' => $consulta_historial
|
|
]);
|
|
}
|
|
|
|
|
|
public function pagar(){
|
|
|
|
//auth2.0
|
|
$client_id='5ml5b35ijjk13f4q99utnsm97e';
|
|
$client_secret='1b3703aq0o6t1rrfti13q7uf7f42glik3gsf81s0d783shblmt8l';
|
|
$api_key='aHLdWJTpuM9YYnV9ruq4V6bH5esa8wUN69E1qmrM';
|
|
$endpoint="https://oauth.sandbox.nequi.com/oauth2/token?grant_type=client_credentials";
|
|
$date= Carbon::now()->format('Y-m-d\TH:i:s.z\Z');
|
|
//dd($date);
|
|
|
|
//dd($this->numero_nequi);
|
|
//push
|
|
$RestEndpoint = '/payments/v2/-services-paymentservice-unregisteredpayment';
|
|
|
|
try{
|
|
$authorization = 'Basic ' . base64_encode($client_id . ':' . $client_secret);
|
|
$headers = array(
|
|
'Content-Type' => 'application/x-www-form-urlencoded',
|
|
'Accept' => 'application/json',
|
|
'Authorization' => $authorization
|
|
);
|
|
|
|
//Solicitud
|
|
$request = Requests::post($endpoint, $headers);
|
|
|
|
|
|
if (isset($request->status_code) && $request->status_code == 200 && isset($request->body) && !empty($request->body))
|
|
{
|
|
$response = json_decode($request->body);
|
|
|
|
$tokenExpiresAt = new DateTime();
|
|
$tokenExpiresAt->add(new DateInterval('PT' . $response->expires_in . 'S'));
|
|
$token = $response->access_token;
|
|
$tokenType = $response->token_type;
|
|
} else {
|
|
|
|
throw new Exception('Unable to connect to Nequi, please check the information sent.');
|
|
}
|
|
}catch(Exception $e){
|
|
throw $e;
|
|
}
|
|
|
|
|
|
// pago
|
|
$headers1 = array(
|
|
'Content-Type' => 'application/json',
|
|
'Accept' => 'application/json',
|
|
'Authorization' => $token,
|
|
'x-api-key' => $api_key
|
|
);
|
|
|
|
$options1 = array(
|
|
'timeout' => 30
|
|
|
|
);
|
|
$endpoint1 = 'https://api.sandbox.nequi.com' . $RestEndpoint;
|
|
|
|
$body1 = json_encode(array(
|
|
'RequestMessage' => array(
|
|
'RequestHeader' => array(
|
|
'Channel' => 'PNP04-C001',
|
|
'RequestDate' => $date,
|
|
'MessageID' => '1234567890',
|
|
'ClientID' => $client_id,
|
|
'Destination' => array(
|
|
'ServiceName' => 'PaymentsService',
|
|
'ServiceOperation' => 'unregisteredPayment',
|
|
'ServiceRegion' => 'C001',
|
|
'ServiceVersion' => '1.2.0'
|
|
)
|
|
),
|
|
'RequestBody' => array(
|
|
'any' => array(
|
|
'unregisteredPaymentRQ' => array(
|
|
'phoneNumber'=> $this->numero_nequi,
|
|
'code' => 'NIT_1',
|
|
'value' => $this->valor_recarga,
|
|
'reference1' => 'Recarga SirPremium',
|
|
|
|
)
|
|
)
|
|
)
|
|
)
|
|
));
|
|
|
|
$request1 = Requests::post($endpoint1, $headers1, $body1, $options1);
|
|
|
|
|
|
if (isset($request1->status_code) && $request1->status_code == 200 && isset($request1->body) && !empty($request1->body)
|
|
) {
|
|
|
|
|
|
try {
|
|
$response1 = json_decode($request1->body);
|
|
//dd($response1 = json_decode($request1->body));
|
|
$status = $response1->ResponseMessage->ResponseHeader->Status;
|
|
$statusCode = isset($status) ? $status->StatusCode : '';
|
|
$statusDesc = isset($status) ? $status->StatusDesc : '';
|
|
|
|
if ($statusCode == 0) {
|
|
|
|
$this->alert('success', 'Solicitud de pago enviada', [
|
|
'position' => 'top'
|
|
]);
|
|
|
|
$payment = $response1->ResponseMessage->ResponseBody->any->unregisteredPaymentRS;
|
|
$trnId = isset($payment) ? trim($payment->transactionId) : '';
|
|
|
|
if ( !empty($trnId)) {
|
|
|
|
$recarga = new recarga;
|
|
$recarga-> payment_type_id= '';
|
|
$recarga-> payment_method_id= '';
|
|
$recarga-> status= $statusCode;
|
|
$recarga-> usuario_id= Auth::user()->id;
|
|
$recarga-> saldo_id= Auth::user()->saldo->id;
|
|
$recarga-> monto= $this->valor_recarga;
|
|
$recarga->save();
|
|
|
|
$consulta_saldo = Saldo::where('usuario_id',Auth::user()->id)->first();
|
|
$nuevo_saldo = $consulta_saldo->valor + $this->valor_recarga ;
|
|
$consulta_saldo->update(['valor'=> $nuevo_saldo]);
|
|
$this->alert('success', 'Saldo actualizado', [
|
|
'position' => 'top'
|
|
]);
|
|
|
|
$this->nequi = false;
|
|
}
|
|
$this->alert('success', $trnId, [
|
|
'position' => 'top'
|
|
]);
|
|
|
|
} else {
|
|
$this->alert('warning', $statusCode . ' = ' . $statusDesc, [
|
|
'position' => 'top'
|
|
]);
|
|
|
|
}
|
|
} catch (Exception $e) {
|
|
throw $e;
|
|
}
|
|
} else {
|
|
|
|
$this->alert('wanrning','Unable to connect to Nequi, please check the information sent', [
|
|
'position' => 'top'
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|