/** * Updates a contact. * * @param Model_Contact $contact The contact to update. * @param array $data The data to use to update the contact. * * @return Model_Contact */ public static function update(Model_Contact $contact, array $data = array()) { $contact->populate($data); try { $contact->save(); } catch (FuelException $e) { Log::error($e); return false; } return $contact; }
/** * Processes the shopping cart with the payment processor, saves the user's * cart, clears it and redirects to a success method. * * @return null */ public function action_process() { $order = Auth::instance()->get_user()->cart(); $user_post = arr::get($_POST, 'user', array()); $user = arr::get($_POST, 'user', Auth::instance()->get_user()); if (!is_object($user)) { $temp = $user; $user = AutoModeler_ORM::factory('user'); $user->set_fields($temp); } $address = AutoModeler_ORM::factory('vendo_address'); $address->set_fields(arr::get($_POST, 'address', array())); // Build the contact model $contact = new Model_Contact(); $contact->set_fields(array('email' => $user->email, 'first_name' => $user->first_name, 'last_name' => $user->last_name)); // Build the credit card model $credit_card_post = arr::get($_POST, 'payment'); $credit_card = new Model_Credit_Card(arr::get($credit_card_post, 'card_number'), arr::get($credit_card_post, 'months') . arr::get($credit_card_post, 'years'), arr::get($credit_card_post, 'card_code'), $contact, $address); $errors = array(); // Check for a new user registration, and make a user if so if ($this->should_create_account($user)) { $status = $this->process_new_account($user, $user_post, $address); if (!$status) { return; } $contact->save(); $order->user_id = $user->id; } else { $user_address = $user->address->as_array(); unset($user_address['id']); if ($user_address != $address and TRUE === $address->is_valid()) { $address->save(); $user->address_id = $address->id; } elseif (TRUE !== $address->is_valid()) { $errors += $address->errors('form_errors'); } else { $address = $user->address; } $contact->address_id = $address->id; if (TRUE !== $contact->is_valid()) { $errors += $contact->errors('form_errors'); } if (Auth::instance()->logged_in()) { $order->user_id = $user->id; } } // Verify the credit card is valid if (TRUE !== ($cc_errors = $credit_card->validate())) { $errors += $cc_errors; } if ($errors) { // If we've failed, and we aren't registering a new user, delete // the address if (!$user->id) { $address->delete(); } $this->request->response = new View_Checkout_Index(); $this->request->response->set(array('user' => $user->as_array(), 'address' => $address->as_array(), 'cart' => Auth::instance()->get_user()->cart(), 'credit_card' => $credit_card)); $errors = (string) View::factory('form_errors')->set(array('errors' => $errors)); $this->request->response->errors = $errors; return; } $order->credit_card = $credit_card; // Process the credit card try { $status = Payment::process($order); if (1 != $status->response_code) { throw new Payment_Exception('Problem processing your payment.'); } // Persist the order $contact->save(); $order->contact_id = $contact->id; $order->address_id = $address->id; $order->save(); Auth::instance()->get_user()->cart(new Model_Order()); // Show success message! $this->request->response = new View_Checkout_Process(); } catch (Payment_Exception $e) { // If we've failed, and we aren't registering a new user, delete // the address if (!$user->id) { $address->delete(); } $this->request->response = new View_Checkout_Index(); $this->request->response->set(array('user' => $user->as_array(), 'address' => $address->as_array(), 'cart' => Auth::instance()->get_user()->cart(), 'credit_card' => $credit_card)); $errors = (string) View::factory('form_errors')->set(array('errors' => array('general' => $e->getMessage()))); $this->request->response->errors = $errors; return; } }