Ejemplo n.º 1
0
 function update_store($data)
 {
     $this->db->where('site_id', $data["site_id"]);
     $this->db->update('br_store', $data);
     remove_from_cache('config');
     return true;
 }
Ejemplo n.º 2
0
 /**
  * delete_product function.
  * 
  * @access public
  * @param mixed $product_id
  * @return void
  */
 public function delete_product($product_id)
 {
     $entry_id = $this->get_product_entry($product_id);
     $this->EE->db->delete('br_product', array('product_id' => $product_id));
     $this->EE->db->delete('br_product_addon', array('parent_id' => $product_id));
     $this->EE->db->delete('br_product_attributes', array('product_id' => $product_id));
     $this->EE->db->delete('br_product_attributes_option', array('product_id' => $product_id));
     $this->EE->db->delete('br_product_bundle', array('parent_id' => $product_id));
     $this->EE->db->delete('br_product_category', array('product_id' => $product_id));
     $this->EE->db->delete('br_product_configurable', array('product_id' => $product_id));
     # DO NOT DELETE THIS
     # $this->EE->db->delete('br_product_images', array('product_id' => $product_id));
     $this->EE->db->delete('br_product_entry', array('product_id' => $product_id));
     $this->EE->db->delete('br_product_feeds', array('product_id' => $product_id));
     $this->EE->db->delete('br_product_options', array('product_id' => $product_id));
     $this->EE->db->delete('br_product_price', array('product_id' => $product_id));
     $this->EE->db->delete('br_product_related', array('parent_id' => $product_id));
     // Added 1.1
     // To remove the
     $this->EE->db->delete('channel_titles', array('entry_id' => $entry_id));
     $this->EE->db->delete('channel_data', array('entry_id' => $entry_id));
     // Clear from cache
     remove_from_cache('product_' . $product_id);
     return true;
 }
 function checkout()
 {
     $this->EE->load->model('customer_model');
     $this->EE->load->model('order_model');
     $this->EE->load->library('user_agent');
     // For order email
     $has_donation = FALSE;
     $has_item = FALSE;
     // Some Defaults
     $member_id = $this->EE->session->userdata["member_id"];
     $email = $this->EE->session->userdata["email"];
     // Create the default container for all shipping fields
     // in case the form ommits one of the required fields
     $shipping_fields = array("br_fname", "br_lname", "br_billing_fname", "br_billing_lname", "br_billing_company", "br_billing_address1", "br_billing_address2", "br_billing_city", "br_billing_state", "br_billing_zip", "br_billing_country", "br_billing_phone", "br_shipping_fname", "br_shipping_lname", "br_shipping_company", "br_shipping_address1", "br_shipping_address2", "br_shipping_city", "br_shipping_state", "br_shipping_zip", "br_shipping_country", "br_shipping_phone");
     foreach ($shipping_fields as $f) {
         $data[$f] = '';
     }
     foreach ($_POST as $key => $val) {
         $data[$key] = $this->EE->input->post($key, TRUE);
     }
     // If we don't have br_fname/lname then try to set them on the billing fname/lname
     if ($data["br_fname"] == '') {
         $data["br_fname"] = $data["br_billing_fname"];
     }
     if ($data["br_lname"] == '') {
         $data["br_lname"] = $data["br_billing_lname"];
     }
     // Minimum required fields
     $required_fields = array('br_fname' => lang('br_fname'), 'br_lname' => lang('br_lname'), 'email' => lang('br_email'), 'br_billing_fname' => lang('br_billing_fname'), 'br_billing_lname' => lang('br_billing_lname'), 'br_billing_phone' => lang('br_billing_phone'), 'br_billing_address1' => lang('br_billing_address1'), 'br_billing_city' => lang('br_billing_city'), 'br_billing_zip' => lang('br_billing_zip'), 'br_billing_country' => lang('br_billing_country'));
     // If the member is logged in we already have their email so we
     // don't need to require it
     if ($member_id != 0) {
         unset($required_fields['email']);
     }
     // Do we need to require the shipping fields, too?
     $ship_same_address = isset($data['ship_same_address']) && $data['ship_same_address'] != '';
     // Save this one for later
     $_SESSION['br_ship_same_address'] = $ship_same_address;
     if (!$ship_same_address) {
         // Add additional fields
         $required_fields = array_merge($required_fields, array('br_shipping_fname' => lang('br_shipping_fname'), 'br_shipping_lname' => lang('br_shipping_lname'), 'br_shipping_address1' => lang('br_shipping_address1'), 'br_shipping_city' => lang('br_shipping_city'), 'br_shipping_zip' => lang('br_shipping_zip'), 'br_shipping_country' => lang('br_shipping_country'), 'br_shipping_phone' => lang('br_shipping_phone')));
     }
     // Should we validate the State
     $country = isset($data['br_billing_country']) ? $data['br_billing_country'] : '';
     $validate_state = strcasecmp($country, "US") == 0 || strcasecmp($country, "United States") == 0 || strcasecmp($country, "CA") == 0 || strcasecmp($country, "Canada") == 0;
     if ($validate_state) {
         array_merge($required_fields, array('br_billing_state' => lang('br_billing_state')));
         if (!$ship_same_address) {
             array_merge($required_fields, array('br_shipping_state' => lang('br_shipping_state')));
         }
     }
     // Allow updating of the $data (address) array passed to validation
     // Added 1.6.2.4 - dpd
     if ($this->EE->extensions->active_hook('br_order_validate_before') === TRUE) {
         $data = $this->EE->extensions->call('br_order_validate_before', $data);
     }
     // Let's do some validation...
     try {
         // Create new arrays on each request to hold form info
         $_SESSION['br_form_errors'] = array();
         $_SESSION['br_form_post_data'] = array();
         // Load our data into a temporary session store so we can redisplay it on error
         foreach ($data as $key => $val) {
             $_SESSION['br_form_post_data'][$key] = form_prep($this->EE->input->post($key, TRUE));
         }
         // ---------------------------------------------------
         // Handle required fields
         $missing_fields = array();
         foreach ($required_fields as $field => $name) {
             if (!isset($data[$field]) || trim($data[$field]) == '') {
                 $_SESSION['br_form_errors'][$field] = lang('br_this_field_is_required');
                 $missing_fields[] = $name;
             }
         }
         if (count($missing_fields)) {
             throw new Exception(lang('br_the_following_fields_are_required') . " " . implode(', ', $missing_fields));
         }
         // ---------------------------------------------------
         // Well-formedness checks
         $validation_errors = array();
         // Handle email validity
         if ($member_id == 0) {
             $this->EE->load->library('email_validation');
             if (isset($data['confirm_email']) && $data['email'] != $data['confirm_email']) {
                 $_SESSION['br_form_errors']['email'] = 'The email confirmation you entered doesn\'t match the email you entered';
                 $_SESSION['br_form_errors']['confirm_email'] = 'The email confirmation you entered doesn\'t match the email you entered';
                 $validation_errors[] = "The email confirmation you entered doesn't match the email you entered.";
             }
             if (!$this->EE->email_validation->check_email_address(trim($data['email']))) {
                 $_SESSION['br_form_errors']['email'] = 'This doesn\'t look like a valid email. Try again or contact us for help placing your order';
                 $validation_errors[] = "The email you entered doesn't appear to be valid.";
             }
         }
         // US-specific validation for shipping
         if (!$ship_same_address && $data['br_shipping_country'] == 'US') {
             // Handle shipping phone validity
             if (!preg_match("/^(?:1(?:[. -])?)?(?:\\((?=\\d{3}\\)))?([2-9]\\d{2})(?:(?<=\\(\\d{3})\\))? ?(?:(?<=\\d{3})[.-])?([2-9]\\d{2})[. -]?(\\d{4})(?: (?i:ext)\\.? ?(\\d{1,5}))?\$/", $data['br_shipping_phone'])) {
                 $_SESSION['br_form_errors']['br_shipping_phone'] = 'This doesn\'t look like a valid phone number. Try again or contact us for help placing your order';
                 $validation_errors[] = "The shipping phone number you entered doesn't appear to be valid.";
             }
             // Handle shipping zip validity
             if (!preg_match("/^([0-9]{5})(-[0-9]{4})?\$/", $data['br_shipping_zip'])) {
                 $_SESSION['br_form_errors']['br_shipping_zip'] = 'This doesn\'t look like a valid US zip code. Try again or contact us for help placing your order';
                 $validation_errors[] = "The shipping zip code you entered doesn't appear to be valid.";
             }
         }
         if (count($validation_errors)) {
             throw new Exception("You'll need to fix these errors before you can complete your checkout: <ul><li>" . implode('</li><li>', $validation_errors) . "</li></ul>");
         }
     } catch (Exception $e) {
         //Caught an exception? Error out.
         $_SESSION["br_alert"] = $e->getMessage();
         $this->EE->functions->redirect($this->_secure_url($this->_config["store"][$this->site_id]["checkout_url"]));
         exit;
     }
     // Shipping Rate
     // We have a problem
     if (!isset($_POST["SID"])) {
         $_SESSION["br_alert"] = lang('br_checkout_shipping_error');
         $this->EE->functions->redirect($this->_secure_url($this->_config["store"][$this->site_id]["checkout_url"]));
         exit;
     }
     // Check if shipping is even necessary
     $sid = $_POST["SID"];
     $shippable = isset($_SESSION[$sid]) ? $_SESSION[$sid] : 0;
     if ($shippable == 0 && !isset($data["shipping"])) {
         $data["shipping"] = 'free';
         $_SESSION["shipping"]['free'] = array("code" => "N/A", "rate" => "0.00", "label" => "N/A", "method" => "N/A");
     }
     if ($shippable > 0 && !isset($data["shipping"])) {
         $_SESSION["br_alert"] = lang('br_checkout_shipping_error');
         $this->EE->functions->redirect($this->_secure_url($this->_config["store"][$this->site_id]["checkout_url"]));
         exit;
     }
     $data["cart_shipping"] = 0;
     if (isset($_SESSION["shipping"][$data["shipping"]]["rate"])) {
         $data["cart_shipping"] = $_SESSION["shipping"][$data["shipping"]]["rate"];
     }
     if ($data["cart_shipping"] == '') {
         $data["cart_shipping"] = 0;
     }
     // Set the shipping automatically
     if (isset($data["ship_same_address"])) {
         unset($data["ship_same_address"]);
         $data["br_shipping_fname"] = $data["br_billing_fname"];
         $data["br_shipping_lname"] = $data["br_billing_lname"];
         $data["br_shipping_company"] = $data["br_billing_company"];
         $data["br_shipping_phone"] = $data["br_billing_phone"];
         $data["br_shipping_address1"] = $data["br_billing_address1"];
         $data["br_shipping_address2"] = $data["br_billing_address2"];
         $data["br_shipping_country"] = $data["br_billing_country"];
         $data["br_shipping_city"] = $data["br_billing_city"];
         $data["br_shipping_zip"] = $data["br_billing_zip"];
         $data["br_shipping_state"] = $data["br_billing_state"];
     }
     if ($this->EE->extensions->active_hook('br_order_validate_after') === TRUE) {
         $data = $this->EE->extensions->call('br_order_validate_after', $data);
     }
     // End Shipping stuff
     if ($member_id == '') {
         if ($mem = $this->EE->customer_model->get_customer_by_email($data["email"])) {
             // user isn't logged in but they do have an account
             $email = $mem[0]["email"];
             $member_id = $mem[0]["member_id"];
         } else {
             // No matching email. Create customer
             $email = $data["email"];
             $group_id = $this->_config["store"][$this->site_id]["register_group"];
             $password = strtolower(substr(md5(time()), 0, 8));
             $member_id = $this->EE->customer_model->create_customer($data, $password, $group_id);
             $eml[0] = array("fname" => $data["br_fname"], "lname" => $data["br_lname"], "email" => $data["email"], "password" => $password, "username" => $data["email"], "join_date" => $this->EE->localize->now, "activation_url" => "");
             // Call the member_member_register hook
             $edata = $this->EE->extensions->call('member_member_register', $eml[0], $member_id);
             if ($this->EE->extensions->end_script === TRUE) {
                 return;
             }
             // Send the email notice
             // Added a suppress flag in version 1.1.0.1
             if ($this->EE->config->item('br_suppress_new_account_email') !== TRUE) {
                 $this->_send_email('customer-account-new', $eml);
             }
             // Auto Login?
             if ($this->EE->config->item('br_autologin_checkout') == 'y') {
                 // Check to make sure they aren't logged in already
                 if ($this->EE->session->userdata('member_id') == 0) {
                     $this->_autologin($member_id);
                 }
             }
         }
     }
     $data["cart"] = $this->EE->product_model->cart_get();
     $data["cart_coupon_code"] = isset($_SESSION["discount"]["code"]) ? $_SESSION["discount"]["code"] : '';
     $data["cart_tax"] = $this->_get_cart_tax($data["br_shipping_country"], $data["br_shipping_state"], $data["br_shipping_zip"], $data["br_shipping_address1"], $data["br_shipping_address2"], $data["br_shipping_city"], $data["cart_shipping"]);
     $data["cart_subtotal"] = $this->cart_subtotal();
     $data["cart_discount"] = $this->_get_cart_discount();
     $data["cart_total"] = $this->_get_cart_total();
     $data["order_total"] = $data["cart_total"] + $data["cart_tax"] + $data["cart_shipping"];
     // Get Custom Fields
     $tmp = $this->EE->customer_model->_get_custom_fields();
     foreach ($tmp as $key => $val) {
         $fields[$val] = $key;
     }
     // Get Member
     $member = $this->EE->customer_model->get_customer_profile($member_id);
     foreach ($member as $key => $val) {
         if (substr($key, 0, 3) == 'br_') {
             if (trim($val) == '') {
                 // Exceptions for the first few parameters
                 if ($key == 'br_fname' && $member[$key] == '') {
                     $data[$key] = $data["br_billing_fname"];
                 }
                 if ($key == 'br_lname' && $member[$key] == '') {
                     $data[$key] = $data["br_billing_lname"];
                 }
                 if ($key == 'br_phone' && $member[$key] == '') {
                     $data[$key] = $data["br_billing_phone"];
                 }
                 $update[$fields[$key]] = isset($data[$key]) ? $data[$key] : '';
             }
         }
     }
     if (isset($update)) {
         $this->EE->customer_model->update_member_profile('', $update, $member_id);
     }
     // We've validated addresses and shipping so lets go ahead and get an order number
     // so we can send it to the payment gateway.
     $data["order_id"] = $this->EE->order_model->create_order_id();
     // Process the payment
     $data["transaction_id"] = md5(time() . rand(1000, 1000000) . time());
     $data["email"] = $email;
     $data["member_id"] = $member_id;
     $data["payment"] = $this->_process_payment($data);
     if (isset($data["payment"]["error"])) {
         $_SESSION["br_alert"] = $data["payment"]["error"];
         $this->EE->functions->redirect($this->_secure_url($this->_config["store"][$this->site_id]["checkout_url"]));
     }
     // Create the order
     $order = array("site_id" => $this->EE->session->userdata["site_id"], "order_id" => $data["order_id"], "member_id" => $member_id, "status_id" => $data["payment"]["status"], "base" => $this->_currency_round($data["cart_subtotal"]), "tax" => $this->_currency_round($data["cart_tax"]), "shipping" => $this->_currency_round($data["cart_shipping"]), "total" => $this->_currency_round($data["cart_total"]), "discount" => $this->_currency_round($data["cart_discount"]), "cart_id" => $data["cart"]["cart_id"], "merchant_id" => $data["transaction_id"], "coupon_code" => $data["cart_coupon_code"], "created" => $this->EE->localize->now, "updated" => $this->EE->localize->now, "agent_string" => $this->EE->agent->agent_string(), "ip_address" => $_SERVER["REMOTE_ADDR"]);
     // Hook before we create the order
     // Only fire the hook on direct payment orders
     if ($order["status_id"] != -1) {
         if ($this->EE->extensions->active_hook('br_order_create_before') === TRUE) {
             $order = $this->EE->extensions->call('br_order_create_before', $order);
         }
     }
     $this->EE->order_model->create_order($order);
     // Order is a success, destory old form field values
     if (isset($_SESSION['br_form_post_data'])) {
         unset($_SESSION['br_form_post_data']);
     }
     // Now that the order has been created lets create a shipment if
     // a shipment is necessary
     if (isset($_SESSION["shipping"][$data["shipping"]]["rate"])) {
         $_SESSION["shipping"][$data["shipping"]]["order_id"] = $data["order_id"];
         $this->EE->order_model->create_shipment($_SESSION["shipping"][$data["shipping"]]);
     }
     // Address
     $address[0] = array("order_id" => $data["order_id"], "billing_fname" => $data["br_billing_fname"], "billing_lname" => $data["br_billing_lname"], "billing_company" => $data["br_billing_company"], "billing_phone" => $data["br_billing_phone"], "billing_address1" => $data["br_billing_address1"], "billing_address2" => $data["br_billing_address2"], "billing_city" => $data["br_billing_city"], "billing_state" => $data["br_billing_state"], "billing_zip" => $data["br_billing_zip"], "billing_country" => $data["br_billing_country"], "shipping_fname" => $data["br_shipping_fname"], "shipping_lname" => $data["br_shipping_lname"], "shipping_company" => $data["br_shipping_company"], "shipping_phone" => $data["br_shipping_phone"], "shipping_address1" => $data["br_shipping_address1"], "shipping_address2" => $data["br_shipping_address2"], "shipping_state" => $data["br_shipping_state"], "shipping_zip" => $data["br_shipping_zip"], "shipping_city" => $data["br_shipping_city"], "shipping_country" => $data["br_shipping_country"]);
     $this->EE->order_model->create_order_address($address[0]);
     // Add the payment info to the database now that we have
     // have an order_id for the item.
     $payment[0] = array('order_id' => $data["order_id"], 'transaction_id' => $data["payment"]["transaction_id"], 'payment_type' => $data["payment"]["payment_type"], 'details' => $data["payment"]["details"], 'amount' => $this->_currency_round($data["payment"]["amount"]), 'approval' => $data["payment"]["approval"], 'created' => $this->EE->localize->now);
     $this->EE->order_model->create_order_payment($payment[0]);
     // Add items to order
     $i = 0;
     foreach ($data["cart"]["items"] as $items) {
         if ($items["type_id"] == 7) {
             $has_donation = TRUE;
         } else {
             $has_item = TRUE;
         }
         $item = array('order_id' => $data["order_id"], 'product_id' => $items["product_id"], 'configurable_id' => $items["configurable_id"], 'base' => $this->_currency_round($items["base"]), 'price' => $this->_currency_round($items["price"]), 'cost' => $this->_currency_round($items["cost"]), 'discount' => $this->_currency_round($items["discount"]), 'quantity' => $items["quantity"], 'status' => 1, 'title' => $items["title"], 'taxable' => $items["taxable"], 'weight' => $items["weight"], 'shippable' => $items["shippable"], 'url' => $items["url_title"], 'sku' => $items["sku"], 'options' => $items["options"]);
         $this->EE->order_model->create_order_item($item);
         // We need to create downloadable product links
         // If its a downloadable product or if its a bundle
         // containing a downloadable product
         if ($items["type_id"] == 4 || $items["type_id"] == 2) {
             // Create a file container
             $file = array();
             // A download purchase is easy just go ahead and add it
             if ($items["type_id"] == 4) {
                 $file[] = $item;
             } else {
                 $bundle = $this->EE->product_model->get_product_bundle($item["product_id"]);
                 foreach ($bundle as $b) {
                     if ($b["type_id"] == 4) {
                         $file[] = array('quantity' => $items["quantity"], 'order_id' => $data["order_id"], 'product_id' => $b["product_id"]);
                     }
                 }
             }
             foreach ($file as $f) {
                 // Get the file info
                 $dl = $this->EE->order_model->_get_download_file($f);
                 // Generate a uuid as the license key...
                 unset($dl["title"]);
                 unset($dl["filenm_orig"]);
                 unset($dl["filenm"]);
                 unset($dl["created"]);
                 // Need to loop through based on item count
                 for ($j = 0; $j < $f["quantity"]; $j++) {
                     // Insert into the db
                     $dl["license"] = uuid();
                     $dl["member_id"] = $member_id;
                     // Hook to modify the $dl array prior to creating the order download record
                     if ($this->EE->extensions->active_hook('br_license_create_after') === TRUE) {
                         $dl = $this->EE->extensions->call('br_license_create_after', $dl);
                     }
                     $this->EE->order_model->create_order_download($dl);
                 }
                 unset($dl);
             }
         }
         // Only reduce inventory on direct sales
         if ($order["status_id"] >= 1) {
             // Reduce the item inventory
             $this->EE->order_model->reduce_item_inventory($item);
             remove_from_cache('product_' . $items["product_id"]);
         }
         // Setup the items array for the notification email
         $order_items[$i] = $item;
         $order_items[$i]["title"] = $items["title"];
         $order_items[$i]["url_title"] = $items["url_title"];
         $order_items[$i]["sku"] = $items["sku"];
         $order_items[$i]["image_large"] = $items["image_large"];
         $order_items[$i]["image_thumb"] = str_replace('products/', 'products/thumb/', $items["image_thumb"]);
         $order_items[$i]["price_html"] = $items["price_html"];
         $order_items[$i]["price"] = $this->_currency_round($items["price"]);
         $order_items[$i]["options"] = $items["options"];
         $order_items[$i]["subtotal"] = $this->_currency_round($items["price"] * $items["quantity"]);
         $i++;
     }
     // Add a note to the order
     if (isset($data["instructions"])) {
         if (trim($data["instructions"]) != '') {
             $arr = array('order_id' => $data["order_id"], 'order_note' => $data["instructions"], 'created' => $this->EE->localize->now, 'member_id' => $member_id);
             $this->EE->order_model->create_order_note($arr);
         }
     }
     // Finish up successful direct payments
     if ($order["status_id"] != -1) {
         // Send the email!
         $vars[0] = array("fname" => $data["br_fname"], "lname" => $data["br_lname"], "email" => $email, "address" => $address, "payment" => $payment, "order_id" => $data["order_id"], "order_num" => $data["order_id"], "order_note" => $data["instructions"], "delivery_method" => $_SESSION["shipping"][$data["shipping"]]["method"], "delivery_label" => $_SESSION["shipping"][$data["shipping"]]["label"], "items" => $order_items, "order_subtotal" => $this->_currency_round($data["cart_subtotal"]), "discount_total" => $this->_currency_round($data["cart_discount"]), "tax_total" => $this->_currency_round($data["cart_tax"]), "shipping" => $this->_currency_round($data["cart_shipping"]), "order_total" => $this->_currency_round($data["order_total"]), "has_item" => $has_item, "has_donation" => $has_donation);
         if ($this->EE->config->item('br_suppress_new_order_email') !== TRUE) {
             // Hook after we create the order before cleanup
             if ($this->EE->extensions->active_hook('br_order_email_before') === TRUE) {
                 $vars = $this->EE->extensions->call('br_order_email_before', $vars, $data);
             }
             $this->_send_email('customer-order-new', $vars);
         }
         // Hook after we create the order before cleanup
         if ($this->EE->extensions->active_hook('br_order_create_after') === TRUE) {
             $data = $this->EE->extensions->call('br_order_create_after', $data);
         }
         // Clear out the cart!
         $this->EE->product_model->cart_clear();
         unset($_SESSION["discount"]);
         // Clear out the cache
         $this->EE->functions->clear_caching('db');
         // Redirect to thank you page with the order_id
         $_SESSION["order_id"] = $data["order_id"];
         $this->EE->functions->redirect($this->EE->functions->create_url($this->_config["store"][$this->site_id]["thankyou_url"]));
         exit;
     } else {
         // Deal with the IPN
         // Clear out the cart!
         $this->EE->product_model->cart_update_status(session_id(), 2);
         unset($_SESSION["discount"]);
         // Clear out the cache
         $this->EE->functions->clear_caching('db');
         $data["order_id"] = $data["order_id"];
         $data["email"] = $email;
         // Get the gateway id
         $code = $this->EE->order_model->_get_gateway($data["gateway"]);
         $gid = $this->_config["gateway"][$this->site_id][$code]["config_id"];
         // build urls
         $data["return"] = $this->EE->functions->fetch_site_index(0, 0);
         $data["sagepay_return"] = $this->EE->functions->fetch_site_index(0, 0) . QUERY_MARKER . 'ACT=' . $this->EE->core_model->get_aid('Brilliant_retail', 'gateway_ipn') . '&GID=' . $gid . '&tid=' . $data['transaction_id'];
         $data["cancel_return"] = $this->EE->functions->fetch_site_index(0, 0) . QUERY_MARKER . 'ACT=' . $this->EE->core_model->get_aid('Brilliant_retail', 'gateway_ipn') . '&GID=' . $gid . '&cancel=true';
         $data["notify_url"] = $this->EE->functions->fetch_site_index(0, 0) . QUERY_MARKER . 'ACT=' . $this->EE->core_model->get_aid('Brilliant_retail', 'gateway_ipn') . '&GID=' . $gid;
         // Fire ways
         $config = array();
         if (isset($this->_config["gateway"][$this->site_id][$code]["config_data"])) {
             $config_data = $this->_config["gateway"][$this->site_id][$code]["config_data"];
             foreach ($config_data as $c) {
                 $config[$c["code"]] = $c["value"];
             }
         }
         // We need to fire the process_payment_before hook so we can do any calculations
         // before sending the user on to the third party payment processor.
         // Added 1.3.1 - DPD
         if ($this->EE->extensions->active_hook('br_process_payment_before') === TRUE) {
             $data = $this->EE->extensions->call('br_process_payment_before', $data);
         }
         // Start the IPN
         $str = 'Gateway_' . $code;
         $ipn = new $str();
         $ipn->start_ipn($data, $config);
         exit;
     }
 }
 function __construct()
 {
     $this->EE =& get_instance();
     $this->EE->load->add_package_path(PATH_THIRD . 'brilliant_retail/');
     // This is required so we can create third party extensions!
     // We need to make sure we have an instance of the template parser loaded
     // incase we are doing any ACT processing
     if (!isset($this->EE->TMPL)) {
         $this->EE->load->library('template');
         $this->EE->TMPL = new EE_Template();
     }
     // Load libraries we use throughout
     $this->EE->load->library('logger');
     $this->EE->load->library('table');
     // Load Helpers
     $this->EE->load->helper('form');
     $this->EE->load->helper('brilliant_retail');
     // Load all BR models
     // We do this so we can access them in via extension
     $this->EE->load->model('core_model');
     $this->EE->load->model('customer_model');
     $this->EE->load->model('feed_model');
     $this->EE->load->model('email_model');
     $this->EE->load->model('order_model');
     $this->EE->load->model('product_model');
     $this->EE->load->model('promo_model');
     $this->EE->load->model('store_model');
     $this->EE->load->model('tax_model');
     $this->EE->lang->loadfile('brilliant_retail');
     // Create a language overloader! We want to be able to extend
     // Brilliant Retail with custom language files so lets locally
     // pick any file in the _local/language/[language]/directory
     // that follow the lang.XXXX.php pattern
     $this->_load_lang_files();
     $this->site_id = $this->EE->config->item('site_id');
     // Get the BR _config variable & Set the site_id
     $sites = $this->EE->core_model->get_sites();
     $stores = $this->EE->core_model->get_stores();
     if (count($sites) != count($stores)) {
         foreach ($sites as $key => $val) {
             if (!isset($stores[$key])) {
                 $this->EE->core_model->create_store($key);
             }
         }
         // Need to delete the cache
         remove_from_cache('config');
     }
     $this->_config = $this->EE->core_model->get_config();
     // Check for local configuration of path / url settings
     $local_config_opts = array('br_media_url', 'br_secure_url', 'br_media_dir', 'br_license', 'br_display_out_of_stock', 'br_downloads_use_local', 'br_downloads_use_s3', 'br_downlaods_s3_access_key', 'br_downlaods_s3_secret_key', 'br_downlaods_s3_length');
     foreach ($local_config_opts as $opts) {
         $c = $this->EE->config->item($opts);
         if ($c != '' && $c !== FALSE) {
             $this->_config["store"][$this->site_id][ltrim($opts, 'br_')] = $c;
         }
     }
     foreach ($this->_config["store"][$this->site_id] as $key => $val) {
         $ignore = array('store_id', 'site_id', 'channel_id');
         if (!in_array($key, $ignore)) {
             $this->_config["store"][$this->site_id][$key] = $this->_set_config_value($val);
         }
     }
     $this->br_channel_id = $this->_config["store"][$this->site_id]["channel_id"];
     $this->_config["currency"] = $this->_config["store"][$this->site_id]["currency"];
     $this->_config["currency_id"] = $this->_config["store"][$this->site_id]["currency_id"];
     $this->_config["currency_marker"] = $this->_config["store"][$this->site_id]["currency_marker"];
     $this->_config["result_limit"] = $this->_config["store"][$this->site_id]["result_limit"];
     # Limit the number of records returned by search
     $this->_config["result_per_page"] = $this->_config["store"][$this->site_id]["result_per_page"];
     # Limit the number of records per search result page
     $this->_config["result_paginate"] = $this->_config["store"][$this->site_id]["result_paginate"];
     # Number to list in the pagination links
     $this->_config["register_group"] = $this->_config["store"][$this->site_id]["register_group"];
     # Number to list in the pagination links
     // Set the product types
     $this->_config['product_type'] = array(1 => lang('br_basic'), 2 => lang('br_bundle'), 3 => lang('br_configurable'), 4 => lang('br_downloadable'), 5 => lang('br_virtual'), 7 => lang('br_donation'));
     // Sort them alphabetically but maintain the key association (uasort not sort!)
     //
     uasort($this->_config['product_type'], array($this, '_product_type_sort'));
     // Check the license
     $lic = $this->_config["store"][$this->site_id]["license"];
     $this->_validate_license($lic);
     // Set the statuses in a more usable format
     foreach ($this->_config["system"][$this->site_id]["status"]["config_data"] as $key => $val) {
         $this->_config["status"][$val["value"]] = $val["label"];
     }
     // Set allowed filetypes for order notes
     $this->_config["allowed_filetypes"] = 'doc|docx|pdf|ppt|pptx|zip|gif|jpg|png';
     // Set the media path
     $this->_config["media_dir"] = rtrim($this->_config["store"][$this->site_id]["media_dir"], '/') . '/';
     $this->_config["media_url"] = rtrim($this->_config["store"][$this->site_id]["media_url"], '/') . '/';
     // Load Gateway / Shipping Files
     $this->_load_files('gateway');
     $this->_load_files('shipping');
     $this->_load_files('integration');
     // Adding a hook to update the system _config variable after the system
     // defaults are loaded. -dpd v.1.4.2
     if ($this->EE->extensions->active_hook('br_core_config_after') === TRUE) {
         $this->_config = $this->EE->extensions->call('br_core_config_after', $this->_config);
     }
     // Make a global reference to the currency_marker variable that we
     // can use in all of our view files
     $this->vars["currency_marker"] = $this->_config["currency_marker"];
     // Make sure the snippets are all initiated
     $this->_init_snippets();
 }
 /**
  * _check_product_entry_pair function.
  * 
  * Create a method for verifying that there is a product and 
  * entry for all products in the system
  * 
  * @access private
  * @return void
  */
 function _check_product_entry_pair()
 {
     $this->EE->load->database();
     $sql = "SELECT \n    \t\t\t\t\tp.product_id, \n    \t\t\t\t\tp.title, \n    \t\t\t\t\tp.enabled \n    \t\t\t\tFROM \n    \t\t\t\t\t" . $this->EE->db->dbprefix . "br_product p \n    \t\t\t\tWHERE \n    \t\t\t\t\tp.site_id = " . $this->vars["site_id"] . " \n    \t\t\t\tAND \n    \t\t\t\t\tp.product_id \n    \t\t\t\t\t\tNOT IN \n    \t\t\t\t\t\t\t(SELECT product_id FROM " . $this->EE->db->dbprefix . "br_product_entry)";
     $qry = $this->EE->db->query($sql);
     if ($qry->num_rows() >= 0) {
         foreach ($qry->result_array() as $rst) {
             $data = array('title' => $rst["title"], 'status' => $rst["enabled"] == 1 ? 'open' : 'closed', 'entry_date' => time());
             $this->EE->api_channel_entries->submit_new_entry($this->br_channel_id, $data);
             $qry = $this->EE->db->query("SELECT entry_id FROM exp_channel_titles ORDER BY entry_id DESC LIMIT 1");
             $result = $qry->result_array();
             $this->EE->db->query("\tINSERT INTO \n    \t\t\t\t\t\t\t\t\t\t\texp_br_product_entry \n    \t\t\t\t\t\t\t\t\t\t(product_id, entry_id) \n    \t\t\t\t\t\t\t\t\t\t\tVALUES \n    \t\t\t\t\t\t\t\t\t\t(" . $rst["product_id"] . "," . $result[0]["entry_id"] . ")");
             // Remove cache file
             remove_from_cache('product_' . $rst["product_id"]);
         }
     }
 }
 function ipn_create_order($merchant_id, $status)
 {
     // Load the extension library so we can get add our hooks
     $this->EE->load->library('extensions');
     // And we need our order model
     $this->EE->load->model('order_model');
     // Check for a valid ipn order
     if (!($order = $this->_ipn_validate_order($merchant_id))) {
         return;
     }
     // There is an order waiting for IPN verfication
     // This is a brand new order
     if ($order["status_id"] == -1) {
         // Process order_create_before hook
         if ($this->EE->extensions->active_hook('br_order_create_before') === TRUE) {
             $order = $this->EE->extensions->call('br_order_create_before', $order);
         }
         // To 'create' this order we just need to update the status from
         // -1 to the new status.
         $order = $this->EE->order_model->get_order($order["order_id"]);
         $note = isset($order["notes"][0]["order_note"]) ? $order["notes"][0]["order_note"] : '';
         $has_item = false;
         $has_subscription = false;
         $has_donation = false;
         $vars[0] = array("fname" => $order["member"]["br_fname"], "lname" => $order["member"]["br_lname"], "email" => $order["member"]["email"], "address" => $order["address"], "payment" => $order["payment"], "order_id" => $order["order_id"], "order_num" => $order["order_id"], "order_note" => $note, "delivery_method" => $order["shipment"][0]["method"], "delivery_label" => $order["shipment"][0]["label"], "items" => $order["items"], "order_subtotal" => $this->_currency_round($order["base"]), "discount_total" => $this->_currency_round($order["discount"]), "tax_total" => $this->_currency_round($order["tax"]), "shipping" => $this->_currency_round($order["shipping"]), "order_total" => $this->_currency_round($order["total"] + $order["tax"] + $order["shipping"]), "has_item" => $has_item, "has_subscription" => $has_subscription, "has_donation" => $has_donation);
         $arr = array("order_id" => $order["order_id"], "status_id" => $status);
         $this->EE->order_model->update_order_status($arr);
         // Send the notification
         if ($this->EE->config->item('br_suppress_new_order_email') !== TRUE) {
             // Hook after we create the order before cleanup
             if ($this->EE->extensions->active_hook('br_order_email_before') === TRUE) {
                 $vars = $this->EE->extensions->call('br_order_email_before', $vars, $data);
             }
             $this->_send_email('customer-order-new', $vars);
         }
         // Reduce the inventory
         foreach ($order["items"] as $items) {
             // Reduce the item inventory
             $this->EE->order_model->reduce_item_inventory($items);
             remove_from_cache('product_' . $items["product_id"]);
         }
         // Hook after we create the order before cleanup
         $order["status_id"] = $status;
         if ($this->EE->extensions->active_hook('br_order_create_after') === TRUE) {
             $order = $this->EE->extensions->call('br_order_create_after', $order);
         }
     } elseif ($order["status_id"] == 2) {
         // This is an order moving from pending to
         // processing
         $data = array("order_id" => $order["order_id"], "status_id" => $status);
         // Hook before we update the order
         if ($this->EE->extensions->active_hook('br_order_update_before') === TRUE) {
             $data = $this->EE->extensions->call('br_order_update_before', $data);
         }
         $this->EE->order_model->update_order_status($data);
         // Hook after we update the order
         if ($this->EE->extensions->active_hook('br_order_update_after') === TRUE) {
             $data = $this->EE->extensions->call('br_order_update_after', $data);
         }
         $tmp = $this->EE->order_model->get_order($order["order_id"]);
         $eml[0]["email"] = $tmp["member"]["email"];
         $eml[0]["order_id"] = $order["order_id"];
         $eml[0]["order_num"] = $order["order_num"];
         $eml[0]["order_status"] = $this->_config["status"][$status_id];
         foreach ($tmp["member"] as $key => $val) {
             if (substr($key, 0, 3) == 'br_') {
                 $eml[0][str_replace("br_", "", $key)] = $val;
             }
         }
         $this->_send_email('customer-order-status', $eml);
     } elseif ($order["status_id"] == 3) {
         // This is an order moving from processing to
         // something else
         $data = array("order_id" => $order["order_id"], "status_id" => $status);
         // Hook before we update the order
         if ($this->EE->extensions->active_hook('br_order_update_before') === TRUE) {
             $data = $this->EE->extensions->call('br_order_update_before', $data);
         }
         $this->EE->order_model->update_order_status($data);
         // Hook after we update the order
         if ($this->EE->extensions->active_hook('br_order_update_after') === TRUE) {
             $data = $this->EE->extensions->call('br_order_update_after', $data);
         }
         $tmp = $this->EE->order_model->get_order($order["order_id"]);
         $eml[0]["email"] = $tmp["member"]["email"];
         $eml[0]["order_id"] = $order["order_id"];
         $eml[0]["order_num"] = $order["order_num"];
         $eml[0]["order_status"] = $this->_config["status"][$status_id];
         foreach ($tmp["member"] as $key => $val) {
             if (substr($key, 0, 3) == 'br_') {
                 $eml[0][str_replace("br_", "", $key)] = $val;
             }
         }
         $this->_send_email('customer-order-status', $eml);
     }
     $_SESSION["order_id"] = $order["order_id"];
     return;
 }