CheckoutConfigProvider.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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\Model;
  17. use Magento\Checkout\Model\ConfigProviderInterface;
  18. use Magento\Customer\Model\Session as CustomerSession;
  19. use Magento\Checkout\Model\Session as CheckoutSession;
  20. class CheckoutConfigProvider implements ConfigProviderInterface
  21. {
  22. /**
  23. * @var CustomerSession
  24. */
  25. private $customerSession;
  26. /**
  27. * @var CheckoutSession
  28. */
  29. private $checkoutSession;
  30. /**
  31. * CheckoutConfigProvider constructor.
  32. * @param CustomerSession $customerSession
  33. * @param CheckoutSession $checkoutSession
  34. */
  35. public function __construct(
  36. CustomerSession $customerSession,
  37. CheckoutSession $checkoutSession
  38. ) {
  39. $this->customerSession = $customerSession;
  40. $this->checkoutSession = $checkoutSession;
  41. }
  42. /**
  43. * {@inheritdoc}
  44. */
  45. public function getConfig()
  46. {
  47. $config = [];
  48. /** @var \Amazon\Core\Api\Data\AmazonCustomerInterface $amazonCustomer */
  49. if ($amazonCustomer = $this->customerSession->getAmazonCustomer()) {
  50. $config['amazon_customer_email'] = $amazonCustomer->getEmail();
  51. }
  52. if (!isset($config['amazon_customer_email'])) {
  53. $quote = $this->checkoutSession->getQuote();
  54. $config['amazon_customer_email'] = $quote->getCustomerEmail();
  55. }
  56. // return a stdClass so that the resulting JSON is an empty object, not an empty array
  57. return ['amazonLogin' => empty($config) ? new \stdClass : $config];
  58. }
  59. }