/**
  * @dataProvider processBraintreePaymentSkipDataProvider
  */
 public function testProcessBraintreePaymentSkip($config)
 {
     $index = 0;
     foreach ($config as $key => $value) {
         $this->configMock->expects($this->at($index))->method('getConfigData')->with($key)->willReturn($value);
         $index++;
     }
     $paymentObj = new \Magento\Framework\DataObject(['method' => PaymentMethod::METHOD_CODE]);
     $orderMock = $this->getMockBuilder('\\Magento\\Sales\\Model\\Order')->disableOriginalConstructor()->getMock();
     $orderMock->expects($this->once())->method('getPayment')->willReturn($paymentObj);
     $orderMock->expects($this->once())->method('canInvoice')->willReturn(true);
     $observer = new \Magento\Framework\Event\Observer(['event' => new \Magento\Framework\DataObject(['shipment' => new \Magento\Framework\DataObject(['order' => $orderMock])])]);
     $this->transactionFactoryMock->expects($this->never())->method('create');
     $this->assertEquals($this->processBraintreePaymentObserver, $this->processBraintreePaymentObserver->execute($observer));
 }
 public function testInitEnvironment()
 {
     $prefix = 'payment/braintree/';
     $storeId = 2;
     $environment2 = \Magento\Braintree\Model\Source\Environment::ENVIRONMENT_SANDBOX;
     $merchantId2 = 'merchantId_2';
     $publicKey2 = 'public_key_2';
     $privateKey2 = 'private_key_2';
     $merchantAccountId2 = 'merchantAccountId_2';
     $clientToken2 = 'clientToken_2';
     $this->scopeConfigMock->expects($this->at(0))->method('getValue')->with($prefix . Config::KEY_ACTIVE, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, null)->willReturn(1);
     //for the initEnvironment call
     $this->scopeConfigMock->expects($this->at(6))->method('getValue')->with($prefix . Config::KEY_ENVIRONMENT, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $storeId)->willReturn($environment2);
     $this->scopeConfigMock->expects($this->at(7))->method('getValue')->with($prefix . Config::KEY_MERCHANT_ID, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $storeId)->willReturn($merchantId2);
     $this->scopeConfigMock->expects($this->at(8))->method('getValue')->with($prefix . Config::KEY_PUBLIC_KEY, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $storeId)->willReturn($publicKey2);
     $this->scopeConfigMock->expects($this->at(9))->method('getValue')->with($prefix . Config::KEY_PRIVATE_KEY, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $storeId)->willReturn($privateKey2);
     $this->scopeConfigMock->expects($this->at(10))->method('getValue')->with($prefix . Config::KEY_MERCHANT_ACCOUNT_ID, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $storeId)->willReturn($merchantAccountId2);
     //for the initEnvironment call
     $this->braintreeConfigurationMock->expects($this->at(4))->method('environment')->with($environment2);
     $this->braintreeConfigurationMock->expects($this->at(5))->method('merchantId')->with($merchantId2);
     $this->braintreeConfigurationMock->expects($this->at(6))->method('publicKey')->with($publicKey2);
     $this->braintreeConfigurationMock->expects($this->at(7))->method('privateKey')->with($privateKey2);
     $this->model = new Config($this->scopeConfigMock, $this->braintreeConfigurationMock, $this->braintreeClientTokenMock, $this->sourceCountryMock);
     $this->model->initEnvironment($storeId);
     $this->assertEquals($merchantAccountId2, $this->model->getMerchantAccountId());
     $this->braintreeClientTokenMock->expects($this->once())->method('generate')->willReturn($clientToken2);
     $this->assertEquals($clientToken2, $this->model->getClientToken());
     //second call will return cached version
     $this->assertEquals($clientToken2, $this->model->getClientToken());
 }
Exemple #3
0
 /**
  * Save config section
  * Require set: section, website, store and groups
  *
  * @throws \Exception
  * @return $this
  */
 public function save()
 {
     $this->initScope();
     $sectionId = $this->getSection();
     $groups = $this->getGroups();
     if (empty($groups)) {
         return $this;
     }
     $oldConfig = $this->_getConfig(true);
     $deleteTransaction = $this->_transactionFactory->create();
     /* @var $deleteTransaction \Magento\Framework\DB\Transaction */
     $saveTransaction = $this->_transactionFactory->create();
     /* @var $saveTransaction \Magento\Framework\DB\Transaction */
     // Extends for old config data
     $extraOldGroups = array();
     foreach ($groups as $groupId => $groupData) {
         $this->_processGroup($groupId, $groupData, $groups, $sectionId, $extraOldGroups, $oldConfig, $saveTransaction, $deleteTransaction);
     }
     try {
         $deleteTransaction->delete();
         $saveTransaction->save();
         // re-init configuration
         $this->_appConfig->reinit();
         $this->_storeManager->reinitStores();
         // website and store codes can be used in event implementation, so set them as well
         $this->_eventManager->dispatch("admin_system_config_changed_section_{$this->getSection()}", array('website' => $this->getWebsite(), 'store' => $this->getStore()));
     } catch (\Exception $e) {
         // re-init configuration
         $this->_appConfig->reinit();
         $this->_storeManager->reinitStores();
         throw $e;
     }
     return $this;
 }
Exemple #4
0
 /**
  * Process Configured Payment Action: Direct Sale, create invoice if state is Pending
  *
  * @return void
  */
 protected function _processDirectSale()
 {
     $order = $this->_getOrder();
     $status = $this->getRequest()->getParam('STATUS');
     try {
         if ($status == \Magento\Ogone\Model\Api::OGONE_AUTH_PROCESSING) {
             $order->setState(\Magento\Sales\Model\Order::STATE_PROCESSING, \Magento\Ogone\Model\Api::WAITING_AUTHORIZATION, __('Authorization Waiting from Ogone'));
             $order->save();
         } elseif ($order->getState() == \Magento\Sales\Model\Order::STATE_PENDING_PAYMENT) {
             if ($status == \Magento\Ogone\Model\Api::OGONE_AUTHORIZED) {
                 if ($order->getStatus() != \Magento\Sales\Model\Order::STATE_PENDING_PAYMENT) {
                     $order->setState(\Magento\Sales\Model\Order::STATE_PROCESSING, \Magento\Ogone\Model\Api::PROCESSING_OGONE_STATUS, __('Processed by Ogone'));
                 }
             } else {
                 $order->setState(\Magento\Sales\Model\Order::STATE_PROCESSING, \Magento\Ogone\Model\Api::PROCESSED_OGONE_STATUS, __('Processed by Ogone'));
             }
             if (!$order->getInvoiceCollection()->getSize()) {
                 $invoice = $order->prepareInvoice();
                 $invoice->register();
                 $invoice->setState(\Magento\Sales\Model\Order\Invoice::STATE_PAID);
                 $invoice->getOrder()->setIsInProcess(true);
                 $this->_transactionFactory->create()->addObject($invoice)->addObject($invoice->getOrder())->save();
                 $order->sendNewOrderEmail();
             }
         } else {
             $order->save();
         }
         $this->_redirect('checkout/onepage/success');
         return;
     } catch (\Exception $e) {
         $this->messageManager->addError(__('Order can\'t save'));
         $this->_redirect('checkout/cart');
         return;
     }
 }
Exemple #5
0
 /**
  * @param \Magento\Sales\Model\Order\Item $orderItem
  * @return void
  */
 protected function refundOrder(\Magento\Sales\Model\Order\Item $orderItem)
 {
     $creditmemoLoader = $this->creditmemoLoaderFactory->create();
     $creditmemoLoader->setOrderId($orderItem->getOrderId());
     $creditmemoLoader->setCreditmemo($this->getCreditmemoData($orderItem));
     $creditmemo = $creditmemoLoader->load();
     if ($creditmemo && $creditmemo->isValidGrandTotal()) {
         $creditmemo->setOfflineRequested(true);
         $creditmemo->register();
         $creditmemoTransaction = $this->transactionFactory->create()->addObject($creditmemo)->addObject($creditmemo->getOrder());
         $creditmemoTransaction->save();
     }
 }
 /**
  * Delete design configuration from storage
  *
  * @param DesignConfigInterface $designConfig
  * @return void
  */
 public function delete(DesignConfigInterface $designConfig)
 {
     $fieldsData = $designConfig->getExtensionAttributes()->getDesignConfigData();
     /* @var $deleteTransaction \Magento\Framework\DB\Transaction */
     $deleteTransaction = $this->transactionFactory->create();
     foreach ($fieldsData as $fieldData) {
         /** @var ValueInterface|Value $backendModel */
         $backendModel = $this->backendModelFactory->create(['value' => $fieldData->getValue(), 'scope' => $designConfig->getScope(), 'scopeId' => $designConfig->getScopeId(), 'config' => $fieldData->getFieldConfig()]);
         if (!$backendModel->isObjectNew()) {
             $deleteTransaction->addObject($backendModel);
         }
     }
     $deleteTransaction->delete();
 }
 /**
  * @param \Magento\Sales\Model\Order\Item $orderItem
  * @return void
  */
 protected function shipOrder(\Magento\Sales\Model\Order\Item $orderItem)
 {
     $shipmentLoader = $this->shipmentLoaderFactory->create();
     $shipmentData = [$orderItem->getId() => $orderItem->getQtyToShip()];
     $shipmentLoader->setOrderId($orderItem->getOrderId());
     $shipmentLoader->setShipment($shipmentData);
     $shipment = $shipmentLoader->load();
     if ($shipment) {
         $shipment->register();
         $shipment->getOrder()->setIsInProcess(true);
         $shipmentTransaction = $this->transactionFactory->create()->addObject($shipment)->addObject($shipment->getOrder());
         $shipmentTransaction->save();
     }
 }
Exemple #8
0
 /**
  * If it's configured to capture on shipment - do this
  * 
  * @param \Magento\Framework\Object $observer
  * @return $this
  */
 public function processBraintreePayment(\Magento\Framework\Object $observer)
 {
     $shipment = $observer->getEvent()->getShipment();
     $order = $shipment->getOrder();
     if ($order->getPayment()->getMethod() == PaymentMethod::METHOD_CODE && $order->canInvoice() && $this->shouldInvoice()) {
         $qtys = [];
         foreach ($shipment->getAllItems() as $shipmentItem) {
             $qtys[$shipmentItem->getOrderItem()->getId()] = $shipmentItem->getQty();
         }
         foreach ($order->getAllItems() as $orderItem) {
             if (!array_key_exists($orderItem->getId(), $qtys)) {
                 $qtys[$orderItem->getId()] = 0;
             }
         }
         $invoice = $order->prepareInvoice($qtys);
         $invoice->setRequestedCaptureCase(\Magento\Sales\Model\Order\Invoice::CAPTURE_ONLINE);
         $invoice->register();
         /** @var \Magento\Framework\DB\Transaction $transaction */
         $transaction = $this->transactionFactory->create();
         $transaction->addObject($invoice)->addObject($invoice->getOrder())->save();
     }
     return $this;
 }
 public function testDelete()
 {
     $scope = 'website';
     $scopeId = 1;
     $backendModel = $this->getMockBuilder('Magento\\Framework\\App\\Config\\Value')->disableOriginalConstructor()->getMock();
     $this->designConfig->expects($this->once())->method('getExtensionAttributes')->willReturn($this->designConfigExtension);
     $this->designConfigExtension->expects($this->once())->method('getDesignConfigData')->willReturn([$this->designConfigData]);
     $this->transactionFactoryMock->expects($this->once())->method('create')->willReturn($this->transactionMock);
     $this->designConfigData->expects($this->once())->method('getValue')->willReturn('value');
     $this->designConfigData->expects($this->once())->method('getFieldConfig')->willReturn([]);
     $this->designConfig->expects($this->once())->method('getScope')->willReturn($scope);
     $this->designConfig->expects($this->once())->method('getScopeId')->willReturn($scopeId);
     $this->backendModelFactoryMock->expects($this->once())->method('create')->with(['value' => 'value', 'scope' => $scope, 'scopeId' => $scopeId, 'config' => []])->willReturn($backendModel);
     $this->transactionMock->expects($this->once())->method('addObject')->with($backendModel);
     $this->transactionMock->expects($this->once())->method('delete');
     $this->model->delete($this->designConfig);
 }
Exemple #10
0
 /**
  * Create shipment
  *
  * @throws bool
  */
 protected function _createShipment()
 {
     $this->_adyenLogger->addAdyenNotificationCronjob('Creating shipment for order');
     // create shipment for cash payment
     $payment = $this->_order->getPayment()->getMethodInstance();
     if ($this->_order->canShip()) {
         $itemQty = [];
         $shipment = $this->_order->prepareShipment($itemQty);
         if ($shipment) {
             $shipment->register();
             $shipment->getOrder()->setIsInProcess(true);
             $comment = __('Shipment created by Adyen');
             $shipment->addComment($comment);
             /** @var \Magento\Framework\DB\Transaction $transaction */
             $transaction = $this->_transactionFactory->create();
             $transaction->addObject($shipment)->addObject($shipment->getOrder())->save();
             $this->_adyenLogger->addAdyenNotificationCronjob('Order is shipped');
         }
     } else {
         $this->_adyenLogger->addAdyenNotificationCronjob('Order can\'t be shipped');
     }
 }
Exemple #11
0
 /**
  *
  */
 protected function _createShipment()
 {
     $this->_debugData['_createShipment'] = 'Creating shipment for order';
     // create shipment for cash payment
     $payment = $this->_order->getPayment()->getMethodInstance();
     if ($this->_order->canShip()) {
         $itemQty = array();
         $shipment = $this->_order->prepareShipment($itemQty);
         if ($shipment) {
             $shipment->register();
             $shipment->getOrder()->setIsInProcess(true);
             $comment = __('Shipment created by Adyen');
             $shipment->addComment($comment);
             /** @var \Magento\Framework\DB\Transaction $transaction */
             $transaction = $this->_transactionFactory->create();
             $transaction->addObject($shipment)->addObject($shipment->getOrder())->save();
             $this->_debugData['_createShipment done'] = 'Order is shipped';
         }
     } else {
         $this->_debugData['_createShipment error'] = 'Order can\'t be shipped';
     }
 }
Exemple #12
0
 protected function _processInvoice()
 {
     if ($this->_order->canInvoice()) {
         $invoice = $this->_order->prepareInvoice();
         switch ($this->_paymentInst->getConfigPaymentAction()) {
             case \Magento\Payment\Model\Method\AbstractMethod::ACTION_AUTHORIZE:
                 $invoice->register();
                 $this->_order->setState(\Magento\Sales\Model\Order::STATE_PROCESSING, 'authorized');
                 break;
             case \Magento\Payment\Model\Method\AbstractMethod::ACTION_AUTHORIZE_CAPTURE:
                 $this->_paymentInst->setAssistCaptureResponse(true);
                 $invoice->register()->capture();
                 break;
         }
         /** @var \Magento\Framework\DB\Transaction $transaction */
         $transaction = $this->_transactionFactory->create();
         $transaction->addObject($invoice)->addObject($invoice->getOrder())->save();
         $this->_invoiceSender->send($invoice);
     } elseif ($this->_order->isCanceled()) {
         throw new \Magento\Framework\Exception\LocalizedException(__('Order canceled'));
     } else {
         throw new \Magento\Framework\Exception\LocalizedException(__('Order paid'));
     }
 }
Exemple #13
0
 /**
  * Submit the quote. Quote submit process will create the order based on quote data
  *
  * @return \Magento\Sales\Model\Order
  * @throws \Exception
  */
 public function submitOrderWithDataObject()
 {
     $this->_deleteNominalItems();
     $this->_validate();
     $quote = $this->_quote;
     $isVirtual = $quote->isVirtual();
     $transaction = $this->_transactionFactory->create();
     $customerData = null;
     if (!$quote->getCustomerIsGuest()) {
         $customerData = $quote->getCustomerData();
         $addresses = $quote->getCustomerAddressData();
         $customerDetails = $this->_customerDetailsBuilder->setCustomer($customerData)->setAddresses($addresses)->create();
         if ($customerData->getId()) {
             $this->_customerAccountService->updateCustomer($customerDetails);
         } else {
             //for new customers
             $customerData = $this->_customerAccountService->createCustomerWithPasswordHash($customerDetails, $quote->getPasswordHash());
             $addresses = $this->_customerAddressService->getAddresses($customerData->getId());
             //Update quote address information
             foreach ($addresses as $address) {
                 if ($address->isDefaultBilling()) {
                     $quote->getBillingAddress()->setCustomerAddressData($address);
                 } else {
                     if ($address->isDefaultShipping()) {
                         $quote->getShippingAddress()->setCustomerAddressData($address);
                     }
                 }
             }
             if ($quote->getShippingAddress() && $quote->getShippingAddress()->getSameAsBilling()) {
                 $quote->getShippingAddress()->setCustomerAddressData($quote->getBillingAddress()->getCustomerAddressData());
             }
         }
         $quote->setCustomerData($customerData)->setCustomerAddressData($addresses);
     }
     $transaction->addObject($quote);
     $quote->reserveOrderId();
     if ($isVirtual) {
         $order = $this->_convertor->addressToOrder($quote->getBillingAddress());
     } else {
         $order = $this->_convertor->addressToOrder($quote->getShippingAddress());
     }
     $order->setBillingAddress($this->_convertor->addressToOrderAddress($quote->getBillingAddress()));
     if ($quote->getBillingAddress()->getCustomerAddressData()) {
         $order->getBillingAddress()->setCustomerAddressData($quote->getBillingAddress()->getCustomerAddressData());
     }
     if (!$isVirtual) {
         $order->setShippingAddress($this->_convertor->addressToOrderAddress($quote->getShippingAddress()));
         if ($quote->getShippingAddress()->getCustomerAddressData()) {
             $order->getShippingAddress()->setCustomerAddressData($quote->getShippingAddress()->getCustomerAddressData());
         }
     }
     $order->setPayment($this->_convertor->paymentToOrderPayment($quote->getPayment()));
     foreach ($this->_orderData as $key => $value) {
         $order->setData($key, $value);
     }
     foreach ($quote->getAllItems() as $item) {
         $orderItem = $this->_convertor->itemToOrderItem($item);
         if ($item->getParentItem()) {
             $orderItem->setParentItem($order->getItemByQuoteItemId($item->getParentItem()->getId()));
         }
         $order->addItem($orderItem);
     }
     if ($customerData) {
         $order->setCustomerId($customerData->getId());
     }
     $order->setQuote($quote);
     $transaction->addObject($order);
     $transaction->addCommitCallback(array($order, 'place'));
     $transaction->addCommitCallback(array($order, 'save'));
     /**
      * We can use configuration data for declare new order status
      */
     $this->_eventManager->dispatch('checkout_type_onepage_save_order', array('order' => $order, 'quote' => $quote));
     $this->_eventManager->dispatch('sales_model_service_quote_submit_before', array('order' => $order, 'quote' => $quote));
     try {
         $transaction->save();
         $this->_inactivateQuote();
         $this->_eventManager->dispatch('sales_model_service_quote_submit_success', array('order' => $order, 'quote' => $quote));
     } catch (\Exception $e) {
         //reset order ID's on exception, because order not saved
         $order->setId(null);
         /** @var $item \Magento\Sales\Model\Order\Item */
         foreach ($order->getItemsCollection() as $item) {
             $item->setOrderId(null);
             $item->setItemId(null);
         }
         $this->_eventManager->dispatch('sales_model_service_quote_submit_failure', array('order' => $order, 'quote' => $quote));
         throw $e;
     }
     $this->_order = $order;
     return $order;
 }
Exemple #14
0
 protected function _createInvoice($order, $message)
 {
     if (!$order->hasInvoices()) {
         $invoice = $order->prepareInvoice();
         $invoice->register();
         $invoice->pay();
         $this->_transactionFactory->create()->addObject($invoice)->addObject($invoice->getOrder())->save();
         $this->_invoiceSender->send($invoice, true, $message);
     }
 }