PaymentMethodManagement.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\Quote\Model;
  8. use Magento\Framework\Exception\State\InvalidTransitionException;
  9. /**
  10. * Class PaymentMethodManagement
  11. */
  12. class PaymentMethodManagement implements \Magento\Quote\Api\PaymentMethodManagementInterface
  13. {
  14. /**
  15. * @var \Magento\Quote\Api\CartRepositoryInterface
  16. */
  17. protected $quoteRepository;
  18. /**
  19. * @var \Magento\Payment\Model\Checks\ZeroTotal
  20. */
  21. protected $zeroTotalValidator;
  22. /**
  23. * @var \Magento\Payment\Model\MethodList
  24. */
  25. protected $methodList;
  26. /**
  27. * Constructor
  28. *
  29. * @param \Magento\Quote\Api\CartRepositoryInterface $quoteRepository
  30. * @param \Magento\Payment\Model\Checks\ZeroTotal $zeroTotalValidator
  31. * @param \Magento\Payment\Model\MethodList $methodList
  32. */
  33. public function __construct(
  34. \Magento\Quote\Api\CartRepositoryInterface $quoteRepository,
  35. \Magento\Payment\Model\Checks\ZeroTotal $zeroTotalValidator,
  36. \Magento\Payment\Model\MethodList $methodList
  37. ) {
  38. $this->quoteRepository = $quoteRepository;
  39. $this->zeroTotalValidator = $zeroTotalValidator;
  40. $this->methodList = $methodList;
  41. }
  42. /**
  43. * {@inheritdoc}
  44. */
  45. public function set($cartId, \Magento\Quote\Api\Data\PaymentInterface $method)
  46. {
  47. /** @var \Magento\Quote\Model\Quote $quote */
  48. $quote = $this->quoteRepository->get($cartId);
  49. $quote->setTotalsCollectedFlag(false);
  50. $method->setChecks([
  51. \Magento\Payment\Model\Method\AbstractMethod::CHECK_USE_CHECKOUT,
  52. \Magento\Payment\Model\Method\AbstractMethod::CHECK_USE_FOR_COUNTRY,
  53. \Magento\Payment\Model\Method\AbstractMethod::CHECK_USE_FOR_CURRENCY,
  54. \Magento\Payment\Model\Method\AbstractMethod::CHECK_ORDER_TOTAL_MIN_MAX,
  55. ]);
  56. if ($quote->isVirtual()) {
  57. $address = $quote->getBillingAddress();
  58. } else {
  59. $address = $quote->getShippingAddress();
  60. // check if shipping address is set
  61. if ($address->getCountryId() === null) {
  62. throw new InvalidTransitionException(
  63. __('The shipping address is missing. Set the address and try again.')
  64. );
  65. }
  66. $address->setCollectShippingRates(true);
  67. }
  68. $paymentData = $method->getData();
  69. $payment = $quote->getPayment();
  70. $payment->importData($paymentData);
  71. $address->setPaymentMethod($payment->getMethod());
  72. if (!$this->zeroTotalValidator->isApplicable($payment->getMethodInstance(), $quote)) {
  73. throw new InvalidTransitionException(__('The requested Payment Method is not available.'));
  74. }
  75. $quote->save();
  76. return $quote->getPayment()->getId();
  77. }
  78. /**
  79. * {@inheritdoc}
  80. */
  81. public function get($cartId)
  82. {
  83. /** @var \Magento\Quote\Model\Quote $quote */
  84. $quote = $this->quoteRepository->get($cartId);
  85. $payment = $quote->getPayment();
  86. if (!$payment->getId()) {
  87. return null;
  88. }
  89. return $payment;
  90. }
  91. /**
  92. * {@inheritdoc}
  93. */
  94. public function getList($cartId)
  95. {
  96. /** @var \Magento\Quote\Model\Quote $quote */
  97. $quote = $this->quoteRepository->get($cartId);
  98. return $this->methodList->getAvailableMethods($quote);
  99. }
  100. }