Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Agreement.php
Go to the documentation of this file.
1 <?php
7 
9 
35 {
36  const STATUS_ACTIVE = 'active';
37 
38  const STATUS_CANCELED = 'canceled';
39 
45  protected $_relatedOrders = [];
46 
51 
55  protected $_dateFactory;
56 
67  public function __construct(
68  \Magento\Framework\Model\Context $context,
69  \Magento\Framework\Registry $registry,
70  \Magento\Payment\Helper\Data $paymentData,
71  \Magento\Paypal\Model\ResourceModel\Billing\Agreement\CollectionFactory $billingAgreementFactory,
72  \Magento\Framework\Stdlib\DateTime\DateTimeFactory $dateFactory,
73  \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
74  \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
75  array $data = []
76  ) {
77  parent::__construct($context, $registry, $paymentData, $resource, $resourceCollection, $data);
78  $this->_billingAgreementFactory = $billingAgreementFactory;
79  $this->_dateFactory = $dateFactory;
80  }
81 
87  protected function _construct()
88  {
89  $this->_init(\Magento\Paypal\Model\ResourceModel\Billing\Agreement::class);
90  }
91 
97  public function beforeSave()
98  {
99  $date = $this->_dateFactory->create()->gmtDate();
100  if ($this->isObjectNew() && !$this->getCreatedAt()) {
101  $this->setCreatedAt($date);
102  } else {
103  $this->setUpdatedAt($date);
104  }
105  return parent::beforeSave();
106  }
107 
113  public function afterSave()
114  {
115  if (!empty($this->_relatedOrders)) {
116  $this->_saveOrderRelations();
117  }
118  return parent::afterSave();
119  }
120 
126  public function getStatusLabel()
127  {
128  switch ($this->getStatus()) {
129  case self::STATUS_ACTIVE:
130  return __('Active');
132  return __('Canceled');
133  default:
134  return '';
135  }
136  }
137 
143  public function initToken()
144  {
145  $this->getPaymentMethodInstance()
146  ->initBillingAgreementToken($this);
147 
148  return $this->getRedirectUrl();
149  }
150 
157  public function verifyToken()
158  {
159  $this->getPaymentMethodInstance()
160  ->getBillingAgreementTokenInfo($this);
161 
162  return $this;
163  }
164 
170  public function place()
171  {
172  $this->verifyToken();
173 
174  $paymentMethodInstance = $this->getPaymentMethodInstance();
175  $paymentMethodInstance->placeBillingAgreement($this);
176 
177  $this->setCustomerId($this->getCustomerId())
178  ->setMethodCode($this->getMethodCode())
179  ->setReferenceId($this->getBillingAgreementId())
180  ->setStatus(self::STATUS_ACTIVE)
181  ->setAgreementLabel($paymentMethodInstance->getTitle())
182  ->save();
183 
184  return $this;
185  }
186 
192  public function cancel()
193  {
194  $this->setStatus(self::STATUS_CANCELED);
195  $this->getPaymentMethodInstance()->updateBillingAgreementStatus($this);
196  return $this->save();
197  }
198 
204  public function canCancel()
205  {
206  return $this->getStatus() != self::STATUS_CANCELED;
207  }
208 
214  public function getStatusesArray()
215  {
216  return [
217  self::STATUS_ACTIVE => __('Active'),
218  self::STATUS_CANCELED => __('Canceled')
219  ];
220  }
221 
227  public function isValid()
228  {
229  $result = parent::isValid();
230  if (!$this->getCustomerId()) {
231  $this->_errors[] = __('The customer ID is not set.');
232  }
233  if (!$this->getStatus()) {
234  $this->_errors[] = __('The Billing Agreement status is not set.');
235  }
236  return $result && empty($this->_errors);
237  }
238 
250  {
251  $baData = $payment->getBillingAgreementData();
252 
253  $this->_paymentMethodInstance = (isset($baData['method_code']))
254  ? $this->_paymentData->getMethodInstance($baData['method_code'])
255  : $payment->getMethodInstance();
256 
257  $this->_paymentMethodInstance->setStore($payment->getMethodInstance()->getStore());
258  $this->setCustomerId($payment->getOrder()->getCustomerId())
259  ->setMethodCode($this->_paymentMethodInstance->getCode())
260  ->setReferenceId($baData['billing_agreement_id'])
261  ->setStatus(self::STATUS_ACTIVE)
262  ->setAgreementLabel($this->_paymentMethodInstance->getTitle());
263 
264  return $this;
265  }
266 
274  {
275  $collection = $this->_billingAgreementFactory->create();
276  $collection->addFieldToFilter('customer_id', $customerId)
277  ->addFieldToFilter('status', self::STATUS_ACTIVE)
278  ->setOrder('agreement_id');
279  return $collection;
280  }
281 
289  {
290  return $customerId ? count($this->getAvailableCustomerBillingAgreements($customerId)) == 0 : false;
291  }
292 
299  public function addOrderRelation($orderId)
300  {
301  $this->_relatedOrders[] = $orderId;
302  return $this;
303  }
304 
310  protected function _saveOrderRelations()
311  {
312  foreach ($this->_relatedOrders as $order) {
313  $orderId = $order instanceof \Magento\Sales\Model\Order ? $order->getId() : (int)$order;
314  $this->getResource()->addOrderRelation($this->getId(), $orderId);
315  }
316  }
317 }
$order
Definition: order.php:55
__()
Definition: __.php:13
$resource
Definition: bulk.php:12
$payment
Definition: order.php:17
getAvailableCustomerBillingAgreements($customerId)
Definition: Agreement.php:273
__construct(\Magento\Framework\Model\Context $context, \Magento\Framework\Registry $registry, \Magento\Payment\Helper\Data $paymentData, \Magento\Paypal\Model\ResourceModel\Billing\Agreement\CollectionFactory $billingAgreementFactory, \Magento\Framework\Stdlib\DateTime\DateTimeFactory $dateFactory, \Magento\Framework\Model\ResourceModel\AbstractResource $resource=null, \Magento\Framework\Data\Collection\AbstractDb $resourceCollection=null, array $data=[])
Definition: Agreement.php:67