예제 #1
0
 /**
  * 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;
 }
예제 #2
0
 /**
  * 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;
 }
예제 #3
0
 /**
  * Attempts to get a product option from a given ID.
  *
  * @param int $id Product option ID.
  *
  * @return Model_Product_Option
  */
 protected function get_option($id)
 {
     $option = Service_Product_Option::find_one($id);
     if (!$option || $option->product->seller != Seller::active()) {
         throw new HttpNotFoundException();
     }
     return $option;
 }
예제 #4
0
 /**
  * Generates products.
  *
  * @return void
  */
 protected static function products()
 {
     foreach (self::$sellers as $seller) {
         $product = \Service_Product::create('Product Line ' . \Str::upper(\Str::random('alpha', 1)), $seller);
         if ($product) {
             for ($i = 1; $i <= 3; $i++) {
                 $option = \Service_Product_Option::create('Product ' . \Str::upper(\Str::random('alpha', 1)), $product);
                 if ($option) {
                     self::$product_options[$seller->id][] = $option;
                     \Service_Product_Option_Fee::create('Subscription', 1, 'month', mt_rand(5, 15), $option);
                 }
             }
         }
     }
     \Cli::write('Product Simulation Complete', 'green');
 }