BillingAgreement.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Paypal\CustomerData;
  7. use Magento\Customer\CustomerData\SectionSourceInterface;
  8. use Magento\Customer\Helper\Session\CurrentCustomer;
  9. use Magento\Framework\Escaper;
  10. use Magento\Framework\UrlInterface;
  11. use Magento\Paypal\Helper\Data;
  12. use Magento\Paypal\Model\Config;
  13. use Magento\Paypal\Model\ConfigFactory;
  14. /**
  15. * BillingAgreement section
  16. */
  17. class BillingAgreement implements SectionSourceInterface
  18. {
  19. /**
  20. * @var CurrentCustomer
  21. */
  22. private $currentCustomer;
  23. /**
  24. * Paypal data
  25. *
  26. * @var Data
  27. */
  28. private $paypalData;
  29. /**
  30. * @var Config
  31. */
  32. private $config;
  33. /**
  34. * Url Builder
  35. *
  36. * @var UrlInterface
  37. */
  38. private $urlBuilder;
  39. /**
  40. * Escaper
  41. *
  42. * @var Escaper
  43. */
  44. private $escaper;
  45. /**
  46. * Start express action
  47. *
  48. * @var string
  49. */
  50. private $startAction = 'paypal/express/start/button/1';
  51. /**
  52. * @param CurrentCustomer $currentCustomer
  53. * @param Data $paypalData
  54. * @param ConfigFactory $paypalConfigFactory
  55. * @param UrlInterface $urlBuilder
  56. * @param Escaper $escaper
  57. */
  58. public function __construct(
  59. CurrentCustomer $currentCustomer,
  60. Data $paypalData,
  61. ConfigFactory $paypalConfigFactory,
  62. UrlInterface $urlBuilder,
  63. Escaper $escaper
  64. ) {
  65. $this->currentCustomer = $currentCustomer;
  66. $this->paypalData = $paypalData;
  67. $this->urlBuilder = $urlBuilder;
  68. $this->escaper = $escaper;
  69. $this->config = $paypalConfigFactory->create();
  70. $this->config->setMethod(Config::METHOD_EXPRESS);
  71. }
  72. /**
  73. * @inheritdoc
  74. */
  75. public function getSectionData()
  76. {
  77. $customerId = $this->currentCustomer->getCustomerId();
  78. if ($this->paypalData->shouldAskToCreateBillingAgreement($this->config, $customerId)) {
  79. return [
  80. 'askToCreate' => true,
  81. 'confirmUrl' => $this->escaper->escapeUrl(
  82. $this->urlBuilder->getUrl(
  83. $this->startAction,
  84. [\Magento\Paypal\Model\Express\Checkout::PAYMENT_INFO_TRANSPORT_BILLING_AGREEMENT => 1]
  85. )
  86. ),
  87. 'confirmMessage' => $this->escaper->escapeHtml(
  88. __('Would you like to sign a billing agreement to streamline further purchases with PayPal?')
  89. )
  90. ];
  91. }
  92. return [];
  93. }
  94. }