123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Paypal\Model\Billing;
- use Magento\Sales\Model\Order\Payment;
- /**
- * Billing Agreement abstract model
- *
- * @api
- * @method int getCustomerId()
- * @method \Magento\Paypal\Model\Billing\Agreement setCustomerId(int $value)
- * @method string getMethodCode()
- * @method \Magento\Paypal\Model\Billing\Agreement setMethodCode(string $value)
- * @method string getReferenceId()
- * @method \Magento\Paypal\Model\Billing\Agreement setReferenceId(string $value)
- * @method string getStatus()
- * @method \Magento\Paypal\Model\Billing\Agreement setStatus(string $value)
- * @method string getCreatedAt()
- * @method \Magento\Paypal\Model\Billing\Agreement setCreatedAt(string $value)
- * @method string getUpdatedAt()
- * @method \Magento\Paypal\Model\Billing\Agreement setUpdatedAt(string $value)
- * @method int getStoreId()
- * @method \Magento\Paypal\Model\Billing\Agreement setStoreId(int $value)
- * @method string getAgreementLabel()
- * @method \Magento\Paypal\Model\Billing\Agreement setAgreementLabel(string $value)
- *
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- * @since 100.0.2
- */
- class Agreement extends \Magento\Paypal\Model\Billing\AbstractAgreement
- {
- const STATUS_ACTIVE = 'active';
- const STATUS_CANCELED = 'canceled';
- /**
- * Related agreement orders
- *
- * @var array
- */
- protected $_relatedOrders = [];
- /**
- * @var \Magento\Paypal\Model\ResourceModel\Billing\Agreement\CollectionFactory
- */
- protected $_billingAgreementFactory;
- /**
- * @var \Magento\Framework\Stdlib\DateTime\DateTimeFactory
- */
- protected $_dateFactory;
- /**
- * @param \Magento\Framework\Model\Context $context
- * @param \Magento\Framework\Registry $registry
- * @param \Magento\Payment\Helper\Data $paymentData
- * @param \Magento\Paypal\Model\ResourceModel\Billing\Agreement\CollectionFactory $billingAgreementFactory
- * @param \Magento\Framework\Stdlib\DateTime\DateTimeFactory $dateFactory
- * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
- * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
- * @param array $data
- */
- public function __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 = []
- ) {
- parent::__construct($context, $registry, $paymentData, $resource, $resourceCollection, $data);
- $this->_billingAgreementFactory = $billingAgreementFactory;
- $this->_dateFactory = $dateFactory;
- }
- /**
- * Init model
- *
- * @return void
- */
- protected function _construct()
- {
- $this->_init(\Magento\Paypal\Model\ResourceModel\Billing\Agreement::class);
- }
- /**
- * Set created_at parameter
- *
- * @return \Magento\Framework\Model\AbstractModel
- */
- public function beforeSave()
- {
- $date = $this->_dateFactory->create()->gmtDate();
- if ($this->isObjectNew() && !$this->getCreatedAt()) {
- $this->setCreatedAt($date);
- } else {
- $this->setUpdatedAt($date);
- }
- return parent::beforeSave();
- }
- /**
- * Save agreement order relations
- *
- * @return \Magento\Framework\Model\AbstractModel
- */
- public function afterSave()
- {
- if (!empty($this->_relatedOrders)) {
- $this->_saveOrderRelations();
- }
- return parent::afterSave();
- }
- /**
- * Retrieve billing agreement status label
- *
- * @return \Magento\Framework\Phrase|string
- */
- public function getStatusLabel()
- {
- switch ($this->getStatus()) {
- case self::STATUS_ACTIVE:
- return __('Active');
- case self::STATUS_CANCELED:
- return __('Canceled');
- default:
- return '';
- }
- }
- /**
- * Initialize token
- *
- * @return string
- */
- public function initToken()
- {
- $this->getPaymentMethodInstance()
- ->initBillingAgreementToken($this);
- return $this->getRedirectUrl();
- }
- /**
- * Get billing agreement details
- * Data from response is inside this object
- *
- * @return $this
- */
- public function verifyToken()
- {
- $this->getPaymentMethodInstance()
- ->getBillingAgreementTokenInfo($this);
- return $this;
- }
- /**
- * Create billing agreement
- *
- * @return $this
- */
- public function place()
- {
- $this->verifyToken();
- $paymentMethodInstance = $this->getPaymentMethodInstance();
- $paymentMethodInstance->placeBillingAgreement($this);
- $this->setCustomerId($this->getCustomerId())
- ->setMethodCode($this->getMethodCode())
- ->setReferenceId($this->getBillingAgreementId())
- ->setStatus(self::STATUS_ACTIVE)
- ->setAgreementLabel($paymentMethodInstance->getTitle())
- ->save();
- return $this;
- }
- /**
- * Cancel billing agreement
- *
- * @return $this
- */
- public function cancel()
- {
- $this->setStatus(self::STATUS_CANCELED);
- $this->getPaymentMethodInstance()->updateBillingAgreementStatus($this);
- return $this->save();
- }
- /**
- * Check whether can cancel billing agreement
- *
- * @return bool
- */
- public function canCancel()
- {
- return $this->getStatus() != self::STATUS_CANCELED;
- }
- /**
- * Retrieve billing agreement statuses array
- *
- * @return array
- */
- public function getStatusesArray()
- {
- return [
- self::STATUS_ACTIVE => __('Active'),
- self::STATUS_CANCELED => __('Canceled')
- ];
- }
- /**
- * Validate data
- *
- * @return bool
- */
- public function isValid()
- {
- $result = parent::isValid();
- if (!$this->getCustomerId()) {
- $this->_errors[] = __('The customer ID is not set.');
- }
- if (!$this->getStatus()) {
- $this->_errors[] = __('The Billing Agreement status is not set.');
- }
- return $result && empty($this->_errors);
- }
- /**
- * Import payment data to billing agreement
- *
- * $payment->getBillingAgreementData() contains array with following structure :
- * [billing_agreement_id] => string
- * [method_code] => string
- *
- * @param Payment $payment
- * @return $this
- */
- public function importOrderPayment(Payment $payment)
- {
- $baData = $payment->getBillingAgreementData();
- $this->_paymentMethodInstance = (isset($baData['method_code']))
- ? $this->_paymentData->getMethodInstance($baData['method_code'])
- : $payment->getMethodInstance();
- $this->_paymentMethodInstance->setStore($payment->getMethodInstance()->getStore());
- $this->setCustomerId($payment->getOrder()->getCustomerId())
- ->setMethodCode($this->_paymentMethodInstance->getCode())
- ->setReferenceId($baData['billing_agreement_id'])
- ->setStatus(self::STATUS_ACTIVE)
- ->setAgreementLabel($this->_paymentMethodInstance->getTitle());
- return $this;
- }
- /**
- * Retrieve available customer Billing Agreements
- *
- * @param int $customerId
- * @return \Magento\Paypal\Model\ResourceModel\Billing\Agreement\Collection
- */
- public function getAvailableCustomerBillingAgreements($customerId)
- {
- $collection = $this->_billingAgreementFactory->create();
- $collection->addFieldToFilter('customer_id', $customerId)
- ->addFieldToFilter('status', self::STATUS_ACTIVE)
- ->setOrder('agreement_id');
- return $collection;
- }
- /**
- * Check whether need to create billing agreement for customer
- *
- * @param int $customerId
- * @return bool
- */
- public function needToCreateForCustomer($customerId)
- {
- return $customerId ? count($this->getAvailableCustomerBillingAgreements($customerId)) == 0 : false;
- }
- /**
- * Add order relation to current billing agreement
- *
- * @param int|\Magento\Sales\Model\Order $orderId
- * @return $this
- */
- public function addOrderRelation($orderId)
- {
- $this->_relatedOrders[] = $orderId;
- return $this;
- }
- /**
- * Save related orders
- *
- * @return void
- */
- protected function _saveOrderRelations()
- {
- foreach ($this->_relatedOrders as $order) {
- $orderId = $order instanceof \Magento\Sales\Model\Order ? $order->getId() : (int)$order;
- $this->getResource()->addOrderRelation($this->getId(), $orderId);
- }
- }
- }
|