/**
  * @param Observer $observer
  * @return void
  */
 public function execute(\Magento\Framework\Event\Observer $observer)
 {
     $webhook = $this->storeManager->getStore()->getBaseUrl() . "cpwebhook/";
     try {
         $client = new Client($this->model->getPublicKey(), $this->model->getPrivateKey(), $this->model->getLiveMode());
         Validations::validateGateway($client);
         $client->api->createWebhook($webhook);
         $response = $client->api->hookRetro($this->model->getConfigData('active') == 1);
         if ($response[0]) {
             $this->messageManager->addNotice("ComproPago: {$response[1]}");
         }
     } catch (\Exception $e) {
         $this->messageManager->addError("ComproPago: {$e->getMessage()}");
     }
 }
Example #2
0
 public function procesWebhook($json = null)
 {
     /**
      * Se valida el request y se transforma con la cadena a un objeto de tipo CpOrderInfo con el Factory
      */
     if (empty($json) || !($resp_webhook = Factory::cpOrderInfo($json))) {
         die('Tipo de Request no Valido');
     }
     /**
      * Gurdamos la informacion necesaria para el Cliente
      * las llaves de compropago y el modo de ejecucion de la tienda
      */
     $publickey = $this->model->getPublicKey();
     $privatekey = $this->model->getPrivateKey();
     $live = $this->model->getLiveMode();
     // si es modo pruebas cambiar por 'false'
     /**
      * Se valida que las llaves no esten vacias (No es obligatorio pero si recomendado)
      */
     //keys set?
     if (empty($publickey) || empty($privatekey)) {
         die("Se requieren las llaves de compropago");
     }
     try {
         /**
          * Se incializa el cliente
          */
         $client = new Client($publickey, $privatekey, $live);
         /**
          * Validamos que nuestro cliente pueda procesar informacion
          */
         Validations::validateGateway($client);
     } catch (\Throwable $e) {
         //something went wrong at sdk lvl
         die($e->getMessage());
     }
     /**
      * Verificamos si recivimos una peticion de prueba
      */
     if ($resp_webhook->getId() == "ch_00000-000-0000-000000") {
         die("Probando el WebHook?, <b>Ruta correcta.</b>");
     }
     try {
         /**
          * Verificamos la informacion del Webhook recivido
          */
         $response = $client->api->verifyOrder($resp_webhook->getId());
         /**
          * Comprovamos que la verificacion fue exitosa
          */
         if ($response->getType() == 'error') {
             die('Error procesando el número de orden');
         }
         $this->orderManager->loadByIncrementId($response->getOrderInfo()->getOrderId());
         /**
          * Generamos las rutinas correspondientes para cada uno de los casos posible del webhook
          */
         switch ($response->getType()) {
             case 'charge.success':
                 $this->orderManager->setState('processing');
                 break;
             case 'charge.pending':
                 $this->orderManager->setState('pending_payment');
                 break;
             case 'charge.declined':
                 $this->orderManager->setState('canceled');
                 break;
             case 'charge.expired':
                 $this->orderManager->setState('canceled');
                 break;
             case 'charge.deleted':
                 $this->orderManager->setState('canceled');
                 break;
             case 'charge.canceled':
                 $this->orderManager->setState('canceled');
                 break;
             default:
                 die('Invalid Response type');
         }
     } catch (\Exception $e) {
         //something went wrong at sdk lvl
         die($e->getMessage());
     }
 }
Example #3
0
 /**
  * Despliegue de retroalimentacion en el panel de administración
  *
  * @param bool   $enabled
  * @return array
  */
 public function hookRetro($enabled = true)
 {
     $error = array(false, '', 'yes');
     if ($enabled) {
         if (!empty($this->client->getPublickey()) && !empty($this->client->getPrivatekey())) {
             try {
                 $compropagoResponse = Validations::evalAuth($this->client);
                 //eval keys
                 if (!Validations::validateGateway($this->client)) {
                     $error[1] = 'Invalid Keys, The Public Key and Private Key must be valid before using this module.';
                     $error[0] = true;
                 } else {
                     if ($compropagoResponse->mode_key != $compropagoResponse->livemode) {
                         $error[1] = 'Your Keys and Your ComproPago account are set to different Modes.';
                         $error[0] = true;
                     } else {
                         if ($this->client->getMode() != $compropagoResponse->livemode) {
                             $error[1] = 'Your Store and Your ComproPago account are set to different Modes.';
                             $error[0] = true;
                         } else {
                             if ($this->client->getMode() != $compropagoResponse->mode_key) {
                                 $error[1] = 'Your keys are for a different Mode.';
                                 $error[0] = true;
                             } else {
                                 if (!$compropagoResponse->mode_key && !$compropagoResponse->livemode) {
                                     $error[1] = 'Account is running in TEST mode, NO REAL OPERATIONS';
                                     $error[0] = true;
                                 }
                             }
                         }
                     }
                 }
             } catch (\Exception $e) {
                 $error[2] = 'no';
                 $error[1] = $e->getMessage();
                 $error[0] = true;
             }
         } else {
             $error[1] = 'The Public Key and Private Key must be set before using';
             $error[2] = 'no';
             $error[0] = true;
         }
     } else {
         $error[1] = 'The module is not enable';
         $error[2] = 'no';
         $error[0] = true;
     }
     return $error;
 }