smaex / additional-payment-checks
在 Magento 2 中启用支付方式的额外检查。
Requires
- php: ^7.1
- magento/module-payment: ^100.2
Requires (Dev)
- phpunit/phpunit: ^7.0
This package is auto-updated.
Last update: 2023-06-04 09:20:36 UTC
README
在 Magento 2 中启用支付方式的额外检查。
简介
在 Magento 2 中,添加支付方式的额外检查(决定某种支付方式是否适用于特定客户)相当简单直接。
Magento 提供了一个相对简单的接口 用于自定义支付方式检查,并使用 组合检查 来处理这些单个检查。因此,添加自定义检查只是通过依赖注入将其注入到 Magento 的组合检查中。
/** * 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; } }
好吧,至少在一个理想的世界里。但是,由于我们在这里谈论的是 Magento,事情变得有点复杂。不同的场景可能需要不同的检查,因此 Magento 使用一个工厂 来动态实例化其组合检查,不同位置具有不同的检查列表。
/** * 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]); } }
然而,典型的 Magento 风格,这种机制只是半成品。Magento 使用一个名为 MethodList 的类 来检索适用的支付方式,并硬编码了要执行的检查列表,以确定要提供的支付方式。
/** * 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); } }
幸运的是,此扩展提供了一个方便的解决方案。
如何安装
只需通过 Composer 需求此扩展。
$ composer require smaex/additional-payment-checks ^1.0
最后,通过 Magento 的 CLI 启用模块。
$ magento module:enable Smaex_AdditionalPaymentChecks
如何使用
此扩展连接到工厂的 create
-方法,并在创建时扩展传递给 Magento 的组合检查的检查列表。现在我们除了将我们的自定义检查注入到工厂(如上所述)外,还需要将其添加到此扩展提供的插件中。
<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>
为了实际示例,请参阅 smaex/customer-group-payments。
我们正在招聘!
想要为 德国领先的 Magento 合作伙伴之一 工作?使用敏捷方法、小型团队和大型客户?我们目前正在寻找经验丰富的 受虐狂 慕尼黑的 PHP & Magento 开发者。听起来很有趣?请通过 j.scherbl@techdivision.com 联系我。