Files
sirpremiumv2/app/Http/Controllers/TwilioSMSController.php
T
2023-11-07 22:43:10 +00:00

217 lines
6.8 KiB
PHP
Executable File

<?php
namespace App\Http\Controllers;
use DateInterval;
use DateTime;
use Exception;
use WpOrg\Requests\Requests;
use Illuminate\Support\Facades\Route;
use Twilio\Jwt\AccessToken;
use Twilio\Rest\Client;
class TwilioSMSController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function index()
{
//auth2.0
$client_id=getenv("NEQUI_CLIENT_ID");
$client_secret=getenv("NEQUI_CLIENT_SECRET");
$api_key=getenv("NEQUI_API_KEY");
$endpoint=getenv("NEQUI_ENDPOINT");
//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
);
$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
);
$options = array(
'timeout' => 30
);
$endpoint1 = 'https://api.sandbox.nequi.com' . $RestEndpoint;
$body1 = json_encode(array(
'RequestMessage' => array(
'RequestHeader' => array(
'Channel' => 'PNP04-C001',
'RequestDate' => '2020-01-14T10:26:12.654Z',
'MessageID' => '1234567890',
'ClientID' => '12345',
'Destination' => array(
'ServiceName' => 'PaymentsService',
'ServiceOperation' => 'unregisteredPayment',
'ServiceRegion' => 'C001',
'ServiceVersion' => '1.2.0'
)
),
'RequestBody' => array(
'any' => array(
'unregisteredPaymentRQ' => array(
'phoneNumber'=> '3193590918',
'code' => 'NIT_1',
'value' => '1000',
'reference1' => 'Referencia numero 1',
'reference2' => 'Referencia numero 2',
'reference3' => 'Referencia numero 3'
)
)
)
)
));
$request1 = Requests::post($endpoint1, $headers1, $body1, $options);
if (
isset($request1->status_code) && $request1->status_code == 200
&& isset($request1->body) && !empty($request1->body)
) {
dd($request1);
try {
$response1 = json_decode($request->body);
$status = $response1->ResponseMessage->ResponseHeader->Status;
$statusCode = isset($status) ? $status->StatusCode : '';
$statusDesc = isset($status) ? $status->StatusDesc : '';
if ($statusCode == Constan::NEQUI_STATUS_CODE_SUCCESS) {
$payment = $response1->ResponseMessage->ResponseBody->any->unregisteredPaymentRS;
$trnId = isset($payment) ? trim($payment->transactionId) : '';
} else {
throw new Exception('Error ' . $statusCode . ' = ' . $statusDesc);
}
} catch (Exception $e) {
throw $e;
}
} else {
throw new Exception('Unable to connect to Nequi, please check the information sent.');
}
// $auth_basic = base64_encode("5ml5b35ijjk13f4q99utnsm97e:1b3703aq0o6t1rrfti13q7uf7f42glik3gsf81s0d783shblmt8l");
// $curl = curl_init();
// curl_setopt_array($curl, array(
// CURLOPT_URL => "https://oauth.sandbox.nequi.com/oauth2/token?grant_type=client_credentials",
// CURLOPT_RETURNTRANSFER => true,
// CURLOPT_ENCODING => "",
// CURLOPT_MAXREDIRS => 10,
// CURLOPT_TIMEOUT => 30,
// CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
// CURLOPT_CUSTOMREQUEST => "POST",
// //CURLOPT_POSTFIELDS => '{"message":"Text of the SMS message", "tpoa":"Sender","recipient":[{"msisdn":"573168950803"},{"msisdn":"573022548060"}]}',
// CURLOPT_HTTPHEADER => array(
// "Authorization: Basic ".$auth_basic,
// "Cache-Control: no-cache",
// "Content-Type: application/x-www-form-urlencoded"
// ),
// ));
// $response = curl_exec($curl);
// $err = curl_error($curl);
// curl_close($curl);
// if ($err) {
// echo "cURL Error #:" . $err;
// } else {
// echo $response;
// $AccessToken=json_decode($response)->access_token;
// //dd($AccessToken);
// $receiverNumber = "RECEIVER_NUMBER";
// $message = "All About Laravel";
// try {
// $auth_basic = base64_encode(getenv("USER_LABSMOBILE").':'.getenv("PASS_LABSMOBILE"));
// $curl = curl_init();
// curl_setopt_array($curl, array(
// CURLOPT_URL => "https://api.labsmobile.com/json/send",
// CURLOPT_RETURNTRANSFER => true,
// CURLOPT_ENCODING => "",
// CURLOPT_MAXREDIRS => 10,
// CURLOPT_TIMEOUT => 30,
// CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
// CURLOPT_CUSTOMREQUEST => "POST",
// CURLOPT_POSTFIELDS => '{"message":"Text of the SMS message", "tpoa":"Sender","recipient":[{"msisdn":"573168950803"},{"msisdn":"573022548060"}]}',
// CURLOPT_HTTPHEADER => array(
// "Authorization: Basic ".$auth_basic,
// "Cache-Control: no-cache",
// "Content-Type: application/json"
// ),
// ));
// $response = curl_exec($curl);
// $err = curl_error($curl);
// curl_close($curl);
// if ($err) {
// echo "cURL Error #:" . $err;
// } else {
// echo $response;
// }
//$account_sid = getenv("TWILIO_SID");
//$auth_token = getenv("TWILIO_TOKEN");
//$twilio_number = getenv("TWILIO_FROM");
//$client = new Client($account_sid, $auth_token);
//$client->messages->create('+573168950803', [
// 'from' => $twilio_number,
// 'body' => $message]);
//dd('SMS Sent Successfully.');
// } catch (Exception $e) {
// dd("Error: ". $e->getMessage());
// }
}
}