Free.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Payment\Model\Method;
  7. use Magento\Framework\Pricing\PriceCurrencyInterface;
  8. /**
  9. * Free payment method
  10. * @method \Magento\Quote\Api\Data\PaymentMethodExtensionInterface getExtensionAttributes()
  11. *
  12. * This is an implementation of payment method that allows order for free.
  13. * Magento contains special flow for handling this payment method.
  14. * Inheritance is allowed to modify it behavior.
  15. *
  16. * @api
  17. * @since 100.0.2
  18. */
  19. class Free extends \Magento\Payment\Model\Method\AbstractMethod
  20. {
  21. const PAYMENT_METHOD_FREE_CODE = 'free';
  22. /**
  23. * XML Paths for configuration constants
  24. */
  25. const XML_PATH_PAYMENT_FREE_ACTIVE = 'payment/free/active';
  26. const XML_PATH_PAYMENT_FREE_ORDER_STATUS = 'payment/free/order_status';
  27. const XML_PATH_PAYMENT_FREE_PAYMENT_ACTION = 'payment/free/payment_action';
  28. /**
  29. * Payment Method features
  30. *
  31. * @var bool
  32. */
  33. protected $_canAuthorize = true;
  34. /**
  35. * Payment code name
  36. *
  37. * @var string
  38. */
  39. protected $_code = self::PAYMENT_METHOD_FREE_CODE;
  40. /**
  41. * @var PriceCurrencyInterface
  42. */
  43. protected $priceCurrency;
  44. /**
  45. * @param \Magento\Framework\Model\Context $context
  46. * @param \Magento\Framework\Registry $registry
  47. * @param \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory
  48. * @param \Magento\Framework\Api\AttributeValueFactory $customAttributeFactory
  49. * @param \Magento\Payment\Helper\Data $paymentData
  50. * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
  51. * @param Logger $logger
  52. * @param PriceCurrencyInterface $priceCurrency
  53. * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
  54. * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
  55. * @param array $data
  56. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  57. */
  58. public function __construct(
  59. \Magento\Framework\Model\Context $context,
  60. \Magento\Framework\Registry $registry,
  61. \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory,
  62. \Magento\Framework\Api\AttributeValueFactory $customAttributeFactory,
  63. \Magento\Payment\Helper\Data $paymentData,
  64. \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
  65. \Magento\Payment\Model\Method\Logger $logger,
  66. PriceCurrencyInterface $priceCurrency,
  67. \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
  68. \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
  69. array $data = []
  70. ) {
  71. parent::__construct(
  72. $context,
  73. $registry,
  74. $extensionFactory,
  75. $customAttributeFactory,
  76. $paymentData,
  77. $scopeConfig,
  78. $logger,
  79. $resource,
  80. $resourceCollection,
  81. $data
  82. );
  83. $this->priceCurrency = $priceCurrency;
  84. }
  85. /**
  86. * Check whether method is available
  87. *
  88. * @param \Magento\Quote\Api\Data\CartInterface|\Magento\Quote\Model\Quote|null $quote
  89. * @return bool
  90. */
  91. public function isAvailable(\Magento\Quote\Api\Data\CartInterface $quote = null)
  92. {
  93. return parent::isAvailable(
  94. $quote
  95. ) && null !== $quote && $this->priceCurrency->round(
  96. $quote->getGrandTotal()
  97. ) == 0;
  98. }
  99. /**
  100. * Check whether method is enabled in config
  101. *
  102. * @param \Magento\Quote\Model\Quote|null $quote
  103. * @return bool
  104. */
  105. public function isAvailableInConfig($quote = null)
  106. {
  107. return parent::isAvailable($quote);
  108. }
  109. /**
  110. * Get config payment action, do nothing if status is pending
  111. *
  112. * @return string|null
  113. */
  114. public function getConfigPaymentAction()
  115. {
  116. return $this->getConfigData('order_status') == 'pending' ? null : parent::getConfigPaymentAction();
  117. }
  118. }