diff --git a/CHANGELOG.md b/CHANGELOG.md index 8246f258..356896af 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,16 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [3.13.0] - 2021-12-01 + +### Added +- Support to Paycash payment in Mexico +- Faster local builds + +### Fixed +- Fixed Bank Transfer JS call +- Fixed refund rule observer + ## [3.12.3] - 2021-11-16 ### Fixed diff --git a/Dockerfile b/Dockerfile index 3b14986d..48132e89 100644 --- a/Dockerfile +++ b/Dockerfile @@ -41,4 +41,6 @@ COPY src magento2/app/code COPY phpunit.xml phpunit.xml # Fix permissions -RUN chmod 777 -Rf magento2 +RUN cd magento2 && find var vendor generated pub/static pub/media app/etc -type f -print0 | xargs -0 chmod u+w +RUN cd magento2 && find var vendor generated pub/static pub/media app/etc -type d -print0 | xargs -0 chmod u+w +RUN chmod u+x magento2/bin/magento diff --git a/README.md b/README.md index 41772138..712362ec 100755 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@

-# Magento 2 - Mercado Pago Module (v3.12.3) +# Magento 2 - Mercado Pago Module (v3.13.0) The Mercado Pago plugin for Magento 2 allows you to expand the functionalities of your online store and offer a unique payment experience for your customers. diff --git a/composer.json b/composer.json index cce785c5..a6876f7f 100755 --- a/composer.json +++ b/composer.json @@ -8,7 +8,7 @@ } ], "type": "magento2-module", - "version": "3.12.3", + "version": "3.13.0", "license": [ "OSL-3.0", "AFL-3.0" diff --git a/src/MercadoPago/Core/Helper/Data.php b/src/MercadoPago/Core/Helper/Data.php index 067a6dc7..0e4f6ca8 100644 --- a/src/MercadoPago/Core/Helper/Data.php +++ b/src/MercadoPago/Core/Helper/Data.php @@ -23,6 +23,7 @@ use MercadoPago\Core\Lib\RestClient; use MercadoPago\Core\Logger\Logger; use MercadoPago\Core\Model\Custom\Payment; +use MercadoPago\Core\Helper\PaymentPlaces; /** * Class Data @@ -89,6 +90,11 @@ class Data extends \Magento\Payment\Helper\Data */ protected $_moduleResource; + /** + * @var Api $api + */ + protected $_api; + /** * Data constructor. * @param Message\MessageInterface $messageInterface @@ -105,6 +111,7 @@ class Data extends \Magento\Payment\Helper\Data * @param Switcher $switcher * @param ComposerInformation $composerInformation * @param ResourceInterface $moduleResource + * @param Api $api */ public function __construct( Message\MessageInterface $messageInterface, @@ -120,7 +127,8 @@ public function __construct( OrderFactory $orderFactory, Switcher $switcher, ComposerInformation $composerInformation, - ResourceInterface $moduleResource + ResourceInterface $moduleResource, + Api $api ) { parent::__construct($context, $layoutFactory, $paymentMethodFactory, $appEmulation, $paymentConfig, $initialConfig); $this->_messageInterface = $messageInterface; @@ -131,6 +139,7 @@ public function __construct( $this->_switcher = $switcher; $this->_composerInformation = $composerInformation; $this->_moduleResource = $moduleResource; + $this->_api = $api; } /** @@ -173,7 +182,9 @@ public function getApiInstance($accessToken = null) throw new LocalizedException(__('The ACCESS_TOKEN has not been configured, without this credential the module will not work correctly.')); } - $api = new Api($accessToken); + //$api = new Api($accessToken); + $api = $this->_api; + $api->set_access_token($accessToken); $api->set_platform(self::PLATFORM_OPENPLATFORM); $api->set_type(self::TYPE); @@ -201,31 +212,13 @@ public function isValidAccessToken($accessToken) return true; } - $mp = $this->getApiInstance($accessToken); - $isValid = $mp->is_valid_access_token(); - - $this->_mpCache->saveCache($cacheKey, $isValid); - return $isValid; - } + $response = $this->getMercadoPagoPaymentMethods($accessToken); - /** - * ClientId and Secret valid? - * - * @param $clientId - * @param $clientSecret - * - * @return bool - * @throws LocalizedException - */ - public function isValidClientCredentials($clientId, $clientSecret) - { - $mp = $this->getApiInstance($clientId, $clientSecret); - try { - $mp->get_access_token(); - } catch (Exception $e) { + if ((!$response) || (isset($response['status']) && ($response['status'] == 401 || $response['status'] == 400))) { return false; } + $this->_mpCache->saveCache($cacheKey, true); return true; } @@ -269,53 +262,6 @@ public function setOrderSubtotals($data, $order) $order->save(); } - /** - * Modify payment array adding specific fields - * - * @param $payment - * - * @return mixed - * @refactor - */ - public function setPayerInfo(&$payment) - { - $this->log("setPayerInfo", 'mercadopago-custom.log', $payment); - - if ($payment['payment_method_id']) { - $payment["payment_method"] = $payment['payment_method_id']; - } - - if ($payment['installments']) { - $payment["installments"] = $payment['installments']; - } - if ($payment['id']) { - $payment["payment_id_detail"] = $payment['id']; - } - if (isset($payment['trunc_card'])) { - $payment["trunc_card"] = $payment['trunc_card']; - } elseif (isset($payment['card']) && isset($payment['card']['last_four_digits'])) { - $payment["trunc_card"] = "xxxx xxxx xxxx " . $payment['card']["last_four_digits"]; - } - - if (isset($payment['card']["cardholder"]["name"])) { - $payment["cardholder_name"] = $payment['card']["cardholder"]["name"]; - } - - if (isset($payment['payer']['first_name'])) { - $payment['payer_first_name'] = $payment['payer']['first_name']; - } - - if (isset($payment['payer']['last_name'])) { - $payment['payer_last_name'] = $payment['payer']['last_name']; - } - - if (isset($payment['payer']['email'])) { - $payment['payer_email'] = $payment['payer']['email']; - } - - return $payment; - } - /** * Return sum of fields separated with | * @@ -342,26 +288,39 @@ protected function _getMultiCardValue($data, $field) } /** - * return the list of payment methods or null + * return the list of payment methods or false * * @param mixed|null $accessToken * - * @return mixed + * @return array */ public function getMercadoPagoPaymentMethods($accessToken) { + + $this->log('GET /v1/payment_methods', 'mercadopago'); + try { $mp = $this->getApiInstance($accessToken); - $response = $mp->get("/v1/payment_methods"); - if ($response['status'] == 401 || $response['status'] == 400) { - return false; + $payment_methods = $mp->get("/v1/payment_methods"); + + $treated_payments_methods = []; + + foreach ($payment_methods['response'] as $payment_method) { + if (!isset($payment_method['payment_places'])) { + $payment_method['payment_places'] = PaymentPlaces::getPaymentPlaces($payment_method['id']); + } + + array_push($treated_payments_methods, $payment_method); } + + $payment_methods['response'] = $treated_payments_methods; + + return $payment_methods; + } catch (Exception $e) { - return false; + return []; } - - return $response['response']; } /** @@ -411,42 +370,6 @@ public function getModuleVersion() return $this->_moduleResource->getDbVersion('MercadoPago_Core'); } - /** - * Summary: Get client id from access token. - * Description: Get client id from access token. - * - * @param String $at - * - * @return String client id. - */ - public static function getClientIdFromAccessToken($at) - { - $t = explode('-', $at); - - if (count($t) > 0) { - return $t[1]; - } - - return ''; - } - - /** - * @param $additionalInfo - * @return string|null - */ - public function getPaymentId($additionalInfo) - { - if (isset($additionalInfo['payment_id_detail']) && !empty($additionalInfo['payment_id_detail'])) { - return $additionalInfo['payment_id_detail']; - } - - if (isset($additionalInfo['paymentResponse']) && !empty($additionalInfo['paymentResponse'])) { - return $additionalInfo['paymentResponse']['id']; - } - - return null; - } - /** * Get modal link * diff --git a/src/MercadoPago/Core/Helper/PaymentPlaces.php b/src/MercadoPago/Core/Helper/PaymentPlaces.php new file mode 100644 index 00000000..7164a6fd --- /dev/null +++ b/src/MercadoPago/Core/Helper/PaymentPlaces.php @@ -0,0 +1,49 @@ + [ + [ + "payment_option_id" => "7eleven", + "name" => "7 Eleven", + "status" => "active", + "thumbnail" => "https://http2.mlstatic.com/storage/logos-api-admin/417ddb90-34ab-11e9-b8b8-15cad73057aa-s.png" + ], + [ + "payment_option_id" => "circlek", + "name" => "Circle K", + "status" => "active", + "thumbnail" => "https://http2.mlstatic.com/storage/logos-api-admin/6f952c90-34ab-11e9-8357-f13e9b392369-s.png" + ], + [ + "payment_option_id" => "soriana", + "name" => "Soriana", + "status" => "active", + "thumbnail" => "https://http2.mlstatic.com/storage/logos-api-admin/dac0bf10-01eb-11ec-ad92-052532916206-s.png" + ], + [ + "payment_option_id" => "extra", + "name" => "Extra", + "status" => "active", + "thumbnail" => "https://http2.mlstatic.com/storage/logos-api-admin/9c8f26b0-34ab-11e9-b8b8-15cad73057aa-s.png" + ], + [ + "payment_option_id" => "calimax", + "name" => "Calimax", + "status" => "active", + "thumbnail" => "https://http2.mlstatic.com/storage/logos-api-admin/52efa730-01ec-11ec-ba6b-c5f27048193b-s.png" + ] + ], + ]; + + return $payment_places[$paymentId] ?? []; + } +} diff --git a/src/MercadoPago/Core/Lib/Api.php b/src/MercadoPago/Core/Lib/Api.php index 9ee00730..22d07852 100644 --- a/src/MercadoPago/Core/Lib/Api.php +++ b/src/MercadoPago/Core/Lib/Api.php @@ -7,9 +7,11 @@ * Access MercadoPago for payments integration * * @author hcasatti + * + * @codeCoverageIgnore * */ -class Api +class Api implements ApiInterface { /** @@ -58,9 +60,9 @@ public function __construct() { $i = func_num_args(); - if ($i > 2 || $i < 1) { + /*if ($i > 2 || $i < 1) { throw new \Exception('Invalid arguments. Use CLIENT_ID and CLIENT SECRET, or ACCESS_TOKEN'); - } + }*/ if ($i == 1) { $this->ll_access_token = func_get_arg(0); @@ -86,6 +88,11 @@ public function sandbox_mode($enable = null) return $this->sandbox; } + public function set_access_token($access_token) + { + $this->ll_access_token = $access_token; + } + /** * Get Access Token for API use * @@ -347,33 +354,7 @@ public function check_discount_campaigns($transaction_amount, $payer_email, $cou $url = "/discount_campaigns?transaction_amount=$transaction_amount&payer_email=$payer_email&coupon_code=$coupon_code"; return RestClient::get($url, null, ["Authorization: Bearer " . $access_token]); } - - /** - * @return bool - */ - public function is_valid_access_token() - { - if (empty($this->ll_access_token)) { - return false; - } - - try { - $response = $this->get("/v1/payment_methods"); - - if (empty($response)) { - return false; - } - - if ((isset($response['status'])) && ($response['status'] == 401 || $response['status'] == 400)) { - return false; - } - - return true; - } catch (\Exception $e) { - return false; - } - } - + /** * @param $id * @return array diff --git a/src/MercadoPago/Core/Lib/ApiInterface.php b/src/MercadoPago/Core/Lib/ApiInterface.php new file mode 100644 index 00000000..4f8bf38f --- /dev/null +++ b/src/MercadoPago/Core/Lib/ApiInterface.php @@ -0,0 +1,8 @@ +_coreHelper->getMercadoPagoPaymentMethods($accessToken); foreach ($paymentMethods['response'] as $pm) { if (!in_array($pm['id'], $excludePaymentMethods)) { diff --git a/src/MercadoPago/Core/Model/Core.php b/src/MercadoPago/Core/Model/Core.php index 635a5d28..49aef2d6 100644 --- a/src/MercadoPago/Core/Model/Core.php +++ b/src/MercadoPago/Core/Model/Core.php @@ -35,6 +35,8 @@ * Class Core * * @package MercadoPago\Core\Model + * + * @codeCoverageIgnore */ class Core extends \Magento\Payment\Model\Method\AbstractMethod { @@ -679,7 +681,8 @@ public function getPaymentV1($payment_id) */ public function getPaymentMethods() { - return $this->getMercadoPagoInstance()->get("/v1/payment_methods"); + $this->getMercadoPagoInstance(); + return $this->_coreHelper->getMercadoPagoPaymentMethods($this->_accessToken); } /** @@ -799,6 +802,7 @@ protected function getMercadoPagoInstance() if (!$this->_accessToken) { $this->_accessToken = $this->_scopeConfig->getValue(\MercadoPago\Core\Helper\ConfigData::PATH_ACCESS_TOKEN, \Magento\Store\Model\ScopeInterface::SCOPE_STORE); } + return $this->_coreHelper->getApiInstance($this->_accessToken); } diff --git a/src/MercadoPago/Core/Model/CustomConfigProvider.php b/src/MercadoPago/Core/Model/CustomConfigProvider.php index bc394922..0f338371 100644 --- a/src/MercadoPago/Core/Model/CustomConfigProvider.php +++ b/src/MercadoPago/Core/Model/CustomConfigProvider.php @@ -192,7 +192,7 @@ public function getPaymentMethods() try { $cards = []; - $paymentMethods = RestClient::get("/v1/payment_methods", null, ["Authorization: Bearer " . $accessToken]); + $paymentMethods = $this->_coreHelper->getMercadoPagoPaymentMethods($accessToken); $response = $paymentMethods['response']; foreach ($response as $card) { diff --git a/src/MercadoPago/Core/Model/CustomTicket/Payment.php b/src/MercadoPago/Core/Model/CustomTicket/Payment.php index 8ae3bbbd..9e667d6f 100644 --- a/src/MercadoPago/Core/Model/CustomTicket/Payment.php +++ b/src/MercadoPago/Core/Model/CustomTicket/Payment.php @@ -78,7 +78,11 @@ public function initialize($paymentAction, $stateObject) $preference = $this->_coreModel->makeDefaultPreferencePaymentV1($payment_info, $quote, $order); - $preference['payment_method_id'] = $payment->getAdditionalInformation("payment_method"); + $payment_method = $payment->getAdditionalInformation("payment_method"); + $payment_method_id = str_contains($payment_method, '|') ? explode('|', $payment_method)[0] : $payment_method; + $payment_option_id = str_contains($payment_method, '|') ? explode('|', $payment_method)[1] : ''; + + $preference['payment_method_id'] = $payment_method_id; if ($payment->getAdditionalInformation("firstName") != "") { $preference['payer']['first_name'] = $payment->getAdditionalInformation("firstName"); @@ -116,6 +120,7 @@ public function initialize($paymentAction, $stateObject) $preference['metadata']['checkout'] = 'custom'; $preference['metadata']['checkout_type'] = 'ticket'; + $preference['metadata']['payment_option_id'] = $payment_option_id; $this->_helperData->log("CustomPaymentTicket::initialize - Preference to POST", 'mercadopago-custom.log', $preference); } catch (\Exception $e) { diff --git a/src/MercadoPago/Core/Model/System/Config/Source/PaymentMethods/PaymentMethodsAbstract.php b/src/MercadoPago/Core/Model/System/Config/Source/PaymentMethods/PaymentMethodsAbstract.php index 57ba76d0..dafadeaf 100644 --- a/src/MercadoPago/Core/Model/System/Config/Source/PaymentMethods/PaymentMethodsAbstract.php +++ b/src/MercadoPago/Core/Model/System/Config/Source/PaymentMethods/PaymentMethodsAbstract.php @@ -45,9 +45,8 @@ public function toOptionArray() return ['methods' => $methods]; } - $this->coreHelper->log("GET /v1/payment_methods", 'mercadopago'); try { - $response = RestClient::get("/v1/payment_methods", null, ["Authorization: Bearer " . $accessToken]); + $response = $this->coreHelper->getMercadoPagoPaymentMethods($accessToken); if ($response['status'] > 201) { return ['methods' => $methods]; } diff --git a/src/MercadoPago/Core/Model/System/Config/Source/PaymentMethods/PaymentMethodsTicket.php b/src/MercadoPago/Core/Model/System/Config/Source/PaymentMethods/PaymentMethodsTicket.php index 2d2c4e35..71615ee4 100644 --- a/src/MercadoPago/Core/Model/System/Config/Source/PaymentMethods/PaymentMethodsTicket.php +++ b/src/MercadoPago/Core/Model/System/Config/Source/PaymentMethods/PaymentMethodsTicket.php @@ -21,8 +21,8 @@ public function toOptionArray() if (isset($pm['id'], $pm['name'], $pm['payment_type_id']) && !empty($pm['payment_type_id']) && ($pm['payment_type_id'] === "ticket" || $pm['payment_type_id'] === "atm") - ) { - $methods[] = ['value' => $pm['id'], 'label' => __($pm['name'])]; + ) { + $methods[] = ['value' => $pm['id'], 'label' => __($this->formatLabel($pm))]; } } } @@ -31,4 +31,22 @@ public function toOptionArray() return $methods; } + + /** + * @return string + */ + public function formatLabel($pm) + { + $payment = ''; + $concat = ''; + if (!empty($pm['payment_places'])) { + foreach($pm['payment_places'] as $payment_place) { + $payment .= $concat . $payment_place['name']; + $concat = ', '; + } + $payment = " ($payment)"; + } + + return $pm['name'] . $payment; + } } diff --git a/src/MercadoPago/Core/Model/System/Config/Source/PaymentMethodsTicket.php b/src/MercadoPago/Core/Model/System/Config/Source/PaymentMethodsTicket.php deleted file mode 100644 index 5b215d82..00000000 --- a/src/MercadoPago/Core/Model/System/Config/Source/PaymentMethodsTicket.php +++ /dev/null @@ -1,101 +0,0 @@ -scopeConfig = $scopeConfig; - $this->coreHelper = $coreHelper; - $this->_switcher = $switcher; - } //end __construct() - - - /** - * Return available payment methods - * - * @return array - */ - public function toOptionArray() - { - $methods = []; - - // default empty value - $methods[] = [ - 'value' => '', - 'label' => __('Accept all payment methods'), - ]; - $accessToken = $this->scopeConfig->getValue(ConfigData::PATH_ACCESS_TOKEN, ScopeInterface::SCOPE_WEBSITE, $this->_switcher->getWebsiteId()); - - if (empty($accessToken)) { - return $methods; - } - - $this->coreHelper->log('GET /v1/payment_methods', 'mercadopago'); - - try { - $response = RestClient::get('/v1/payment_methods', null, ['Authorization: Bearer ' . $accessToken]); - } catch (Exception $e) { - $this->coreHelper->log('PaymentMethodsTicket:: An error occurred at the time of obtaining the ticket payment methods: ' . $e); - return []; - } - - if (isset($response['error']) || (isset($response['status']) && ($response['status'] != '200' && $response['status'] != '201'))) { - return $methods; - } - - $response = $response['response']; - - foreach ($response as $pm) { - if ((isset($pm['payment_type_id']) && $pm['payment_type_id'] == 'ticket') || $pm['payment_type_id'] == 'atm') { - $methods[] = [ - 'value' => $pm['id'], - 'label' => __($pm['name']), - ]; - } - } - - $this->coreHelper->log('PaymentMethodsTicket:: Displayed', 'mercadopago', $methods); - - return $methods; - } //end toOptionArray() -}//end class diff --git a/src/MercadoPago/Core/Observer/RefundObserverBeforeSave.php b/src/MercadoPago/Core/Observer/RefundObserverBeforeSave.php index 8a87432f..f19be5fe 100644 --- a/src/MercadoPago/Core/Observer/RefundObserverBeforeSave.php +++ b/src/MercadoPago/Core/Observer/RefundObserverBeforeSave.php @@ -137,7 +137,7 @@ protected function creditMemoRefundBeforeSave($order, $creditMemo) $responseRefund = $mp->post("/v1/payments/$paymentID/refunds", $params); } - if (!is_null($responseRefund) || $responseRefund['status'] == 200 || $responseRefund['status'] == 201) { + if (!is_null($responseRefund) && ($responseRefund['status'] == 200 || $responseRefund['status'] == 201)) { $successMessageRefund = "Mercado Pago - " . __('Refund of %1 was processed successfully.', $amountRefund); $this->messageManager->addSuccessMessage($successMessageRefund); $this->dataHelper->log("RefundObserverBeforeSave::creditMemoRefundBeforeSave - " . $successMessageRefund, 'mercadopago-custom.log', $responseRefund); diff --git a/src/MercadoPago/Core/composer.json b/src/MercadoPago/Core/composer.json index c3c77c78..24729e0d 100644 --- a/src/MercadoPago/Core/composer.json +++ b/src/MercadoPago/Core/composer.json @@ -2,7 +2,7 @@ "name": "mercadopago/core", "description": "Mercado Pago Magento 2 Plugin", "type": "magento2-module", - "version": "3.12.3", + "version": "3.13.0", "license": [ "OSL-3.0", "AFL-3.0" diff --git a/src/MercadoPago/Core/etc/di.xml b/src/MercadoPago/Core/etc/di.xml index 9220b9f9..af04c2e1 100644 --- a/src/MercadoPago/Core/etc/di.xml +++ b/src/MercadoPago/Core/etc/di.xml @@ -3,6 +3,7 @@ + Magento\Framework\Filesystem\Driver\File diff --git a/src/MercadoPago/Core/etc/module.xml b/src/MercadoPago/Core/etc/module.xml index 719d420c..cc136e4e 100644 --- a/src/MercadoPago/Core/etc/module.xml +++ b/src/MercadoPago/Core/etc/module.xml @@ -1,6 +1,6 @@ - + diff --git a/src/MercadoPago/Core/view/frontend/web/css/MPv1.css b/src/MercadoPago/Core/view/frontend/web/css/MPv1.css index 9688f82c..5d596164 100644 --- a/src/MercadoPago/Core/view/frontend/web/css/MPv1.css +++ b/src/MercadoPago/Core/view/frontend/web/css/MPv1.css @@ -1,7 +1,11 @@ -#co-mercadopago-form, #co-mercadopago-form-ticket { +#co-mercadopago-form { max-width: 400px; } +#co-mercadopago-form-mlb { + display: inline-block; +} + #mp-box-form { width: 100%; max-width: 375px; diff --git a/src/MercadoPago/Core/view/frontend/web/js/view/method-renderer/custom-method-bank-transfer.js b/src/MercadoPago/Core/view/frontend/web/js/view/method-renderer/custom-method-bank-transfer.js index ec6dd121..ec7852a3 100644 --- a/src/MercadoPago/Core/view/frontend/web/js/view/method-renderer/custom-method-bank-transfer.js +++ b/src/MercadoPago/Core/view/frontend/web/js/view/method-renderer/custom-method-bank-transfer.js @@ -108,7 +108,7 @@ define( }, getFingerPrintLink: function () { - return configPayment['fingerprint_link']; + return window.checkoutConfig.payment['fingerprint_link']; }, existBanner: function () { diff --git a/src/MercadoPago/Core/view/frontend/web/template/payment/custom_ticket.html b/src/MercadoPago/Core/view/frontend/web/template/payment/custom_ticket.html index 65c120db..18af1205 100644 --- a/src/MercadoPago/Core/view/frontend/web/template/payment/custom_ticket.html +++ b/src/MercadoPago/Core/view/frontend/web/template/payment/custom_ticket.html @@ -1,16 +1,12 @@
- + " />