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

Commit

Permalink
v3.14.0 (#186)
Browse files Browse the repository at this point in the history
* feat(PPWP-377): unified calls

* fix(PPWP-377): unified calls

* fix(PPWP-377): unified calls

* fix(PPWP-377): unified calls

* fix(PPWP-377): unified calls

* added payment places method

* comments removed

* refactored access token validation

* fixed ternary if structure

* fixed instantiation of access token

* 🐛 Fix the PR cover checker when there is no test (#189)

This fixes an issue that is workflow failure when the pull request does not contain tests and Added hotfix branch filter to check tests

* feat(PPWP-421): paycash front-end

* feat(PPWP-422): paycash admin

* fix(helper): 🐛 fixed float to int convertion

* feat(bin): ✨ new command sync with container

* improv: init tests

* improv: changing chmod command

* build: 📦 new patch version of mercardo pago for magento 2 v3.12.3

* getMercadoPagoPaymentMethods testing

* fix(ppwp-337): fixing exception testing

* testing config provider and api

* testing PaymentMethodsTicket

* feat(ppwp-377): testing custom config provider

* refactored constants

* refactored model test classes

* refactored helper test classes

* added annotation for Code Coverage Ignore

* fixed test variables

* fixed html structure of ticket

* feat(ppwp-377): adding metadata payment_option_id

* fixing data tests

* removed exception to access token

* hotfix(PLUG-1533): fixing refund error handling

* fixing pr coverage checker

* testing pr coverage checker

* fixing pr coverage checker

* hotfix: fixed js - undefined variable

* feat(PPWP-569): initial commit

* build: 📦 new minor version of mercardo pago for magento 2 v3.13.0

* build(v3.13.0): 📦 updated readme

* expiration date mask and validations

* PPWP-569: inserted translations for titles

* PPWP-569: added space between fields

* improv(PPWP-611): dinamically validating bank transfer on admin page

* implementing cache

* PPWP-611: payment unit tests

* PPWP-611: fixed abstractElementMock

* fix(wallet button): 🐛 create order even with cho pro disabled

* fixing cache

* separating cache values with constants

* PPWP-611: refactored test directory

* PPWP-570: added translation

* PPWP-569: inserted style in title

* PPWP-569: fixed cache for new installs

* PPWP-569: fixed cache for new installs

* feature(ppwp-630): new front-end definitions

* removing cache from bank_transfer validation

* fixing payment fieldset and removing debit text

* improv(ppwp-531): adding mlu to available lists

* adding payment methods validation using API

* removing old validation

* improving cache and applying rules to other checkouts

* fixing pix rules

* disabling invalid payments

* clearing config activation flag caching

* testing new methods

* fixing cache code coverage

* fixing observer code coverage

* fixing exception syntax

* code enhancement

* fixing prefix issue

* fix(ticket): 🐛 changed str_contains to strpos

* build(v3.14.0): 📦 updated version files and changelog

* build(v3.14.0): 📦 updated changelog

* fix: treating paymentId on disablePayment function

Co-authored-by: Gustavo Brito <[email protected]>
Co-authored-by: sleaof <[email protected]>
Co-authored-by: Douglas <[email protected]>
  • Loading branch information
4 people authored Jan 6, 2022
1 parent db61907 commit a697806
Show file tree
Hide file tree
Showing 33 changed files with 1,172 additions and 426 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.14.0] - 2022-01-04

### Added
- Improvements to credit card flow
- Dynamically removing unavailable payment methods from admin

### Fixed
- Changed str_contains to srtpos
- Wallet Button working without CHO PRO enabled

## [3.13.0] - 2021-12-01

### 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.13.0)
# Magento 2 - Mercado Pago Module (v3.14.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.13.0",
"version": "3.14.0",
"license": [
"OSL-3.0",
"AFL-3.0"
Expand Down
168 changes: 131 additions & 37 deletions src/MercadoPago/Core/Block/Adminhtml/System/Config/Fieldset/Payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,29 @@
use Magento\Backend\Model\Auth\Session;
use Magento\Config\Block\System\Config\Form\Fieldset;
use Magento\Config\Model\ResourceModel\Config;
use Magento\Framework\App\Cache\TypeListInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Data\Form\Element\AbstractElement;
use Magento\Framework\View\Helper\Js;
use Magento\Store\Model\ScopeInterface;
use MercadoPago\Core\Helper\ConfigData;
use MercadoPago\Core\Helper\Data;
use MercadoPago\Core\Helper\Cache;

/**
* Config form FieldSet renderer
*/
class Payment extends Fieldset
{

/**
* checkout types
*/
const CHECKOUT_CUSTOM_CARD = 'custom_checkout';
const CHECKOUT_CUSTOM_PIX= 'custom_checkout_pix';
const CHECKOUT_CUSTOM_TICKET = 'custom_checkout_ticket';
const CHECKOUT_CUSTOM_BANK_TRANSFER = 'custom_checkout_bank_transfer';

/**
* @var ScopeConfigInterface
*/
Expand All @@ -29,12 +41,30 @@ class Payment extends Fieldset
*/
protected $configResource;

/**
*
* @var TypeListInterface
*/
protected $cacheTypeList;

/**
*
* @var Switcher
*/
protected $switcher;

/**
*
* @var Data
*/
protected $coreHelper;

/**
*
* @var Cache
*/
protected $cache;

/**
* @param Context $context
* @param Session $authSession
Expand All @@ -43,6 +73,9 @@ class Payment extends Fieldset
* @param Config $configResource
* @param Switcher $switcher
* @param array $data
* @param Data $coreHelper
* @param Cache $cache
* @param TypeLIstInterface $cacheTypeList
*/
public function __construct(
Context $context,
Expand All @@ -51,12 +84,18 @@ public function __construct(
ScopeConfigInterface $scopeConfig,
Config $configResource,
Switcher $switcher,
array $data = []
array $data = [],
Data $coreHelper,
Cache $cache,
TypeLIstInterface $cacheTypeList
) {
parent::__construct($context, $authSession, $jsHelper, $data);
$this->scopeConfig = $scopeConfig;
$this->configResource = $configResource;
$this->switcher = $switcher;
$this->coreHelper = $coreHelper;
$this->cache = $cache;
$this->cacheTypeList = $cacheTypeList;
}

/**
Expand All @@ -68,39 +107,42 @@ public function render(AbstractElement $element)
//get id element
$paymentId = $element->getId();

//get country (Site id for Mercado Pago)
$siteId = strtoupper(
$this->scopeConfig->getValue(
ConfigData::PATH_SITE_ID,
ScopeInterface::SCOPE_STORE
)
);

//check is bank transfer
if ($this->hideBankTransfer($paymentId, $siteId)) {
return "";
}

//check is pix
if ($this->hidePix($paymentId, $siteId)) {
//check available payment methods
if ($this->hideInvalidCheckoutOptions($paymentId)) {
$this->disablePayment($paymentId);
return "";
}

return parent::render($element);
}

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

$paymentMethods = $this->coreHelper->getMercadoPagoPaymentMethods($accessToken);

return $paymentMethods;
}

/**
* @param $paymentActivePath
* Disables the given payment if it is currently active
*
* @param $paymentId
*/
protected function disablePayment($paymentActivePath)
protected function disablePayment($paymentId)
{
$paymentIdWithoutPrefix = implode('_', array_slice(explode('_', $paymentId), 4));

$paymentActivePath = $this->getPaymentPath($paymentIdWithoutPrefix);

$statusPaymentMethod = $this->scopeConfig->isSetFlag(
$paymentActivePath,
ScopeInterface::SCOPE_STORE
);

//check is active for disable
if ($statusPaymentMethod) {
if ($paymentActivePath && $statusPaymentMethod) {
$value = 0;

if ($this->switcher->getWebsiteId() == 0) {
Expand All @@ -113,41 +155,93 @@ protected function disablePayment($paymentActivePath)
$this->switcher->getWebsiteId()
);
}
$this->cacheTypeList->cleanType(\Magento\Framework\App\Cache\Type\Config::TYPE_IDENTIFIER);
}
}

/**
* @param $paymentId
* @param $siteId
* @return bool
*/
protected function hideBankTransfer($paymentId, $siteId)
protected function hideInvalidCheckoutOptions($paymentId)
{
if (strpos($paymentId, 'custom_checkout_bank_transfer') !== false) {
//hide payment method if not Chile or Colombia
if ($siteId !== "MLC" && $siteId !== "MCO") {
$this->disablePayment(ConfigData::PATH_CUSTOM_BANK_TRANSFER_ACTIVE);
return true;
}
if (!$this->coreHelper->getAccessToken()) {
return true;
}

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

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

return false;
return !in_array($paymentIdWithoutPrefix, $validCheckoutOptions);
}

/**
* @param $paymentId
* @param $siteId
* @return bool
* Get available checkout options based on payment methods of the used credentials
*
* @param string $accessToken
* @return array
*/
protected function hidePix($paymentId, $siteId)
public function getAvailableCheckoutOptions()
{
if (strpos($paymentId, 'custom_checkout_pix') !== false) {
if ($siteId !== "MLB") {
$this->disablePayment(ConfigData::PATH_CUSTOM_PIX_ACTIVE);
return true;
try {
$availableCheckouts = array();
$paymentMethods = $this->getPaymentMethods();

foreach ($paymentMethods['response'] as $paymentMethod) {
switch (strtolower($paymentMethod['payment_type_id'])) {
case 'credit_card':
case 'debid_card':
case 'prepaid_card':
if (!in_array(self::CHECKOUT_CUSTOM_CARD, $availableCheckouts)) {
$availableCheckouts[] = self::CHECKOUT_CUSTOM_CARD;
}
break;

case 'atm':
case 'ticket':
if (!in_array(self::CHECKOUT_CUSTOM_TICKET, $availableCheckouts)) {
$availableCheckouts[] = self::CHECKOUT_CUSTOM_TICKET;
}
break;

case 'bank_transfer':
if (!in_array(self::CHECKOUT_CUSTOM_PIX, $availableCheckouts) && strtolower($paymentMethod['id']) === 'pix') {
$availableCheckouts[] = self::CHECKOUT_CUSTOM_PIX;
}
if (!in_array(self::CHECKOUT_CUSTOM_BANK_TRANSFER, $availableCheckouts) && strtolower($paymentMethod['id']) !== 'pix') {
$availableCheckouts[] = self::CHECKOUT_CUSTOM_BANK_TRANSFER;
}
break;
}
}

return $availableCheckouts;
} catch (\Exception $e) {
$this->coreHelper->log('Payment Fieldset getAvailableCheckoutOptions error: ' . $e->getMessage());
return [];
}
}

return false;
public function getPaymentPath($paymentId)
{
switch ($paymentId) {
case (self::CHECKOUT_CUSTOM_CARD):
return ConfigData::PATH_CUSTOM_ACTIVE;

case (self::CHECKOUT_CUSTOM_TICKET):
return ConfigData::PATH_CUSTOM_TICKET_ACTIVE;

case (self::CHECKOUT_CUSTOM_PIX):
return ConfigData::PATH_CUSTOM_PIX_ACTIVE;

case (self::CHECKOUT_CUSTOM_BANK_TRANSFER):
return ConfigData::PATH_CUSTOM_BANK_TRANSFER_ACTIVE;
}
}
}
3 changes: 3 additions & 0 deletions src/MercadoPago/Core/Helper/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@
/**
* Class Cache
* @package MercadoPago\Core\Helper
*
* @codeCoverageIgnore
*/
class Cache
{
const PREFIX_KEY = 'MP_';
const IS_VALID_AT = 'IS_VALID_ACCESS_TOKEN';
const VALID_PAYMENT_METHODS = 'VALID_PAYMENT_METHODS';

/**
* @var CacheInterface
Expand Down
Loading

0 comments on commit a697806

Please sign in to comment.