/**
  * Controller Action
  */
 public function execute()
 {
     $request = $this->getRequest();
     $this->coreHelper->log("Custom Received notification", self::LOG_NAME, $request->getParams());
     $dataId = $request->getParam('data_id');
     $type = $request->getParam('type');
     if (!empty($dataId) && $type == 'payment') {
         $response = $this->coreModel->getPaymentV1($dataId);
         $this->coreHelper->log("Return payment", self::LOG_NAME, $response);
         if ($response['status'] == 200 || $response['status'] == 201) {
             $payment = $response['response'];
             $payment = $this->coreHelper->setPayerInfo($payment);
             $this->coreHelper->log("Update Order", self::LOG_NAME);
             $this->coreModel->updateOrder($payment);
             $setStatusResponse = $this->coreModel->setStatusOrder($payment);
             $this->getResponse()->setBody($setStatusResponse['text']);
             $this->getResponse()->setHttpResponseCode($setStatusResponse['code']);
             $this->coreHelper->log("Http code", self::LOG_NAME, $this->getResponse()->getHttpResponseCode());
             return;
         }
     }
     $this->coreHelper->log("Payment not found", self::LOG_NAME, $request->getParams());
     $this->getResponse()->getBody("Payment not found");
     $this->getResponse()->setHttpResponseCode(\MercadoPago\Core\Helper\Response::HTTP_NOT_FOUND);
     $this->coreHelper->log("Http code", self::LOG_NAME, $this->getResponse()->getHttpResponseCode());
 }
 /**
  * Fetch coupon info
  *
  * Controller Action
  */
 public function execute()
 {
     $coupon_id = $this->getRequest()->getParam('id');
     if (!empty($coupon_id)) {
         $response = $this->coreModel->validCoupon($coupon_id);
     } else {
         $response = $this->getArrayErrorResponse();
     }
     if ($response['status'] != 200 && $response['status'] != 201) {
         $response = $this->getArrayErrorResponse();
     }
     //save value to DiscountCoupon collect
     $this->_registry->register('mercadopago_discount_amount', (double) $response['response']['coupon_amount']);
     $quote = $this->_checkoutSession->getQuote();
     $this->quoteRepository->save($quote->collectTotals());
     $jsonData = json_encode($response);
     $this->getResponse()->setHeader('Content-type', 'application/json');
     $this->getResponse()->setBody($jsonData);
 }
 /**
  * Collect data from notification content
  *
  * @param $merchantOrder
  *
  * @return array
  */
 protected function _getDataPayments($merchantOrder)
 {
     $data = array();
     foreach ($merchantOrder['payments'] as $payment) {
         $response = $this->coreModel->getPayment($payment['id']);
         $payment = $response['response']['collection'];
         $data = $this->_formatArrayPayment($data, $payment);
     }
     return $data;
 }
 /**
  * @param \Magento\Framework\Event\Observer $observer
  *
  * @throws \Exception
  * @throws bool
  */
 public function execute(\Magento\Framework\Event\Observer $observer)
 {
     $merchantOrder = $observer->getMerchantOrder();
     if (!count($merchantOrder['shipments']) > 0) {
         return;
     }
     $data = $observer->getPayment();
     $order = $this->_coreModel->_getOrder($data["external_reference"]);
     //if order has shipments, status is updated. If it doesn't the shipment is created.
     if ($merchantOrder['shipments'][0]['status'] == 'ready_to_ship') {
         if ($order->hasShipments()) {
             $shipment = $this->_shipment->load($order->getId(), 'order_id');
         } else {
             $shipment = $this->_shipmentFactory->create($order);
             $order->setIsInProcess(true);
         }
         $shipment->setShippingLabel($merchantOrder['shipments'][0]['id']);
         $shipmentInfo = $this->_shipmentHelper->getShipmentInfo($merchantOrder['shipments'][0]['id']);
         $this->_coreHelper->log("Shipment Info", 'mercadopago-notification.log', $shipmentInfo);
         $serviceInfo = $this->_shipmentHelper->getServiceInfo($merchantOrder['shipments'][0]['service_id'], $merchantOrder['site_id']);
         $this->_coreHelper->log("Service Info by service id", 'mercadopago-notification.log', $serviceInfo);
         if ($shipmentInfo && isset($shipmentInfo->tracking_number)) {
             $tracking['number'] = $shipmentInfo->tracking_number;
             $tracking['description'] = str_replace('#{trackingNumber}', $shipmentInfo->tracking_number, $serviceInfo->tracking_url);
             $tracking['title'] = self::CODE;
             $existingTracking = $this->_trackFactory->create()->load($shipment->getOrderId(), 'order_id');
             if ($existingTracking->getId()) {
                 $track = $shipment->getTrackById($existingTracking->getId());
                 $track->setNumber($tracking['number'])->setDescription($tracking['description'])->setTitle($tracking['title'])->save();
             } else {
                 $track = $this->_trackFactory->create()->addData($tracking);
                 $track->setCarrierCode(\MercadoPago\MercadoEnvios\Model\Carrier\MercadoEnvios::CODE);
                 $shipment->addTrack($track);
                 $shipment->save();
             }
             $this->_coreHelper->log("Track added", 'mercadopago-notification.log', $track);
         }
         $this->_transaction->addObject($order)->save();
     }
 }
 /**
  * @param \Magento\Framework\Event\Observer $observer
  */
 public function execute(\Magento\Framework\Event\Observer $observer)
 {
     $observerData = $observer->getData();
     $orderId = $observerData['orderId'];
     $shipmentData = $observerData['shipmentData'];
     $order = $this->coreModel->_getOrder($orderId);
     $method = $order->getShippingMethod();
     if ($this->shipmentHelper->isMercadoEnviosMethod($method)) {
         $methodId = $shipmentData['shipping_option']['shipping_method_id'];
         $name = $shipmentData['shipping_option']['name'];
         $order->setShippingMethod('mercadoenvios_' . $methodId);
         $estimatedDate = $this->_timezone->formatDate($shipmentData['shipping_option']['estimated_delivery']['date']);
         $estimatedDate = __('(estimated date %1)', $estimatedDate);
         $shippingDescription = 'MercadoEnvíos - ' . $name . ' ' . $estimatedDate;
         $order->setShippingDescription($shippingDescription);
         try {
             $order->save();
             $this->shipmentHelper->log('Order ' . $order->getIncrementId() . ' shipping data set ', $shipmentData);
         } catch (\Exception $e) {
             $this->shipmentHelper->log("error when update shipment data: " . $e);
             $this->shipmentHelper->log($e);
         }
     }
 }
 /**
  * Get stored customers and cards from api
  *
  * @return mixed
  */
 public function getCustomerAndCards()
 {
     $email = $this->_coreModel->getEmailCustomer();
     $customer = $this->getOrCreateCustomer($email);
     return $customer;
 }