ShippingInformationManagement.php 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /**
  3. * Copyright 2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License").
  6. * You may not use this file except in compliance with the License.
  7. * A copy of the License is located at
  8. *
  9. * http://aws.amazon.com/apache2.0
  10. *
  11. * or in the "license" file accompanying this file. This file is distributed
  12. * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  13. * express or implied. See the License for the specific language governing
  14. * permissions and limitations under the License.
  15. */
  16. namespace Amazon\Payment\Plugin;
  17. use Amazon\Payment\Api\OrderInformationManagementInterface;
  18. use Amazon\Payment\Domain\AmazonConstraint;
  19. use Closure;
  20. use Magento\Checkout\Api\Data\ShippingInformationInterface;
  21. use Magento\Checkout\Api\ShippingInformationManagementInterface;
  22. use Magento\Quote\Api\CartRepositoryInterface;
  23. use Amazon\Login\Helper\Session as LoginSessionHelper;
  24. class ShippingInformationManagement
  25. {
  26. /**
  27. * @var CartRepositoryInterface
  28. */
  29. private $cartRepository;
  30. /**
  31. * @var OrderInformationManagementInterface
  32. */
  33. private $orderInformationManagement;
  34. /**
  35. * @var LoginSessionHelper
  36. */
  37. private $loginSessionHelper;
  38. /**
  39. * ShippingInformationManagement constructor.
  40. *
  41. * @param LoginSessionHelper $loginSessionHelper
  42. * @param OrderInformationManagementInterface $orderInformationManagement
  43. * @param CartRepositoryInterface $cartRepository
  44. */
  45. public function __construct(
  46. LoginSessionHelper $loginSessionHelper,
  47. OrderInformationManagementInterface $orderInformationManagement,
  48. CartRepositoryInterface $cartRepository
  49. ) {
  50. $this->loginSessionHelper = $loginSessionHelper;
  51. $this->cartRepository = $cartRepository;
  52. $this->orderInformationManagement = $orderInformationManagement;
  53. }
  54. /**
  55. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  56. */
  57. public function beforeSaveAddressInformation(
  58. ShippingInformationManagementInterface $subject,
  59. $cartId,
  60. ShippingInformationInterface $shippingInformation
  61. ) {
  62. $return = [$cartId, $shippingInformation];
  63. $quote = $this->cartRepository->getActive($cartId);
  64. /* Grand total is 0, skip rest of the plugin */
  65. if ($quote->getGrandTotal() <= 0) {
  66. return $return;
  67. }
  68. // Add Amazon Order Reference ID only when logged in using Amazon Account
  69. $amazonCustomer = $this->loginSessionHelper->getAmazonCustomer();
  70. if (!$amazonCustomer) {
  71. return $return;
  72. }
  73. $amazonOrderReferenceId = $quote->getExtensionAttributes()->getAmazonOrderReferenceId();
  74. if ($amazonOrderReferenceId) {
  75. $this->orderInformationManagement->saveOrderInformation(
  76. $amazonOrderReferenceId,
  77. [
  78. AmazonConstraint::PAYMENT_PLAN_NOT_SET_ID,
  79. AmazonConstraint::PAYMENT_METHOD_NOT_ALLOWED_ID
  80. ]
  81. );
  82. }
  83. return $return;
  84. }
  85. }