BillingAgreementConfigProvider.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Paypal\Model;
  7. use Magento\Checkout\Model\ConfigProviderInterface;
  8. use Magento\Paypal\Model\Billing\AgreementFactory;
  9. use Magento\Customer\Helper\Session\CurrentCustomer;
  10. use Magento\Paypal\Model\Payment\Method\Billing\AbstractAgreement;
  11. /**
  12. * Class BillingAgreementConfigProvider
  13. */
  14. class BillingAgreementConfigProvider implements ConfigProviderInterface
  15. {
  16. /**
  17. * @var CurrentCustomer
  18. */
  19. protected $currentCustomer;
  20. /**
  21. * @var AgreementFactory
  22. */
  23. protected $agreementFactory;
  24. /**
  25. * @param CurrentCustomer $currentCustomer
  26. * @param AgreementFactory $agreementFactory
  27. */
  28. public function __construct(
  29. CurrentCustomer $currentCustomer,
  30. AgreementFactory $agreementFactory
  31. ) {
  32. $this->currentCustomer = $currentCustomer;
  33. $this->agreementFactory = $agreementFactory;
  34. }
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public function getConfig()
  39. {
  40. $config = [
  41. 'payment' => [
  42. 'paypalBillingAgreement' => [
  43. 'agreements' => $this->getBillingAgreements(),
  44. 'transportName' => AbstractAgreement::TRANSPORT_BILLING_AGREEMENT_ID
  45. ]
  46. ]
  47. ];
  48. return $config;
  49. }
  50. /**
  51. * Retrieve available customer billing agreements
  52. *
  53. * @return array
  54. */
  55. protected function getBillingAgreements()
  56. {
  57. $customerId = $this->currentCustomer->getCustomerId();
  58. $data = [];
  59. if (!$customerId) {
  60. return $data;
  61. }
  62. $collection = $this->agreementFactory->create()->getAvailableCustomerBillingAgreements(
  63. $customerId
  64. );
  65. foreach ($collection as $item) {
  66. $data[] = ['id' => $item->getId(), 'referenceId' => $item->getReferenceId()];
  67. }
  68. return $data;
  69. }
  70. }