OrderCustomerManagement.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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\Login\Plugin;
  17. use Amazon\Core\Helper\Data;
  18. use Amazon\Login\Api\CustomerLinkManagementInterface;
  19. use Amazon\Login\Helper\Session as LoginSessionHelper;
  20. use Amazon\Payment\Gateway\Config\Config;
  21. use Magento\Customer\Api\Data\CustomerInterface;
  22. use Magento\Sales\Api\OrderCustomerManagementInterface;
  23. use Magento\Sales\Api\OrderRepositoryInterface;
  24. class OrderCustomerManagement
  25. {
  26. /**
  27. * @var LoginSessionHelper
  28. */
  29. private $loginSessionHelper;
  30. /**
  31. * @var OrderRepositoryInterface
  32. */
  33. private $orderRepository;
  34. /**
  35. * @var CustomerLinkManagementInterface
  36. */
  37. private $customerLinkManagement;
  38. /**
  39. * @var Data
  40. */
  41. private $coreHelper;
  42. /**
  43. * @param LoginSessionHelper $loginSessionHelper
  44. * @param OrderRepositoryInterface $orderRepository
  45. * @param CustomerLinkManagementInterface $customerLinkManagement
  46. * @param Data $coreHelper
  47. */
  48. public function __construct(
  49. LoginSessionHelper $loginSessionHelper,
  50. OrderRepositoryInterface $orderRepository,
  51. CustomerLinkManagementInterface $customerLinkManagement,
  52. Data $coreHelper
  53. ) {
  54. $this->loginSessionHelper = $loginSessionHelper;
  55. $this->orderRepository = $orderRepository;
  56. $this->customerLinkManagement = $customerLinkManagement;
  57. $this->coreHelper = $coreHelper;
  58. }
  59. /**
  60. * @param OrderCustomerManagementInterface $subject
  61. * @param CustomerInterface $result
  62. * @param int $orderId
  63. * @return CustomerInterface
  64. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  65. */
  66. public function afterCreate(OrderCustomerManagementInterface $subject, $result, $orderId)
  67. {
  68. if ($this->coreHelper->isLwaEnabled()) {
  69. $paymentMethodName = $this->orderRepository->get($orderId)->getPayment()->getMethod();
  70. $isAmazonPayment = $paymentMethodName === Config::CODE;
  71. $amazonCustomer = $this->loginSessionHelper->getAmazonCustomer();
  72. if ($isAmazonPayment && $amazonCustomer) {
  73. $this->customerLinkManagement->updateLink($result->getId(), $amazonCustomer->getId());
  74. }
  75. }
  76. return $result;
  77. }
  78. }