BillingAgreement.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Paypal\Block\Checkout\Onepage\Success;
  7. /**
  8. * Billing agreement information on Order success page
  9. *
  10. * @api
  11. * @since 100.0.2
  12. */
  13. class BillingAgreement extends \Magento\Framework\View\Element\Template
  14. {
  15. /**
  16. * @var \Magento\Checkout\Model\Session
  17. */
  18. protected $_checkoutSession;
  19. /**
  20. * @var \Magento\Customer\Model\Session
  21. */
  22. protected $_customerSession;
  23. /**
  24. * @var \Magento\Paypal\Model\Billing\AgreementFactory
  25. */
  26. protected $_agreementFactory;
  27. /**
  28. * @param \Magento\Framework\View\Element\Template\Context $context
  29. * @param \Magento\Checkout\Model\Session $checkoutSession
  30. * @param \Magento\Customer\Model\Session $customerSession
  31. * @param \Magento\Paypal\Model\Billing\AgreementFactory $agreementFactory
  32. * @param array $data
  33. */
  34. public function __construct(
  35. \Magento\Framework\View\Element\Template\Context $context,
  36. \Magento\Checkout\Model\Session $checkoutSession,
  37. \Magento\Customer\Model\Session $customerSession,
  38. \Magento\Paypal\Model\Billing\AgreementFactory $agreementFactory,
  39. array $data = []
  40. ) {
  41. $this->_checkoutSession = $checkoutSession;
  42. $this->_customerSession = $customerSession;
  43. $this->_agreementFactory = $agreementFactory;
  44. parent::__construct($context, $data);
  45. }
  46. /**
  47. * Return billing agreement information
  48. *
  49. * @return string
  50. */
  51. protected function _toHtml()
  52. {
  53. $agreementReferenceId = $this->_checkoutSession->getLastBillingAgreementReferenceId();
  54. $customerId = $this->_customerSession->getCustomerId();
  55. if (!$agreementReferenceId || !$customerId) {
  56. return '';
  57. }
  58. $agreement = $this->_agreementFactory->create()->load($agreementReferenceId, 'reference_id');
  59. if ($agreement->getId() && $customerId == $agreement->getCustomerId()) {
  60. $this->addData(
  61. [
  62. 'agreement_ref_id' => $agreement->getReferenceId(),
  63. 'agreement_url' => $this->getUrl(
  64. 'paypal/billing_agreement/view',
  65. ['agreement' => $agreement->getId()]
  66. ),
  67. ]
  68. );
  69. return parent::_toHtml();
  70. }
  71. return '';
  72. }
  73. }