358 lines
15 KiB
PHP
Executable File
358 lines
15 KiB
PHP
Executable File
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
namespace SmsNotifier;
|
|
|
|
use Psr\Log\LogLevel;
|
|
use SmsNotifier\Facade\TwilioNotifierFacade;
|
|
use SmsNotifier\Facade\PluginNotifierFacade;
|
|
use SmsNotifier\Facade\PluginOxxoNotifierFacade;
|
|
use SmsNotifier\Factory\NotificationDataFactory;
|
|
use SmsNotifier\Service\Logger;
|
|
use SmsNotifier\Service\OptionsManager;
|
|
use SmsNotifier\Service\PluginDataValidator;
|
|
use Twilio\Exceptions\TwilioException;
|
|
|
|
class Plugin
|
|
{
|
|
/**
|
|
* @var Logger
|
|
*/
|
|
private $logger;
|
|
|
|
/**
|
|
* @var OptionsManager
|
|
*/
|
|
private $optionsManager;
|
|
|
|
/**
|
|
* @var PluginDataValidator
|
|
*/
|
|
private $pluginDataValidator;
|
|
|
|
/**
|
|
* @var PluginNotifierFacade
|
|
*/
|
|
private $pluginNotifierFacade;
|
|
/**
|
|
* @var PluginOxxoNotifierFacade
|
|
*/
|
|
private $pluginOxxoNotifierFacade;
|
|
/**
|
|
* @var TwilioNotifierFacade
|
|
*/
|
|
private $notifierFacade;
|
|
|
|
/**
|
|
* @var NotificationDataFactory
|
|
*/
|
|
private $notificationDataFactory;
|
|
|
|
public function __construct(
|
|
Logger $logger,
|
|
OptionsManager $optionsManager,
|
|
PluginDataValidator $pluginDataValidator,
|
|
TwilioNotifierFacade $notifierFacade,
|
|
PluginNotifierFacade $pluginNotifierFacade,
|
|
PluginOxxoNotifierFacade $pluginOxxoNotifierFacade,
|
|
NotificationDataFactory $notificationDataFactory
|
|
) {
|
|
$this->logger = $logger;
|
|
$this->optionsManager = $optionsManager;
|
|
$this->pluginDataValidator = $pluginDataValidator;
|
|
$this->notifierFacade = $notifierFacade;
|
|
$this->pluginNotifierFacade = $pluginNotifierFacade;
|
|
$this->pluginOxxoNotifierFacade = $pluginOxxoNotifierFacade;
|
|
$this->notificationDataFactory = $notificationDataFactory;
|
|
}
|
|
|
|
public function run(): void
|
|
{
|
|
// $hola = PHP_SAPI;
|
|
// $this->logger->info('valor de PHP_SAPI: ' . $hola);
|
|
if (PHP_SAPI === 'fpm-fcgi') {
|
|
$this->logger->debug('Whatsapp over HTTP started');
|
|
$this->processHttpRequest();
|
|
$this->logger->debug('HTTP request processing ended.');
|
|
} elseif (PHP_SAPI === 'cli') {
|
|
$this->logger->debug('Whatsapp over CLI started');
|
|
$this->processCli();
|
|
$this->logger->debug('CLI process ended.');
|
|
} else {
|
|
throw new \UnexpectedValueException('Unknown PHP_SAPI type: ' . PHP_SAPI);
|
|
}
|
|
|
|
}
|
|
|
|
private function processCli(): void
|
|
{
|
|
if ($this->pluginDataValidator->validate()) {
|
|
$this->logger->info('Validating config');
|
|
$this->optionsManager->load();
|
|
}
|
|
}
|
|
|
|
private function processHttpRequest(): void
|
|
{
|
|
$pluginData = $this->optionsManager->load();
|
|
if ($pluginData->logging_level) {
|
|
$this->logger->setLogLevelThreshold(LogLevel::DEBUG);
|
|
}
|
|
|
|
|
|
$userInput = file_get_contents('php://input');
|
|
|
|
|
|
|
|
if (!$userInput) {
|
|
$this->logger->warning('no input');
|
|
|
|
return;
|
|
}
|
|
$event_json = json_decode($userInput);
|
|
$webhook_string = json_encode($event_json);
|
|
$this->logger->debug("El valor del evento recibio en json: " . $webhook_string . PHP_EOL);
|
|
// Maneja el evento del webhook
|
|
if ($event_json) {
|
|
switch ($event_json->type) {
|
|
case 'customer_cash_balance_transaction.created':
|
|
$this->logger->info('Evento de transfencia al cliente encontrado');
|
|
$this->logger->info('Valor del EventJSON: ' . json_encode($event_json) . PHP_EOL);
|
|
$this->pluginNotifierFacade->createPaymentIntent($event_json);
|
|
break;
|
|
case 'inbound_payment.payment_attempt':
|
|
$this->logger->info('Evento de Pagode OXXO recibido');
|
|
$this->logger->info('Valor del EventJSON: ' . json_encode($event_json) . PHP_EOL);
|
|
break;
|
|
case 'energy.alert':
|
|
$this->logger->info('Evento de Energía recibido: ' . $event_json->message . PHP_EOL);
|
|
break;
|
|
case 'oxxo.request':
|
|
$this->logger->info('Evento de referencia de oxxo recibido' . PHP_EOL);
|
|
|
|
|
|
// Construir la URL basada en el "client_id"
|
|
// $url = "https://siip.mx/wp/wp-content/uploads/img/voucher.png";
|
|
if (!empty($event_json->amount )) {
|
|
$this->logger->info('Referencia persnoalizada, Valor del monto: ' . $event_json->amount . PHP_EOL);
|
|
$intentos = 0;
|
|
do {
|
|
if($intentos>1){
|
|
sleep(2);
|
|
}
|
|
$url = $this->pluginOxxoNotifierFacade->createOxxoPaymentIntent($event_json, $event_json->amount );
|
|
$intentos++;
|
|
} while (strpos($url, 'https') !== 0 && $intentos < 5);
|
|
} else {
|
|
$intentos = 0;
|
|
do {
|
|
if($intentos>1){
|
|
sleep(2);
|
|
}
|
|
$url = $this->pluginOxxoNotifierFacade->createOxxoPaymentIntent($event_json);
|
|
$intentos++;
|
|
} while (strpos($url, 'https') !== 0 && $intentos < 5);
|
|
}
|
|
|
|
// Crear una respuesta en formato JSON
|
|
// $response = [
|
|
// 'event' => 'response.siip',
|
|
// 'status' => 'success',
|
|
// 'url' => $url
|
|
// ];
|
|
|
|
$response = '{' .
|
|
'"url": "' . $url . '"' .
|
|
'}';
|
|
|
|
$this->logger->debug('Reponse que se envía a CallBell: ' . $response);
|
|
// $json_codificado = json_encode($response);
|
|
// if (json_last_error() !== JSON_ERROR_NONE) {
|
|
// $this->logger->error('Error en la codificación JSON: ' . json_last_error_msg() . PHP_EOL);
|
|
|
|
// }
|
|
|
|
//$this->logger->info('Se está enviando esta respuesta: ' . json_encode($json_codificado) . PHP_EOL);
|
|
|
|
// Enviar el encabezado de respuesta como JSON
|
|
header('Content-Type: application/json');
|
|
|
|
// Enviar la respuesta en formato JSON
|
|
//echo json_encode($response);
|
|
echo $response;
|
|
|
|
break;
|
|
|
|
// Otros eventos relevantes
|
|
}
|
|
}
|
|
|
|
$jsonData = @json_decode($userInput, true, 10);
|
|
if (!isset($jsonData['uuid'])) {
|
|
$this->logger->error('JSON error: ' . json_last_error_msg());
|
|
|
|
return;
|
|
}
|
|
|
|
$notification = $this->notificationDataFactory->getObject($jsonData);
|
|
$this->logger->debug('valor el evento recibido por webhook: ' . $notification->eventName . PHP_EOL);
|
|
|
|
if ($notification->changeType === 'test') {
|
|
$this->logger->info('Webhook test successful.');
|
|
|
|
return;
|
|
}
|
|
if (!$notification->clientId) {
|
|
$this->logger->warning('No client specified, cannot notify them.');
|
|
|
|
return;
|
|
}
|
|
|
|
$configManager = \Ubnt\UcrmPluginSdk\Service\PluginConfigManager::create();
|
|
$config = $configManager->loadConfig();
|
|
// the "exportFormat" key must be defined in plugin's manifest file, see the link above
|
|
|
|
try {
|
|
|
|
if ($notification->eventName === 'payment.add') {
|
|
|
|
$result = json_encode($notification);
|
|
$this->logger->debug('datos del notification para el invoice add:' . $result . PHP_EOL);
|
|
|
|
$datos_payment = $notification->paymentData;
|
|
$this->logger->debug('valor del payment data: ' . json_encode($datos_payment) . PHP_EOL);
|
|
$payment_method_id = $notification->paymentData['methodId'];
|
|
//$this->logger->debug('Metodo de pago: ' . $notification->paymentData['methodId'] . PHP_EOL);
|
|
|
|
|
|
|
|
$payment_method = '';
|
|
|
|
|
|
switch ($payment_method_id) {
|
|
|
|
case '11721cdf-a498-48be-903e-daa67552e4f6':
|
|
$payment_method = 'Cheque';
|
|
if ($config['checkPaymentMethodId']) {
|
|
$this->notifierFacade->verifyPaymentActionToDo($notification);
|
|
}
|
|
break;
|
|
case '6efe0fa8-36b2-4dd1-b049-427bffc7d369':
|
|
$payment_method = 'Efectivo';
|
|
if ($config['cashPaymentMethodId']) {
|
|
$this->notifierFacade->verifyPaymentActionToDo($notification);
|
|
}
|
|
break;
|
|
case '4145b5f5-3bbc-45e3-8fc5-9cda970c62fb':
|
|
$payment_method = 'Transferencia bancaria';
|
|
if ($config['bankTransferPaymentMethodId']) {
|
|
$this->notifierFacade->verifyPaymentActionToDo($notification);
|
|
}
|
|
break;
|
|
case '78e84000-9b5b-44a4-8367-da43df86ce34':
|
|
$payment_method = 'PayPal';
|
|
if ($config['paypalPaymentMethodId']) {
|
|
$this->notifierFacade->verifyPaymentActionToDo($notification);
|
|
}
|
|
break;
|
|
case '6da98bb9-6df7-4c41-8608-5cdd7fde7d5d':
|
|
$payment_method = 'Tarjeta de crédito PayPal';
|
|
if ($config['creditCardPaypalPaymentMethodId']) {
|
|
$this->notifierFacade->verifyPaymentActionToDo($notification);
|
|
}
|
|
break;
|
|
case '1dd098fa-5d63-4c8d-88b7-3c27ffbbb6ae':
|
|
$payment_method = 'Tarjeta de crédito Stripe';
|
|
if ($config['creditCardStripePaymentMethodId']) {
|
|
$this->notifierFacade->verifyPaymentActionToDo($notification);
|
|
}
|
|
break;
|
|
case 'b9e1e9d1-5c7b-41d2-b6b2-3e568d700290':
|
|
$payment_method = 'Suscripción de Stripe (tarjeta de crédito)';
|
|
if ($config['stripeSubscriptionCreditCardPaymentMethodId']) {
|
|
$this->notifierFacade->verifyPaymentActionToDo($notification);
|
|
}
|
|
break;
|
|
case '939f7701-00b7-4676-9b1e-17afb268c8ba':
|
|
$payment_method = 'Suscripción de PayPal';
|
|
if ($config['paypalSubscriptionPaymentMethodId']) {
|
|
$this->notifierFacade->verifyPaymentActionToDo($notification);
|
|
}
|
|
break;
|
|
case '1c963e35-df24-444d-95d2-12592d5107e8':
|
|
$payment_method = 'MercadoPago';
|
|
if ($config['mercadopagoPaymentMethodId']) {
|
|
$this->notifierFacade->verifyPaymentActionToDo($notification);
|
|
}
|
|
break;
|
|
case 'd8c1eae9-d41d-479f-aeaf-38497975d7b3':
|
|
$payment_method = 'Personalizado';
|
|
if ($config['customPaymentMethodId']) {
|
|
$this->notifierFacade->verifyPaymentActionToDo($notification);
|
|
}
|
|
break;
|
|
case '72271b72-5c0a-45e2-94d1-cdf4d7cf10e2':
|
|
$payment_method = 'Cortesía';
|
|
if ($config['courtesyPaymentMethodId']) {
|
|
$this->notifierFacade->verifyPaymentActionToDo($notification);
|
|
}
|
|
break;
|
|
default:
|
|
$payment_method = 'Desconocido, revisar metodos de pago no contemplados';
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
} else if ($notification->eventName === 'client.edit') {
|
|
$this->logger->debug('Se actualiza a un cliente' . PHP_EOL);
|
|
$this->notifierFacade->verifyClientActionToDo($notification);
|
|
} else if ($notification->eventName === 'client.add') {
|
|
$this->logger->debug('Se agregó un nuevo cliente' . PHP_EOL);
|
|
$this->pluginNotifierFacade->createStripeClient($notification);
|
|
} else if ($notification->eventName === 'service.suspend') {
|
|
$this->logger->debug('Se suspendió el servicio a un cliente' . PHP_EOL);
|
|
$this->notifierFacade->verifyServiceActionToDo($notification);
|
|
} else if ($notification->eventName === 'service.suspend_cancel') {
|
|
$this->logger->debug('Se reactivó el servicio a un cliente' . PHP_EOL);
|
|
$this->notifierFacade->verifyServiceActionToDo($notification);
|
|
} else if ($notification->eventName === 'service.postpone') {
|
|
$this->logger->debug('Se pospuso la suspención del servicio a un cliente' . PHP_EOL);
|
|
$this->notifierFacade->verifyServiceActionToDo($notification);
|
|
} else if ($notification->eventName === 'invoice.near_due') {
|
|
$this->logger->debug('Factura casi por vencer' . PHP_EOL);
|
|
$this->notifierFacade->notifyOverDue($notification);
|
|
} else if ($notification->eventName === 'invoice.overdue') {
|
|
$this->logger->debug('Factura vencida' . PHP_EOL);
|
|
$result = json_encode($notification);
|
|
$this->logger->debug('datos del notification para el invoice overdue:' . $result . PHP_EOL);
|
|
$this->notifierFacade->notifyOverDue($notification);
|
|
} else if ($notification->eventName === 'invoice.add') {
|
|
$this->logger->debug('Adición de Factura' . PHP_EOL);
|
|
$result = json_encode($notification);
|
|
$this->logger->debug('datos del notification para el invoice add:' . $result . PHP_EOL);
|
|
|
|
$accountBalance = $notification->clientData['accountBalance'];
|
|
$invoiceAmountPaid = $notification->invoiceData['amountPaid'];
|
|
$this->logger->debug("Account Balance: " . $accountBalance . PHP_EOL);
|
|
$this->logger->debug("Pago hecho con la factura: " . $invoiceAmountPaid . PHP_EOL);
|
|
$this->notifierFacade->verifyInvoiceActionToDo($notification);
|
|
} else if ($notification->eventName === 'invoice.edit') {
|
|
$this->logger->debug('Edición de Factura' . PHP_EOL);
|
|
$this->notifierFacade->verifyInvoiceActionToDo($notification);
|
|
}
|
|
|
|
|
|
|
|
//$this->notifierFacade->update($notification);
|
|
} catch (TwilioException $exception) {
|
|
$this->logger->error($exception->getMessage());
|
|
} catch (\Exception $ex) {
|
|
$this->logger->error($ex->getMessage());
|
|
$this->logger->info($ex->getTraceAsString());
|
|
}
|
|
}
|
|
} |