示例#1
0
 /**
  * Return array with data to send to MP api
  *
  * @return array
  */
 public function makePreference()
 {
     $orderIncrementId = $this->_checkoutSession->getLastRealOrderId();
     $order = $this->_orderFactory->create()->loadByIncrementId($orderIncrementId);
     $customer = $this->_customerSession->getCustomer();
     $payment = $order->getPayment();
     $paramsShipment = new \Magento\Framework\DataObject();
     $paramsShipment->setParams([]);
     $this->_eventManager->dispatch('mercadopago_standard_make_preference_before', ['params' => $paramsShipment, 'order' => $order]);
     $arr = [];
     $arr['external_reference'] = $orderIncrementId;
     $arr['items'] = $this->getItems($order);
     $this->_calculateDiscountAmount($arr['items'], $order);
     $this->_calculateBaseTaxAmount($arr['items'], $order);
     $total_item = $this->getTotalItems($arr['items']);
     $total_item += (double) $order->getBaseShippingAmount();
     $order_amount = (double) $order->getBaseGrandTotal();
     if (!$order_amount) {
         $order_amount = (double) $order->getBasePrice() + $order->getBaseShippingAmount();
     }
     if ($total_item > $order_amount || $total_item < $order_amount) {
         $diff_price = $order_amount - $total_item;
         $arr['items'][] = ["title" => "Difference amount of the items with a total", "description" => "Difference amount of the items with a total", "category_id" => $this->_scopeConfig->getValue('payment/mercadopago/category_id', \Magento\Store\Model\ScopeInterface::SCOPE_STORE), "quantity" => 1, "unit_price" => (double) $diff_price];
         $this->_helperData->log("Total itens: " . $total_item, 'mercadopago-standard.log');
         $this->_helperData->log("Total order: " . $order_amount, 'mercadopago-standard.log');
         $this->_helperData->log("Difference add itens: " . $diff_price, 'mercadopago-standard.log');
     }
     if ($order->canShip()) {
         $shippingAddress = $order->getShippingAddress();
         $shipping = $shippingAddress->getData();
         $arr['payer']['phone'] = ["area_code" => "-", "number" => $shipping['telephone']];
         $arr['shipments'] = $this->_getParamShipment($paramsShipment, $order, $shippingAddress);
     }
     $billing_address = $order->getBillingAddress()->getData();
     $arr['payer']['date_created'] = date('Y-m-d', $customer->getCreatedAtTimestamp()) . "T" . date('H:i:s', $customer->getCreatedAtTimestamp());
     $arr['payer']['email'] = htmlentities($customer->getEmail());
     $arr['payer']['first_name'] = htmlentities($customer->getFirstname());
     $arr['payer']['last_name'] = htmlentities($customer->getLastname());
     if (isset($payment['additional_information']['doc_number']) && $payment['additional_information']['doc_number'] != "") {
         $arr['payer']['identification'] = ["type" => "CPF", "number" => $payment['additional_information']['doc_number']];
     }
     $arr['payer']['address'] = ["zip_code" => $billing_address['postcode'], "street_name" => $billing_address['street'] . " - " . $billing_address['city'] . " - " . $billing_address['country_id'], "street_number" => ""];
     $url = $this->_helperData->getSuccessUrl();
     $arr['back_urls'] = ['success' => $this->_urlBuilder->getUrl($url), 'pending' => $this->_urlBuilder->getUrl($url), 'failure' => $this->_urlBuilder->getUrl('checkout/onepage/failure')];
     $arr['notification_url'] = $this->_urlBuilder->getUrl("mercadopago/notifications/standard");
     $arr['payment_methods']['excluded_payment_methods'] = $this->getExcludedPaymentsMethods();
     $installments = $this->getConfigData('installments');
     $arr['payment_methods']['installments'] = (int) $installments;
     $auto_return = $this->getConfigData('auto_return');
     if ($auto_return == 1) {
         $arr['auto_return'] = "approved";
     }
     $sponsor_id = $this->_scopeConfig->getValue('payment/mercadopago/sponsor_id', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
     $this->_helperData->log("Sponsor_id", 'mercadopago-standard.log', $sponsor_id);
     if (!empty($sponsor_id)) {
         $this->_helperData->log("Sponsor_id identificado", 'mercadopago-standard.log', $sponsor_id);
         $arr['sponsor_id'] = (int) $sponsor_id;
     }
     return $arr;
 }
示例#2
0
 /**
  * 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;
 }
示例#3
0
 /**
  * @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();
     }
 }
示例#4
0
 /**
  * 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;
     }
 }