/** * Creates a new validation instance for order create. * * @param Model_Customer $customer The ordering customer. * * @return Validation */ public static function create(Model_Customer $customer) { $validator = Validation::forge('order'); $validator->add('products', 'Products')->add_rule('required')->add_rule(array('invalid_products' => function ($products) use($customer) { $ids = array_keys($products); foreach ($ids as $id) { if (!is_numeric($id)) { return false; } $product_option = Service_Product_Option::find_one($id); if (!$product_option || $product_option->product->seller != $customer->seller) { return false; } } return true; })); $validator->add('paymentmethod', 'Paymentmethod')->add_rule(array('invalid_paymentmethod' => function ($paymentmethod) use($customer) { if (!$paymentmethod) { return true; } $paymentmethod = Service_Customer_Paymentmethod::find_one($paymentmethod); if (!$paymentmethod || $paymentmethod->customer != $customer) { return false; } return true; })); return $validator; }
/** * 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; }
/** * Attempts to get a payment method from a given ID. * * @param int $id Payment method ID. * @param \Model_Customer $customer Customer the payment method should belong to. * * @return \Model_Customer_Paymentmethod */ protected function get_paymentmethod($id, \Model_Customer $customer) { if (!$id) { throw new HttpNotFoundException(); } $payment_method = \Service_Customer_Paymentmethod::find_one($id); if (!$payment_method || $payment_method->customer != $customer || $payment_method->customer->seller != \Seller::active()) { throw new HttpNotFoundException(); } return $payment_method; }
/** * Generates customers. * * @return void */ protected static function customers() { $balances = array(0, 5, 10, 12.5, 24.3); $first_names = array('Daniel', 'Steve', 'Elon', 'Bill', 'Stephan', 'Olivia', 'Keithia', 'Caroline', 'Porschia', 'Marie'); $last_names = array('Sposito', 'Manos', 'Myers', 'Jenkins', 'Gates', 'Jobs', 'Musk', 'Wyrick', 'Natalie', 'Stevens'); $streets = array('Wisteria Ln', 'Pebble Creek Blvd', 'Bakerstreet Dr', 'Miranda Point', 'Infinite Loop'); $cities = array('Oklahoma City', 'Phoenix', 'Palo Alto', 'Saigon', 'Caracas'); $states = array('OK', 'NY', 'CA', 'WA', 'FL'); $countries = array('US', 'VN', 'VE'); $customers = array(); for ($i = 1; $i <= self::TOTAL_CUSTOMERS; $i++) { $first_name = $first_names[array_rand($first_names)]; $last_name = $last_names[array_rand($last_names)]; // Random created_at between the begin date and now. $date = date("Y-m-d H:i:s", mt_rand(self::BEGIN_DATETIME, time())); $customers[] = array('balance' => $balances[array_rand($balances)], 'contact' => array('first_name' => $first_name, 'last_name' => $last_name, 'email' => $first_name . '.' . $last_name . mt_rand(1, 100) . '@gmail.com', 'address' => mt_rand(1, 5000) . ' ' . $streets[array_rand($streets)], 'city' => $cities[array_rand($cities)], 'state' => $states[array_rand($states)], 'zip' => mt_rand(10000, 99999), 'country' => $countries[array_rand($countries)]), 'status' => mt_rand(1, 10) <= 8 ? 'active' : 'deleted', 'created_at' => $date); } foreach ($customers as $data) { $seller = self::$sellers[array_rand(self::$sellers)]; $customer = \Service_Customer::create($seller, $data); // Create a payment method for the faux customer. $payment_method = \Service_Customer_Paymentmethod::create($customer, self::$gateway, array('contact' => $data['contact'], 'account' => array('provider' => 'Volcano', 'number' => '6011000000000012', 'expiration_month' => '12', 'expiration_year' => '5'))); // Subscribe a random number of customers to a product option. if ($customer && mt_rand(1, 10) >= 5) { $option = self::$product_options[$seller->id][array_rand(self::$product_options[$seller->id])]; $order = \Service_Customer_Order::create($customer, array($option->id => 'My ' . \Str::random('alpha', 5)), null, array('created_at' => $customer->created_at)); // Reset the order's updated_at. self::updateUpdatedAt($order); } // Reset the models' updated_at. self::updateUpdatedAt($customer); foreach ($customer->products as $product) { self::updateUpdatedAt($product); } } \Cli::write('Customer Simulation Complete', 'green'); }