Coder Social home page Coder Social logo

additional-payment-checks's Introduction

Magento 2: Additional Payment Checks

Enables additional checks for payment methods in Magento 2.

Intro

Adding additional checks to payment methods – to decide if a certain payment method is applicable to a certain customer or not – is pretty easy and straight forward in Magento 2.

Magento provides a rather simplistic interface for custom payment method checks, and uses a composite check to process these individual checks. Adding a custom check is therefore just a matter of injecting it into Magento’s composite check via dependency injection.

/**
 * Combines several checks with logic "AND" operation.
 *
 * Use this class to register own specifications.
 */
class Composite implements SpecificationInterface
{
    /**
     * Check whether payment method is applicable to quote
     */
    public function isApplicable(MethodInterface $paymentMethod, Quote $quote): bool
    {
        foreach ($this->list as $specification) {
            if (!$specification->isApplicable($paymentMethod, $quote)) {
                return false;
            }
        }
        return true;
    }
}

Well, at least in an ideal world. But since we’re talking about Magento here, it’s geting a bit more messy complex. Different checks might be necessary in different scenarios, so Magento uses a factory to instantiate its composite check dynamically with a varying list of checks in different places.

/**
 * Creates complex specification.
 *
 * Use this class to register predefined list of specifications
 * that should be added to any complex specification.
 */
class SpecificationFactory
{
    /**
     * Creates new instances of payment method models
     */
    public function create(array $data): Composite
    {
        $specifications = array_intersect_key($this->mapping, array_flip((array)$data));
        return $this->compositeFactory->create(['list' => $specifications]);
    }
}

However, and in typical Magento fashion, this mechanism is only half baked. Magento uses a class called MethodList to retrieve applicable payment methods, and hardcodes the list of checks that are performed to figure out which payment methods to offer.

/**
 * Methods List service class.
 */
class MethodList
{
    /**
     * Check payment method model
     */
    protected function _canUseMethod(MethodInterface $method, CartInterface $quote): bool
    {
        return $this->methodSpecificationFactory->create(
            [
                AbstractMethod::CHECK_USE_CHECKOUT,
                AbstractMethod::CHECK_USE_FOR_COUNTRY,
                AbstractMethod::CHECK_USE_FOR_CURRENCY,
                AbstractMethod::CHECK_ORDER_TOTAL_MIN_MAX,
            ]
        )->isApplicable($method, $quote);
    }
}

Fortunately, this extension provides a handy workaround.

How to install

Simply require the extension via Composer.

$ composer require smaex/additional-payment-checks ^1.0

Finally, enable the module via Magento’s CLI.

$ magento module:enable Smaex_AdditionalPaymentChecks

How to use

The extension plugs into the factory’s create-method and extends the list of checks that is passed to Magento’s composite check upon creation. All we have to do now, besides injecting our custom check into the factory as described above, is adding it to the plugin provided by this extension.

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Payment\Model\Checks\SpecificationFactory">
        <arguments>
            <argument name="mapping" xsi:type="array">
                <item name="acme_custom_payment_method_check" xsi:type="object">
                    Acme\Payment\Model\Checks\CustomPaymentMethodCheck
                </item>
            </argument>
        </arguments>
    </type>
    <type name="Smaex\AdditionalPaymentChecks\Plugin\WhitelistAdditionalChecks">
        <arguments>
            <argument name="additionalChecks" xsi:type="array">
                <item name="acme_custom_payment_method_check" xsi:type="string">
                    acme_custom_payment_method_check
                </item>
            </argument>
        </arguments>
    </type>
</config>

For a real-life example, check out smaex/customer-group-payments as well.

We’re hiring!

Wanna work for one of Germany’s leading Magento partners? With agile methods, small teams and big clients? We’re currently looking for experienced masochists PHP & Magento developers in Munich. Sounds interesting? Just drop me a line via [email protected]

additional-payment-checks's People

Contributors

jensscherbl avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.