shop.balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

amazonus.php (22477B)


      1 <?php
      2 class ControllerExtensionOpenbayAmazonus extends Controller {
      3 	public function order() {
      4 		if ($this->config->get('openbay_amazonus_status') != '1') {
      5 			return;
      6 		}
      7 
      8 		$this->load->model('checkout/order');
      9 		$this->load->model('extension/openbay/amazonus_order');
     10 		$this->load->language('extension/openbay/amazonus_order');
     11 
     12 		$logger = new Log('amazonus.log');
     13 		$logger->write('amazonus/order - started');
     14 
     15 		$incoming_token = isset($this->request->post['token']) ? $this->request->post['token'] : '';
     16 
     17 		if (!hash_equals($this->config->get('openbay_amazonus_token'), $incoming_token)) {
     18 			$logger->write('amazon/order - Incorrect token: ' . $incoming_token);
     19 			return;
     20 		}
     21 
     22         $decrypted = $this->openbay->decrypt($this->request->post['data'], $this->openbay->amazonus->getEncryptionKey(), $this->openbay->amazonus->getEncryptionIv(), false);
     23 
     24 		if (!$decrypted) {
     25 			$logger->write('amazonus/order Failed to decrypt data');
     26 			return;
     27 		}
     28 
     29 		$order_xml = simplexml_load_string(base64_decode($decrypted));
     30 
     31 		$amazonus_order_status = trim(strtolower((string)$order_xml->Status));
     32 
     33 		$amazonus_order_id = (string)$order_xml->AmazonOrderId;
     34 		$order_status = $this->model_extension_openbay_amazonus_order->getMappedStatus((string)$order_xml->Status);
     35 
     36 		$logger->write('Received order ' . $amazonus_order_id);
     37 
     38 		$order_id = $this->model_extension_openbay_amazonus_order->getOrderId($amazonus_order_id);
     39 
     40 		// If the order already exists on opencart, ignore it.
     41 		if ($order_id) {
     42 			$logger->write("Duplicate order $amazonus_order_id. Terminating.");
     43 			$this->response->setOutput('Ok');
     44 			return;
     45 		}
     46 
     47 		/* Check if order comes from subscribed marketplace */
     48 
     49 		$currency_to = $this->config->get('config_currency');
     50 		$order_currency = (string)$order_xml->Payment->CurrencyCode;
     51 
     52 		$products = array();
     53 
     54 		$products_total = 0;
     55 		$products_shipping = 0;
     56 		$products_tax = 0;
     57 		$products_shipping_tax = 0;
     58 		$gift_wrap = 0;
     59 		$gift_wrap_tax = 0;
     60 
     61 		$product_count = 0;
     62 
     63 		$amazonus_order_id = (string)$order_xml->AmazonOrderId;
     64 
     65 		/* SKU => ORDER_ITEM_ID */
     66 		$product_mapping = array();
     67 		$product_gift_messages = array();
     68 
     69 		foreach ($order_xml->Items->Item as $item) {
     70 
     71 			$total_price = $this->currency->convert((double)$item->Totals->Price, $order_currency, $currency_to);
     72 			$tax_total = (double)$item->Totals->Tax;
     73 
     74 			if ($tax_total == 0 && $this->config->get('openbay_amazonus_order_tax') > 0) {
     75 				$tax_total = (double)$item->Totals->Price * ($this->config->get('openbay_amazonus_order_tax') / 100) / (1 + $this->config->get('openbay_amazonus_order_tax') / 100);
     76 			}
     77 
     78 			$tax_total = $this->currency->convert($tax_total, $order_currency, $currency_to);
     79 
     80 			$products_total += $total_price;
     81 			$products_tax += $tax_total;
     82 
     83 			$products_shipping += $this->currency->convert((double)$item->Totals->Shipping, $order_currency, $currency_to);
     84 
     85 			$shipping_tax = (double)$item->Totals->ShippingTax;
     86 
     87 			if ($shipping_tax == 0 && $this->config->get('openbay_amazonus_order_tax') > 0) {
     88 				$shipping_tax = (double)$item->Totals->Shipping * ($this->config->get('openbay_amazonus_order_tax') / 100) / (1 + $this->config->get('openbay_amazonus_order_tax') / 100);
     89 			}
     90 
     91 			$products_shipping_tax += $this->currency->convert($shipping_tax, $order_currency, $currency_to);
     92 
     93 			$gift_wrap += $this->currency->convert((double)$item->Totals->GiftWrap, $order_currency, $currency_to);
     94 
     95 			$item_gift_wrap_tax = (double)$item->Totals->GiftWrapTax;
     96 
     97 			if ($item_gift_wrap_tax == 0 && $this->config->get('openbay_amazonus_order_tax') > 0) {
     98 				$item_gift_wrap_tax = (double)$item->Totals->GiftWrap * ($this->config->get('openbay_amazonus_order_tax') / 100) / (1 + $this->config->get('openbay_amazonus_order_tax') / 100);
     99 			}
    100 
    101 			$gift_wrap_tax += $this->currency->convert($item_gift_wrap_tax, $order_currency, $currency_to);
    102 
    103 			$product_count += (int)$item->Ordered;
    104 
    105 			if ((int)$item->Ordered == 0) {
    106 				continue;
    107 			}
    108 
    109 			$product_id = $this->model_extension_openbay_amazonus_order->getProductId((string)$item->Sku);
    110 			$product_var = $this->model_extension_openbay_amazonus_order->getProductVar((string)$item->Sku);
    111 
    112 			$products[] = array(
    113 				'product_id' => $product_id,
    114 				'var' => $product_var,
    115 				'sku' => (string)$item->Sku,
    116 				'asin' => (string)$item->Asin,
    117 				'order_item_id' => (string)$item->OrderItemId,
    118 				'name' => (string)$item->Title,
    119 				'model' => (string)$item->Sku,
    120 				'quantity' => (int)$item->Ordered,
    121 				'price' => sprintf('%.4f', ($total_price - $tax_total) / (int)$item->Ordered),
    122 				'total' => sprintf('%.4f', $total_price - $tax_total),
    123 				'tax' => $tax_total / (int)$item->Ordered,
    124 				'reward' => '0',
    125 				'option' => $this->model_extension_openbay_amazonus_order->getProductOptionsByVar($product_var),
    126 				'download' => array(),
    127 			);
    128 
    129 			$product_mapping[(string)$item->Sku] = (string)$item->OrderItemId;
    130 
    131 			if ($item->GiftMessage != '') {
    132 				$product_gift_messages[] = (string)$item->Title . ' : ' . (string)$item->GiftMessage;
    133 			}
    134 		}
    135 
    136 		$order_comment = '';
    137 		if (count($product_gift_messages) > 0) {
    138 			$order_comment = $this->language->get('text_gift_message') . '<br />' . implode('<br />', $product_gift_messages);
    139 		}
    140 
    141 		$total = sprintf('%.4f', $this->currency->convert((double)$order_xml->Payment->Amount, $order_currency, $currency_to));
    142 
    143 		$address_line_2 = (string)$order_xml->Shipping->AddressLine2;
    144 		if ((string)$order_xml->Shipping->AddressLine3 != '') {
    145 			$address_line_2 .= ', ' . (string)$order_xml->Shipping->AddressLine3;
    146 		}
    147 
    148 		$customer_info = $this->db->query("SELECT `customer_id` FROM " . DB_PREFIX . "customer WHERE email = '" . $this->db->escape((string)$order_xml->Payment->Email) . "'")->row;
    149 		$customer_id = '0';
    150 
    151 		if(isset($customer_info['customer_id'])) {
    152 			$customer_id = $customer_info['customer_id'];
    153 		} else {
    154 			/* Add a new customer */
    155 			$customer_data = array(
    156 				'firstname' => (string)$order_xml->Shipping->Name,
    157 				'lastname' => '',
    158 				'email' => (string)$order_xml->Payment->Email,
    159 				'telephone' => (string)$order_xml->Shipping->Phone,
    160 				'fax' => '',
    161 				'newsletter' => '0',
    162 				'customer_group_id' => $this->config->get('openbay_amazonus_order_customer_group'),
    163 				'password' => '',
    164 				'status' => '0',
    165 			);
    166 
    167 			$this->db->query("
    168 				INSERT INTO " . DB_PREFIX . "customer
    169 				SET firstname = '" . $this->db->escape($customer_data['firstname']) . "',
    170 					lastname = '" . $this->db->escape($customer_data['lastname']) . "',
    171 					email = '" . $this->db->escape($customer_data['email']) . "',
    172 					telephone = '" . $this->db->escape($customer_data['telephone']) . "',
    173 					newsletter = '" . (int)$customer_data['newsletter'] . "',
    174 					customer_group_id = '" . (int)$customer_data['customer_group_id'] . "',
    175 					password = '',
    176 					status = '" . (int)$customer_data['status'] . "',
    177 					date_added = NOW()");
    178 
    179 			$customer_id = $this->db->getLastId();
    180 		}
    181 
    182 		$shipping_first_name = (string)$order_xml->Shipping->FirstName;
    183 		$shipping_last_name = (string)$order_xml->Shipping->LastName;
    184 
    185 		if (empty($shipping_first_name) || empty($shipping_last_name)) {
    186 			$shipping_first_name = (string)$order_xml->Shipping->Name;
    187 			$shipping_last_name = '';
    188 		}
    189 
    190 		$order = array(
    191 			'invoice_prefix' => $this->config->get('config_invoice_prefix'),
    192 			'store_id' => $this->config->get('config_store_id'),
    193 			'store_name' => $this->config->get('config_name') . ' / Amazon US',
    194 			'store_url' => $this->config->get('config_url'),
    195 			'customer_id' => (int)$customer_id,
    196 			'customer_group_id' => $this->config->get('openbay_amazonus_order_customer_group'),
    197 			'firstname' => $shipping_first_name,
    198 			'lastname' => $shipping_last_name,
    199 			'email' => (string)$order_xml->Payment->Email,
    200 			'telephone' => (string)$order_xml->Shipping->Phone,
    201 			'shipping_firstname' => $shipping_first_name,
    202 			'shipping_lastname' => $shipping_last_name,
    203 			'shipping_company' => '',
    204 			'shipping_address_1' => (string)$order_xml->Shipping->AddressLine1,
    205 			'shipping_address_2' => $address_line_2,
    206 			'shipping_city' => (string)$order_xml->Shipping->City,
    207 			'shipping_postcode' => (string)$order_xml->Shipping->PostCode,
    208 			'shipping_country' => $this->model_extension_openbay_amazonus_order->getCountryName((string)$order_xml->Shipping->CountryCode),
    209 			'shipping_country_id' => $this->model_extension_openbay_amazonus_order->getCountryId((string)$order_xml->Shipping->CountryCode),
    210 			'shipping_zone' => (string)$order_xml->Shipping->State,
    211 			'shipping_zone_id' => $this->model_extension_openbay_amazonus_order->getZoneId((string)$order_xml->Shipping->State),
    212 			'shipping_address_format' => '',
    213 			'shipping_method' => (string)$order_xml->Shipping->Type,
    214 			'shipping_code' => 'amazonus.' . (string)$order_xml->Shipping->Type,
    215 			'payment_firstname' => $shipping_first_name,
    216 			'payment_lastname' => $shipping_last_name,
    217 			'payment_company' => '',
    218 			'payment_address_1' => (string)$order_xml->Shipping->AddressLine1,
    219 			'payment_address_2' => $address_line_2,
    220 			'payment_city' => (string)$order_xml->Shipping->City,
    221 			'payment_postcode' => (string)$order_xml->Shipping->PostCode,
    222 			'payment_country' => $this->model_extension_openbay_amazonus_order->getCountryName((string)$order_xml->Shipping->CountryCode),
    223 			'payment_country_id' => $this->model_extension_openbay_amazonus_order->getCountryId((string)$order_xml->Shipping->CountryCode),
    224 			'payment_zone' => (string)$order_xml->Shipping->State,
    225 			'payment_zone_id' => $this->model_extension_openbay_amazonus_order->getZoneId((string)$order_xml->Shipping->State),
    226 			'payment_address_format' => '',
    227 			'payment_method' => $this->language->get('text_paid_amazon'),
    228 			'payment_code' => 'amazonus.amazonus',
    229 			'payment_company_id' => 0,
    230 			'payment_tax_id' => 0,
    231 			'comment' => $order_comment,
    232 			'total' => $total,
    233 			'affiliate_id' => '0',
    234 			'commission' => '0.00',
    235 			'language_id' => (int)$this->config->get('config_language_id'),
    236 			'currency_id' => $this->currency->getId($order_currency),
    237 			'currency_code' => (string)$order_currency,
    238 			'currency_value' => $this->currency->getValue($order_currency),
    239 			'ip' => '',
    240 			'forwarded_ip' => '',
    241 			'user_agent' => 'OpenBay Pro for Amazon US',
    242 			'accept_language' => '',
    243 			'products' => $products,
    244 			'vouchers' => array(),
    245             'marketing_id' => 0,
    246             'tracking' => 0,
    247 			'totals' => array(
    248 				array(
    249 					'code' => 'sub_total',
    250 					'title' => $this->language->get('text_total_sub'),
    251 					'value' => sprintf('%.4f', $products_total),
    252 					'sort_order' => '1',
    253 				),
    254 				array(
    255 					'code' => 'shipping',
    256 					'title' => $this->language->get('text_total_shipping'),
    257 					'value' => sprintf('%.4f', $products_shipping),
    258 					'sort_order' => '3',
    259 				),
    260 				array(
    261 					'code' => 'tax',
    262 					'title' => $this->language->get('text_tax'),
    263 					'value' => sprintf('%.4f', $products_tax),
    264 					'sort_order' => '4',
    265 				),
    266 				array(
    267 					'code' => 'shipping_tax',
    268 					'title' => $this->language->get('text_total_shipping_tax'),
    269 					'value' => sprintf('%.4f', $products_shipping_tax),
    270 					'sort_order' => '6',
    271 				),
    272 				array(
    273 					'code' => 'gift_wrap',
    274 					'title' => $this->language->get('text_total_giftwrap'),
    275 					'value' => sprintf('%.4f', $gift_wrap),
    276 					'sort_order' => '2',
    277 				),
    278 				array(
    279 					'code' => 'gift_wrap_tax',
    280 					'title' => $this->language->get('text_total_giftwrap_tax'),
    281 					'value' => sprintf('%.4f', $gift_wrap_tax),
    282 					'sort_order' => '5',
    283 				),
    284 				array(
    285 					'code' => 'total',
    286 					'title' => $this->language->get('text_total'),
    287 					'value' => sprintf('%.4f', $total),
    288 					'sort_order' => '7',
    289 				),
    290 			),
    291 		);
    292 
    293 		$order_id = $this->model_checkout_order->addOrder($order);
    294 
    295 		$this->model_extension_openbay_amazonus_order->updateOrderStatus($order_id, $order_status);
    296 		$this->model_extension_openbay_amazonus_order->addAmazonusOrder($order_id, $amazonus_order_id);
    297 		$this->model_extension_openbay_amazonus_order->addAmazonusOrderProducts($order_id, $product_mapping);
    298 
    299 		foreach($products as $product) {
    300 			if($product['product_id'] != 0) {
    301 				$this->model_extension_openbay_amazonus_order->decreaseProductQuantity($product['product_id'], $product['quantity'], $product['var']);
    302 			}
    303 		}
    304 
    305 		$logger->write('Order ' . $amazonus_order_id . ' was added to the database (ID: ' . $order_id . ')');
    306 		$logger->write("Finished processing the order");
    307 
    308 		$this->model_extension_openbay_amazonus_order->acknowledgeOrder($order_id);
    309 
    310 		//send an email to the administrator about the sale
    311 		if ($this->config->get('openbay_amazonus_notify_admin') == 1){
    312 			$this->openbay->newOrderAdminNotify($order_id, $order_status);
    313 		}
    314 
    315 		$this->event->trigger('model/checkout/order/addOrderHistory/after', array('model/checkout/order/addOrderHistory/after', array($order_id, $order_status)));
    316 
    317 		$logger->write("Ok");
    318 		$this->response->setOutput('Ok');
    319 	}
    320 
    321 	public function listing() {
    322 		if ($this->config->get('openbay_amazonus_status') != '1') {
    323 			return;
    324 		}
    325 
    326 		$this->load->model('extension/openbay/amazonus_listing');
    327 		$this->load->model('extension/openbay/amazonus_product');
    328 
    329 		$logger = new Log('amazonus_listing.log');
    330 		$logger->write('amazonus/listing - started');
    331 
    332 		$incoming_token = isset($this->request->post['token']) ? $this->request->post['token'] : '';
    333 
    334 		if (!hash_equals($this->config->get('openbay_amazonus_token'), $incoming_token)) {
    335 			$logger->write('amazonus/listing - Incorrect token: ' . $incoming_token);
    336 			return;
    337 		}
    338 
    339         $decrypted = $this->openbay->decrypt($this->request->post['data'], $this->openbay->amazonus->getEncryptionKey(), $this->openbay->amazonus->getEncryptionIv(), false);
    340 
    341 		if (!$decrypted) {
    342 			$logger->write('amazonus/order Failed to decrypt data');
    343 			return;
    344 		}
    345 
    346 		$data = json_decode($decrypted, 1);
    347 
    348 		$logger->write("Received data: " . print_r($data, 1));
    349 
    350 		if ($data['status']) {
    351 			$logger->write("Updating " . $data['product_id'] . ' as successful');
    352 			$this->model_extension_openbay_amazonus_listing->listingSuccessful($data['product_id']);
    353 			$this->model_extension_openbay_amazonus_product->linkProduct($data['sku'], $data['product_id']);
    354 			$logger->write("Updated successfully");
    355 		} else {
    356 			$logger->write("Updating " . $data['product_id'] . ' as failed');
    357 			$this->model_extension_openbay_amazonus_listing->listingFailed($data['product_id'], $data['messages']);
    358 			$logger->write("Updated successfully");
    359 		}
    360 	}
    361 
    362 	public function listingReport() {
    363 		if ($this->config->get('openbay_amazonus_status') != '1') {
    364 			return;
    365 		}
    366 
    367 		$this->load->model('extension/openbay/amazonus_product');
    368 
    369 		$logger = new Log('amazonus.log');
    370 		$logger->write('amazonus/listing_reports - started');
    371 
    372 		$incoming_token = isset($this->request->post['token']) ? $this->request->post['token'] : '';
    373 
    374 		if (!hash_equals($this->config->get('openbay_amazonus_token'), $incoming_token)) {
    375 			$logger->write('amazonus/listing_reports - Incorrect token: ' . $incoming_token);
    376 			return;
    377 		}
    378 
    379         $decrypted = $this->openbay->decrypt($this->request->post['data'], $this->openbay->amazonus->getEncryptionKey(), $this->openbay->amazonus->getEncryptionIv(), false);
    380 
    381 		if (!$decrypted) {
    382 			$logger->write('amazonus/listing_reports - Failed to decrypt data');
    383 			return;
    384 		}
    385 
    386 		$logger->write('Received Listing Report: ' . $decrypted);
    387 
    388 		$request = json_decode($decrypted, 1);
    389 
    390 		$data = array();
    391 
    392 		foreach ($request['products'] as $product) {
    393 			$data[] = array(
    394 				'sku' => $product['sku'],
    395 				'quantity' => $product['quantity'],
    396 				'asin' => $product['asin'],
    397 				'price' => $product['price'],
    398 			);
    399 		}
    400 
    401 		if ($data) {
    402 			$this->model_extension_openbay_amazonus_product->addListingReport($data);
    403 		}
    404 
    405 		$this->model_extension_openbay_amazonus_product->removeListingReportLock($request['marketplace']);
    406 
    407 		$logger->write('amazonus/listing_reports - Finished');
    408 	}
    409 
    410 	public function product() {
    411 		if ($this->config->get('openbay_amazonus_status') != '1') {
    412 			$this->response->setOutput("disabled");
    413 			return;
    414 		}
    415 
    416 		ob_start();
    417 
    418 		$this->load->model('extension/openbay/amazonus_product');
    419 		$logger = new Log('amazonus_product.log');
    420 
    421 		$logger->write("AmazonusProduct/inbound: incoming data");
    422 
    423 		$incoming_token = isset($this->request->post['token']) ? $this->request->post['token'] : '';
    424 
    425 		if($incoming_token != $this->config->get('openbay_amazonus_token')) {
    426 			$logger->write("Error - Incorrect token: " . $this->request->post['token']);
    427 			ob_get_clean();
    428 			$this->response->setOutput("tokens did not match");
    429 			return;
    430 		}
    431 
    432         $decrypted = $this->openbay->decrypt($this->request->post['data'], $this->openbay->amazonus->getEncryptionKey(), $this->openbay->amazonus->getEncryptionIv(), false);
    433 
    434 		if(!$decrypted) {
    435 			$logger->write("Error - Failed to decrypt received data.");
    436 			ob_get_clean();
    437 			$this->response->setOutput("failed to decrypt");
    438 			return;
    439 		}
    440 
    441 		$decoded_data = (array)json_decode($decrypted);
    442 		$logger->write("Received data: " . print_r($decoded_data, true));
    443 		$status = $decoded_data['status'];
    444 
    445 		if($status == "submit_error") {
    446 			$message = 'Product was not submited to amazonus properly. Please try again or contact OpenBay.';
    447 			$this->model_extension_openbay_amazonus_product->setSubmitError($decoded_data['insertion_id'], $message);
    448 		} else {
    449 			$status = (array)$status;
    450 			if($status['successful'] == 1) {
    451 				$this->model_extension_openbay_amazonus_product->setStatus($decoded_data['insertion_id'], 'ok');
    452 				$insertion_product = $this->model_extension_openbay_amazonus_product->getProduct($decoded_data['insertion_id']);
    453 				$this->model_extension_openbay_amazonus_product->linkProduct($insertion_product['sku'], $insertion_product['product_id'], $insertion_product['var']);
    454 				$this->model_extension_openbay_amazonus_product->deleteErrors($decoded_data['insertion_id']);
    455 
    456 				$quantity_data = array(
    457 					$insertion_product['sku'] => $this->model_extension_openbay_amazonus_product->getProductQuantity($insertion_product['product_id'], $insertion_product['var'])
    458 				);
    459 				$logger->write('Updating quantity with data: ' . print_r($quantity_data, true));
    460 				$logger->write('Response: ' . print_r($this->openbay->amazonus->updateQuantities($quantity_data), true));
    461 			} else {
    462 				$msg = 'Product was not accepted by amazonus. Please try again or contact OpenBay.';
    463 				$this->model_extension_openbay_amazonus_product->setSubmitError($decoded_data['insertion_id'], $msg);
    464 
    465 				if(isset($decoded_data['error_details'])) {
    466 					foreach($decoded_data['error_details'] as $error) {
    467 						$error = (array)$error;
    468 						$error_data = array(
    469 							'sku' => $error['sku'],
    470 							'error_code' => $error['error_code'],
    471 							'message' => $error['message'],
    472 							'insertion_id' => $decoded_data['insertion_id']
    473 						);
    474 						$this->model_extension_openbay_amazonus_product->insertError($error_data);
    475 
    476 					}
    477 				}
    478 			}
    479 		}
    480 
    481 		$logger->write("Data processed successfully.");
    482 		ob_get_clean();
    483 		$this->response->setOutput("ok");
    484 	}
    485 
    486 	public function search() {
    487 		if ($this->config->get('openbay_amazonus_status') != '1') {
    488 			return;
    489 		}
    490 
    491 		$this->load->model('extension/openbay/amazonus_product');
    492 
    493 		$logger = new Log('amazonus.log');
    494 		$logger->write('amazonus/search - started');
    495 
    496 		$incoming_token = isset($this->request->post['token']) ? $this->request->post['token'] : '';
    497 
    498 		if (!hash_equals($this->config->get('openbay_amazonus_token'), $incoming_token)) {
    499 			$logger->write('amazonus/search - Incorrect token: ' . $incoming_token);
    500 			return;
    501 		}
    502 
    503         $decrypted = $this->openbay->decrypt($this->request->post['data'], $this->openbay->amazonus->getEncryptionKey(), $this->openbay->amazonus->getEncryptionIv(), false);
    504 
    505 		if (!$decrypted) {
    506 			$logger->write('amazonus/search Failed to decrypt data');
    507 			return;
    508 		}
    509 
    510 		$logger->write($decrypted);
    511 
    512 		$json = json_decode($decrypted, 1);
    513 
    514 		$this->model_extension_openbay_amazonus_product->updateSearch($json);
    515 	}
    516 
    517 	public function dev() {
    518 		if ($this->config->get('openbay_amazonus_status') != '1') {
    519 			$this->response->setOutput("error 001");
    520 			return;
    521 		}
    522 
    523 		$incoming_token = isset($this->request->post['token']) ? $this->request->post['token'] : '';
    524 
    525 		if ($incoming_token != $this->config->get('openbay_amazonus_token')) {
    526 			$this->response->setOutput("error 002");
    527 			return;
    528 		}
    529 
    530         $decrypted = $this->openbay->decrypt($this->request->post['data'], $this->openbay->amazonus->getEncryptionKey(), $this->openbay->amazonus->getEncryptionIv(), false);
    531 
    532 		if (!$decrypted) {
    533 			$this->response->setOutput("error 003");
    534 			return;
    535 		}
    536 
    537 		$data_xml = simplexml_load_string($decrypted);
    538 
    539 		if(!isset($data_xml->action)) {
    540 			$this->response->setOutput("error 004");
    541 			return;
    542 		}
    543 
    544 		$action = trim((string)$data_xml->action);
    545 
    546 		if ($action === "get_amazonus_product") {
    547 			if(!isset($data_xml->product_id)) {
    548 				$this->response->setOutput("error 005");
    549 				return;
    550 			}
    551 
    552 			$product_id = trim((string)$data_xml->product_id);
    553 
    554 			if ($product_id === "all") {
    555 				$all_rows = $this->db->query("SELECT * FROM `" . DB_PREFIX . "amazonus_product`")->rows;
    556 
    557 				$response = array();
    558 
    559 				foreach ($all_rows as $row) {
    560 					unset($row['data']);
    561 					$response[] = $row;
    562 				}
    563 
    564 				$this->response->setOutput(print_r($response, true));
    565 
    566 				return;
    567 			} else {
    568 				$response = $this->db->query("SELECT * FROM `" . DB_PREFIX . "amazonus_product` WHERE `product_id` = '" . (int)$product_id . "'")->rows;
    569 
    570 				$this->response->setOutput(print_r($response, true));
    571 				return;
    572 			}
    573 		} else {
    574 			$this->response->setOutput("error 999");
    575 			return;
    576 		}
    577 	}
    578 
    579 	public function eventAddOrderHistory($route, $data) {
    580 		$logger = new \Log('amazonus.log');
    581 		$logger->write('eventAddOrderHistory Event fired: ' . $route);
    582 
    583 		if (isset($data[0]) && !empty($data[0])) {
    584 			$this->load->model('extension/openbay/amazonus_order');
    585 
    586 			$logger->write('Order ID: ' . (int)$data[0]);
    587 
    588 			$this->model_extension_openbay_amazonus_order->addOrderHistory((int)$data[0]);
    589 		}
    590 	}
    591 }