/**
  * @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();
     }
 }
 public function testGetConfigWithEmptyCustomer()
 {
     $customerId = 0;
     $expected = ['payment' => ['paypalBillingAgreement' => ['agreements' => [], 'transportName' => AbstractAgreement::TRANSPORT_BILLING_AGREEMENT_ID]]];
     $this->currentCustomerMock->expects($this->once())->method('getCustomerId')->willReturn($customerId);
     $this->agreementFactoryMock->expects($this->never())->method('create');
     $this->assertEquals($expected, $this->configProvider->getConfig());
 }
Example #3
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 #4
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;
 }
 /**
  * @param int $count
  * @dataProvider aroundIsApplicableDataProvider
  */
 public function testAroundIsApplicable($count)
 {
     $paymentMethod = $this->getPaymentMethod('paypal_billing_agreement');
     $quote = $this->getQuote(1);
     $proceed = $this->getProceedClosure(true, $paymentMethod, $quote);
     $agreementCollection = $this->getMock('Magento\\Paypal\\Model\\Resource\\Billing\\Agreement\\Collection', [], [], '', false);
     $agreementCollection->expects($this->once())->method('count')->will($this->returnValue($count));
     $agreement = $this->getMock('Magento\\Paypal\\Model\\Billing\\Agreement', [], [], '', false);
     $agreement->expects($this->once())->method('getAvailableCustomerBillingAgreements')->with(1)->will($this->returnValue($agreementCollection));
     $this->agreementFactory->expects($this->once())->method('create')->will($this->returnValue($agreement));
     $this->assertEquals($count > 0, $this->callAroundIsApplicable($proceed, $paymentMethod, $quote));
 }
 /**
  * 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 #7
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;
 }
 public function testAssignData()
 {
     $baId = '1678235';
     $customerId = 67;
     $referenceId = '1234124';
     $data = new DataObject([PaymentInterface::KEY_ADDITIONAL_DATA => [AbstractAgreement::TRANSPORT_BILLING_AGREEMENT_ID => $baId]]);
     $paymentInfo = $this->getMockBuilder(Payment::class)->disableOriginalConstructor()->getMock();
     $quote = $this->getMockBuilder(Quote::class)->disableOriginalConstructor()->setMethods(['__wakeup', 'getCustomerId'])->getMock();
     $this->payment->setInfoInstance($paymentInfo);
     $agreementModel = $this->getMockBuilder(Agreement::class)->disableOriginalConstructor()->setMethods(['__wakeup', 'load', 'getCustomerId', 'getId', 'getReferenceId'])->getMock();
     $this->agreementFactory->expects(static::once())->method('create')->willReturn($agreementModel);
     $paymentInfo->expects(static::once())->method('getQuote')->willReturn($quote);
     $agreementModel->expects(static::once())->method('load')->with($baId);
     $agreementModel->expects(static::once())->method('getId')->willReturn($baId);
     $agreementModel->expects(static::atLeastOnce())->method('getCustomerId')->willReturn($customerId);
     $agreementModel->expects(static::atLeastOnce())->method('getReferenceId')->willReturn($referenceId);
     $quote->expects(static::once())->method('getCustomerId')->willReturn($customerId);
     $paymentInfo->expects(static::exactly(2))->method('setAdditionalInformation')->willReturnMap([AbstractAgreement::TRANSPORT_BILLING_AGREEMENT_ID, $baId, AbstractAgreement::PAYMENT_INFO_REFERENCE_ID, $referenceId]);
     $this->payment->assignData($data);
 }
Example #11
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 #13
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;
 }