Commit 49bc2f63 authored by Vincent Mrose's avatar Vincent Mrose 💬
Browse files

QA Release of Version 0.9.4

Fixes #8

Signed-off-by: Vincent Mrose's avatarVincent Mrose <v.mrose@crefopay.de>
parent 8493e610
......@@ -6,11 +6,17 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
## [Known issues]
## [Unreleased]
## [0.9.4] - 2019-05-14
### Added
- Added a setting to do an automatic capture based on categories
### Fixed
- Error messages in the frontend no longer default to an empty string in some cases
- Translations for all the translatable module strings were added
- Help texts for the module configuration were added
- Changed the initialization of the Javascript Library to always happen
- Fixed issue #8
- Changing the shipping address no longer causes a frontend error (#8)
## [0.9.3-mnshandling] - 2019-05-02
### Fixed
......
<?php
namespace Trilix\CrefoPay\Block\Adminhtml\System\Config\Form\Field;
use Magento\Ui\Model\UiComponentGenerator;
use Magento\Backend\Block\Template\Context;
class Categories extends \Magento\Backend\Block\Template
implements \Magento\Framework\Data\Form\Element\Renderer\RendererInterface
{
private $context;
/** @var UiComponentGenerator */
private $uiComponentGenerator;
/**
* {@inheritdoc}
*/
public function __construct(Context $context, UiComponentGenerator $uiComponentGenerator, array $data = [])
{
parent::__construct($context, $data);
$this->uiComponentGenerator = $uiComponentGenerator;
$this->context = $context;
}
/**
* Get the UI component HTML as a form field.
*
* @param \Magento\Framework\Data\Form\Element\AbstractElement $element
*
* @return string
*/
public function render(\Magento\Framework\Data\Form\Element\AbstractElement $element)
{
$uiComponent = $this->uiComponentGenerator->generateUiComponent('category_multiselect', $this->_layout);
$html = $uiComponent->render();
return $html;
}
}
\ No newline at end of file
<?php
namespace Trilix\CrefoPay\Controller\Adminhtml\Autocapture;
use Magento\Backend\App\Action\Context;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\Config\Storage\WriterInterface as ConfigWriter;
use Trilix\CrefoPay\Gateway\Config\Config;
class Save extends \Magento\Backend\App\Action
{
/**
* @var ConfigWriter
*/
private $configWriter;
/**
* Save constructor.
*
* @param ConfigWriter $configWriter
* @param Context $context
*/
public function __construct(ConfigWriter $configWriter, Context $context)
{
parent::__construct($context);
$this->configWriter = $configWriter;
}
/**
* Save auto capture categories.
*
* @return \Magento\Framework\Controller\Result\Raw
*/
public function execute()
{
/** @var \Magento\Framework\Controller\Result\Raw $result */
$result = $this->resultFactory->create(ResultFactory::TYPE_RAW);
$categories = $this->getRequest()->getParam('categories');
$scope = $this->getRequest()->getParam('scope');
if (!is_array($categories)) {
return $result;
}
$configScope = ScopeConfigInterface::SCOPE_TYPE_DEFAULT;
$configScopeId = 0;
if (is_array($scope) && array_key_exists('website', $scope) && $scope['website']) {
$configScope = 'websites';
$configScopeId = $scope['website'];
}
$this->configWriter->save(
Config::KEY_AUTO_CAPTURE_CATEGORIES,
implode(',', $categories),
$configScope,
$configScopeId
);
return $result;
}
}
\ No newline at end of file
......@@ -2,6 +2,7 @@
namespace Trilix\CrefoPay\Gateway\Config;
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory as CategoryCollectionFactory;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Payment\Model\CcConfig;
use Trilix\CrefoPay\Model\Adminhtml\Source\Environment;
......@@ -16,6 +17,7 @@ class Config extends \Magento\Payment\Gateway\Config\Config
const KEY_PUBLIC_TOKEN = 'public_token';
const KEY_IS_B2B_ENABLED = 'is_b2b_enabled';
const KEY_RISK_CLASS = 'risk_class';
const KEY_AUTO_CAPTURE_CATEGORIES = 'auto_capture_categories';
/**
* @var CcConfig
......@@ -33,21 +35,29 @@ class Config extends \Magento\Payment\Gateway\Config\Config
private $secureFieldsUrl;
/**
* Initialize dependencies.
* @var CategoryCollectionFactory
*/
private $categoryCollectionFactory;
/**
* Config constructor.
*
* @param ScopeConfigInterface $scopeConfig
* @param CcConfig $ccConfig
* @param null $methodCode
* @param string $pathPattern
* @param ScopeConfigInterface $scopeConfig
* @param CcConfig $ccConfig
* @param CategoryCollectionFactory $categoryCollectionFactory
* @param null $methodCode
* @param string $pathPattern
*/
public function __construct(
ScopeConfigInterface $scopeConfig,
CcConfig $ccConfig,
CategoryCollectionFactory $categoryCollectionFactory,
$methodCode = null,
$pathPattern = self::DEFAULT_PATH_PATTERN
) {
parent::__construct($scopeConfig, $methodCode, $pathPattern);
$this->ccConfig = $ccConfig;
$this->categoryCollectionFactory = $categoryCollectionFactory;
}
/**
......@@ -155,4 +165,27 @@ class Config extends \Magento\Payment\Gateway\Config\Config
return $this->baseUrl;
}
/**
* Return auto capture category IDs as plain array.
*
* @return array
*/
public function getAutoCaptureCategoryIds(): array
{
return array_filter(explode(',', $this->getValue(self::KEY_AUTO_CAPTURE_CATEGORIES)));
}
/**
* Return auto capture category collection.
*
* @return \Magento\Catalog\Model\ResourceModel\Category\Collection
*/
public function getAutoCaptureCategories(): \Magento\Catalog\Model\ResourceModel\Category\Collection
{
$categories = $this->categoryCollectionFactory->create();
$categories->addIdFilter($this->getAutoCaptureCategoryIds());
return $categories;
}
}
......@@ -5,6 +5,7 @@ use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Framework\Exception\LocalizedException;
use Magento\Sales\Model\Order as SalesOrder;
use Magento\Sales\Model\OrderRepository;
use Trilix\CrefoPay\Gateway\Config\Config as GatewayConfig;
class Order
{
......@@ -18,16 +19,26 @@ class Order
*/
private $searchCriteriaBuilder;
/**
* @var GatewayConfig
*/
private $gatewayConfig;
/**
* Order constructor.
*
* @param OrderRepository $orderRepository
* @param OrderRepository $orderRepository
* @param SearchCriteriaBuilder $searchCriteriaBuilder
* @param GatewayConfig $gatewayConfig
*/
public function __construct(OrderRepository $orderRepository, SearchCriteriaBuilder $searchCriteriaBuilder)
{
public function __construct(
OrderRepository $orderRepository,
SearchCriteriaBuilder $searchCriteriaBuilder,
GatewayConfig $gatewayConfig
) {
$this->orderRepository = $orderRepository;
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
$this->gatewayConfig = $gatewayConfig;
}
/**
......@@ -69,4 +80,47 @@ class Order
{
return $this->orderRepository;
}
/**
* Checks whether all the order products contained in the allowed categories for auto capture. And whether payment method
* allowed to perform auto capture.
*
* @param SalesOrder $order
*
* @return bool
* @throws LocalizedException
*/
public function isEligibleForAutoCapture(SalesOrder $order): bool
{
/** @var \Magento\Sales\Model\Order\Payment $payment */
$payment = $order->getPayment();
if (!$payment->canCapture()) {
return false;
}
/** @var \Trilix\CrefoPay\Model\Method\Adapter $methodInstance */
$methodInstance = $payment->getMethodInstance();
if (!$methodInstance->isAutoCaptureEnabled()) {
return false;
}
$allowedCategoryIds = array_map('intval', $this->gatewayConfig->getAutoCaptureCategoryIds());
if (empty($allowedCategoryIds)) {
return true;
}
/** @var \Magento\Sales\Model\Order\Item $orderItem */
foreach ($order->getItemsCollection() as $orderItem) {
$productCategoryIds = array_map('intval', $orderItem->getProduct()->getCategoryIds());
if (!empty(array_diff($productCategoryIds, $allowedCategoryIds))) {
return false;
}
}
return true;
}
}
\ No newline at end of file
......@@ -8,7 +8,7 @@ class Adapter extends \Magento\Payment\Model\Method\Adapter
*
* @return bool
*/
public function isAutoCapture(): bool
public function isAutoCaptureEnabled(): bool
{
return (bool)$this->getConfigData('auto_capture');
}
......
......@@ -48,7 +48,7 @@ class MerchantPending extends AbstractConsumer implements MnsConsumerInterface
* @param MnsEvent $event
*
* @throws \Magento\Framework\Exception\LocalizedException
* @throws \Trilix\CrefoPay\Mns\Consumers\MnsConsumerException
* @throws \Trilix\CrefoPay\Model\Mns\Consumers\MnsConsumerException
*/
public function process(MnsEvent $event)
{
......@@ -62,7 +62,7 @@ class MerchantPending extends AbstractConsumer implements MnsConsumerInterface
$order->setState(Order::STATE_PROCESSING);
$order->setStatus($order->getConfig()->getStateDefaultStatus(Order::STATE_PROCESSING));
if ($methodInstance->isAutoCapture() && $payment->canCapture()) {
if ($this->orderHelper->isEligibleForAutoCapture($order)) {
$methodInstance->capture($payment, $event->getAmount());
$payment->registerCaptureNotification($event->getAmount());
} else if ($payment->getMethod() === CIA::CODE) {
......
<?php
namespace Trilix\CrefoPay\Ui\Component\CategoryMultiselect;
use Magento\Config\Model\ResourceModel\Config\Data\Collection as ConfigCollection;
use Magento\Ui\DataProvider\AbstractDataProvider;
use Trilix\CrefoPay\Gateway\Config\Config as GatewayConfig;
class AutoCaptureDataProvider extends AbstractDataProvider
{
/**
* @var GatewayConfig
*/
private $gatewayConfig;
public function __construct(
string $name,
string $primaryFieldName,
string $requestFieldName,
GatewayConfig $gatewayConfig,
ConfigCollection $configCollection,
array $meta = [],
array $data = []
) {
parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);
$this->gatewayConfig = $gatewayConfig;
$this->collection = $configCollection;
}
public function getData()
{
$categoryIds = $this->gatewayConfig->getAutoCaptureCategoryIds();
return [null => $categoryIds];
}
}
\ No newline at end of file
......@@ -285,6 +285,14 @@
</field>
</group>
</group>
<group id="crefopay_autocapture" translate="label" showInDefault="1" showInWebsite="0" showInStore="0" sortOrder="40">
<label>Auto Capture Settings</label>
<field id="autocapture_categories" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="0" showInStore="0">
<label>Apply to categories</label>
<frontend_model>Trilix\CrefoPay\Block\Adminhtml\System\Config\Form\Field\Categories</frontend_model>
<config_path>payment/crefopay/auto_capture_categories</config_path>
</field>
</group>
</group>
</section>
</system>
......
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Trilix_CrefoPay" setup_version="0.9.3">
<module name="Trilix_CrefoPay" setup_version="0.9.4">
<sequence>
<module name="Magento_Sales"/>
<module name="Magento_Checkout"/>
......
<?xml version="1.0" encoding="UTF-8"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
<argument name="data" xsi:type="array">
<item name="js_config" xsi:type="array">
<item name="provider" xsi:type="string">category_multiselect.category_multiselect_data_source</item>
<item name="namespace" xsi:type="string">category_multiselect</item>
</item>
<item name="template" xsi:type="string">templates/form/collapsible</item>
</argument>
<settings>
<ajaxSave>true</ajaxSave>
<ajaxSaveType>simple</ajaxSaveType>
<deps>
<dep>category_multiselect.category_multiselect_data_source</dep>
</deps>
</settings>
<dataSource name="category_multiselect_data_source">
<argument name="data" xsi:type="array">
<item name="js_config" xsi:type="array">
<item name="component" xsi:type="string">Trilix_CrefoPay/js/form/provider</item>
</item>
</argument>
<settings>
<submitUrl path="crefopay/autocapture/save"/>
</settings>
<dataProvider class="Trilix\CrefoPay\Ui\Component\CategoryMultiselect\AutoCaptureDataProvider" name="category_multiselect_data_source">
<settings>
<requestFieldName>id</requestFieldName>
<primaryFieldName>config_id</primaryFieldName>
</settings>
</dataProvider>
</dataSource>
<fieldset name="data">
<settings>
<collapsible>false</collapsible>
<label/>
</settings>
<container name="messages" component="Magento_Catalog/js/components/messages">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="label" xsi:type="string"/>
</item>
</argument>
</container>
<field name="categories" component="Trilix_CrefoPay/js/components/autocapture-categories" sortOrder="20" formElement="select">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="filterOptions" xsi:type="boolean">true</item>
<item name="showCheckbox" xsi:type="boolean">false</item>
<item name="disableLabel" xsi:type="boolean">true</item>
<item name="exports" xsi:type="array">
<item name="value" xsi:type="string">category_multiselect.category_multiselect_data_source:categories</item>
</item>
</item>
</argument>
<settings>
<elementTmpl>ui/grid/filters/elements/ui-select</elementTmpl>
<label translate="true">Categories</label>
<dataScope>data.categories</dataScope>
<componentType>field</componentType>
</settings>
<formElements>
<select>
<settings>
<options class="Magento\Catalog\Ui\Component\Product\Form\Categories\Options"/>
</settings>
</select>
</formElements>
</field>
</fieldset>
</form>
define([
'Magento_Ui/js/form/element/ui-select',
'underscore'
], function (Select, _) {
'use strict';
return Select.extend({
initialize: function () {
this._super();
_.each(this.source.data, function (id) {
this.toggleOptionSelected({value: String(id)});
}, this);
}
});
});
define([
'Magento_Ui/js/form/provider',
'jquery'
], function (Provider, $) {
'use strict';
return Provider.extend({
save: function (options) {
this.data = {
categories: this.categories,
scope: {
// website: $('#website_switcher').val() // per-website loading not implemented
}
};
this._super(options);
}
});
});
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment