/**
  * @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()}");
     }
 }
 /**
  * create an order in ComproPago
  *
  * @return array
  */
 private function createOrder()
 {
     $order = $this->checSession->getLastRealOrder();
     $dataorder = new PlaceOrderInfo($order->getRealOrderId(), $order->getRealOrderId(), $order->getData('total_due'), $order->getCustomerFirstname() . " " . $order->getCustomerLastname(), $order->getCustomerEmail(), $_COOKIE['provider'], null, 'magento2', $this->metada->getVersion());
     if (isset($_COOKIE['payment_method']) && $_COOKIE['payment_method'] == 'compropago') {
         try {
             $client = new Client($this->instance->getPublicKey(), $this->instance->getPrivateKey(), $this->instance->getLiveMode());
             $response = $client->api->placeOrder($dataorder);
             $_COOKIE['provider'] = null;
             $_COOKIE['payment_method'] = null;
             unset($_COOKIE['provider']);
             unset($_COOKIE['payment_method']);
             return ["type" => "success", "value" => base64_encode($response->getId())];
         } catch (\Exception $e) {
             return ["type" => "error", "value" => $e->getMessage()];
         }
     } else {
         return ["type" => "error", "value" => "Metodo no reconocido"];
     }
 }
 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());
     }
 }