/** * Function for setting Config value of current store * @param string $path, * @param string $value, */ public function setCurrentStoreConfigValue($path, $value) { $data = ['path' => $path, 'scope' => 'stores', 'scope_id' => $this->_storeId, 'scope_code' => $this->_storeCode, 'value' => $value]; $this->_backendModel->addData($data); $this->_transaction->addObject($this->_backendModel); $this->_transaction->save(); }
/** * @magentoAppArea adminhtml */ public function testSaveDelete() { $first = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create('Magento\\Store\\Model\\Group'); $first->setData(['website_id' => 1, 'name' => 'test 1', 'root_category_id' => 1, 'default_store_id' => 1]); $second = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create('Magento\\Store\\Model\\Group'); $second->setData(['website_id' => 1, 'name' => 'test 2', 'root_category_id' => 1, 'default_store_id' => 1]); $first->save(); $this->_model->addObject($first)->addObject($second, 'second'); $this->_model->save(); $this->assertNotEmpty($first->getId()); $this->assertNotEmpty($second->getId()); $this->_model->delete(); $test = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create('Magento\\Store\\Model\\Group'); $test->load($first->getId()); $this->assertEmpty($test->getId()); }
/** * @magentoAppArea adminhtml */ public function testSaveDelete() { /** @var Flag $first */ $first = $this->objectManager->create(Flag::class, ['data' => ['flag_code' => 'test1']]); $first->setFlagData('test1data'); $second = $this->objectManager->create(Flag::class, ['data' => ['flag_code' => 'test2']]); $second->setFlagData('test2data'); $first->save(); $this->_model->addObject($first)->addObject($second, 'second'); $this->_model->save(); $this->assertNotEmpty($first->getId()); $this->assertNotEmpty($second->getId()); $this->_model->delete(); $test = $this->objectManager->create(Flag::class); $test->load($first->getId()); $this->assertEmpty($test->getId()); }
protected function prepareShipment() { // Skip shipment observer // --------------------------------------- $this->getHelper('Data\\GlobalData')->unsetValue('skip_shipment_observer'); $this->getHelper('Data\\GlobalData')->setValue('skip_shipment_observer', true); // --------------------------------------- $qtys = array(); foreach ($this->magentoOrder->getAllItems() as $item) { $qtyToShip = $item->getQtyToShip(); if ($qtyToShip == 0) { continue; } $qtys[$item->getId()] = $qtyToShip; } // Create shipment // --------------------------------------- $this->shipment = $this->shipmentFactory->create($this->magentoOrder); $this->shipment->register(); // it is necessary for updating qty_shipped field in sales_flat_order_item table $this->shipment->getOrder()->setIsInProcess(true); $this->transaction->addObject($this->shipment)->addObject($this->shipment->getOrder())->save(); // --------------------------------------- }
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); }
/** * @param \Magento\Framework\Event\Observer $observer * * @throws \Exception * @throws bool */ public function execute(\Magento\Framework\Event\Observer $observer) { $merchantOrder = $observer->getMerchantOrder(); if (!count($merchantOrder['shipments']) > 0) { return; } $data = $observer->getPayment(); $order = $this->_coreModel->_getOrder($data["external_reference"]); //if order has shipments, status is updated. If it doesn't the shipment is created. if ($merchantOrder['shipments'][0]['status'] == 'ready_to_ship') { if ($order->hasShipments()) { $shipment = $this->_shipment->load($order->getId(), 'order_id'); } else { $shipment = $this->_shipmentFactory->create($order); $order->setIsInProcess(true); } $shipment->setShippingLabel($merchantOrder['shipments'][0]['id']); $shipmentInfo = $this->_shipmentHelper->getShipmentInfo($merchantOrder['shipments'][0]['id']); $this->_coreHelper->log("Shipment Info", 'mercadopago-notification.log', $shipmentInfo); $serviceInfo = $this->_shipmentHelper->getServiceInfo($merchantOrder['shipments'][0]['service_id'], $merchantOrder['site_id']); $this->_coreHelper->log("Service Info by service id", 'mercadopago-notification.log', $serviceInfo); if ($shipmentInfo && isset($shipmentInfo->tracking_number)) { $tracking['number'] = $shipmentInfo->tracking_number; $tracking['description'] = str_replace('#{trackingNumber}', $shipmentInfo->tracking_number, $serviceInfo->tracking_url); $tracking['title'] = self::CODE; $existingTracking = $this->_trackFactory->create()->load($shipment->getOrderId(), 'order_id'); if ($existingTracking->getId()) { $track = $shipment->getTrackById($existingTracking->getId()); $track->setNumber($tracking['number'])->setDescription($tracking['description'])->setTitle($tracking['title'])->save(); } else { $track = $this->_trackFactory->create()->addData($tracking); $track->setCarrierCode(\MercadoPago\MercadoEnvios\Model\Carrier\MercadoEnvios::CODE); $shipment->addTrack($track); $shipment->save(); } $this->_coreHelper->log("Track added", 'mercadopago-notification.log', $track); } $this->_transaction->addObject($order)->save(); } }
/** * Process group data * * @param string $groupId * @param array $groupData * @param array $groups * @param string $sectionPath * @param array &$extraOldGroups * @param array &$oldConfig * @param \Magento\Framework\DB\Transaction $saveTransaction * @param \Magento\Framework\DB\Transaction $deleteTransaction * @return void */ protected function _processGroup($groupId, array $groupData, array $groups, $sectionPath, array &$extraOldGroups, array &$oldConfig, \Magento\Framework\DB\Transaction $saveTransaction, \Magento\Framework\DB\Transaction $deleteTransaction) { $groupPath = $sectionPath . '/' . $groupId; $scope = $this->getScope(); $scopeId = $this->getScopeId(); $scopeCode = $this->getScopeCode(); /** * * Map field names if they were cloned */ /** @var $group \Magento\Backend\Model\Config\Structure\Element\Group */ $group = $this->_configStructure->getElement($groupPath); // set value for group field entry by fieldname // use extra memory $fieldsetData = array(); if (isset($groupData['fields'])) { if ($group->shouldCloneFields()) { $cloneModel = $group->getCloneModel(); $mappedFields = array(); /** @var $field \Magento\Backend\Model\Config\Structure\Element\Field */ foreach ($group->getChildren() as $field) { foreach ($cloneModel->getPrefixes() as $prefix) { $mappedFields[$prefix['field'] . $field->getId()] = $field->getId(); } } } foreach ($groupData['fields'] as $fieldId => $fieldData) { $fieldsetData[$fieldId] = is_array($fieldData) && isset($fieldData['value']) ? $fieldData['value'] : null; } foreach ($groupData['fields'] as $fieldId => $fieldData) { $originalFieldId = $fieldId; if ($group->shouldCloneFields() && isset($mappedFields[$fieldId])) { $originalFieldId = $mappedFields[$fieldId]; } /** @var $field \Magento\Backend\Model\Config\Structure\Element\Field */ $field = $this->_configStructure->getElement($groupPath . '/' . $originalFieldId); /** @var \Magento\Framework\App\Config\ValueInterface $backendModel */ $backendModel = $field->hasBackendModel() ? $field->getBackendModel() : $this->_configValueFactory->create(); $data = array('field' => $fieldId, 'groups' => $groups, 'group_id' => $group->getId(), 'scope' => $scope, 'scope_id' => $scopeId, 'scope_code' => $scopeCode, 'field_config' => $field->getData(), 'fieldset_data' => $fieldsetData); $backendModel->addData($data); $this->_checkSingleStoreMode($field, $backendModel); if (false == isset($fieldData['value'])) { $fieldData['value'] = null; } $path = $field->getGroupPath() . '/' . $fieldId; /** * Look for custom defined field path */ if ($field && $field->getConfigPath()) { $configPath = $field->getConfigPath(); if (!empty($configPath) && strrpos($configPath, '/') > 0) { // Extend old data with specified section group $configGroupPath = substr($configPath, 0, strrpos($configPath, '/')); if (!isset($extraOldGroups[$configGroupPath])) { $oldConfig = $this->extendConfig($configGroupPath, true, $oldConfig); $extraOldGroups[$configGroupPath] = true; } $path = $configPath; } } $inherit = !empty($fieldData['inherit']); $backendModel->setPath($path)->setValue($fieldData['value']); if (isset($oldConfig[$path])) { $backendModel->setConfigId($oldConfig[$path]['config_id']); /** * Delete config data if inherit */ if (!$inherit) { $saveTransaction->addObject($backendModel); } else { $deleteTransaction->addObject($backendModel); } } elseif (!$inherit) { $backendModel->unsConfigId(); $saveTransaction->addObject($backendModel); } } } if (isset($groupData['groups'])) { foreach ($groupData['groups'] as $subGroupId => $subGroupData) { $this->_processGroup($subGroupId, $subGroupData, $groups, $groupPath, $extraOldGroups, $oldConfig, $saveTransaction, $deleteTransaction); } } }
protected function _loadCache() { if (!($raw_input = @file_get_contents('php://input'))) { $this->_s2pLogger->write('No input'); echo 'No input'; exit; } $this->_s2pLogger->write(' ### Notification START'); $method_config = $this->_s2pModel->getFullConfigArray(); parse_str($raw_input, $response); $recomposedHashString = ''; if (!empty($raw_input)) { $pairs = explode('&', $raw_input); foreach ($pairs as $pair) { $nv = explode("=", $pair, 2); if (!isset($nv[1])) { continue; } if (strtolower($nv[0]) != 'hash') { $recomposedHashString .= $nv[0] . $nv[1]; } } } $recomposedHashString .= $method_config['signature']; $this->_s2pLogger->write('NotificationRecevied: "' . $raw_input . '"'); if (empty($response['Hash'])) { $response['Hash'] = ''; } if (empty($response['StatusID'])) { $response['StatusID'] = 0; } $log_message = ''; /* @var \Magento\Sales\Model\Order $order */ $order = $this->_orderFactory->create(); // Message is intact if ($this->_helper->computeSHA256Hash($recomposedHashString) != $response['Hash']) { $this->_s2pLogger->write('Hashes do not match! received: [' . $response['Hash'] . '] recomposed [' . $this->_helper->computeSHA256Hash($recomposedHashString) . ']', 'error'); } elseif (empty($response['MerchantTransactionID'])) { $this->_s2pLogger->write('Unknown merchant transaction ID in request', 'error'); } elseif (!$order->loadByIncrementId($response['MerchantTransactionID']) or !$order->getEntityId()) { $this->_s2pLogger->write('Unknown order', 'error'); } else { $this->_s2pLogger->write('Hashes match'); $order->addStatusHistoryComment('Smart2Pay notification : "' . $raw_input . '"'); /** * Check status ID */ switch ($response['StatusID']) { case \Smart2Pay\GlobalPay\Model\Smart2Pay::S2P_STATUS_OPEN: if (!empty($response['MethodID']) and $method_config['notify_payment_instructions'] and in_array($response['MethodID'], [\Smart2Pay\GlobalPay\Model\Smart2Pay::PAYMENT_METHOD_BT, \Smart2Pay\GlobalPay\Model\Smart2Pay::PAYMENT_METHOD_SIBS])) { $payment_details_arr = self::defaultPaymentDetailsParams(); if (isset($response['ReferenceNumber'])) { $payment_details_arr['reference_number'] = $response['ReferenceNumber']; } if (isset($response['AmountToPay'])) { $payment_details_arr['amount_to_pay'] = $response['AmountToPay']; } if (isset($response['AccountHolder'])) { $payment_details_arr['account_holder'] = $response['AccountHolder']; } if (isset($response['BankName'])) { $payment_details_arr['bank_name'] = $response['BankName']; } if (isset($response['AccountNumber'])) { $payment_details_arr['account_number'] = $response['AccountNumber']; } if (isset($response['AccountCurrency'])) { $payment_details_arr['account_currency'] = $response['AccountCurrency']; } if (isset($response['SWIFT_BIC'])) { $payment_details_arr['swift_bic'] = $response['SWIFT_BIC']; } if (isset($response['IBAN'])) { $payment_details_arr['iban'] = $response['IBAN']; } if (isset($response['EntityNumber'])) { $payment_details_arr['entity_number'] = $response['EntityNumber']; } // Inform customer if ($this->sendPaymentDetails($order, $payment_details_arr)) { $order->addStatusHistoryComment('Smart2Pay :: Sending payment details to client.'); } } break; case \Smart2Pay\GlobalPay\Model\Smart2Pay::S2P_STATUS_SUCCESS: // cheking amount and currency $orderAmount = number_format($order->getGrandTotal(), 2, '.', '') * 100; $orderCurrency = $order->getOrderCurrency()->getCurrencyCode(); if (strcmp($orderAmount, $response['Amount']) != 0 or $orderCurrency != $response['Currency']) { $order->addStatusHistoryComment('Smart2Pay :: Notification has different amount [' . $orderAmount . '/' . $response['Amount'] . '] and/or currency [' . $orderCurrency . '/' . $response['Currency'] . ']! Please contact support@smart2pay.com', $method_config['order_status_on_4']); $this->_s2pLogger->write('Currency or amount doesn\'t match for order [' . $order->getRealOrderId() . '].'); } else { $order->addStatusHistoryComment('Smart2Pay :: Order has been paid.', $method_config['order_status_on_2']); $this->_s2pLogger->write('Order paid'); // Generate invoice if ($method_config['auto_invoice']) { // Create and pay Order Invoice if (!$order->canInvoice()) { $this->_s2pLogger->write('Order can not be invoiced', 'warning'); } else { /** @var \Magento\Sales\Model\Order\Invoice $invoice */ $invoice = $order->prepareInvoice(); $invoice->setRequestedCaptureCase(Invoice::CAPTURE_OFFLINE); $invoice->register(); $this->_dbTransaction->addObject($invoice)->addObject($invoice->getOrder()); $this->_dbTransaction->save(); $order->addStatusHistoryComment('Smart2Pay :: Order has been automatically invoiced.', $method_config['order_status_on_2']); } } // Check shipment if (!empty($method_config['auto_ship'])) { if (!$order->canShip()) { $this->_s2pLogger->write('Order can not be shipped', 'warning'); } else { //! TODO: Find how to do auto-shipping //$itemQty = $order->getItemsCollection()->count(); //$shipment = Mage::getModel( 'sales/service_order', $order )->prepareShipment( $itemQty ); //$shipment = new Mage_Sales_Model_Order_Shipment_Api(); //$shipmentId = $shipment->create( $order->getIncrementId() ); //$order->addStatusHistoryComment( 'Smart2Pay :: order has been automatically shipped.', $method_config['order_status_on_2'] ); } } // Inform customer if ($method_config['notify_customer']) { if ($this->informCustomer($order, $response['Amount'], $response['Currency'])) { $order->addStatusHistoryComment('Smart2Pay :: Customer informed about successful payment.'); } } } break; // Status = canceled // Status = canceled case \Smart2Pay\GlobalPay\Model\Smart2Pay::S2P_STATUS_CANCELLED: $order->addStatusHistoryComment('Smart2Pay :: payment has been canceled.', $method_config['order_status_on_3']); if (!$order->canCancel()) { $this->_s2pLogger->write('Can not cancel the order', 'warning'); } else { $order->cancel(); } break; // Status = failed // Status = failed case \Smart2Pay\GlobalPay\Model\Smart2Pay::S2P_STATUS_FAILED: $order->addStatusHistoryComment('Smart2Pay :: payment has failed.', $method_config['order_status_on_4']); break; // Status = expired // Status = expired case \Smart2Pay\GlobalPay\Model\Smart2Pay::S2P_STATUS_EXPIRED: $order->addStatusHistoryComment('Smart2Pay :: payment has expired.', $method_config['order_status_on_5']); break; default: $order->addStatusHistoryComment('Smart2Pay status "' . $response['StatusID'] . '" occurred.', $method_config['order_status']); break; } $order->save(); if ($s2p_transaction_obj = $this->_s2pTransaction->create()->loadByMerchantTransactionId($response['MerchantTransactionID']) and $s2p_transaction_obj->getID()) { if (isset($response['PaymentID'])) { $s2p_transaction_obj->setPaymentID($response['PaymentID']); } if (isset($response['StatusID'])) { $s2p_transaction_obj->setPaymentStatus($response['StatusID']); } $s2p_transaction_extra_arr = array(); $s2p_default_transaction_extra_arr = \Smart2Pay\GlobalPay\Helper\Smart2Pay::defaultTransactionLoggerExtraParams(); foreach ($s2p_default_transaction_extra_arr as $key => $val) { if (array_key_exists($key, $response)) { $s2p_transaction_extra_arr[$key] = $response[$key]; } } if (!empty($s2p_transaction_extra_arr)) { $s2p_transaction_obj->setExtraDataArray($s2p_transaction_extra_arr); } $s2p_transaction_obj->save(); } // NotificationType IS payment if (strtolower($response['NotificationType']) == 'payment') { // prepare string for 'da hash $responseHashString = "notificationTypePaymentPaymentId" . $response['PaymentID'] . $method_config['signature']; // prepare response data $responseData = array('NotificationType' => 'Payment', 'PaymentID' => $response['PaymentID'], 'Hash' => $this->_helper->computeSHA256Hash($responseHashString)); // output response echo 'NotificationType=payment&PaymentID=' . $responseData['PaymentID'] . '&Hash=' . $responseData['Hash']; } } if (!empty($error_message)) { $this->_s2pLogger->write($error_message); $this->_s2pLogger->write(' ### Notification END'); echo $error_message; exit; } $this->_s2pLogger->write(' ### Notification END'); exit; }