/** * Set configuration value sponsor_id based on current credentials * @throws \Magento\Framework\Exception\LocalizedException */ public function setSponsor() { $this->coreHelper->log("Sponsor_id: " . $this->scopeConfig->getValue('payment/mercadopago/sponsor_id'), self::LOG_NAME); $sponsorId = ""; $this->coreHelper->log("Valid user test", self::LOG_NAME); $accessToken = $this->scopeConfig->getValue(\MercadoPago\Core\Helper\Data::XML_PATH_ACCESS_TOKEN); $this->coreHelper->log("Get access_token: " . $accessToken, self::LOG_NAME); if (!$accessToken) { return; } $mp = $this->coreHelper->getApiInstance($accessToken); $user = $mp->get("/users/me"); $this->coreHelper->log("API Users response", self::LOG_NAME, $user); if ($user['status'] == 200 && !in_array("test_user", $user['response']['tags'])) { $sponsors = ['MLA' => 186172525, 'MLB' => 186175129, 'MLM' => 186175064, 'MCO' => 206959966, 'MLC' => 206959756, 'MLV' => 206960619, 'MPE' => 217178514]; $countryCode = $user['response']['site_id']; if (isset($sponsors[$countryCode])) { $sponsorId = $sponsors[$countryCode]; } else { $sponsorId = ''; } $this->coreHelper->log("Sponsor id set", self::LOG_NAME, $sponsorId); } $this->_saveWebsiteConfig('payment/mercadopago/sponsor_id', $sponsorId); $this->coreHelper->log("Sponsor saved", self::LOG_NAME, $sponsorId); }
/** * Set order and payment info * * @param $data */ public function updateOrder($data, $order = null) { $this->_coreHelper->log("Update Order", 'mercadopago-notification.log'); if (true or !$this->_coreHelper->isStatusUpdated()) { try { if (!$order) { $order = $this->_getOrder($data["external_reference"]); } //update payment info $payment_order = $order->getPayment(); $additionalFields = array('status', 'status_detail', 'payment_id', 'transaction_amount', 'cardholderName', 'installments', 'statement_descriptor', 'trunc_card'); foreach ($additionalFields as $field) { if (isset($data[$field])) { $payment_order->setAdditionalInformation($field, $data[$field]); } } if (isset($data['payment_method_id'])) { $payment_order->setAdditionalInformation('payment_method', $data['payment_method_id']); } $payment_status = $payment_order->save(); $this->_coreHelper->log("Update Payment", 'mercadopago.log', $payment_status->getData()); $status_save = $order->save(); $this->_coreHelper->log("Update order", 'mercadopago.log', $status_save->getData()); } catch (\Exception $e) { $this->_coreHelper->log("erro in update order status: " . $e, 'mercadopago.log'); $this->getResponse()->setBody($e); //if notification proccess returns error, mercadopago will resend the notification. $this->getResponse()->setHttpResponseCode(\MercadoPago\Core\Helper\Response::HTTP_BAD_REQUEST); } } }
/** * 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()); }
/** * Collect data from notification content to update order info * * @param $data * @param $payment * * @return mixed */ protected function _formatArrayPayment($data, $payment) { $this->coreHelper->log("Format Array", self::LOG_NAME); $fields = ["status", "status_detail", "id", "payment_method_id", "transaction_amount", "total_paid_amount", "coupon_amount", "installments", "shipping_cost"]; foreach ($fields as $field) { if (isset($payment[$field])) { if (isset($data[$field])) { $data[$field] .= " | " . $payment[$field]; } else { $data[$field] = $payment[$field]; } } } if (isset($payment["last_four_digits"])) { if (isset($data["trunc_card"])) { $data["trunc_card"] .= " | " . "xxxx xxxx xxxx " . $payment["last_four_digits"]; } else { $data["trunc_card"] = "xxxx xxxx xxxx " . $payment["last_four_digits"]; } } if (isset($payment['cardholder']['name'])) { if (isset($data["cardholder_name"])) { $data["cardholder_name"] .= " | " . $payment["cardholder"]["name"]; } else { $data["cardholder_name"] = $payment["cardholder"]["name"]; } } if (isset($payment['statement_descriptor'])) { $data['statement_descriptor'] = $payment['statement_descriptor']; } $data['external_reference'] = $payment['external_reference']; $data['payer_first_name'] = $payment['payer']['first_name']; $data['payer_last_name'] = $payment['payer']['last_name']; $data['payer_email'] = $payment['payer']['email']; return $data; }