diff --git a/classes/ObjectModel.php b/classes/ObjectModel.php index dff3e5ceb..24e90d7aa 100644 --- a/classes/ObjectModel.php +++ b/classes/ObjectModel.php @@ -1007,7 +1007,19 @@ abstract class ObjectModelCore implements Core_Foundation_Database_EntityInterfa // Check if field is required $required_fields = (isset(self::$fieldsRequiredDatabase[get_class($this)])) ? self::$fieldsRequiredDatabase[get_class($this)] : array(); if (!$id_lang || $id_lang == $ps_lang_default) { - if (!in_array('required', $skip) && (!empty($data['required']) || in_array($field, $required_fields))) { + // if validation is being done for webservice request then check if that field is required in webservice parameters or not + $isWebserviceFieldRequired = true; + if (isset($this->webservice_validation) && $this->webservice_validation) { + $webserviceParams = $this->getWebserviceParameters(); + if (isset($webserviceParams['fields'][$field]['required']) && !$webserviceParams['fields'][$field]['required']) { + $isWebserviceFieldRequired = false; + } + } + + if (!in_array('required', $skip) + && (!empty($data['required']) || in_array($field, $required_fields)) + && $isWebserviceFieldRequired + ) { if (Tools::isEmpty($value)) { if ($human_errors) { return sprintf(Tools::displayError('The %s field is required.'), $this->displayFieldName($field, get_class($this))); @@ -1251,7 +1263,10 @@ abstract class ObjectModelCore implements Core_Foundation_Database_EntityInterfa } else { $current_field['i18n'] = false; } - if ((isset($details['required']) && $details['required'] === true) || in_array($field_name, $required_fields)) { + // For webservice request then check if that field is required in webservice parameters or not to set this 'required' + if (((isset($details['required']) && $details['required'] === true) || in_array($field_name, $required_fields)) + && (!isset($resource_parameters['fields'][$field_name]['required']) || $resource_parameters['fields'][$field_name]['required']) + ) { $current_field['required'] = true; } else { $current_field['required'] = false; diff --git a/classes/PaymentModule.php b/classes/PaymentModule.php index 39b850a28..ec4d451a8 100644 --- a/classes/PaymentModule.php +++ b/classes/PaymentModule.php @@ -374,7 +374,11 @@ abstract class PaymentModuleCore extends Module // We don't use the following condition to avoid the float precision issues : http://www.php.net/manual/en/language.types.float.php // if ($order->total_paid != $order->total_paid_real) // We use number_format in order to compare two string - if ($order_status->logable && number_format($cart_total_paid, _PS_PRICE_COMPUTE_PRECISION_) != number_format($amount_paid, _PS_PRICE_COMPUTE_PRECISION_)) { + // If webservice order request then no need to impose equal amounts(total cart and sent amount) condition + if ($order_status->logable + && number_format($cart_total_paid, _PS_PRICE_COMPUTE_PRECISION_) != number_format($amount_paid, _PS_PRICE_COMPUTE_PRECISION_) + && $this->name != 'wsorder' + ) { // if customer is paying full payment amount $id_order_state = Configuration::get('PS_OS_ERROR'); } diff --git a/classes/order/Order.php b/classes/order/Order.php index 548e58610..d16178fa4 100644 --- a/classes/order/Order.php +++ b/classes/order/Order.php @@ -256,6 +256,7 @@ class OrderCore extends ObjectModel 'fields' => array( 'id_address_delivery' => array('xlink_resource'=> 'addresses'), 'id_address_invoice' => array('xlink_resource'=> 'addresses'), + 'id_address_tax' => array('xlink_resource'=> 'addresses', 'required' => false), 'id_cart' => array('xlink_resource'=> 'carts'), 'id_currency' => array('xlink_resource'=> 'currencies'), 'id_lang' => array('xlink_resource'=> 'languages'), @@ -264,12 +265,16 @@ class OrderCore extends ObjectModel 'xlink_resource'=> 'order_states', 'setter' => 'setWsCurrentState' ), - 'module' => array('required' => true), + 'module' => array('required' => false), 'invoice_number' => array(), 'invoice_date' => array(), 'valid' => array(), 'date_add' => array(), 'date_upd' => array(), + 'transaction_id' => array( + 'getter' => false, + 'setter' => 'setWsTransactionId' + ), ), 'hidden_fields' => array ( 'total_shipping', @@ -1605,11 +1610,33 @@ class OrderCore extends ObjectModel public function addWs($autodate = true, $null_values = false) { /** @var PaymentModule $payment_module */ - $payment_module = Module::getInstanceByName($this->module); - $payment_module->orderSource = $this->source; + $paymentModule = new WebserviceOrder(); + $paymentModule->orderSource = $this->source; $customer = new Customer($this->id_customer); - $payment_module->validateOrder($this->id_cart, Configuration::get('PS_OS_WS_PAYMENT'), $this->total_paid, $this->payment, null, array(), null, false, $customer->secure_key); - $this->id = $payment_module->currentOrder; + + $extraVars = array(); + $extraVars['transaction_id'] = $this->transaction_id; + + if ($this->total_paid_real > 0) { + $orderStatus = Configuration::get('PS_OS_WS_PAYMENT'); + } else { + $orderStatus = Configuration::get('PS_OS_AWAITING_REMOTE_PAYMENT'); + } + + $paymentModule->validateOrder( + $this->id_cart, + $orderStatus, + $this->total_paid, + $paymentModule->displayName, + 'Order created by API request', + $extraVars, + null, + false, + $customer->secure_key + ); + + $this->id = $paymentModule->currentOrder; + return true; } @@ -2526,4 +2553,9 @@ class OrderCore extends ObjectModel 'SELECT `id` FROM `'._DB_PREFIX_.'htl_booking_detail` WHERE `id_order` = '.(int)$this->id.' ORDER BY `id` ASC' ); } + + public function setWsTransactionId($transactionId) + { + $this->transaction_id = $transactionId; + } } diff --git a/classes/order/OrderHistory.php b/classes/order/OrderHistory.php index 2da4bf501..bd9553982 100644 --- a/classes/order/OrderHistory.php +++ b/classes/order/OrderHistory.php @@ -315,7 +315,12 @@ class OrderHistoryCore extends ObjectModel if ($new_os->paid == 1) { $invoices = $order->getInvoicesCollection(); if ($order->total_paid != 0) { - $payment_method = Module::getInstanceByName($order->module); + // if order is created by API then create a direct object instead of creating an object from module + if ($order->module == 'wsorder') { + $payment_method = new WebserviceOrder(); + } else { + $payment_method = Module::getInstanceByName($order->module); + } } foreach ($invoices as $invoice) { diff --git a/classes/webservice/WebserviceOrder.php b/classes/webservice/WebserviceOrder.php new file mode 100644 index 000000000..4b1d337e4 --- /dev/null +++ b/classes/webservice/WebserviceOrder.php @@ -0,0 +1,30 @@ + +* @copyright 2010-2022 Webkul IN +* @license https://store.webkul.com/license.html +*/ + +class WebserviceOrderCore extends PaymentModule +{ + public $active = 1; + public $name = 'wsorder'; + + public function __construct() + { + $this->displayName = $this->l('Order from API'); + } +} diff --git a/classes/webservice/WebserviceOutputBuilder.php b/classes/webservice/WebserviceOutputBuilder.php index 71959299b..4315c8f8d 100644 --- a/classes/webservice/WebserviceOutputBuilder.php +++ b/classes/webservice/WebserviceOutputBuilder.php @@ -627,11 +627,11 @@ class WebserviceOutputBuilderCore $output_details = ''; foreach ($objects_assoc as $object_assoc) { if ($depth == 0 || $class_name === null) { - $value = null; - if (!empty($object_assoc)) { - $value = $object_assoc; + if (empty($object_assoc)) { + continue; } - if (empty($fields_assoc)) { + $value = $object_assoc; + if (empty($fields_assoc) && !empty($value['id'])) { $fields_assoc = array(array('id' => $value['id'])); } $output_details .= $this->renderFlatAssociation($object, $depth, $assoc_name, $association['resource'], $fields_assoc, $value, $parent_details); @@ -676,13 +676,14 @@ class WebserviceOutputBuilderCore foreach ($fields_assoc as $field_name => $field) { if (!is_array($this->fieldsToDisplay) || in_array($field_name, $this->fieldsToDisplay[$assoc_name])) { - if ($field_name == 'id' && !isset($field['sqlId'])) { + if (isset($field['id']) && !isset($field['sqlId'])) { $field['sqlId'] = 'id'; - $field['value'] = $object_assoc['id']; + $field['value'] = isset($object_assoc['id']) ? $object_assoc['id'] : null; } elseif (!isset($field['sqlId'])) { $field['sqlId'] = $field_name; - $field['value'] = $object_assoc[$field_name]; + $field['value'] = isset($object_assoc[$field_name]) ? $object_assoc[$field_name] : null; } + $field['entities_name'] = $assoc_name; $field['entity_name'] = $resource_name; diff --git a/classes/webservice/WebserviceOutputJSON.php b/classes/webservice/WebserviceOutputJSON.php index f1e8cf7c5..0e2da03c4 100644 --- a/classes/webservice/WebserviceOutputJSON.php +++ b/classes/webservice/WebserviceOutputJSON.php @@ -40,7 +40,7 @@ class WebserviceOutputJSON implements WebserviceOutputInterface /** * Current association */ - protected $currentAssociatedEntity; + protected $currentAssociatedEntity = array(); /** * Json content diff --git a/classes/webservice/WebserviceRequest.php b/classes/webservice/WebserviceRequest.php index f38dc17c0..2d35c03a2 100644 --- a/classes/webservice/WebserviceRequest.php +++ b/classes/webservice/WebserviceRequest.php @@ -1481,6 +1481,7 @@ class WebserviceRequestCore } if (!$this->hasErrors()) { + $object->webservice_validation = 1; if ($i18n && ($retValidateFieldsLang = $object->validateFieldsLang(false, true)) !== true) { $this->setError(400, 'Validation error: "'.$retValidateFieldsLang.'"', 84); return false;