PHP code example of smaex / additional-payment-checks

1. Go to this page and download the library: Download smaex/additional-payment-checks library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

smaex / additional-payment-checks example snippets


/**
 * 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;
    }
}

/**
 * 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]);
    }
}

/**
 * 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);
    }
}
xml
<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>