/**
  * @param EventObserver $observer
  * @return void
  */
 public function execute(EventObserver $observer)
 {
     /** @var \Magento\Sales\Model\Order\Payment $orderPayment */
     $orderPayment = $observer->getEvent()->getPayment();
     $agreementCreated = false;
     if ($orderPayment->getBillingAgreementData()) {
         $order = $orderPayment->getOrder();
         /** @var \Magento\Paypal\Model\Billing\Agreement $agreement */
         $agreement = $this->agreementFactory->create()->importOrderPayment($orderPayment);
         if ($agreement->isValid()) {
             $message = __('Created billing agreement #%1.', $agreement->getReferenceId());
             $order->addRelatedObject($agreement);
             $agreement->addOrderRelation($order);
             $this->checkoutSession->setLastBillingAgreementReferenceId($agreement->getReferenceId());
             $agreementCreated = true;
         } else {
             $message = __('We can\'t create a billing agreement for this order.');
         }
         $comment = $order->addStatusHistoryComment($message);
         $order->addRelatedObject($comment);
     }
     if (!$agreementCreated) {
         $this->checkoutSession->unsLastBillingAgreementReferenceId();
     }
 }
Example #2
0
 /**
  * Override check for Billing Agreements
  *
  * @param SpecificationInterface $specification
  * @param \Closure $proceed
  * @param PaymentMethodChecksInterface $paymentMethod
  * @param Quote $quote
  * @return bool
  * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  */
 public function aroundIsApplicable(SpecificationInterface $specification, \Closure $proceed, PaymentMethodChecksInterface $paymentMethod, Quote $quote)
 {
     $originallyIsApplicable = $proceed($paymentMethod, $quote);
     if (!$originallyIsApplicable || $paymentMethod->getCode() != 'paypal_billing_agreement' || !$quote->getCustomerId()) {
         return $originallyIsApplicable;
     }
     $availableBA = $this->_agreementFactory->create()->getAvailableCustomerBillingAgreements($quote->getCustomerId());
     return count($availableBA) > 0;
 }
Example #3
0
 /**
  * Check whether customer should be asked confirmation whether to sign a billing agreement
  *
  * @param \Magento\Paypal\Model\Config $config
  * @param int $customerId
  * @return bool
  */
 public function shouldAskToCreateBillingAgreement(\Magento\Paypal\Model\Config $config, $customerId)
 {
     if (null === self::$_shouldAskToCreateBillingAgreement) {
         self::$_shouldAskToCreateBillingAgreement = false;
         if ($customerId && $config->shouldAskToCreateBillingAgreement()) {
             if ($this->_agreementFactory->create()->needToCreateForCustomer($customerId)) {
                 self::$_shouldAskToCreateBillingAgreement = true;
             }
         }
     }
     return self::$_shouldAskToCreateBillingAgreement;
 }
 /**
  * Retrieve available customer billing agreements
  *
  * @return array
  */
 protected function getBillingAgreements()
 {
     $customerId = $this->currentCustomer->getCustomerId();
     $data = [];
     if (!$customerId) {
         return $data;
     }
     $collection = $this->agreementFactory->create()->getAvailableCustomerBillingAgreements($customerId);
     foreach ($collection as $item) {
         $data[] = ['id' => $item->getId(), 'referenceId' => $item->getReferenceId()];
     }
     return $data;
 }
Example #5
0
 /**
  * Retrieve available customer billing agreements
  *
  * @return array
  */
 public function getBillingAgreements()
 {
     $data = array();
     /** @var \Magento\Sales\Model\Quote $quote */
     $quote = $this->getParentBlock()->getQuote();
     if (!$quote || !$quote->getCustomerId()) {
         return $data;
     }
     $collection = $this->_agreementFactory->create()->getAvailableCustomerBillingAgreements($quote->getCustomerId());
     foreach ($collection as $item) {
         $data[$item->getId()] = $item->getReferenceId();
     }
     return $data;
 }
 /**
  * Return billing agreement information
  *
  * @return string
  */
 protected function _toHtml()
 {
     $agreementReferenceId = $this->_checkoutSession->getLastBillingAgreementReferenceId();
     $customerId = $this->_customerSession->getCustomerId();
     if (!$agreementReferenceId || !$customerId) {
         return '';
     }
     $agreement = $this->_agreementFactory->create()->load($agreementReferenceId, 'reference_id');
     if ($agreement->getId() && $customerId == $agreement->getCustomerId()) {
         $this->addData(['agreement_ref_id' => $agreement->getReferenceId(), 'agreement_url' => $this->getUrl('paypal/billing_agreement/view', ['agreement' => $agreement->getId()])]);
         return parent::_toHtml();
     }
     return '';
 }
 /**
  * Override check for Billing Agreements
  *
  * @param SpecificationInterface $specification
  * @param \Closure $proceed
  * @param MethodInterface $paymentMethod
  * @param Quote $quote
  * @return bool
  * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  */
 public function aroundIsApplicable(SpecificationInterface $specification, \Closure $proceed, MethodInterface $paymentMethod, Quote $quote)
 {
     $originallyIsApplicable = $proceed($paymentMethod, $quote);
     if (!$originallyIsApplicable) {
         return false;
     }
     if ($paymentMethod->getCode() == Config::METHOD_BILLING_AGREEMENT) {
         if ($quote->getCustomerId()) {
             $availableBA = $this->_agreementFactory->create()->getAvailableCustomerBillingAgreements($quote->getCustomerId());
             return count($availableBA) > 0;
         }
         return false;
     }
     return true;
 }
Example #8
0
 /**
  * Assign data to info model instance
  *
  * @param mixed $data
  * @return \Magento\Payment\Model\Info
  */
 public function assignData($data)
 {
     $result = parent::assignData($data);
     $key = self::TRANSPORT_BILLING_AGREEMENT_ID;
     $id = false;
     if (is_array($data) && isset($data[$key])) {
         $id = $data[$key];
     } elseif ($data instanceof \Magento\Framework\Object && $data->getData($key)) {
         $id = $data->getData($key);
     }
     if ($id) {
         $info = $this->getInfoInstance();
         $ba = $this->_agreementFactory->create()->load($id);
         if ($ba->getId() && $ba->getCustomerId() == $info->getQuote()->getCustomerId()) {
             $info->setAdditionalInformation($key, $id)->setAdditionalInformation(self::PAYMENT_INFO_REFERENCE_ID, $ba->getReferenceId());
         }
     }
     return $result;
 }
 /**
  * Assign data to info model instance
  *
  * @param \Magento\Framework\DataObject $data
  * @return \Magento\Payment\Model\Info
  * @throws \Magento\Framework\Exception\LocalizedException
  */
 public function assignData(\Magento\Framework\DataObject $data)
 {
     parent::assignData($data);
     $additionalData = $data->getData(PaymentInterface::KEY_ADDITIONAL_DATA);
     if (!is_array($additionalData) || !isset($additionalData[self::TRANSPORT_BILLING_AGREEMENT_ID])) {
         return $this;
     }
     $id = $additionalData[self::TRANSPORT_BILLING_AGREEMENT_ID];
     if (!$id || !is_numeric($id)) {
         return $this;
     }
     $info = $this->getInfoInstance();
     /** @var Agreement $ba */
     $ba = $this->_agreementFactory->create();
     $ba->load($id);
     if ($ba->getId() && $ba->getCustomerId() == $info->getQuote()->getCustomerId()) {
         $info->setAdditionalInformation(self::TRANSPORT_BILLING_AGREEMENT_ID, $id);
         $info->setAdditionalInformation(self::PAYMENT_INFO_REFERENCE_ID, $ba->getReferenceId());
     }
     return $this;
 }
Example #10
0
 /**
  * Set create billing agreement flag to api call
  *
  * @return $this
  */
 protected function _setBillingAgreementRequest()
 {
     if (!$this->_customerId) {
         return $this;
     }
     $isRequested = $this->_isBARequested || $this->_quote->getPayment()->getAdditionalInformation(self::PAYMENT_INFO_TRANSPORT_BILLING_AGREEMENT);
     if (!($this->_config->getValue('allow_ba_signup') == PaypalConfig::EC_BA_SIGNUP_AUTO || $isRequested && $this->_config->shouldAskToCreateBillingAgreement())) {
         return $this;
     }
     if (!$this->_agreementFactory->create()->needToCreateForCustomer($this->_customerId)) {
         return $this;
     }
     $this->_api->setBillingType($this->_api->getBillingAgreementType());
     return $this;
 }