amazon.php (22340B)
1 <?php 2 class ControllerExtensionOpenbayAmazon extends Controller { 3 public function order() { 4 if ($this->config->get('openbay_amazon_status') != '1') { 5 return; 6 } 7 8 $this->load->model('checkout/order'); 9 $this->load->model('extension/openbay/amazon_order'); 10 $this->load->language('extension/openbay/amazon_order'); 11 12 $logger = new Log('amazon.log'); 13 $logger->write('amazon/order - started'); 14 15 $incoming_token = isset($this->request->post['token']) ? $this->request->post['token'] : ''; 16 17 if (!hash_equals($this->config->get('openbay_amazon_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->amazon->getEncryptionKey(), $this->openbay->amazon->getEncryptionIv(), false); 23 24 if (!$decrypted) { 25 $logger->write('amazon/order Failed to decrypt data'); 26 return; 27 } 28 29 $order_xml = simplexml_load_string(base64_decode($decrypted)); 30 31 $amazon_order_status = trim(strtolower((string)$order_xml->Status)); 32 33 $amazon_order_id = (string)$order_xml->AmazonOrderId; 34 $order_status = $this->model_extension_openbay_amazon_order->getMappedStatus((string)$order_xml->Status); 35 36 $logger->write('Received order ' . $amazon_order_id); 37 38 $order_id = $this->model_extension_openbay_amazon_order->getOrderId($amazon_order_id); 39 40 // If the order already exists on opencart, ignore it. 41 if ($order_id) { 42 $logger->write("Duplicate order $amazon_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 $amazon_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_amazon_order_tax') > 0) { 75 $tax_total = (double)$item->Totals->Price * ($this->config->get('openbay_amazon_order_tax') / 100) / (1 + $this->config->get('openbay_amazon_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_amazon_order_tax') > 0) { 88 $shipping_tax = (double)$item->Totals->Shipping * ($this->config->get('openbay_amazon_order_tax') / 100) / (1 + $this->config->get('openbay_amazon_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_amazon_order_tax') > 0) { 98 $item_gift_wrap_tax = (double)$item->Totals->GiftWrap * ($this->config->get('openbay_amazon_order_tax') / 100) / (1 + $this->config->get('openbay_amazon_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_amazon_order->getProductId((string)$item->Sku); 110 $product_var = $this->model_extension_openbay_amazon_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_amazon_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 'newsletter' => '0', 161 'customer_group_id' => $this->config->get('openbay_amazon_order_customer_group'), 162 'password' => '', 163 'status' => '0', 164 ); 165 166 $this->db->query(" 167 INSERT INTO " . DB_PREFIX . "customer 168 SET firstname = '" . $this->db->escape($customer_data['firstname']) . "', 169 lastname = '" . $this->db->escape($customer_data['lastname']) . "', 170 email = '" . $this->db->escape($customer_data['email']) . "', 171 telephone = '" . $this->db->escape($customer_data['telephone']) . "', 172 newsletter = '" . (int)$customer_data['newsletter'] . "', 173 customer_group_id = '" . (int)$customer_data['customer_group_id'] . "', 174 password = '', 175 status = '" . (int)$customer_data['status'] . "', 176 date_added = NOW()"); 177 178 $customer_id = $this->db->getLastId(); 179 } 180 181 $shipping_first_name = (string)$order_xml->Shipping->FirstName; 182 $shipping_last_name = (string)$order_xml->Shipping->LastName; 183 184 if (empty($shipping_first_name) || empty($shipping_last_name)) { 185 $shipping_first_name = (string)$order_xml->Shipping->Name; 186 $shipping_last_name = ''; 187 } 188 189 $order = array( 190 'invoice_prefix' => $this->config->get('config_invoice_prefix'), 191 'store_id' => $this->config->get('config_store_id'), 192 'store_name' => $this->config->get('config_name') . ' / Amazon', 193 'store_url' => $this->config->get('config_url'), 194 'customer_id' => (int)$customer_id, 195 'customer_group_id' => $this->config->get('openbay_amazon_order_customer_group'), 196 'firstname' => $shipping_first_name, 197 'lastname' => $shipping_last_name, 198 'email' => (string)$order_xml->Payment->Email, 199 'telephone' => (string)$order_xml->Shipping->Phone, 200 'fax' => '', 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_amazon_order->getCountryName((string)$order_xml->Shipping->CountryCode), 209 'shipping_country_id' => $this->model_extension_openbay_amazon_order->getCountryId((string)$order_xml->Shipping->CountryCode), 210 'shipping_zone' => (string)$order_xml->Shipping->State, 211 'shipping_zone_id' => $this->model_extension_openbay_amazon_order->getZoneId((string)$order_xml->Shipping->State), 212 'shipping_address_format' => '', 213 'shipping_method' => (string)$order_xml->Shipping->Type, 214 'shipping_code' => 'amazon.' . (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_amazon_order->getCountryName((string)$order_xml->Shipping->CountryCode), 223 'payment_country_id' => $this->model_extension_openbay_amazon_order->getCountryId((string)$order_xml->Shipping->CountryCode), 224 'payment_zone' => (string)$order_xml->Shipping->State, 225 'payment_zone_id' => $this->model_extension_openbay_amazon_order->getZoneId((string)$order_xml->Shipping->State), 226 'payment_address_format' => '', 227 'payment_method' => $this->language->get('text_paid_amazon'), 228 'payment_code' => 'amazon.amazon', 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', 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_amazon_order->updateOrderStatus($order_id, $order_status); 296 $this->model_extension_openbay_amazon_order->addAmazonOrder($order_id, $amazon_order_id); 297 $this->model_extension_openbay_amazon_order->addAmazonOrderProducts($order_id, $product_mapping); 298 299 foreach($products as $product) { 300 if($product['product_id'] != 0) { 301 $this->model_extension_openbay_amazon_order->decreaseProductQuantity($product['product_id'], $product['quantity'], $product['var']); 302 } 303 } 304 305 $logger->write('Order ' . $amazon_order_id . ' was added to the database (ID: ' . $order_id . ')'); 306 $logger->write("Finished processing the order"); 307 308 $this->model_extension_openbay_amazon_order->acknowledgeOrder($order_id); 309 310 if($this->config->get('openbay_amazon_notify_admin') == 1){ 311 $this->openbay->newOrderAdminNotify($order_id, $order_status); 312 } 313 314 $this->event->trigger('model/checkout/order/addOrderHistory/after', array('model/checkout/order/addOrderHistory/after', array($order_id, $order_status))); 315 316 $logger->write("Ok"); 317 $this->response->setOutput('Ok'); 318 } 319 320 public function listing() { 321 if ($this->config->get('openbay_amazon_status') != '1') { 322 return; 323 } 324 325 $this->load->model('extension/openbay/amazon_listing'); 326 $this->load->model('extension/openbay/amazon_product'); 327 328 $logger = new Log('amazon_listing.log'); 329 $logger->write('amazon/listing - started'); 330 331 $incoming_token = isset($this->request->post['token']) ? $this->request->post['token'] : ''; 332 333 if (!hash_equals($this->config->get('openbay_amazon_token'), $incoming_token)) { 334 $logger->write('amazon/listing - Incorrect token: ' . $incoming_token); 335 return; 336 } 337 338 $decrypted = $this->openbay->decrypt($this->request->post['data'], $this->openbay->amazon->getEncryptionKey(), $this->openbay->amazon->getEncryptionIv(), false); 339 340 if (!$decrypted) { 341 $logger->write('amazon/order Failed to decrypt data'); 342 return; 343 } 344 345 $data = json_decode($decrypted, 1); 346 347 $logger->write("Received data: " . print_r($data, 1)); 348 349 if ($data['status']) { 350 $logger->write("Updating " . $data['product_id'] . ' from ' . $data['marketplace'] . ' as successful'); 351 $this->model_extension_openbay_amazon_listing->listingSuccessful($data['product_id'], $data['marketplace']); 352 $this->model_extension_openbay_amazon_product->linkProduct($data['sku'], $data['product_id']); 353 $logger->write("Updated successfully"); 354 } else { 355 $logger->write("Updating " . $data['product_id'] . ' from ' . $data['marketplace'] . ' as failed'); 356 $this->model_extension_openbay_amazon_listing->listingFailed($data['product_id'], $data['marketplace'], $data['messages']); 357 $logger->write("Updated successfully"); 358 } 359 } 360 361 public function listingReport() { 362 if ($this->config->get('openbay_amazon_status') != '1') { 363 return; 364 } 365 366 $this->load->model('extension/openbay/amazon_product'); 367 368 $logger = new Log('amazon.log'); 369 $logger->write('amazon/listing_reports - started'); 370 371 $incoming_token = isset($this->request->post['token']) ? $this->request->post['token'] : ''; 372 373 if (!hash_equals($this->config->get('openbay_amazon_token'), $incoming_token)) { 374 $logger->write('amazon/listing_reports - Incorrect token: ' . $incoming_token); 375 return; 376 } 377 378 $decrypted = $this->openbay->decrypt($this->request->post['data'], $this->openbay->amazon->getEncryptionKey(), $this->openbay->amazon->getEncryptionIv(), false); 379 380 if (!$decrypted) { 381 $logger->write('amazon/listing_reports - Failed to decrypt data'); 382 return; 383 } 384 385 $logger->write('Received Listing Report: ' . $decrypted); 386 387 $request = json_decode($decrypted, 1); 388 389 $data = array(); 390 391 foreach ($request['products'] as $product) { 392 $data[] = array( 393 'marketplace' => $request['marketplace'], 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_amazon_product->addListingReport($data); 403 } 404 405 $this->model_extension_openbay_amazon_product->removeListingReportLock($request['marketplace']); 406 407 $logger->write('amazon/listing_reports - Finished'); 408 } 409 410 public function product() { 411 if ($this->config->get('openbay_amazon_status') != '1') { 412 $this->response->setOutput("disabled"); 413 return; 414 } 415 416 ob_start(); 417 418 $this->load->model('extension/openbay/amazon_product'); 419 $logger = new Log('amazon_product.log'); 420 421 $logger->write("AmazonProduct/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_amazon_token')) { 426 $logger->write("Error - Incorrect token: " . $this->request->post['token']); 427 ob_get_clean(); 428 $this->response->setOutput("tokens did not match"); 429 430 return; 431 } 432 433 $decrypted = $this->openbay->decrypt($this->request->post['data'], $this->openbay->amazon->getEncryptionKey(), $this->openbay->amazon->getEncryptionIv(), false); 434 435 if(!$decrypted) { 436 $logger->write("Error - Failed to decrypt received data."); 437 ob_get_clean(); 438 $this->response->setOutput("failed to decrypt"); 439 440 return; 441 } 442 443 $decoded_data = (array)json_decode($decrypted); 444 $logger->write("Received data: " . print_r($decoded_data, true)); 445 $status = $decoded_data['status']; 446 447 if($status == "submit_error") { 448 $message = 'Product was not submited to amazon properly. Please try again or contact OpenBay.'; 449 $this->model_extension_openbay_amazon_product->setSubmitError($decoded_data['insertion_id'], $message); 450 } else { 451 $status = (array)$status; 452 if($status['successful'] == 1) { 453 $this->model_extension_openbay_amazon_product->setStatus($decoded_data['insertion_id'], 'ok'); 454 $insertion_product = $this->model_extension_openbay_amazon_product->getProduct($decoded_data['insertion_id']); 455 $this->model_extension_openbay_amazon_product->linkProduct($insertion_product['sku'], $insertion_product['product_id'], $insertion_product['var']); 456 $this->model_extension_openbay_amazon_product->deleteErrors($decoded_data['insertion_id']); 457 458 $quantity_data = array( 459 $insertion_product['sku'] => $this->model_extension_openbay_amazon_product->getProductQuantity($insertion_product['product_id'], $insertion_product['var']) 460 ); 461 $logger->write('Updating quantity with data: ' . print_r($quantity_data, true)); 462 $logger->write('Response: ' . print_r($this->openbay->amazon->updateQuantities($quantity_data), true)); 463 } else { 464 $msg = 'Product was not accepted by amazon. Please try again or contact OpenBay.'; 465 $this->model_extension_openbay_amazon_product->setSubmitError($decoded_data['insertion_id'], $msg); 466 467 if(isset($decoded_data['error_details'])) { 468 foreach($decoded_data['error_details'] as $error) { 469 $error = (array)$error; 470 $error_data = array( 471 'sku' => $error['sku'], 472 'error_code' => $error['error_code'], 473 'message' => $error['message'], 474 'insertion_id' => $decoded_data['insertion_id'] 475 ); 476 $this->model_extension_openbay_amazon_product->insertError($error_data); 477 } 478 } 479 } 480 } 481 482 $logger->write("Data processed successfully."); 483 ob_get_clean(); 484 $this->response->setOutput("ok"); 485 } 486 487 public function search() { 488 if ($this->config->get('openbay_amazon_status') != '1') { 489 return; 490 } 491 492 $this->load->model('extension/openbay/amazon_product'); 493 494 $logger = new Log('amazon.log'); 495 $logger->write('amazon/search - started'); 496 497 $incoming_token = isset($this->request->post['token']) ? $this->request->post['token'] : ''; 498 499 if (!hash_equals($this->config->get('openbay_amazon_token'), $incoming_token)) { 500 $logger->write('amazon/search - Incorrect token: ' . $incoming_token); 501 return; 502 } 503 504 $decrypted = $this->openbay->decrypt($this->request->post['data'], $this->openbay->amazon->getEncryptionKey(), $this->openbay->amazon->getEncryptionIv(), false); 505 506 if (!$decrypted) { 507 $logger->write('amazon/search Failed to decrypt data'); 508 return; 509 } 510 511 $logger->write($decrypted); 512 513 $json = json_decode($decrypted, 1); 514 515 $this->model_extension_openbay_amazon_product->updateSearch($json); 516 } 517 518 public function dev() { 519 if ($this->config->get('openbay_amazon_status') != '1') { 520 $this->response->setOutput("error 001"); 521 return; 522 } 523 524 $incoming_token = isset($this->request->post['token']) ? $this->request->post['token'] : ''; 525 526 if ($incoming_token != $this->config->get('openbay_amazon_token')) { 527 $this->response->setOutput("error 002"); 528 return; 529 } 530 531 $decrypted = $this->openbay->decrypt($this->request->post['data'], $this->openbay->amazon->getEncryptionKey(), $this->openbay->amazon->getEncryptionIv(), false); 532 533 if (!$decrypted) { 534 $this->response->setOutput("error 003"); 535 return; 536 } 537 538 $data_xml = simplexml_load_string(base64_decode($decrypted)); 539 540 if(!isset($data_xml->action)) { 541 $this->response->setOutput("error 004"); 542 return; 543 } 544 545 $action = trim((string)$data_xml->action); 546 547 if ($action === "get_amazon_product") { 548 if(!isset($data_xml->product_id)) { 549 $this->response->setOutput("error 005"); 550 return; 551 } 552 553 $product_id = trim((string)$data_xml->product_id); 554 555 if ($product_id === "all") { 556 $all_rows = $this->db->query("SELECT * FROM `" . DB_PREFIX . "amazon_product`")->rows; 557 558 $response = array(); 559 foreach ($all_rows as $row) { 560 unset($row['data']); 561 $response[] = $row; 562 } 563 564 $this->response->setOutput(print_r($response, true)); 565 return; 566 } else { 567 $response = $this->db->query("SELECT * FROM `" . DB_PREFIX . "amazon_product` WHERE `product_id` = '" . (int)$product_id . "'")->rows; 568 569 $this->response->setOutput(print_r($response, true)); 570 return; 571 } 572 } else { 573 $this->response->setOutput("error 999"); 574 return; 575 } 576 } 577 578 public function eventAddOrderHistory($route, $data) { 579 $logger = new \Log('amazon.log'); 580 $logger->write('eventAddOrderHistory Event fired: ' . $route); 581 582 if (isset($data[0]) && !empty($data[0])) { 583 $this->load->model('extension/openbay/amazon_order'); 584 585 $logger->write('Order ID: ' . (int)$data[0]); 586 587 $this->model_extension_openbay_amazon_order->addOrderHistory((int)$data[0]); 588 } 589 } 590 }