/** * Checkout */ public function checkout() { $output = ''; if ($_SERVER['REQUEST_METHOD'] != 'POST') { $output = $this->_checkout_step_1(); } else { //valid helper include 'classes/valid.class.php'; $valid = new Valid(); //validation class include 'classes/validation.class.php'; $step = $_POST['step']; if ($step == '1') { //step 1 validation $post = new Validation($_POST['order']); $post->add_rules('first_name', 'required'); $post->add_rules('last_name', 'required'); $post->add_rules('company', 'required'); $post->add_rules('address', 'required'); $post->add_rules('city', 'required'); $post->add_rules('state', 'required'); $post->add_rules('country', 'required'); $post->add_rules('zip', 'required'); $post->add_rules('phone', 'required', array($valid, 'phone')); $post->add_rules('email', 'required', array($valid, 'email')); if (!isset($_POST['billing_is_shipping'])) { $post->add_rules('ship_first_name', 'required'); $post->add_rules('ship_last_name', 'required'); $post->add_rules('ship_company', 'required'); $post->add_rules('ship_address', 'required'); $post->add_rules('ship_city', 'required'); $post->add_rules('ship_state', 'required'); $post->add_rules('ship_country', 'required'); $post->add_rules('ship_zip', 'required'); $post->add_rules('ship_phone', 'required', array($valid, 'phone')); } $post->pre_filter('trim'); //success, go to step 2 if ($post->validate()) { //save order data $_SESSION['order'] = $_POST['order']; $output = $this->_checkout_step_2(); } else { $errors = $post->errors(); $output = $this->_checkout_step_1($_POST, $errors); } } elseif ($step == '2') { //step 2 validation $post = new Validation($_POST['order']); $post->add_rules('cc_name', 'required'); $post->add_rules('cc_type', 'required'); $post->add_rules('cc_number', 'required', array($valid, 'credit_card')); $post->add_rules('cc_cvv', 'required', 'length[3,4]', array($valid, 'digit')); $post->add_rules('cc_exp_month', 'required'); $post->add_rules('cc_exp_year', 'required'); if (isset($_POST['order']['cc_exp_month']) && isset($_POST['order']['cc_exp_year'])) { $post->add_callbacks('cc_exp_year', array($this, '_validate_cc_exp_date')); } $post->pre_filter('trim'); if ($post->validate()) { $cart = new Cart('shopping_cart'); //order data array $order_arr = array_merge($_SESSION['order'], $_POST['order']); $full_cc_number = $order_arr['cc_number']; $order_arr['cc_number'] = substr($order_arr['cc_number'], -4); $order_arr['promo_discount'] = $cart->getDiscount($order_arr['promo_code']); $order_arr['subtotal'] = $cart->getTotal(); $order_arr['tax'] = $cart->getTax(); //process payment include 'merchants/firstdata.class.php'; $merchant = new FirstData(); //billing info $merchant->name = $order_arr['first_name'] . ' ' . $order_arr['last_name']; $merchant->company = $order_arr['company']; $merchant->address = $order_arr['address']; $merchant->address2 = $order_arr['address2']; $merchant->city = $order_arr['city']; $merchant->state = $order_arr['state']; $merchant->country = $order_arr['country']; $merchant->phone = $order_arr['phone']; $merchant->fax = $order_arr['fax']; $merchant->email = $order_arr['email']; $merchant->zip = $order_arr['zip']; //shipping info $merchant->ship_name = $order_arr['ship_first_name'] . ' ' . $order_arr['ship_last_name']; $merchant->ship_address = $order_arr['ship_address']; $merchant->ship_saddress2 = $order_arr['ship_address2']; $merchant->ship_city = $order_arr['ship_city']; $merchant->ship_state = $order_arr['ship_state']; $merchant->ship_country = $order_arr['ship_country']; $merchant->ship_zip = $order_arr['ship_zip']; //payment info $merchant->cc_number = $full_cc_number; $merchant->cc_exp_month = $order_arr['cc_exp_month']; $merchant->cc_exp_year = substr($order_arr['cc_exp_year'], -2); $merchant->cc_cvv = $order_arr['cc_cvv']; $merchant->subtotal = $order_arr['subtotal']; $merchant->shipping = 0; $merchant->tax = $order_arr['tax']; $merchant->total = $order_arr['subtotal'] + $order_arr['tax'] - $order_arr['promo_discount']; // set to GOOD for test or LIVE $merchant->result = 'LIVE'; $merchant_success = false; $result = $merchant->sale(); if ($result['r_approved'] == "APPROVED") { $merchant_success = true; } //merchant error if (!$merchant_success) { $errors = $post->errors(); $this->set_flash($result['r_error'], 'error'); $output = $this->_checkout_step_2($_POST, $errors); } else { //save order to database $record = Record::insert('ecommerce_order', $order_arr); $order_id = Record::lastInsertId(); //save order items to database foreach ($cart->getItems() as $variant_id => $quantity) { //get variant data $variant = Record::findByIdFrom('ProductVariant', $variant_id); $variant->order_id = $order_id; $variant->quantity = $quantity; $variant_arr = (array) $variant; //remove unneeded fields unset($variant_arr['id']); unset($variant_arr['created_on']); unset($variant_arr['updated_on']); unset($variant_arr['position']); //insert $record = Record::insert('ecommerce_order_variant', $variant_arr); } //save log $this->_insert_log('Order <a href="' . get_url('plugin/ecommerce/order_show/' . $order_id) . '">' . $order_id . '</a> was placed.'); //send emails to client and buyer $this->_send_order_email('*****@*****.**', $order_id, $order_arr, $variant_arr); $this->_send_order_email($order_arr['email'], $order_id, $order_arr, $variant_arr); //success $this->set_flash('Thank you for your order. You will receive a confirmation email shortly.', 'success'); //clear cart and order session unset($_SESSION['order']); unset($_SESSION['Cart']); } } else { $errors = $post->errors(); $output = $this->_checkout_step_2($_POST, $errors); } } } return $output; }