Skip to content
This repository has been archived by the owner on Aug 28, 2023. It is now read-only.

Commit

Permalink
Merge pull request #197 from PluginAndPartners/main
Browse files Browse the repository at this point in the history
Release v3.19.0
  • Loading branch information
cleitonaguiarandrade authored Jan 5, 2023
2 parents 474f739 + 1dd0b24 commit 7d4de23
Show file tree
Hide file tree
Showing 37 changed files with 1,257 additions and 1,300 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@ 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.19.0] - 2023-01-03

### Added
- Secure fields to custom checkout card form
### Changed
- API used to get payment methods
- How credentials are validated
- Improved translations
- Error messages
### Removed
- Hardcoded payment places

## [3.18.0] - 2022-11-16

### Added
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
</a>
</p>

# Magento 2 - Mercado Pago Module (v3.18.0)
# Magento 2 - Mercado Pago Module (v3.19.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.

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
}
],
"type": "magento2-module",
"version": "3.18.0",
"version": "3.19.0",
"license": [
"OSL-3.0",
"AFL-3.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@ class Payment extends Fieldset
* Checkout Custom Card
*/
const CHECKOUT_CUSTOM_CARD = 'custom_checkout';

/**
* Checkout Custom Pix
*/
const CHECKOUT_CUSTOM_PIX= 'custom_checkout_pix';

/**
* Checkout Custom Ticket
*/
const CHECKOUT_CUSTOM_TICKET = 'custom_checkout_ticket';

/**
* Checkout Custom Bank Transfer
*/
Expand Down Expand Up @@ -128,16 +128,16 @@ public function render(AbstractElement $element)
return parent::render($element);
}

public function getPaymentMethods($accessToken)
public function getPaymentMethods()
{
$paymentMethods = $this->coreHelper->getMercadoPagoPaymentMethods($accessToken);
$paymentMethods = $this->coreHelper->getMercadoPagoPaymentMethods();

return $paymentMethods;
}

/**
* Disables the given payment if it is currently active
*
*
* @param $paymentId
*/
protected function disablePayment($paymentId)
Expand Down Expand Up @@ -175,19 +175,12 @@ protected function disablePayment($paymentId)
*/
protected function hideInvalidCheckoutOptions($paymentId)
{
$accessToken = $this->coreHelper->getAccessToken();

if (!$this->coreHelper->isValidAccessToken($accessToken)) {
return true;
}

$cacheKey = Cache::VALID_PAYMENT_METHODS;
$validCheckoutOptions = json_decode($this->cache->getFromCache($cacheKey));
if (!$validCheckoutOptions) {
$validCheckoutOptions = $this->getAvailableCheckoutOptions($accessToken);
$validCheckoutOptions = $this->getAvailableCheckoutOptions();
$this->cache->saveCache($cacheKey, json_encode($validCheckoutOptions));
}

$paymentIdWithoutPrefix = implode('_', array_slice(explode('_', $paymentId), 4));

return !in_array($paymentIdWithoutPrefix, $validCheckoutOptions);
Expand All @@ -199,16 +192,16 @@ protected function hideInvalidCheckoutOptions($paymentId)
* @param string $accessToken
* @return array
*/
public function getAvailableCheckoutOptions($accessToken)
public function getAvailableCheckoutOptions()
{
try {
$availableCheckouts = array();
$paymentMethods = $this->getPaymentMethods($accessToken);
$paymentMethods = $this->getPaymentMethods();

foreach ($paymentMethods['response'] as $paymentMethod) {
switch (strtolower($paymentMethod['payment_type_id'])) {
case 'credit_card':
case 'debid_card':
case 'debit_card':
case 'prepaid_card':
if (!in_array(self::CHECKOUT_CUSTOM_CARD, $availableCheckouts)) {
$availableCheckouts[] = self::CHECKOUT_CUSTOM_CARD;
Expand Down
3 changes: 2 additions & 1 deletion src/MercadoPago/Core/Helper/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@
/**
* Class Cache
* @package MercadoPago\Core\Helper
*
*
* @codeCoverageIgnore
*/
class Cache
{
const PREFIX_KEY = 'MP_';
const IS_VALID_PK = 'IS_VALID_PUBLIC_KEY';
const IS_VALID_AT = 'IS_VALID_ACCESS_TOKEN';
const VALID_PAYMENT_METHODS = 'VALID_PAYMENT_METHODS';

Expand Down
98 changes: 48 additions & 50 deletions src/MercadoPago/Core/Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Exception;
use Magento\Backend\Block\Store\Switcher;
use Magento\Framework\App\Config\Initial;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\Helper\Context;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Composer\ComposerInformation;
Expand All @@ -20,7 +21,6 @@
use Magento\Store\Model\ScopeInterface;
use MercadoPago\Core\Helper\Message\MessageInterface;
use MercadoPago\Core\Lib\Api;
use MercadoPago\Core\Lib\RestClient;
use MercadoPago\Core\Logger\Logger;

/**
Expand Down Expand Up @@ -53,6 +53,11 @@ class Data extends \Magento\Payment\Helper\Data

const PAYMENT_TYPE_CREDIT_CARD = 'credit_card';

/**
* plugins credentials wrapper
*/
const CREDENTIALS_WRAPPER = '/plugins-credentials-wrapper/credentials';

/**
* @var MessageInterface
*/
Expand Down Expand Up @@ -100,6 +105,11 @@ class Data extends \Magento\Payment\Helper\Data
*/
protected $_api;

/**
* @var ScopeConfigInterface $scopeConfig
*/
protected $_scopeConfig;

/**
* Data constructor.
* @param Message\MessageInterface $messageInterface
Expand All @@ -117,6 +127,7 @@ class Data extends \Magento\Payment\Helper\Data
* @param ComposerInformation $composerInformation
* @param ResourceInterface $moduleResource
* @param Api $api
* @param ScopeConfigInterface $scopeConfig
*/
public function __construct(
Message\MessageInterface $messageInterface,
Expand All @@ -133,7 +144,8 @@ public function __construct(
Switcher $switcher,
ComposerInformation $composerInformation,
ResourceInterface $moduleResource,
Api $api
Api $api,
ScopeConfigInterface $scopeConfig
) {
parent::__construct($context, $layoutFactory, $paymentMethodFactory, $appEmulation, $paymentConfig, $initialConfig);
$this->_messageInterface = $messageInterface;
Expand All @@ -145,6 +157,7 @@ public function __construct(
$this->_composerInformation = $composerInformation;
$this->_moduleResource = $moduleResource;
$this->_api = $api;
$this->_scopeConfig = $scopeConfig;
}

/**
Expand All @@ -156,7 +169,7 @@ public function __construct(
*/
public function log($message, $name = "mercadopago", $array = null)
{
$actionLog = $this->scopeConfig->getValue(
$actionLog = $this->_scopeConfig->getValue(
ConfigData::PATH_ADVANCED_LOG,
ScopeInterface::SCOPE_STORE
);
Expand All @@ -179,21 +192,22 @@ public function log($message, $name = "mercadopago", $array = null)
* @return Api
* @throws LocalizedException
*/
public function getApiInstance($accessToken = null)
public function getApiInstance($publicKey = null, $accessToken = null)
{
if (is_null($accessToken)) {
throw new LocalizedException(__('The ACCESS_TOKEN has not been configured, without this credential the module will not work correctly.'));
if (is_null($publicKey) || is_null($accessToken)) {
throw new LocalizedException(__('The PUBLIC_KEY or ACCESS_TOKEN has not been configured, without this credential the module will not work correctly.'));
}

$api = $this->_api;
$api->set_access_token($accessToken);
$api->set_public_key($publicKey);
$api->set_platform(self::PLATFORM_OPENPLATFORM);

$api->set_type(self::TYPE);
RestClient::setModuleVersion((string)$this->getModuleVersion());
RestClient::setUrlStore($this->getUrlStore());
RestClient::setEmailAdmin($this->scopeConfig->getValue('trans_email/ident_sales/email', ScopeInterface::SCOPE_STORE));
RestClient::setCountryInitial($this->getCountryInitial());

$api->set_module_version((string)$this->getModuleVersion());
$api->set_url_store($this->getUrlStore());
$api->set_email_admin($this->_scopeConfig->getValue('trans_email/ident_sales/email', ScopeInterface::SCOPE_STORE));
$api->set_country_initial($this->getCountryInitial());

return $api;
}
Expand All @@ -202,36 +216,29 @@ public function getApiInstance($accessToken = null)
* @param $accessToken
* @return bool
*/
public function isValidAccessToken($accessToken)
public function validateCredentials($publicKey, $accessToken)
{
$cacheKey = Cache::IS_VALID_AT . $accessToken;
$cacheKey = Cache::IS_VALID_PK . $publicKey;
$cacheToken = Cache::IS_VALID_AT . $accessToken;

if ($this->_mpCache->getFromCache($cacheKey)) {
if ($this->_mpCache->getFromCache($cacheToken) && $this->_mpCache->getFromCache($cacheKey)) {
return true;
}

$response = $this->getMercadoPagoPaymentMethods($accessToken);
$api = $this->getApiInstance($publicKey, $accessToken);

if ((!$response) || (isset($response['status']) && ($response['status'] == 401 || $response['status'] == 400))) {
$keyResponse = $api->validate_public_key($publicKey);
$tokenResponse = $api->validade_access_token($accessToken);

if (!$keyResponse || !$tokenResponse || ($keyResponse['client_id'] !== $tokenResponse['client_id'])) {
$this->log('Invalid credential pair');
return false;
}

$this->_mpCache->saveCache($cacheKey, true);
return true;
}
$this->_mpCache->saveCache($cacheToken, true);

/**
* @param string $scopeCode
* @return bool|mixed
*/
public function getAccessToken($scopeCode = ScopeInterface::SCOPE_STORE)
{
$accessToken = $this->scopeConfig->getValue(ConfigData::PATH_ACCESS_TOKEN, $scopeCode);
if (empty($accessToken)) {
return false;
}

return $accessToken;
return true;
}

/**
Expand Down Expand Up @@ -291,36 +298,27 @@ protected function _getMultiCardValue($data, $field)
/**
* return the list of payment methods or false
*
* @param mixed|null $accessToken
*
* @return array
*/
public function getMercadoPagoPaymentMethods($accessToken)
public function getMercadoPagoPaymentMethods()
{
$this->log('GET /v1/payment_methods', 'mercadopago');

try {
$mp = $this->getApiInstance($accessToken);

$payment_methods = $mp->get("/v1/payment_methods");
$publicKey = $this->_scopeConfig->getValue(ConfigData::PATH_PUBLIC_KEY, ScopeInterface::SCOPE_STORE);

$treated_payments_methods = [];
$accessToken = $this->_scopeConfig->getValue(ConfigData::PATH_ACCESS_TOKEN, ScopeInterface::SCOPE_STORE);

foreach ($payment_methods['response'] as $payment_method) {
if (is_array($payment_method) && isset($payment_method['id']) && !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;
if (!$this->validateCredentials($publicKey, $accessToken)) {
return [];
}

return $payment_methods;
try {
$mp = $this->getApiInstance($publicKey, $accessToken);

$payment_methods = $mp->get_payment_methods($accessToken);
} catch (Exception $e) {
return [];
}

return $payment_methods;
}

/**
Expand Down
49 changes: 0 additions & 49 deletions src/MercadoPago/Core/Helper/PaymentPlaces.php

This file was deleted.

Loading

0 comments on commit 7d4de23

Please sign in to comment.