diff options
Diffstat (limited to 'public/catalog/controller/api')
-rw-r--r-- | public/catalog/controller/api/cart.php | 259 | ||||
-rw-r--r-- | public/catalog/controller/api/coupon.php | 36 | ||||
-rw-r--r-- | public/catalog/controller/api/currency.php | 30 | ||||
-rw-r--r-- | public/catalog/controller/api/customer.php | 97 | ||||
-rw-r--r-- | public/catalog/controller/api/login.php | 52 | ||||
-rw-r--r-- | public/catalog/controller/api/order.php | 805 | ||||
-rw-r--r-- | public/catalog/controller/api/payment.php | 274 | ||||
-rw-r--r-- | public/catalog/controller/api/reward.php | 82 | ||||
-rw-r--r-- | public/catalog/controller/api/shipping.php | 251 | ||||
-rw-r--r-- | public/catalog/controller/api/voucher.php | 137 |
10 files changed, 2023 insertions, 0 deletions
diff --git a/public/catalog/controller/api/cart.php b/public/catalog/controller/api/cart.php new file mode 100644 index 0000000..3d2211d --- /dev/null +++ b/public/catalog/controller/api/cart.php @@ -0,0 +1,259 @@ +<?php +class ControllerApiCart extends Controller { + public function add() { + $this->load->language('api/cart'); + + $json = array(); + + if (!isset($this->session->data['api_id'])) { + $json['error']['warning'] = $this->language->get('error_permission'); + } else { + if (isset($this->request->post['product'])) { + $this->cart->clear(); + + foreach ($this->request->post['product'] as $product) { + if (isset($product['option'])) { + $option = $product['option']; + } else { + $option = array(); + } + + $this->cart->add($product['product_id'], $product['quantity'], $option); + } + + $json['success'] = $this->language->get('text_success'); + + unset($this->session->data['shipping_method']); + unset($this->session->data['shipping_methods']); + unset($this->session->data['payment_method']); + unset($this->session->data['payment_methods']); + } elseif (isset($this->request->post['product_id'])) { + $this->load->model('catalog/product'); + + $product_info = $this->model_catalog_product->getProduct($this->request->post['product_id']); + + if ($product_info) { + if (isset($this->request->post['quantity'])) { + $quantity = $this->request->post['quantity']; + } else { + $quantity = 1; + } + + if (isset($this->request->post['option'])) { + $option = array_filter($this->request->post['option']); + } else { + $option = array(); + } + + $product_options = $this->model_catalog_product->getProductOptions($this->request->post['product_id']); + + foreach ($product_options as $product_option) { + if ($product_option['required'] && empty($option[$product_option['product_option_id']])) { + $json['error']['option'][$product_option['product_option_id']] = sprintf($this->language->get('error_required'), $product_option['name']); + } + } + + if (!isset($json['error']['option'])) { + $this->cart->add($this->request->post['product_id'], $quantity, $option); + + $json['success'] = $this->language->get('text_success'); + + unset($this->session->data['shipping_method']); + unset($this->session->data['shipping_methods']); + unset($this->session->data['payment_method']); + unset($this->session->data['payment_methods']); + } + } else { + $json['error']['store'] = $this->language->get('error_store'); + } + } + } + + $this->response->addHeader('Content-Type: application/json'); + $this->response->setOutput(json_encode($json)); + } + + public function edit() { + $this->load->language('api/cart'); + + $json = array(); + + if (!isset($this->session->data['api_id'])) { + $json['error'] = $this->language->get('error_permission'); + } else { + $this->cart->update($this->request->post['key'], $this->request->post['quantity']); + + $json['success'] = $this->language->get('text_success'); + + unset($this->session->data['shipping_method']); + unset($this->session->data['shipping_methods']); + unset($this->session->data['payment_method']); + unset($this->session->data['payment_methods']); + unset($this->session->data['reward']); + } + + $this->response->addHeader('Content-Type: application/json'); + $this->response->setOutput(json_encode($json)); + } + + public function remove() { + $this->load->language('api/cart'); + + $json = array(); + + if (!isset($this->session->data['api_id'])) { + $json['error'] = $this->language->get('error_permission'); + } else { + // Remove + if (isset($this->request->post['key'])) { + $this->cart->remove($this->request->post['key']); + + unset($this->session->data['vouchers'][$this->request->post['key']]); + + $json['success'] = $this->language->get('text_success'); + + unset($this->session->data['shipping_method']); + unset($this->session->data['shipping_methods']); + unset($this->session->data['payment_method']); + unset($this->session->data['payment_methods']); + unset($this->session->data['reward']); + } + } + + $this->response->addHeader('Content-Type: application/json'); + $this->response->setOutput(json_encode($json)); + } + + public function products() { + $this->load->language('api/cart'); + + $json = array(); + + if (!isset($this->session->data['api_id'])) { + $json['error']['warning'] = $this->language->get('error_permission'); + } else { + // Stock + if (!$this->cart->hasStock() && (!$this->config->get('config_stock_checkout') || $this->config->get('config_stock_warning'))) { + $json['error']['stock'] = $this->language->get('error_stock'); + } + + // Products + $json['products'] = array(); + + $products = $this->cart->getProducts(); + + foreach ($products as $product) { + $product_total = 0; + + foreach ($products as $product_2) { + if ($product_2['product_id'] == $product['product_id']) { + $product_total += $product_2['quantity']; + } + } + + if ($product['minimum'] > $product_total) { + $json['error']['minimum'][] = sprintf($this->language->get('error_minimum'), $product['name'], $product['minimum']); + } + + $option_data = array(); + + foreach ($product['option'] as $option) { + $option_data[] = array( + 'product_option_id' => $option['product_option_id'], + 'product_option_value_id' => $option['product_option_value_id'], + 'name' => $option['name'], + 'value' => $option['value'], + 'type' => $option['type'] + ); + } + + $json['products'][] = array( + 'cart_id' => $product['cart_id'], + 'product_id' => $product['product_id'], + 'name' => $product['name'], + 'model' => $product['model'], + 'option' => $option_data, + 'quantity' => $product['quantity'], + 'stock' => $product['stock'] ? true : !(!$this->config->get('config_stock_checkout') || $this->config->get('config_stock_warning')), + 'shipping' => $product['shipping'], + 'price' => $this->currency->format($this->tax->calculate($product['price'], $product['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency']), + 'total' => $this->currency->format($this->tax->calculate($product['price'], $product['tax_class_id'], $this->config->get('config_tax')) * $product['quantity'], $this->session->data['currency']), + 'reward' => $product['reward'] + ); + } + + // Voucher + $json['vouchers'] = array(); + + if (!empty($this->session->data['vouchers'])) { + foreach ($this->session->data['vouchers'] as $key => $voucher) { + $json['vouchers'][] = array( + 'code' => $voucher['code'], + 'description' => $voucher['description'], + 'from_name' => $voucher['from_name'], + 'from_email' => $voucher['from_email'], + 'to_name' => $voucher['to_name'], + 'to_email' => $voucher['to_email'], + 'voucher_theme_id' => $voucher['voucher_theme_id'], + 'message' => $voucher['message'], + 'price' => $this->currency->format($voucher['amount'], $this->session->data['currency']), + 'amount' => $voucher['amount'] + ); + } + } + + // Totals + $this->load->model('setting/extension'); + + $totals = array(); + $taxes = $this->cart->getTaxes(); + $total = 0; + + // Because __call can not keep var references so we put them into an array. + $total_data = array( + 'totals' => &$totals, + 'taxes' => &$taxes, + 'total' => &$total + ); + + $sort_order = array(); + + $results = $this->model_setting_extension->getExtensions('total'); + + foreach ($results as $key => $value) { + $sort_order[$key] = $this->config->get('total_' . $value['code'] . '_sort_order'); + } + + array_multisort($sort_order, SORT_ASC, $results); + + foreach ($results as $result) { + if ($this->config->get('total_' . $result['code'] . '_status')) { + $this->load->model('extension/total/' . $result['code']); + + // We have to put the totals in an array so that they pass by reference. + $this->{'model_extension_total_' . $result['code']}->getTotal($total_data); + } + } + + $sort_order = array(); + + foreach ($totals as $key => $value) { + $sort_order[$key] = $value['sort_order']; + } + + array_multisort($sort_order, SORT_ASC, $totals); + + $json['totals'] = array(); + + foreach ($totals as $total) { + $json['totals'][] = array( + 'title' => $total['title'], + 'text' => $this->currency->format($total['value'], $this->session->data['currency']) + ); + } + } + + $this->response->addHeader('Content-Type: application/json'); + $this->response->setOutput(json_encode($json)); + } +} diff --git a/public/catalog/controller/api/coupon.php b/public/catalog/controller/api/coupon.php new file mode 100644 index 0000000..82e2ab0 --- /dev/null +++ b/public/catalog/controller/api/coupon.php @@ -0,0 +1,36 @@ +<?php +class ControllerApiCoupon extends Controller { + public function index() { + $this->load->language('api/coupon'); + + // Delete past coupon in case there is an error + unset($this->session->data['coupon']); + + $json = array(); + + if (!isset($this->session->data['api_id'])) { + $json['error'] = $this->language->get('error_permission'); + } else { + $this->load->model('extension/total/coupon'); + + if (isset($this->request->post['coupon'])) { + $coupon = $this->request->post['coupon']; + } else { + $coupon = ''; + } + + $coupon_info = $this->model_extension_total_coupon->getCoupon($coupon); + + if ($coupon_info) { + $this->session->data['coupon'] = $this->request->post['coupon']; + + $json['success'] = $this->language->get('text_success'); + } else { + $json['error'] = $this->language->get('error_coupon'); + } + } + + $this->response->addHeader('Content-Type: application/json'); + $this->response->setOutput(json_encode($json)); + } +} diff --git a/public/catalog/controller/api/currency.php b/public/catalog/controller/api/currency.php new file mode 100644 index 0000000..3e9ca09 --- /dev/null +++ b/public/catalog/controller/api/currency.php @@ -0,0 +1,30 @@ +<?php +class ControllerApiCurrency extends Controller { + public function index() { + $this->load->language('api/currency'); + + $json = array(); + + if (!isset($this->session->data['api_id'])) { + $json['error'] = $this->language->get('error_permission'); + } else { + $this->load->model('localisation/currency'); + + $currency_info = $this->model_localisation_currency->getCurrencyByCode($this->request->post['currency']); + + if ($currency_info) { + $this->session->data['currency'] = $this->request->post['currency']; + + unset($this->session->data['shipping_method']); + unset($this->session->data['shipping_methods']); + + $json['success'] = $this->language->get('text_success'); + } else { + $json['error'] = $this->language->get('error_currency'); + } + } + + $this->response->addHeader('Content-Type: application/json'); + $this->response->setOutput(json_encode($json)); + } +} diff --git a/public/catalog/controller/api/customer.php b/public/catalog/controller/api/customer.php new file mode 100644 index 0000000..d5e262b --- /dev/null +++ b/public/catalog/controller/api/customer.php @@ -0,0 +1,97 @@ +<?php +class ControllerApiCustomer extends Controller { + public function index() { + $this->load->language('api/customer'); + + // Delete past customer in case there is an error + unset($this->session->data['customer']); + + $json = array(); + + if (!isset($this->session->data['api_id'])) { + $json['error']['warning'] = $this->language->get('error_permission'); + } else { + // Add keys for missing post vars + $keys = array( + 'customer_id', + 'customer_group_id', + 'firstname', + 'lastname', + 'email', + 'telephone', + ); + + foreach ($keys as $key) { + if (!isset($this->request->post[$key])) { + $this->request->post[$key] = ''; + } + } + + // Customer + if ($this->request->post['customer_id']) { + $this->load->model('account/customer'); + + $customer_info = $this->model_account_customer->getCustomer($this->request->post['customer_id']); + + if (!$customer_info || !$this->customer->login($customer_info['email'], '', true)) { + $json['error']['warning'] = $this->language->get('error_customer'); + } + } + + if ((utf8_strlen(trim($this->request->post['firstname'])) < 1) || (utf8_strlen(trim($this->request->post['firstname'])) > 32)) { + $json['error']['firstname'] = $this->language->get('error_firstname'); + } + + if ((utf8_strlen(trim($this->request->post['lastname'])) < 1) || (utf8_strlen(trim($this->request->post['lastname'])) > 32)) { + $json['error']['lastname'] = $this->language->get('error_lastname'); + } + + if ((utf8_strlen($this->request->post['email']) > 96) || (!filter_var($this->request->post['email'], FILTER_VALIDATE_EMAIL))) { + $json['error']['email'] = $this->language->get('error_email'); + } + + if ((utf8_strlen($this->request->post['telephone']) < 3) || (utf8_strlen($this->request->post['telephone']) > 32)) { + $json['error']['telephone'] = $this->language->get('error_telephone'); + } + + // Customer Group + if (is_array($this->config->get('config_customer_group_display')) && in_array($this->request->post['customer_group_id'], $this->config->get('config_customer_group_display'))) { + $customer_group_id = $this->request->post['customer_group_id']; + } else { + $customer_group_id = $this->config->get('config_customer_group_id'); + } + + // Custom field validation + $this->load->model('account/custom_field'); + + $custom_fields = $this->model_account_custom_field->getCustomFields($customer_group_id); + + foreach ($custom_fields as $custom_field) { + if ($custom_field['location'] == 'account') { + if ($custom_field['required'] && empty($this->request->post['custom_field'][$custom_field['location']][$custom_field['custom_field_id']])) { + $json['error']['custom_field' . $custom_field['custom_field_id']] = sprintf($this->language->get('error_custom_field'), $custom_field['name']); + } elseif (($custom_field['type'] == 'text') && !empty($custom_field['validation']) && !filter_var($this->request->post['custom_field'][$custom_field['location']][$custom_field['custom_field_id']], FILTER_VALIDATE_REGEXP, array('options' => array('regexp' => $custom_field['validation'])))) { + $json['error']['custom_field' . $custom_field['custom_field_id']] = sprintf($this->language->get('error_custom_field'), $custom_field['name']); + } + } + } + + if (!$json) { + $this->session->data['customer'] = array( + 'customer_id' => $this->request->post['customer_id'], + 'customer_group_id' => $customer_group_id, + 'firstname' => $this->request->post['firstname'], + 'lastname' => $this->request->post['lastname'], + 'email' => $this->request->post['email'], + 'telephone' => $this->request->post['telephone'], + 'custom_field' => isset($this->request->post['custom_field']) ? $this->request->post['custom_field'] : array() + ); + + $json['success'] = $this->language->get('text_success'); + } + } + + $this->response->addHeader('Content-Type: application/json'); + $this->response->setOutput(json_encode($json)); + } +} diff --git a/public/catalog/controller/api/login.php b/public/catalog/controller/api/login.php new file mode 100644 index 0000000..48c6b11 --- /dev/null +++ b/public/catalog/controller/api/login.php @@ -0,0 +1,52 @@ +<?php +class ControllerApiLogin extends Controller { + public function index() { + $this->load->language('api/login'); + + $json = array(); + + $this->load->model('account/api'); + + // Login with API Key + if(isset($this->request->post['username'])) { + $api_info = $this->model_account_api->login($this->request->post['username'], $this->request->post['key']); + } else { + $api_info = $this->model_account_api->login('Default', $this->request->post['key']); + } + + if ($api_info) { + // Check if IP is allowed + $ip_data = array(); + + $results = $this->model_account_api->getApiIps($api_info['api_id']); + + foreach ($results as $result) { + $ip_data[] = trim($result['ip']); + } + + if (!in_array($this->request->server['REMOTE_ADDR'], $ip_data)) { + $json['error']['ip'] = sprintf($this->language->get('error_ip'), $this->request->server['REMOTE_ADDR']); + } + + if (!$json) { + $json['success'] = $this->language->get('text_success'); + + $session = new Session($this->config->get('session_engine'), $this->registry); + + $session->start(); + + $this->model_account_api->addApiSession($api_info['api_id'], $session->getId(), $this->request->server['REMOTE_ADDR']); + + $session->data['api_id'] = $api_info['api_id']; + + // Create Token + $json['api_token'] = $session->getId(); + } else { + $json['error']['key'] = $this->language->get('error_key'); + } + } + + $this->response->addHeader('Content-Type: application/json'); + $this->response->setOutput(json_encode($json)); + } +} diff --git a/public/catalog/controller/api/order.php b/public/catalog/controller/api/order.php new file mode 100644 index 0000000..94cf1bd --- /dev/null +++ b/public/catalog/controller/api/order.php @@ -0,0 +1,805 @@ +<?php +class ControllerApiOrder extends Controller { + public function add() { + $this->load->language('api/order'); + + $json = array(); + + if (!isset($this->session->data['api_id'])) { + $json['error'] = $this->language->get('error_permission'); + } else { + // Customer + if (!isset($this->session->data['customer'])) { + $json['error'] = $this->language->get('error_customer'); + } + + // Payment Address + if (!isset($this->session->data['payment_address'])) { + $json['error'] = $this->language->get('error_payment_address'); + } + + // Payment Method + if (!$json && !empty($this->request->post['payment_method'])) { + if (empty($this->session->data['payment_methods'])) { + $json['error'] = $this->language->get('error_no_payment'); + } elseif (!isset($this->session->data['payment_methods'][$this->request->post['payment_method']])) { + $json['error'] = $this->language->get('error_payment_method'); + } + + if (!$json) { + $this->session->data['payment_method'] = $this->session->data['payment_methods'][$this->request->post['payment_method']]; + } + } + + if (!isset($this->session->data['payment_method'])) { + $json['error'] = $this->language->get('error_payment_method'); + } + + // Shipping + if ($this->cart->hasShipping()) { + // Shipping Address + if (!isset($this->session->data['shipping_address'])) { + $json['error'] = $this->language->get('error_shipping_address'); + } + + // Shipping Method + if (!$json && !empty($this->request->post['shipping_method'])) { + if (empty($this->session->data['shipping_methods'])) { + $json['error'] = $this->language->get('error_no_shipping'); + } else { + $shipping = explode('.', $this->request->post['shipping_method']); + + if (!isset($shipping[0]) || !isset($shipping[1]) || !isset($this->session->data['shipping_methods'][$shipping[0]]['quote'][$shipping[1]])) { + $json['error'] = $this->language->get('error_shipping_method'); + } + } + + if (!$json) { + $this->session->data['shipping_method'] = $this->session->data['shipping_methods'][$shipping[0]]['quote'][$shipping[1]]; + } + } + + // Shipping Method + if (!isset($this->session->data['shipping_method'])) { + $json['error'] = $this->language->get('error_shipping_method'); + } + } else { + unset($this->session->data['shipping_address']); + unset($this->session->data['shipping_method']); + unset($this->session->data['shipping_methods']); + } + + // Cart + if ((!$this->cart->hasProducts() && empty($this->session->data['vouchers'])) || (!$this->cart->hasStock() && !$this->config->get('config_stock_checkout'))) { + $json['error'] = $this->language->get('error_stock'); + } + + // Validate minimum quantity requirements. + $products = $this->cart->getProducts(); + + foreach ($products as $product) { + $product_total = 0; + + foreach ($products as $product_2) { + if ($product_2['product_id'] == $product['product_id']) { + $product_total += $product_2['quantity']; + } + } + + if ($product['minimum'] > $product_total) { + $json['error'] = sprintf($this->language->get('error_minimum'), $product['name'], $product['minimum']); + + break; + } + } + + if (!$json) { + $json['success'] = $this->language->get('text_success'); + + $order_data = array(); + + // Store Details + $order_data['invoice_prefix'] = $this->config->get('config_invoice_prefix'); + $order_data['store_id'] = $this->config->get('config_store_id'); + $order_data['store_name'] = $this->config->get('config_name'); + $order_data['store_url'] = $this->config->get('config_url'); + + // Customer Details + $order_data['customer_id'] = $this->session->data['customer']['customer_id']; + $order_data['customer_group_id'] = $this->session->data['customer']['customer_group_id']; + $order_data['firstname'] = $this->session->data['customer']['firstname']; + $order_data['lastname'] = $this->session->data['customer']['lastname']; + $order_data['email'] = $this->session->data['customer']['email']; + $order_data['telephone'] = $this->session->data['customer']['telephone']; + $order_data['custom_field'] = $this->session->data['customer']['custom_field']; + + // Payment Details + $order_data['payment_firstname'] = $this->session->data['payment_address']['firstname']; + $order_data['payment_lastname'] = $this->session->data['payment_address']['lastname']; + $order_data['payment_company'] = $this->session->data['payment_address']['company']; + $order_data['payment_address_1'] = $this->session->data['payment_address']['address_1']; + $order_data['payment_address_2'] = $this->session->data['payment_address']['address_2']; + $order_data['payment_city'] = $this->session->data['payment_address']['city']; + $order_data['payment_postcode'] = $this->session->data['payment_address']['postcode']; + $order_data['payment_zone'] = $this->session->data['payment_address']['zone']; + $order_data['payment_zone_id'] = $this->session->data['payment_address']['zone_id']; + $order_data['payment_country'] = $this->session->data['payment_address']['country']; + $order_data['payment_country_id'] = $this->session->data['payment_address']['country_id']; + $order_data['payment_address_format'] = $this->session->data['payment_address']['address_format']; + $order_data['payment_custom_field'] = (isset($this->session->data['payment_address']['custom_field']) ? $this->session->data['payment_address']['custom_field'] : array()); + + if (isset($this->session->data['payment_method']['title'])) { + $order_data['payment_method'] = $this->session->data['payment_method']['title']; + } else { + $order_data['payment_method'] = ''; + } + + if (isset($this->session->data['payment_method']['code'])) { + $order_data['payment_code'] = $this->session->data['payment_method']['code']; + } else { + $order_data['payment_code'] = ''; + } + + // Shipping Details + if ($this->cart->hasShipping()) { + $order_data['shipping_firstname'] = $this->session->data['shipping_address']['firstname']; + $order_data['shipping_lastname'] = $this->session->data['shipping_address']['lastname']; + $order_data['shipping_company'] = $this->session->data['shipping_address']['company']; + $order_data['shipping_address_1'] = $this->session->data['shipping_address']['address_1']; + $order_data['shipping_address_2'] = $this->session->data['shipping_address']['address_2']; + $order_data['shipping_city'] = $this->session->data['shipping_address']['city']; + $order_data['shipping_postcode'] = $this->session->data['shipping_address']['postcode']; + $order_data['shipping_zone'] = $this->session->data['shipping_address']['zone']; + $order_data['shipping_zone_id'] = $this->session->data['shipping_address']['zone_id']; + $order_data['shipping_country'] = $this->session->data['shipping_address']['country']; + $order_data['shipping_country_id'] = $this->session->data['shipping_address']['country_id']; + $order_data['shipping_address_format'] = $this->session->data['shipping_address']['address_format']; + $order_data['shipping_custom_field'] = (isset($this->session->data['shipping_address']['custom_field']) ? $this->session->data['shipping_address']['custom_field'] : array()); + + if (isset($this->session->data['shipping_method']['title'])) { + $order_data['shipping_method'] = $this->session->data['shipping_method']['title']; + } else { + $order_data['shipping_method'] = ''; + } + + if (isset($this->session->data['shipping_method']['code'])) { + $order_data['shipping_code'] = $this->session->data['shipping_method']['code']; + } else { + $order_data['shipping_code'] = ''; + } + } else { + $order_data['shipping_firstname'] = ''; + $order_data['shipping_lastname'] = ''; + $order_data['shipping_company'] = ''; + $order_data['shipping_address_1'] = ''; + $order_data['shipping_address_2'] = ''; + $order_data['shipping_city'] = ''; + $order_data['shipping_postcode'] = ''; + $order_data['shipping_zone'] = ''; + $order_data['shipping_zone_id'] = ''; + $order_data['shipping_country'] = ''; + $order_data['shipping_country_id'] = ''; + $order_data['shipping_address_format'] = ''; + $order_data['shipping_custom_field'] = array(); + $order_data['shipping_method'] = ''; + $order_data['shipping_code'] = ''; + } + + // Products + $order_data['products'] = array(); + + foreach ($this->cart->getProducts() as $product) { + $option_data = array(); + + foreach ($product['option'] as $option) { + $option_data[] = array( + 'product_option_id' => $option['product_option_id'], + 'product_option_value_id' => $option['product_option_value_id'], + 'option_id' => $option['option_id'], + 'option_value_id' => $option['option_value_id'], + 'name' => $option['name'], + 'value' => $option['value'], + 'type' => $option['type'] + ); + } + + $order_data['products'][] = array( + 'product_id' => $product['product_id'], + 'name' => $product['name'], + 'model' => $product['model'], + 'option' => $option_data, + 'download' => $product['download'], + 'quantity' => $product['quantity'], + 'subtract' => $product['subtract'], + 'price' => $product['price'], + 'total' => $product['total'], + 'tax' => $this->tax->getTax($product['price'], $product['tax_class_id']), + 'reward' => $product['reward'] + ); + } + + // Gift Voucher + $order_data['vouchers'] = array(); + + if (!empty($this->session->data['vouchers'])) { + foreach ($this->session->data['vouchers'] as $voucher) { + $order_data['vouchers'][] = array( + 'description' => $voucher['description'], + 'code' => token(10), + 'to_name' => $voucher['to_name'], + 'to_email' => $voucher['to_email'], + 'from_name' => $voucher['from_name'], + 'from_email' => $voucher['from_email'], + 'voucher_theme_id' => $voucher['voucher_theme_id'], + 'message' => $voucher['message'], + 'amount' => $voucher['amount'] + ); + } + } + + // Order Totals + $this->load->model('setting/extension'); + + $totals = array(); + $taxes = $this->cart->getTaxes(); + $total = 0; + + // Because __call can not keep var references so we put them into an array. + $total_data = array( + 'totals' => &$totals, + 'taxes' => &$taxes, + 'total' => &$total + ); + + $sort_order = array(); + + $results = $this->model_setting_extension->getExtensions('total'); + + foreach ($results as $key => $value) { + $sort_order[$key] = $this->config->get('total_' . $value['code'] . '_sort_order'); + } + + array_multisort($sort_order, SORT_ASC, $results); + + foreach ($results as $result) { + if ($this->config->get('total_' . $result['code'] . '_status')) { + $this->load->model('extension/total/' . $result['code']); + + // We have to put the totals in an array so that they pass by reference. + $this->{'model_extension_total_' . $result['code']}->getTotal($total_data); + } + } + + $sort_order = array(); + + foreach ($total_data['totals'] as $key => $value) { + $sort_order[$key] = $value['sort_order']; + } + + array_multisort($sort_order, SORT_ASC, $total_data['totals']); + + $order_data = array_merge($order_data, $total_data); + + if (isset($this->request->post['comment'])) { + $order_data['comment'] = $this->request->post['comment']; + } else { + $order_data['comment'] = ''; + } + + if (isset($this->request->post['affiliate_id'])) { + $subtotal = $this->cart->getSubTotal(); + + // Affiliate + $this->load->model('account/customer'); + + $affiliate_info = $this->model_account_customer->getAffiliate($this->request->post['affiliate_id']); + + if ($affiliate_info) { + $order_data['affiliate_id'] = $affiliate_info['customer_id']; + $order_data['commission'] = ($subtotal / 100) * $affiliate_info['commission']; + } else { + $order_data['affiliate_id'] = 0; + $order_data['commission'] = 0; + } + + // Marketing + $order_data['marketing_id'] = 0; + $order_data['tracking'] = ''; + } else { + $order_data['affiliate_id'] = 0; + $order_data['commission'] = 0; + $order_data['marketing_id'] = 0; + $order_data['tracking'] = ''; + } + + $order_data['language_id'] = $this->config->get('config_language_id'); + $order_data['currency_id'] = $this->currency->getId($this->session->data['currency']); + $order_data['currency_code'] = $this->session->data['currency']; + $order_data['currency_value'] = $this->currency->getValue($this->session->data['currency']); + $order_data['ip'] = $this->request->server['REMOTE_ADDR']; + + if (!empty($this->request->server['HTTP_X_FORWARDED_FOR'])) { + $order_data['forwarded_ip'] = $this->request->server['HTTP_X_FORWARDED_FOR']; + } elseif (!empty($this->request->server['HTTP_CLIENT_IP'])) { + $order_data['forwarded_ip'] = $this->request->server['HTTP_CLIENT_IP']; + } else { + $order_data['forwarded_ip'] = ''; + } + + if (isset($this->request->server['HTTP_USER_AGENT'])) { + $order_data['user_agent'] = $this->request->server['HTTP_USER_AGENT']; + } else { + $order_data['user_agent'] = ''; + } + + if (isset($this->request->server['HTTP_ACCEPT_LANGUAGE'])) { + $order_data['accept_language'] = $this->request->server['HTTP_ACCEPT_LANGUAGE']; + } else { + $order_data['accept_language'] = ''; + } + + $this->load->model('checkout/order'); + + $json['order_id'] = $this->model_checkout_order->addOrder($order_data); + + // Set the order history + if (isset($this->request->post['order_status_id'])) { + $order_status_id = $this->request->post['order_status_id']; + } else { + $order_status_id = $this->config->get('config_order_status_id'); + } + + $this->model_checkout_order->addOrderHistory($json['order_id'], $order_status_id); + + // clear cart since the order has already been successfully stored. + $this->cart->clear(); + } + } + + $this->response->addHeader('Content-Type: application/json'); + $this->response->setOutput(json_encode($json)); + } + + public function edit() { + $this->load->language('api/order'); + + $json = array(); + + if (!isset($this->session->data['api_id'])) { + $json['error'] = $this->language->get('error_permission'); + } else { + $this->load->model('checkout/order'); + + if (isset($this->request->get['order_id'])) { + $order_id = $this->request->get['order_id']; + } else { + $order_id = 0; + } + + $order_info = $this->model_checkout_order->getOrder($order_id); + + if ($order_info) { + // Customer + if (!isset($this->session->data['customer'])) { + $json['error'] = $this->language->get('error_customer'); + } + + // Payment Address + if (!isset($this->session->data['payment_address'])) { + $json['error'] = $this->language->get('error_payment_address'); + } + + // Payment Method + if (!$json && !empty($this->request->post['payment_method'])) { + if (empty($this->session->data['payment_methods'])) { + $json['error'] = $this->language->get('error_no_payment'); + } elseif (!isset($this->session->data['payment_methods'][$this->request->post['payment_method']])) { + $json['error'] = $this->language->get('error_payment_method'); + } + + if (!$json) { + $this->session->data['payment_method'] = $this->session->data['payment_methods'][$this->request->post['payment_method']]; + } + } + + if (!isset($this->session->data['payment_method'])) { + $json['error'] = $this->language->get('error_payment_method'); + } + + // Shipping + if ($this->cart->hasShipping()) { + // Shipping Address + if (!isset($this->session->data['shipping_address'])) { + $json['error'] = $this->language->get('error_shipping_address'); + } + + // Shipping Method + if (!$json && !empty($this->request->post['shipping_method'])) { + if (empty($this->session->data['shipping_methods'])) { + $json['error'] = $this->language->get('error_no_shipping'); + } else { + $shipping = explode('.', $this->request->post['shipping_method']); + + if (!isset($shipping[0]) || !isset($shipping[1]) || !isset($this->session->data['shipping_methods'][$shipping[0]]['quote'][$shipping[1]])) { + $json['error'] = $this->language->get('error_shipping_method'); + } + } + + if (!$json) { + $this->session->data['shipping_method'] = $this->session->data['shipping_methods'][$shipping[0]]['quote'][$shipping[1]]; + } + } + + if (!isset($this->session->data['shipping_method'])) { + $json['error'] = $this->language->get('error_shipping_method'); + } + } else { + unset($this->session->data['shipping_address']); + unset($this->session->data['shipping_method']); + unset($this->session->data['shipping_methods']); + } + + // Cart + if ((!$this->cart->hasProducts() && empty($this->session->data['vouchers'])) || (!$this->cart->hasStock() && !$this->config->get('config_stock_checkout'))) { + $json['error'] = $this->language->get('error_stock'); + } + + // Validate minimum quantity requirements. + $products = $this->cart->getProducts(); + + foreach ($products as $product) { + $product_total = 0; + + foreach ($products as $product_2) { + if ($product_2['product_id'] == $product['product_id']) { + $product_total += $product_2['quantity']; + } + } + + if ($product['minimum'] > $product_total) { + $json['error'] = sprintf($this->language->get('error_minimum'), $product['name'], $product['minimum']); + + break; + } + } + + if (!$json) { + $json['success'] = $this->language->get('text_success'); + + $order_data = array(); + + // Store Details + $order_data['invoice_prefix'] = $this->config->get('config_invoice_prefix'); + $order_data['store_id'] = $this->config->get('config_store_id'); + $order_data['store_name'] = $this->config->get('config_name'); + $order_data['store_url'] = $this->config->get('config_url'); + + // Customer Details + $order_data['customer_id'] = $this->session->data['customer']['customer_id']; + $order_data['customer_group_id'] = $this->session->data['customer']['customer_group_id']; + $order_data['firstname'] = $this->session->data['customer']['firstname']; + $order_data['lastname'] = $this->session->data['customer']['lastname']; + $order_data['email'] = $this->session->data['customer']['email']; + $order_data['telephone'] = $this->session->data['customer']['telephone']; + $order_data['custom_field'] = $this->session->data['customer']['custom_field']; + + // Payment Details + $order_data['payment_firstname'] = $this->session->data['payment_address']['firstname']; + $order_data['payment_lastname'] = $this->session->data['payment_address']['lastname']; + $order_data['payment_company'] = $this->session->data['payment_address']['company']; + $order_data['payment_address_1'] = $this->session->data['payment_address']['address_1']; + $order_data['payment_address_2'] = $this->session->data['payment_address']['address_2']; + $order_data['payment_city'] = $this->session->data['payment_address']['city']; + $order_data['payment_postcode'] = $this->session->data['payment_address']['postcode']; + $order_data['payment_zone'] = $this->session->data['payment_address']['zone']; + $order_data['payment_zone_id'] = $this->session->data['payment_address']['zone_id']; + $order_data['payment_country'] = $this->session->data['payment_address']['country']; + $order_data['payment_country_id'] = $this->session->data['payment_address']['country_id']; + $order_data['payment_address_format'] = $this->session->data['payment_address']['address_format']; + $order_data['payment_custom_field'] = $this->session->data['payment_address']['custom_field']; + + if (isset($this->session->data['payment_method']['title'])) { + $order_data['payment_method'] = $this->session->data['payment_method']['title']; + } else { + $order_data['payment_method'] = ''; + } + + if (isset($this->session->data['payment_method']['code'])) { + $order_data['payment_code'] = $this->session->data['payment_method']['code']; + } else { + $order_data['payment_code'] = ''; + } + + // Shipping Details + if ($this->cart->hasShipping()) { + $order_data['shipping_firstname'] = $this->session->data['shipping_address']['firstname']; + $order_data['shipping_lastname'] = $this->session->data['shipping_address']['lastname']; + $order_data['shipping_company'] = $this->session->data['shipping_address']['company']; + $order_data['shipping_address_1'] = $this->session->data['shipping_address']['address_1']; + $order_data['shipping_address_2'] = $this->session->data['shipping_address']['address_2']; + $order_data['shipping_city'] = $this->session->data['shipping_address']['city']; + $order_data['shipping_postcode'] = $this->session->data['shipping_address']['postcode']; + $order_data['shipping_zone'] = $this->session->data['shipping_address']['zone']; + $order_data['shipping_zone_id'] = $this->session->data['shipping_address']['zone_id']; + $order_data['shipping_country'] = $this->session->data['shipping_address']['country']; + $order_data['shipping_country_id'] = $this->session->data['shipping_address']['country_id']; + $order_data['shipping_address_format'] = $this->session->data['shipping_address']['address_format']; + $order_data['shipping_custom_field'] = $this->session->data['shipping_address']['custom_field']; + + if (isset($this->session->data['shipping_method']['title'])) { + $order_data['shipping_method'] = $this->session->data['shipping_method']['title']; + } else { + $order_data['shipping_method'] = ''; + } + + if (isset($this->session->data['shipping_method']['code'])) { + $order_data['shipping_code'] = $this->session->data['shipping_method']['code']; + } else { + $order_data['shipping_code'] = ''; + } + } else { + $order_data['shipping_firstname'] = ''; + $order_data['shipping_lastname'] = ''; + $order_data['shipping_company'] = ''; + $order_data['shipping_address_1'] = ''; + $order_data['shipping_address_2'] = ''; + $order_data['shipping_city'] = ''; + $order_data['shipping_postcode'] = ''; + $order_data['shipping_zone'] = ''; + $order_data['shipping_zone_id'] = ''; + $order_data['shipping_country'] = ''; + $order_data['shipping_country_id'] = ''; + $order_data['shipping_address_format'] = ''; + $order_data['shipping_custom_field'] = array(); + $order_data['shipping_method'] = ''; + $order_data['shipping_code'] = ''; + } + + // Products + $order_data['products'] = array(); + + foreach ($this->cart->getProducts() as $product) { + $option_data = array(); + + foreach ($product['option'] as $option) { + $option_data[] = array( + 'product_option_id' => $option['product_option_id'], + 'product_option_value_id' => $option['product_option_value_id'], + 'option_id' => $option['option_id'], + 'option_value_id' => $option['option_value_id'], + 'name' => $option['name'], + 'value' => $option['value'], + 'type' => $option['type'] + ); + } + + $order_data['products'][] = array( + 'product_id' => $product['product_id'], + 'name' => $product['name'], + 'model' => $product['model'], + 'option' => $option_data, + 'download' => $product['download'], + 'quantity' => $product['quantity'], + 'subtract' => $product['subtract'], + 'price' => $product['price'], + 'total' => $product['total'], + 'tax' => $this->tax->getTax($product['price'], $product['tax_class_id']), + 'reward' => $product['reward'] + ); + } + + // Gift Voucher + $order_data['vouchers'] = array(); + + if (!empty($this->session->data['vouchers'])) { + foreach ($this->session->data['vouchers'] as $voucher) { + $order_data['vouchers'][] = array( + 'description' => $voucher['description'], + 'code' => token(10), + 'to_name' => $voucher['to_name'], + 'to_email' => $voucher['to_email'], + 'from_name' => $voucher['from_name'], + 'from_email' => $voucher['from_email'], + 'voucher_theme_id' => $voucher['voucher_theme_id'], + 'message' => $voucher['message'], + 'amount' => $voucher['amount'] + ); + } + } + + // Order Totals + $this->load->model('setting/extension'); + + $totals = array(); + $taxes = $this->cart->getTaxes(); + $total = 0; + + // Because __call can not keep var references so we put them into an array. + $total_data = array( + 'totals' => &$totals, + 'taxes' => &$taxes, + 'total' => &$total + ); + + $sort_order = array(); + + $results = $this->model_setting_extension->getExtensions('total'); + + foreach ($results as $key => $value) { + $sort_order[$key] = $this->config->get('total_' . $value['code'] . '_sort_order'); + } + + array_multisort($sort_order, SORT_ASC, $results); + + foreach ($results as $result) { + if ($this->config->get('total_' . $result['code'] . '_status')) { + $this->load->model('extension/total/' . $result['code']); + + // We have to put the totals in an array so that they pass by reference. + $this->{'model_extension_total_' . $result['code']}->getTotal($total_data); + } + } + + $sort_order = array(); + + foreach ($total_data['totals'] as $key => $value) { + $sort_order[$key] = $value['sort_order']; + } + + array_multisort($sort_order, SORT_ASC, $total_data['totals']); + + $order_data = array_merge($order_data, $total_data); + + if (isset($this->request->post['comment'])) { + $order_data['comment'] = $this->request->post['comment']; + } else { + $order_data['comment'] = ''; + } + + if (isset($this->request->post['affiliate_id'])) { + $subtotal = $this->cart->getSubTotal(); + + // Affiliate + $this->load->model('account/customer'); + + $affiliate_info = $this->model_account_customer->getAffiliate($this->request->post['affiliate_id']); + + if ($affiliate_info) { + $order_data['affiliate_id'] = $affiliate_info['customer_id']; + $order_data['commission'] = ($subtotal / 100) * $affiliate_info['commission']; + } else { + $order_data['affiliate_id'] = 0; + $order_data['commission'] = 0; + } + } else { + $order_data['affiliate_id'] = 0; + $order_data['commission'] = 0; + } + + $this->model_checkout_order->editOrder($order_id, $order_data); + + // Set the order history + if (isset($this->request->post['order_status_id'])) { + $order_status_id = $this->request->post['order_status_id']; + } else { + $order_status_id = $this->config->get('config_order_status_id'); + } + + $this->model_checkout_order->addOrderHistory($order_id, $order_status_id); + } + } else { + $json['error'] = $this->language->get('error_not_found'); + } + } + + $this->response->addHeader('Content-Type: application/json'); + $this->response->setOutput(json_encode($json)); + } + + public function delete() { + $this->load->language('api/order'); + + $json = array(); + + if (!isset($this->session->data['api_id'])) { + $json['error'] = $this->language->get('error_permission'); + } else { + $this->load->model('checkout/order'); + + if (isset($this->request->get['order_id'])) { + $order_id = $this->request->get['order_id']; + } else { + $order_id = 0; + } + + $order_info = $this->model_checkout_order->getOrder($order_id); + + if ($order_info) { + $this->model_checkout_order->deleteOrder($order_id); + + $json['success'] = $this->language->get('text_success'); + } else { + $json['error'] = $this->language->get('error_not_found'); + } + } + + $this->response->addHeader('Content-Type: application/json'); + $this->response->setOutput(json_encode($json)); + } + + public function info() { + $this->load->language('api/order'); + + $json = array(); + + if (!isset($this->session->data['api_id'])) { + $json['error'] = $this->language->get('error_permission'); + } else { + $this->load->model('checkout/order'); + + if (isset($this->request->get['order_id'])) { + $order_id = $this->request->get['order_id']; + } else { + $order_id = 0; + } + + $order_info = $this->model_checkout_order->getOrder($order_id); + + if ($order_info) { + $json['order'] = $order_info; + + $json['success'] = $this->language->get('text_success'); + } else { + $json['error'] = $this->language->get('error_not_found'); + } + } + + $this->response->addHeader('Content-Type: application/json'); + $this->response->setOutput(json_encode($json)); + } + + public function history() { + $this->load->language('api/order'); + + $json = array(); + + if (!isset($this->session->data['api_id'])) { + $json['error'] = $this->language->get('error_permission'); + } else { + // Add keys for missing post vars + $keys = array( + 'order_status_id', + 'notify', + 'override', + 'comment' + ); + + foreach ($keys as $key) { + if (!isset($this->request->post[$key])) { + $this->request->post[$key] = ''; + } + } + + $this->load->model('checkout/order'); + + if (isset($this->request->get['order_id'])) { + $order_id = $this->request->get['order_id']; + } else { + $order_id = 0; + } + + $order_info = $this->model_checkout_order->getOrder($order_id); + + if ($order_info) { + $this->model_checkout_order->addOrderHistory($order_id, $this->request->post['order_status_id'], $this->request->post['comment'], $this->request->post['notify'], $this->request->post['override']); + + $json['success'] = $this->language->get('text_success'); + } else { + $json['error'] = $this->language->get('error_not_found'); + } + } + + $this->response->addHeader('Content-Type: application/json'); + $this->response->setOutput(json_encode($json)); + } +}
\ No newline at end of file diff --git a/public/catalog/controller/api/payment.php b/public/catalog/controller/api/payment.php new file mode 100644 index 0000000..1f859a3 --- /dev/null +++ b/public/catalog/controller/api/payment.php @@ -0,0 +1,274 @@ +<?php +class ControllerApiPayment extends Controller { + public function address() { + $this->load->language('api/payment'); + + // Delete old payment address, payment methods and method so not to cause any issues if there is an error + unset($this->session->data['payment_address']); + unset($this->session->data['payment_methods']); + unset($this->session->data['payment_method']); + + $json = array(); + + if (!isset($this->session->data['api_id'])) { + $json['error']['warning'] = $this->language->get('error_permission'); + } else { + // Add keys for missing post vars + $keys = array( + 'firstname', + 'lastname', + 'company', + 'address_1', + 'address_2', + 'postcode', + 'city', + 'zone_id', + 'country_id' + ); + + foreach ($keys as $key) { + if (!isset($this->request->post[$key])) { + $this->request->post[$key] = ''; + } + } + + if ((utf8_strlen(trim($this->request->post['firstname'])) < 1) || (utf8_strlen(trim($this->request->post['firstname'])) > 32)) { + $json['error']['firstname'] = $this->language->get('error_firstname'); + } + + if ((utf8_strlen(trim($this->request->post['lastname'])) < 1) || (utf8_strlen(trim($this->request->post['lastname'])) > 32)) { + $json['error']['lastname'] = $this->language->get('error_lastname'); + } + + if ((utf8_strlen(trim($this->request->post['address_1'])) < 3) || (utf8_strlen(trim($this->request->post['address_1'])) > 128)) { + $json['error']['address_1'] = $this->language->get('error_address_1'); + } + + if ((utf8_strlen($this->request->post['city']) < 2) || (utf8_strlen($this->request->post['city']) > 32)) { + $json['error']['city'] = $this->language->get('error_city'); + } + + $this->load->model('localisation/country'); + + $country_info = $this->model_localisation_country->getCountry($this->request->post['country_id']); + + if ($country_info && $country_info['postcode_required'] && (utf8_strlen(trim($this->request->post['postcode'])) < 2 || utf8_strlen(trim($this->request->post['postcode'])) > 10)) { + $json['error']['postcode'] = $this->language->get('error_postcode'); + } + + if ($this->request->post['country_id'] == '') { + $json['error']['country'] = $this->language->get('error_country'); + } + + if (!isset($this->request->post['zone_id']) || $this->request->post['zone_id'] == '') { + $json['error']['zone'] = $this->language->get('error_zone'); + } + + // Custom field validation + $this->load->model('account/custom_field'); + + $custom_fields = $this->model_account_custom_field->getCustomFields($this->config->get('config_customer_group_id')); + + foreach ($custom_fields as $custom_field) { + if ($custom_field['location'] == 'address') { + if ($custom_field['required'] && empty($this->request->post['custom_field'][$custom_field['location']][$custom_field['custom_field_id']])) { + $json['error']['custom_field' . $custom_field['custom_field_id']] = sprintf($this->language->get('error_custom_field'), $custom_field['name']); + } elseif (($custom_field['type'] == 'text') && !empty($custom_field['validation']) && !filter_var($this->request->post['custom_field'][$custom_field['location']][$custom_field['custom_field_id']], FILTER_VALIDATE_REGEXP, array('options' => array('regexp' => $custom_field['validation'])))) { + $json['error']['custom_field' . $custom_field['custom_field_id']] = sprintf($this->language->get('error_custom_field'), $custom_field['name']); + } + } + } + + if (!$json) { + $this->load->model('localisation/country'); + + $country_info = $this->model_localisation_country->getCountry($this->request->post['country_id']); + + if ($country_info) { + $country = $country_info['name']; + $iso_code_2 = $country_info['iso_code_2']; + $iso_code_3 = $country_info['iso_code_3']; + $address_format = $country_info['address_format']; + } else { + $country = ''; + $iso_code_2 = ''; + $iso_code_3 = ''; + $address_format = ''; + } + + $this->load->model('localisation/zone'); + + $zone_info = $this->model_localisation_zone->getZone($this->request->post['zone_id']); + + if ($zone_info) { + $zone = $zone_info['name']; + $zone_code = $zone_info['code']; + } else { + $zone = ''; + $zone_code = ''; + } + + $this->session->data['payment_address'] = array( + 'firstname' => $this->request->post['firstname'], + 'lastname' => $this->request->post['lastname'], + 'company' => $this->request->post['company'], + 'address_1' => $this->request->post['address_1'], + 'address_2' => $this->request->post['address_2'], + 'postcode' => $this->request->post['postcode'], + 'city' => $this->request->post['city'], + 'zone_id' => $this->request->post['zone_id'], + 'zone' => $zone, + 'zone_code' => $zone_code, + 'country_id' => $this->request->post['country_id'], + 'country' => $country, + 'iso_code_2' => $iso_code_2, + 'iso_code_3' => $iso_code_3, + 'address_format' => $address_format, + 'custom_field' => isset($this->request->post['custom_field']) ? $this->request->post['custom_field'] : array() + ); + + $json['success'] = $this->language->get('text_address'); + + unset($this->session->data['payment_method']); + unset($this->session->data['payment_methods']); + } + } + + $this->response->addHeader('Content-Type: application/json'); + $this->response->setOutput(json_encode($json)); + } + + public function methods() { + $this->load->language('api/payment'); + + // Delete past shipping methods and method just in case there is an error + unset($this->session->data['payment_methods']); + unset($this->session->data['payment_method']); + + $json = array(); + + if (!isset($this->session->data['api_id'])) { + $json['error'] = $this->language->get('error_permission'); + } else { + // Payment Address + if (!isset($this->session->data['payment_address'])) { + $json['error'] = $this->language->get('error_address'); + } + + if (!$json) { + // Totals + $totals = array(); + $taxes = $this->cart->getTaxes(); + $total = 0; + + // Because __call can not keep var references so we put them into an array. + $total_data = array( + 'totals' => &$totals, + 'taxes' => &$taxes, + 'total' => &$total + ); + + $this->load->model('setting/extension'); + + $sort_order = array(); + + $results = $this->model_setting_extension->getExtensions('total'); + + foreach ($results as $key => $value) { + $sort_order[$key] = $this->config->get('total_' . $value['code'] . '_sort_order'); + } + + array_multisort($sort_order, SORT_ASC, $results); + + foreach ($results as $result) { + if ($this->config->get('total_' . $result['code'] . '_status')) { + $this->load->model('extension/total/' . $result['code']); + + // We have to put the totals in an array so that they pass by reference. + $this->{'model_extension_total_' . $result['code']}->getTotal($total_data); + } + } + + // Payment Methods + $json['payment_methods'] = array(); + + $this->load->model('setting/extension'); + + $results = $this->model_setting_extension->getExtensions('payment'); + + $recurring = $this->cart->hasRecurringProducts(); + + foreach ($results as $result) { + if ($this->config->get('payment_' . $result['code'] . '_status')) { + $this->load->model('extension/payment/' . $result['code']); + + $method = $this->{'model_extension_payment_' . $result['code']}->getMethod($this->session->data['payment_address'], $total); + + if ($method) { + if ($recurring) { + if (property_exists($this->{'model_extension_payment_' . $result['code']}, 'recurringPayments') && $this->{'model_extension_payment_' . $result['code']}->recurringPayments()) { + $json['payment_methods'][$result['code']] = $method; + } + } else { + $json['payment_methods'][$result['code']] = $method; + } + } + } + } + + $sort_order = array(); + + foreach ($json['payment_methods'] as $key => $value) { + $sort_order[$key] = $value['sort_order']; + } + + array_multisort($sort_order, SORT_ASC, $json['payment_methods']); + + if ($json['payment_methods']) { + $this->session->data['payment_methods'] = $json['payment_methods']; + } else { + $json['error'] = $this->language->get('error_no_payment'); + } + } + } + + $this->response->addHeader('Content-Type: application/json'); + $this->response->setOutput(json_encode($json)); + } + + public function method() { + $this->load->language('api/payment'); + + // Delete old payment method so not to cause any issues if there is an error + unset($this->session->data['payment_method']); + + $json = array(); + + if (!isset($this->session->data['api_id'])) { + $json['error'] = $this->language->get('error_permission'); + } else { + // Payment Address + if (!isset($this->session->data['payment_address'])) { + $json['error'] = $this->language->get('error_address'); + } + + // Payment Method + if (empty($this->session->data['payment_methods'])) { + $json['error'] = $this->language->get('error_no_payment'); + } elseif (!isset($this->request->post['payment_method'])) { + $json['error'] = $this->language->get('error_method'); + } elseif (!isset($this->session->data['payment_methods'][$this->request->post['payment_method']])) { + $json['error'] = $this->language->get('error_method'); + } + + if (!$json) { + $this->session->data['payment_method'] = $this->session->data['payment_methods'][$this->request->post['payment_method']]; + + $json['success'] = $this->language->get('text_method'); + } + } + + $this->response->addHeader('Content-Type: application/json'); + $this->response->setOutput(json_encode($json)); + } +} diff --git a/public/catalog/controller/api/reward.php b/public/catalog/controller/api/reward.php new file mode 100644 index 0000000..94c61ac --- /dev/null +++ b/public/catalog/controller/api/reward.php @@ -0,0 +1,82 @@ +<?php +class ControllerApiReward extends Controller { + public function index() { + $this->load->language('api/reward'); + + // Delete past reward in case there is an error + unset($this->session->data['reward']); + + $json = array(); + + if (!isset($this->session->data['api_id'])) { + $json['error'] = $this->language->get('error_permission'); + } else { + $points = $this->customer->getRewardPoints(); + + $points_total = 0; + + foreach ($this->cart->getProducts() as $product) { + if ($product['points']) { + $points_total += $product['points']; + } + } + + if (empty($this->request->post['reward'])) { + $json['error'] = $this->language->get('error_reward'); + } + + if ($this->request->post['reward'] > $points) { + $json['error'] = sprintf($this->language->get('error_points'), $this->request->post['reward']); + } + + if ($this->request->post['reward'] > $points_total) { + $json['error'] = sprintf($this->language->get('error_maximum'), $points_total); + } + + if (!$json) { + $this->session->data['reward'] = abs($this->request->post['reward']); + + $json['success'] = $this->language->get('text_success'); + } + } + + $this->response->addHeader('Content-Type: application/json'); + $this->response->setOutput(json_encode($json)); + } + + public function maximum() { + $this->load->language('api/reward'); + + $json = array(); + + if (!isset($this->session->data['api_id'])) { + $json['error'] = $this->language->get('error_permission'); + } else { + $json['maximum'] = 0; + + foreach ($this->cart->getProducts() as $product) { + if ($product['points']) { + $json['maximum'] += $product['points']; + } + } + } + + $this->response->addHeader('Content-Type: application/json'); + $this->response->setOutput(json_encode($json)); + } + + public function available() { + $this->load->language('api/reward'); + + $json = array(); + + if (!isset($this->session->data['api_id'])) { + $json['error'] = $this->language->get('error_permission'); + } else { + $json['points'] = $this->customer->getRewardPoints(); + } + + $this->response->addHeader('Content-Type: application/json'); + $this->response->setOutput(json_encode($json)); + } +} diff --git a/public/catalog/controller/api/shipping.php b/public/catalog/controller/api/shipping.php new file mode 100644 index 0000000..56d371f --- /dev/null +++ b/public/catalog/controller/api/shipping.php @@ -0,0 +1,251 @@ +<?php +class ControllerApiShipping extends Controller { + public function address() { + $this->load->language('api/shipping'); + + // Delete old shipping address, shipping methods and method so not to cause any issues if there is an error + unset($this->session->data['shipping_address']); + unset($this->session->data['shipping_methods']); + unset($this->session->data['shipping_method']); + + $json = array(); + + if ($this->cart->hasShipping()) { + if (!isset($this->session->data['api_id'])) { + $json['error']['warning'] = $this->language->get('error_permission'); + } else { + // Add keys for missing post vars + $keys = array( + 'firstname', + 'lastname', + 'company', + 'address_1', + 'address_2', + 'postcode', + 'city', + 'zone_id', + 'country_id' + ); + + foreach ($keys as $key) { + if (!isset($this->request->post[$key])) { + $this->request->post[$key] = ''; + } + } + + if ((utf8_strlen(trim($this->request->post['firstname'])) < 1) || (utf8_strlen(trim($this->request->post['firstname'])) > 32)) { + $json['error']['firstname'] = $this->language->get('error_firstname'); + } + + if ((utf8_strlen(trim($this->request->post['lastname'])) < 1) || (utf8_strlen(trim($this->request->post['lastname'])) > 32)) { + $json['error']['lastname'] = $this->language->get('error_lastname'); + } + + if ((utf8_strlen(trim($this->request->post['address_1'])) < 3) || (utf8_strlen(trim($this->request->post['address_1'])) > 128)) { + $json['error']['address_1'] = $this->language->get('error_address_1'); + } + + if ((utf8_strlen($this->request->post['city']) < 2) || (utf8_strlen($this->request->post['city']) > 32)) { + $json['error']['city'] = $this->language->get('error_city'); + } + + $this->load->model('localisation/country'); + + $country_info = $this->model_localisation_country->getCountry($this->request->post['country_id']); + + if ($country_info && $country_info['postcode_required'] && (utf8_strlen(trim($this->request->post['postcode'])) < 2 || utf8_strlen(trim($this->request->post['postcode'])) > 10)) { + $json['error']['postcode'] = $this->language->get('error_postcode'); + } + + if ($this->request->post['country_id'] == '') { + $json['error']['country'] = $this->language->get('error_country'); + } + + if (!isset($this->request->post['zone_id']) || $this->request->post['zone_id'] == '') { + $json['error']['zone'] = $this->language->get('error_zone'); + } + + // Custom field validation + $this->load->model('account/custom_field'); + + $custom_fields = $this->model_account_custom_field->getCustomFields($this->config->get('config_customer_group_id')); + + foreach ($custom_fields as $custom_field) { + if ($custom_field['location'] == 'address') { + if ($custom_field['required'] && empty($this->request->post['custom_field'][$custom_field['location']][$custom_field['custom_field_id']])) { + $json['error']['custom_field' . $custom_field['custom_field_id']] = sprintf($this->language->get('error_custom_field'), $custom_field['name']); + } elseif (($custom_field['type'] == 'text') && !empty($custom_field['validation']) && !filter_var($this->request->post['custom_field'][$custom_field['location']][$custom_field['custom_field_id']], FILTER_VALIDATE_REGEXP, array('options' => array('regexp' => $custom_field['validation'])))) { + $json['error']['custom_field' . $custom_field['custom_field_id']] = sprintf($this->language->get('error_custom_field'), $custom_field['name']); + } + } + } + + if (!$json) { + $this->load->model('localisation/country'); + + $country_info = $this->model_localisation_country->getCountry($this->request->post['country_id']); + + if ($country_info) { + $country = $country_info['name']; + $iso_code_2 = $country_info['iso_code_2']; + $iso_code_3 = $country_info['iso_code_3']; + $address_format = $country_info['address_format']; + } else { + $country = ''; + $iso_code_2 = ''; + $iso_code_3 = ''; + $address_format = ''; + } + + $this->load->model('localisation/zone'); + + $zone_info = $this->model_localisation_zone->getZone($this->request->post['zone_id']); + + if ($zone_info) { + $zone = $zone_info['name']; + $zone_code = $zone_info['code']; + } else { + $zone = ''; + $zone_code = ''; + } + + $this->session->data['shipping_address'] = array( + 'firstname' => $this->request->post['firstname'], + 'lastname' => $this->request->post['lastname'], + 'company' => $this->request->post['company'], + 'address_1' => $this->request->post['address_1'], + 'address_2' => $this->request->post['address_2'], + 'postcode' => $this->request->post['postcode'], + 'city' => $this->request->post['city'], + 'zone_id' => $this->request->post['zone_id'], + 'zone' => $zone, + 'zone_code' => $zone_code, + 'country_id' => $this->request->post['country_id'], + 'country' => $country, + 'iso_code_2' => $iso_code_2, + 'iso_code_3' => $iso_code_3, + 'address_format' => $address_format, + 'custom_field' => isset($this->request->post['custom_field']) ? $this->request->post['custom_field'] : array() + ); + + $json['success'] = $this->language->get('text_address'); + + unset($this->session->data['shipping_method']); + unset($this->session->data['shipping_methods']); + } + } + } + + $this->response->addHeader('Content-Type: application/json'); + $this->response->setOutput(json_encode($json)); + } + + public function methods() { + $this->load->language('api/shipping'); + + // Delete past shipping methods and method just in case there is an error + unset($this->session->data['shipping_methods']); + unset($this->session->data['shipping_method']); + + $json = array(); + + if (!isset($this->session->data['api_id'])) { + $json['error'] = $this->language->get('error_permission'); + } elseif ($this->cart->hasShipping()) { + if (!isset($this->session->data['shipping_address'])) { + $json['error'] = $this->language->get('error_address'); + } + + if (!$json) { + // Shipping Methods + $json['shipping_methods'] = array(); + + $this->load->model('setting/extension'); + + $results = $this->model_setting_extension->getExtensions('shipping'); + + foreach ($results as $result) { + if ($this->config->get('shipping_' . $result['code'] . '_status')) { + $this->load->model('extension/shipping/' . $result['code']); + + $quote = $this->{'model_extension_shipping_' . $result['code']}->getQuote($this->session->data['shipping_address']); + + if ($quote) { + $json['shipping_methods'][$result['code']] = array( + 'title' => $quote['title'], + 'quote' => $quote['quote'], + 'sort_order' => $quote['sort_order'], + 'error' => $quote['error'] + ); + } + } + } + + $sort_order = array(); + + foreach ($json['shipping_methods'] as $key => $value) { + $sort_order[$key] = $value['sort_order']; + } + + array_multisort($sort_order, SORT_ASC, $json['shipping_methods']); + + if ($json['shipping_methods']) { + $this->session->data['shipping_methods'] = $json['shipping_methods']; + } else { + $json['error'] = $this->language->get('error_no_shipping'); + } + } + } else { + $json['shipping_methods'] = array(); + } + + $this->response->addHeader('Content-Type: application/json'); + $this->response->setOutput(json_encode($json)); + } + + public function method() { + $this->load->language('api/shipping'); + + // Delete old shipping method so not to cause any issues if there is an error + unset($this->session->data['shipping_method']); + + $json = array(); + + if (!isset($this->session->data['api_id'])) { + $json['error'] = $this->language->get('error_permission'); + } else { + if ($this->cart->hasShipping()) { + // Shipping Address + if (!isset($this->session->data['shipping_address'])) { + $json['error'] = $this->language->get('error_address'); + } + + // Shipping Method + if (empty($this->session->data['shipping_methods'])) { + $json['error'] = $this->language->get('error_no_shipping'); + } elseif (!isset($this->request->post['shipping_method'])) { + $json['error'] = $this->language->get('error_method'); + } else { + $shipping = explode('.', $this->request->post['shipping_method']); + + if (!isset($shipping[0]) || !isset($shipping[1]) || !isset($this->session->data['shipping_methods'][$shipping[0]]['quote'][$shipping[1]])) { + $json['error'] = $this->language->get('error_method'); + } + } + + if (!$json) { + $this->session->data['shipping_method'] = $this->session->data['shipping_methods'][$shipping[0]]['quote'][$shipping[1]]; + + $json['success'] = $this->language->get('text_method'); + } + } else { + unset($this->session->data['shipping_address']); + unset($this->session->data['shipping_method']); + unset($this->session->data['shipping_methods']); + } + } + + $this->response->addHeader('Content-Type: application/json'); + $this->response->setOutput(json_encode($json)); + } +} diff --git a/public/catalog/controller/api/voucher.php b/public/catalog/controller/api/voucher.php new file mode 100644 index 0000000..3477cfc --- /dev/null +++ b/public/catalog/controller/api/voucher.php @@ -0,0 +1,137 @@ +<?php +class ControllerApiVoucher extends Controller { + public function index() { + $this->load->language('api/voucher'); + + // Delete past voucher in case there is an error + unset($this->session->data['voucher']); + + $json = array(); + + if (!isset($this->session->data['api_id'])) { + $json['error'] = $this->language->get('error_permission'); + } else { + $this->load->model('extension/total/voucher'); + + if (isset($this->request->post['voucher'])) { + $voucher = $this->request->post['voucher']; + } else { + $voucher = ''; + } + + $voucher_info = $this->model_extension_total_voucher->getVoucher($voucher); + + if ($voucher_info) { + $this->session->data['voucher'] = $this->request->post['voucher']; + + $json['success'] = $this->language->get('text_success'); + } else { + $json['error'] = $this->language->get('error_voucher'); + } + } + + $this->response->addHeader('Content-Type: application/json'); + $this->response->setOutput(json_encode($json)); + } + + public function add() { + $this->load->language('api/voucher'); + + $json = array(); + + if (!isset($this->session->data['api_id'])) { + $json['error']['warning'] = $this->language->get('error_permission'); + } else { + // Add keys for missing post vars + $keys = array( + 'from_name', + 'from_email', + 'to_name', + 'to_email', + 'voucher_theme_id', + 'message', + 'amount' + ); + + foreach ($keys as $key) { + if (!isset($this->request->post[$key])) { + $this->request->post[$key] = ''; + } + } + + if (isset($this->request->post['voucher'])) { + $this->session->data['vouchers'] = array(); + + foreach ($this->request->post['voucher'] as $voucher) { + if (isset($voucher['code']) && isset($voucher['to_name']) && isset($voucher['to_email']) && isset($voucher['from_name']) && isset($voucher['from_email']) && isset($voucher['voucher_theme_id']) && isset($voucher['message']) && isset($voucher['amount'])) { + $this->session->data['vouchers'][$voucher['code']] = array( + 'code' => $voucher['code'], + 'description' => sprintf($this->language->get('text_for'), $this->currency->format($this->currency->convert($voucher['amount'], $this->session->data['currency'], $this->config->get('config_currency')), $this->session->data['currency']), $voucher['to_name']), + 'to_name' => $voucher['to_name'], + 'to_email' => $voucher['to_email'], + 'from_name' => $voucher['from_name'], + 'from_email' => $voucher['from_email'], + 'voucher_theme_id' => $voucher['voucher_theme_id'], + 'message' => $voucher['message'], + 'amount' => $this->currency->convert($voucher['amount'], $this->session->data['currency'], $this->config->get('config_currency')) + ); + } + } + + $json['success'] = $this->language->get('text_cart'); + + unset($this->session->data['shipping_method']); + unset($this->session->data['shipping_methods']); + unset($this->session->data['payment_method']); + unset($this->session->data['payment_methods']); + } else { + // Add a new voucher if set + if ((utf8_strlen($this->request->post['from_name']) < 1) || (utf8_strlen($this->request->post['from_name']) > 64)) { + $json['error']['from_name'] = $this->language->get('error_from_name'); + } + + if ((utf8_strlen($this->request->post['from_email']) > 96) || !filter_var($this->request->post['from_email'], FILTER_VALIDATE_EMAIL)) { + $json['error']['from_email'] = $this->language->get('error_email'); + } + + if ((utf8_strlen($this->request->post['to_name']) < 1) || (utf8_strlen($this->request->post['to_name']) > 64)) { + $json['error']['to_name'] = $this->language->get('error_to_name'); + } + + if ((utf8_strlen($this->request->post['to_email']) > 96) || !filter_var($this->request->post['to_email'], FILTER_VALIDATE_EMAIL)) { + $json['error']['to_email'] = $this->language->get('error_email'); + } + + if (($this->request->post['amount'] < $this->config->get('config_voucher_min')) || ($this->request->post['amount'] > $this->config->get('config_voucher_max'))) { + $json['error']['amount'] = sprintf($this->language->get('error_amount'), $this->currency->format($this->config->get('config_voucher_min'), $this->session->data['currency']), $this->currency->format($this->config->get('config_voucher_max'), $this->session->data['currency'])); + } + + if (!$json) { + $code = mt_rand(); + + $this->session->data['vouchers'][$code] = array( + 'code' => $code, + 'description' => sprintf($this->language->get('text_for'), $this->currency->format($this->currency->convert($this->request->post['amount'], $this->session->data['currency'], $this->config->get('config_currency')), $this->session->data['currency']), $this->request->post['to_name']), + 'to_name' => $this->request->post['to_name'], + 'to_email' => $this->request->post['to_email'], + 'from_name' => $this->request->post['from_name'], + 'from_email' => $this->request->post['from_email'], + 'voucher_theme_id' => $this->request->post['voucher_theme_id'], + 'message' => $this->request->post['message'], + 'amount' => $this->currency->convert($this->request->post['amount'], $this->session->data['currency'], $this->config->get('config_currency')) + ); + + $json['success'] = $this->language->get('text_cart'); + + unset($this->session->data['shipping_method']); + unset($this->session->data['shipping_methods']); + unset($this->session->data['payment_method']); + unset($this->session->data['payment_methods']); + } + } + } + + $this->response->addHeader('Content-Type: application/json'); + $this->response->setOutput(json_encode($json)); + } +} |