/** * Check whether payment method can be used * * @param \Magento\Quote\Api\Data\CartInterface|null $quote * * @return bool */ public function isAvailable(\Magento\Quote\Api\Data\CartInterface $quote = null) { $parent = parent::isAvailable($quote); $clientId = $this->_scopeConfig->getValue(\MercadoPago\Core\Helper\Data::XML_PATH_CLIENT_ID, \Magento\Store\Model\ScopeInterface::SCOPE_STORE); $clientSecret = $this->_scopeConfig->getValue(\MercadoPago\Core\Helper\Data::XML_PATH_CLIENT_SECRET, \Magento\Store\Model\ScopeInterface::SCOPE_STORE); $standard = !empty($clientId) && !empty($clientSecret); if (!$parent || !$standard) { return false; } return $this->_helperData->isValidClientCredentials($clientId, $clientSecret); }
/** * Return key sorted shop item categories * @return array */ public function toOptionArray() { $this->coreHelper->log("Get Categories... ", 'mercadopago'); $response = \MercadoPago_Core_Lib_RestClient::get("/item_categories"); $this->coreHelper->log("API item_categories", 'mercadopago', $response); $response = $response['response']; $cat = array(); $count = 0; foreach ($response as $v) { //force category others first if ($v['id'] == "others") { $cat[0] = ['value' => $v['id'], 'label' => __($v['description'])]; } else { $count++; $cat[$count] = ['value' => $v['id'], 'label' => __($v['description'])]; } } //force order by key ksort($cat); return $cat; }
/** * @param $shipmentId * * @return mixed * @throws \Exception * @throws \Zend_Http_Client_Exception */ public function getShipmentInfo($shipmentId) { $client = new \Zend_Http_Client(self::ME_SHIPMENT_URL . $shipmentId); $client->setMethod(\Zend_Http_Client::GET); $client->setParameterGet('access_token', $this->_mpHelper->getAccessToken()); try { $response = $client->request(); } catch (\Exception $e) { $this->log($e); throw new \Exception($e); } return json_decode($response->getBody()); }
/** * @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(); } }
/** * @return int|null * @throws \Magento\Framework\Exception\LocalizedException */ protected function _getDataAllowedMethods() { if (empty($this->_methods) && !empty($this->_request)) { $quote = $this->_helperCarrierData->getQuote(); $shippingAddress = $quote->getShippingAddress(); if (empty($shippingAddress)) { return null; } $postcode = $shippingAddress->getPostcode(); try { $dimensions = $this->_helperCarrierData->getDimensions($this->_helperCarrierData->getAllItems($this->_request->getAllItems())); } catch (\Exception $e) { $this->_methods = self::INVALID_METHOD; return null; } $clientId = $this->_scopeConfig->getValue(\MercadoPago\Core\Helper\Data::XML_PATH_CLIENT_ID, \Magento\Store\Model\ScopeInterface::SCOPE_STORE); $clientSecret = $this->_scopeConfig->getValue(\MercadoPago\Core\Helper\Data::XML_PATH_CLIENT_SECRET, \Magento\Store\Model\ScopeInterface::SCOPE_STORE); $mp = $this->_mpHelper->getApiInstance($clientId, $clientSecret); $params = ["dimensions" => $dimensions, "zip_code" => $postcode]; $freeMethod = $this->_helperCarrierData->getFreeMethod($this->_request); if (!empty($freeMethod)) { $params['free_method'] = $freeMethod; } $response = $mp->get("/shipping_options", $params); if ($response['status'] == 200) { $this->_methods = $response['response']['options']; } else { if (isset($response['response']['message'])) { $this->_registry->register('mercadoenvios_msg', $response['response']['message']); } $this->_methods = self::INVALID_METHOD; $this->_helperCarrierData->log('Request params: ', $params); $this->_helperCarrierData->log('Error response API: ', $response); } } return $this->_methods; }
/** * Saves to be used later by OCP * * @param $email * * @return bool|array * @throws \Magento\Framework\Exception\LocalizedException */ public function getOrCreateCustomer($email) { if (empty($email)) { return false; } //get access_token if (!$this->_accessToken) { $this->_accessToken = $this->_scopeConfig->getValue(\MercadoPago\Core\Helper\Data::XML_PATH_ACCESS_TOKEN); } $mp = $this->_helperData->getApiInstance($this->_accessToken); $customer = $mp->get("/v1/customers/search", ["email" => $email]); $this->_helperData->log("Response search customer", self::LOG_NAME, $customer); if ($customer['status'] == 200) { if ($customer['response']['paging']['total'] > 0) { return $customer['response']['results'][0]; } else { $this->_helperData->log("Customer not found: " . $email, self::LOG_NAME); $customer = $mp->post("/v1/customers", ["email" => $email]); $this->_helperData->log("Response create customer", self::LOG_NAME, $customer); if ($customer['status'] == 201) { return $customer['response']; } else { return false; } } } else { return false; } }