Agreements.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Paypal\Block\Billing;
  7. /**
  8. * Customer account billing agreements block
  9. *
  10. * @api
  11. * @since 100.0.2
  12. */
  13. class Agreements extends \Magento\Framework\View\Element\Template
  14. {
  15. /**
  16. * Payment methods array
  17. *
  18. * @var array
  19. */
  20. protected $_paymentMethods = [];
  21. /**
  22. * Billing agreements collection
  23. *
  24. * @var \Magento\Paypal\Model\ResourceModel\Billing\Agreement\Collection
  25. */
  26. protected $_billingAgreements = null;
  27. /**
  28. * @var \Magento\Customer\Model\Session
  29. */
  30. protected $_customerSession;
  31. /**
  32. * @var \Magento\Paypal\Model\ResourceModel\Billing\Agreement\CollectionFactory
  33. */
  34. protected $_agreementCollection;
  35. /**
  36. * @var \Magento\Paypal\Helper\Data
  37. */
  38. protected $_helper;
  39. /**
  40. * @param \Magento\Framework\View\Element\Template\Context $context
  41. * @param \Magento\Customer\Model\Session $customerSession
  42. * @param \Magento\Paypal\Model\ResourceModel\Billing\Agreement\CollectionFactory $agreementCollection
  43. * @param \Magento\Paypal\Helper\Data $helper
  44. * @param array $data
  45. */
  46. public function __construct(
  47. \Magento\Framework\View\Element\Template\Context $context,
  48. \Magento\Customer\Model\Session $customerSession,
  49. \Magento\Paypal\Model\ResourceModel\Billing\Agreement\CollectionFactory $agreementCollection,
  50. \Magento\Paypal\Helper\Data $helper,
  51. array $data = []
  52. ) {
  53. $this->_helper = $helper;
  54. $this->_customerSession = $customerSession;
  55. $this->_agreementCollection = $agreementCollection;
  56. parent::__construct($context, $data);
  57. $this->_isScopePrivate = true;
  58. }
  59. /**
  60. * Set Billing Agreement instance
  61. *
  62. * @return $this
  63. */
  64. protected function _prepareLayout()
  65. {
  66. parent::_prepareLayout();
  67. $pager = $this->getLayout()->createBlock(
  68. \Magento\Theme\Block\Html\Pager::class
  69. )->setCollection(
  70. $this->getBillingAgreements()
  71. )->setIsOutputRequired(
  72. false
  73. );
  74. $this->setChild('pager', $pager)->setBackUrl($this->getUrl('customer/account/'));
  75. $this->getBillingAgreements()->load();
  76. return $this;
  77. }
  78. /**
  79. * Retrieve billing agreements collection
  80. *
  81. * @return \Magento\Paypal\Model\ResourceModel\Billing\Agreement\Collection
  82. */
  83. public function getBillingAgreements()
  84. {
  85. if ($this->_billingAgreements === null) {
  86. $this->_billingAgreements = $this->_agreementCollection->create()->addFieldToFilter(
  87. 'customer_id',
  88. $this->_customerSession->getCustomerId()
  89. )->setOrder(
  90. 'agreement_id',
  91. 'desc'
  92. );
  93. }
  94. return $this->_billingAgreements;
  95. }
  96. /**
  97. * Retrieve item value by key
  98. *
  99. * @param \Magento\Framework\DataObject|\Magento\Paypal\Model\Billing\Agreement $item
  100. * @param string $key
  101. * @return string
  102. */
  103. public function getItemValue(\Magento\Paypal\Model\Billing\Agreement $item, $key)
  104. {
  105. switch ($key) {
  106. case 'created_at':
  107. case 'updated_at':
  108. $value = $item->getData($key)
  109. ? $this->formatDate($item->getData($key), \IntlDateFormatter::SHORT, true)
  110. : __('N/A');
  111. break;
  112. case 'edit_url':
  113. $value = $this->getUrl('paypal/billing_agreement/view', ['agreement' => $item->getAgreementId()]);
  114. break;
  115. case 'payment_method_label':
  116. $label = $item->getAgreementLabel();
  117. $value = $label ? $label : __('N/A');
  118. break;
  119. case 'status':
  120. $value = $item->getStatusLabel();
  121. break;
  122. default:
  123. $value = $item->getData($key) ? $item->getData($key) : __('N/A');
  124. break;
  125. }
  126. return $this->escapeHtml($value);
  127. }
  128. /**
  129. * Load available billing agreement methods
  130. *
  131. * @return array
  132. */
  133. protected function _loadPaymentMethods()
  134. {
  135. if (!$this->_paymentMethods) {
  136. foreach ($this->_helper->getBillingAgreementMethods() as $paymentMethod) {
  137. $this->_paymentMethods[$paymentMethod->getCode()] = $paymentMethod->getTitle();
  138. }
  139. }
  140. return $this->_paymentMethods;
  141. }
  142. /**
  143. * Retrieve wizard payment options array
  144. *
  145. * @return array
  146. */
  147. public function getWizardPaymentMethodOptions()
  148. {
  149. $paymentMethodOptions = [];
  150. foreach ($this->_helper->getBillingAgreementMethods() as $paymentMethod) {
  151. if ($paymentMethod->getConfigData('allow_billing_agreement_wizard') == 1) {
  152. $paymentMethodOptions[$paymentMethod->getCode()] = $paymentMethod->getTitle();
  153. }
  154. }
  155. return $paymentMethodOptions;
  156. }
  157. /**
  158. * Set data to block
  159. *
  160. * @return string
  161. */
  162. protected function _toHtml()
  163. {
  164. $this->setCreateUrl($this->getUrl('paypal/billing_agreement/startWizard'));
  165. return parent::_toHtml();
  166. }
  167. }