/** * Attach a user's shipping address to their order * * @param Order $order * @param array $address * * @return Order */ private function attachShippingAddress(Order $order, array $address) { // Make sure all required keys are present if (!array_key_exists('first_name', $address) || empty($address['first_name']) || !array_key_exists('last_name', $address) || empty($address['last_name']) || !array_key_exists('email', $address) || empty($address['email']) || !array_key_exists('address1', $address) || empty($address['address1']) || !array_key_exists('address2', $address) || !array_key_exists('city', $address) || empty($address['city']) || !array_key_exists('state', $address) || empty($address['state']) || !array_key_exists('postcode', $address) || empty($address['postcode']) || !array_key_exists('country', $address) || empty($address['country'])) { throw new Exception('Missing required shipping information.'); } // Address cleaning foreach ($address as $key => $value) { $address[$key] = trim($value); } $address['first_name'] = ucfirst(strtolower($address['first_name'])); $address['last_name'] = ucfirst(strtolower($address['last_name'])); // Make the customer $customer = Customer::firstOrCreate(['first_name' => $address['first_name'], 'last_name' => $address['last_name'], 'email' => $address['email']]); // Attach the customer to the order $order->shipping_address = $address; $order->customer_id = $customer->id; $order->save(); return $order; }
/** * Completes a shopping cart * @param Order $order */ public function markAsComplete(Order $order) { $this->load('items.inventory'); foreach ($this->items as $item) { $item->inventory->quantity -= $item->quantity; $item->inventory->save(); } $order->is_complete = true; $order->save(); if (!$this->couponIsApplied || !isset($this->coupon->name)) { $this->coupon_id = null; } else { $this->backup_couponName = $this->coupon->name; } $this->backup_total = $this->total; $this->backup_totalBeforecoupon = $this->totalBeforeCoupon; $this->backup_fullTotal = $this->fullTotal; $this->order_id = $order->id; $this->save(); Session::forget('bedard_shop_order'); }
/** * Attempt to complete the purchase * @return Redirect */ public function onRun() { try { $gateway = $this->openPaypalExpressGateway(); $session = Session::get('bedard_shop_order'); $order = Order::where('hash', $session['hash'])->where('is_complete', false)->find($session['id']); $response = $gateway->completePurchase(['amount' => $order->amount, 'currency' => PaySettings::get('currency_code')])->send(); $data = $response->getData(); $order->gateway_code = $data['PAYMENTINFO_0_TRANSACTIONID']; if ($response->isSuccessful()) { $order->cart->markAsComplete($order); } else { throw new Exception('Failed response from PayPal.'); } } catch (Exception $e) { var_dump($e->getMessage()); } }
/** * Mark selected records as shipped */ public function index_onMarkAsShipped() { $successful = TRUE; if (($checkedIds = post('checked')) && is_array($checkedIds) && count($checkedIds)) { foreach ($checkedIds as $recordId) { if (!($record = Order::find($recordId))) { continue; } if (!$record->touchShipped()) { $successful = FALSE; } } } if ($successful) { Flash::success('Successfully marked orders as shipped.'); } else { Flash::error('An unknown error has occured.'); } return $this->listRefresh(); }
public function run() { //disable foreign key check for this connection before running seeders DB::statement('SET FOREIGN_KEY_CHECKS=0;'); /** * CATEGORY SEEDS */ $i = 3; foreach (['Boards', 'Shirts', 'Hoodies', 'Stickers', 'Jackets'] as $category) { Category::create(['position' => $i, 'name' => $category, 'slug' => strtolower($category), 'is_visible' => 1, 'is_active' => 1]); $i++; } foreach (['Winter', 'DVDs'] as $category) { Category::create(['position' => $i, 'name' => $category, 'slug' => strtolower($category), 'is_visible' => 0, 'is_active' => 1]); $i++; } Category::create(['position' => $i, 'name' => 'Hats', 'slug' => 'hats', 'is_visible' => 1, 'is_active' => 0]); /** * PRODUCT SEEDS */ $colors = ['red', 'blue', 'green', 'black', 'orange', 'yellow', 'purple', 'white']; $products = ['shirt', 'hat', 'board', 'sticker']; $seeds = []; foreach ($products as $product) { foreach ($colors as $color) { $seeds[] = $color . ' ' . $product; } } shuffle($seeds); Product::truncate(); foreach ($seeds as $seed) { $product = Product::create(['name' => $seed, 'slug' => str_replace(' ', '-', $seed), 'full_price' => rand(10, 20), 'description' => "Some awesome {$seed}... You should totaly buy it.", 'is_active' => rand(0, 10) > 0 ? 1 : 0, 'is_visible' => rand(0, 10) > 0 ? 1 : 0]); if (strpos($seed, 'board') !== FALSE) { $product->categories()->attach(3); } elseif (strpos($seed, 'hat') !== FALSE) { $product->categories()->attach(10); } elseif (strpos($seed, 'shirt') !== FALSE) { $product->categories()->attach(4); } elseif (strpos($seed, 'sticker') !== FALSE) { $product->categories()->attach(6); } $small = Inventory::create(['product_id' => $product->id, 'name' => 'Small', 'quantity' => rand(0, 2), 'is_active' => rand(0, 10) > 0 ? 1 : 0]); $medium = Inventory::create(['product_id' => $product->id, 'name' => 'Medium', 'quantity' => rand(0, 2), 'position' => 1, 'is_active' => rand(0, 10) > 0 ? 1 : 0]); $large = Inventory::create(['product_id' => $product->id, 'name' => 'Large', 'quantity' => rand(0, 2), 'position' => 2, 'is_active' => rand(0, 10) > 0 ? 1 : 0]); } /** * DISCOUNT SEEDS */ // Demo category discount DB::table('bedard_shop_discounts')->insert(['name' => 'Category Discount', 'amount' => rand(10, 25), 'is_percentage' => 1]); DB::table('bedard_shop_discountables')->insert(['discount_id' => 1, 'discountable_id' => rand(3, 6), 'discountable_type' => 'Bedard\\Shop\\Models\\Category']); DB::table('bedard_shop_discounts')->insert(['name' => 'Product Discount', 'amount' => rand(5, 8), 'is_percentage' => 0]); DB::table('bedard_shop_discountables')->insert(['discount_id' => 2, 'discountable_id' => rand(1, 20), 'discountable_type' => 'Bedard\\Shop\\Models\\Product']); /** * PROMO CODE */ Coupon::create(['name' => 'Foo', 'message' => 'Thanks for entering "foo".', 'amount' => rand(10, 20), 'is_percentage' => 1, 'cart_value' => rand(20, 50)]); // Enable foreign keys DB::statement('SET FOREIGN_KEY_CHECKS=1;'); /** * Mock carts */ $fnames = ['John', 'Mary', 'Alex', 'Mark', 'Sally']; $lnames = ['Smith', 'Johnson', 'Jones', 'Doe']; for ($i = 0; $i < 30; $i++) { $cart = Cart::create([]); $inventories = []; for ($j = 0; $j < rand(5, 15); $j++) { $inventories[] = rand(1, 95); } foreach ($inventories as $inventory) { $item = CartItem::firstOrCreate(['cart_id' => $cart->id, 'inventory_id' => $inventory, 'quantity' => rand(1, 2)]); } $first = $fnames[rand(0, 4)]; $last = $lnames[rand(0, 3)]; $customer = Customer::firstOrCreate(['first_name' => $first, 'last_name' => $last, 'email' => strtolower("{$first}.{$last}@example.com")]); $order = Order::create(['created_at' => Carbon::now()->subDays(rand(1, 50))]); $order->customer_id = $customer->id; $order->cart_id = $cart->id; $order->amount = $cart->total; $cart->markAsComplete($order); $order->shipping_address = ['first_name' => $first, 'last_name' => $last, 'address1' => '123 Foo Street', 'city' => 'Beverly Hills', 'state' => 'CA', 'postcode' => '90210', 'country' => 'US']; $order->gateway = 'PayPal_Express'; $order->gateway_code = 'FAKE-PAYPAL-ID'; $order->save(); } }