Agreement.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Paypal\Controller\Billing;
  7. use Magento\Framework\App\RequestInterface;
  8. /**
  9. * Billing agreements controller
  10. */
  11. abstract class Agreement extends \Magento\Framework\App\Action\Action
  12. {
  13. /**
  14. * Core registry
  15. *
  16. * @var \Magento\Framework\Registry
  17. */
  18. protected $_coreRegistry = null;
  19. /**
  20. * @param \Magento\Framework\App\Action\Context $context
  21. * @param \Magento\Framework\Registry $coreRegistry
  22. */
  23. public function __construct(
  24. \Magento\Framework\App\Action\Context $context,
  25. \Magento\Framework\Registry $coreRegistry
  26. ) {
  27. $this->_coreRegistry = $coreRegistry;
  28. parent::__construct($context);
  29. }
  30. /**
  31. * Check customer authentication
  32. *
  33. * @param RequestInterface $request
  34. * @return \Magento\Framework\App\ResponseInterface
  35. */
  36. public function dispatch(RequestInterface $request)
  37. {
  38. if (!$request->isDispatched()) {
  39. return parent::dispatch($request);
  40. }
  41. if (!$this->_getSession()->authenticate()) {
  42. $this->_actionFlag->set('', 'no-dispatch', true);
  43. }
  44. return parent::dispatch($request);
  45. }
  46. /**
  47. * Init billing agreement model from request
  48. *
  49. * @return \Magento\Paypal\Model\Billing\Agreement|false
  50. */
  51. protected function _initAgreement()
  52. {
  53. $agreementId = $this->getRequest()->getParam('agreement');
  54. if ($agreementId) {
  55. /** @var \Magento\Paypal\Model\Billing\Agreement $billingAgreement */
  56. $billingAgreement = $this->_objectManager->create(\Magento\Paypal\Model\Billing\Agreement::class)
  57. ->load($agreementId);
  58. $currentCustomerId = $this->_getSession()->getCustomerId();
  59. $agreementCustomerId = $billingAgreement->getCustomerId();
  60. if ($billingAgreement->getId() && $agreementCustomerId == $currentCustomerId) {
  61. $this->_coreRegistry->register('current_billing_agreement', $billingAgreement);
  62. return $billingAgreement;
  63. }
  64. }
  65. $this->messageManager->addErrorMessage(
  66. __('Please specify the correct billing agreement ID and try again.')
  67. );
  68. $this->_redirect('*/*/');
  69. return false;
  70. }
  71. /**
  72. * Retrieve customer session model
  73. *
  74. * @return \Magento\Customer\Model\Session
  75. */
  76. protected function _getSession()
  77. {
  78. return $this->_objectManager->get(\Magento\Customer\Model\Session::class);
  79. }
  80. }