/** * Creates a new customer order. * * @param Model_Customer $customer The customer the order belongs to. * @param array $products An array of one or more products to order. * @param Model_Customer_Paymentmethod $paymentmethod The payment method to use for the transaction. * @param array $data Optional data. * * @return Model_Customer_Order */ public static function create(Model_Customer $customer, array $products, Model_Customer_Paymentmethod $paymentmethod = null, $data = array()) { if ($paymentmethod && $paymentmethod->customer != $customer) { return false; } if (!$paymentmethod) { // Use the customer's primary payment method if none is provided. if (!($paymentmethod = Service_Customer_Paymentmethod::primary($customer))) { return false; } } $product_options = array(); $transaction_total = 0; foreach ($products as $id => $name) { $option = Service_Product_Option::find_one($id); if (!$option instanceof Model_Product_Option) { continue; } $product_options[] = $option; $transaction_total += $option->sum_fees(); } // Attempt to charge the customer for the order's total. if (!($transaction = Service_Customer_Transaction::create($paymentmethod, $transaction_total))) { return false; } $order = Model_Customer_Order::forge(); $order->customer = $customer; $order->transaction = $transaction; $order->populate($data); try { $order->save(); } catch (FuelException $e) { Log::error($e); return false; } // Link products to customer. foreach ($product_options as $option) { Service_Customer_Product_Option::create(Arr::get($products, $option->id), $order, $option, $data); } // Mark the order as completed. self::update($order, array('status' => 'completed')); Service_Event::trigger('customer.order.create', $customer->seller, $order->to_array()); return $order; }
/** * Gets one or more customer transactions. * * @param int $customer_id Customer ID. * @param int $id Transaction ID. * * @return void */ public function get_index($customer_id = null, $id = null) { if (!$customer_id) { throw new HttpNotFoundException(); } $customer = \Service_Customer::find_one($customer_id); if (!$customer || $customer->seller != \Seller::active()) { throw new HttpNotFoundException(); } if (!$id) { $transactions = \Service_Customer_Transaction::find(array('customer' => $customer)); } else { $transactions = \Service_Customer_Transaction::find_one($id); if (!$transactions || $transactions->customer != $customer) { throw new HttpNotFoundException(); } } $this->response($transactions); }
/** * Displays a customer's transactions. * * @param int $customer_id Customer ID. * * @return void */ public function action_index($customer_id = null) { $customer = $this->get_customer($customer_id); $this->view->customer = $customer; $this->view->transactions = Service_Customer_Transaction::find(array('customer' => $customer)); }