protected function seedTestData() { $faker = Faker::create(); $store = Driver::create(array('email' => $faker->email, 'password' => \Hash::make($faker->name), 'photo' => $faker->imageUrl(250, 250), 'name' => $faker->name, 'seller_id' => $faker->firstName, 'address' => array('street' => $faker->streetAddress, 'city' => $faker->city, 'country' => $faker->country, 'postcode' => $faker->postcode), 'currency' => 'USD')); Category::create(array('name' => 'food', 'description' => $faker->text(), 'status' => 1)); Product::create(array('name' => $faker->name, 'driver_id' => $store->id, 'description' => $faker->text(), 'photos' => $faker->imageUrl(250, 250), 'tax_amount' => $faker->randomFloat(2, 0, 2), 'unit_price' => $faker->randomFloat(1, 2, 10), 'category_id' => 1, 'inventory' => 0, 'average_cost' => 0)); }
/** * @param $notification * * @return array|\Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Model|static */ public function transformStock($notification) { $stock = []; $product = Product::find($notification->value); $stock['message'] = $this->translator->trans('notifications.stock_reminder', ['product_name' => $product->name]); $stock['type'] = 'stock_reminder'; return $stock; }
/** * Update the Product average cost and inventory. * * @param \Paxifi\Store\Repository\Product\EloquentProductRepository $product * * @return void */ private function updateProduct($product) { $count = $product->costs->count(); if ($count === 0) { $product->average_cost = 0; $product->inventory = 0; } else { $totalCost = 0; $totalInventory = 0; foreach ($product->costs as $cost) { $totalCost += $cost->unit_cost; $totalInventory += $cost->inventory; } $product->average_cost = round($totalCost / $count, 2); $product->inventory = $totalInventory; } $product->save(); }
public function testCreateProduct() { $data = array('name' => $this->faker->name, 'driver_id' => 1, 'description' => $this->faker->text(), 'photos' => array(array('order' => 1, 'url' => $this->faker->imageUrl(250, 250)), array('order' => 2, 'url' => $this->faker->imageUrl(250, 250)), array('order' => 3, 'url' => $this->faker->imageUrl(250, 250))), 'tax_amount' => $this->faker->randomFloat(2, 0, 2), 'unit_price' => $this->faker->randomFloat(1, 2, 10), 'category_id' => 1, 'inventory' => 0, 'average_cost' => 0, 'costs' => array(array('unit_cost' => 10.7, 'inventory' => 10), array('unit_cost' => 5.25, 'inventory' => 20), array('unit_cost' => 7.0, 'inventory' => 15))); $this->assertEquals(0, Product::all()->count()); $response = json_decode($this->call('post', 'drivers/1/products', $data)->getContent(), true); $this->assertResponseStatus(201); $this->assertEquals(1, Product::all()->count()); $this->assertEquals(3, Product::find(1)->costs->count()); }
/** * Paypal cart payment ipn handler * * @return \Illuminate\Http\JsonResponse */ public function payment() { try { \DB::beginTransaction(); $listener = new Listener(); $verifier = new SocketVerifier(); // $ipn = \Input::all(); $ipnMessage = Message::createFromGlobals(); // uses php://input $verifier->setIpnMessage($ipnMessage); $verifier->setEnvironment(\Config::get('app.paypal_mode')); $listener->setVerifier($verifier); $listener->onVerifiedIpn(function () use($listener) { $ipn = []; $messages = $listener->getVerifier()->getIpnMessage(); parse_str($messages, $ipn); if ($order = EloquentOrderRepository::find(\Input::get('custom'))) { /** * Check the paypal payment. * * Total sales == ipn['mc_gross'] * * Driver Paypal Account == ipn['business] */ if ($ipn['payment_status'] == 'Completed' && $order->total_sales == $ipn['payment_gross'] && $order->OrderDriver()->paypal_account == $ipn['business']) { \Event::fire('paxifi.paypal.payment.' . $ipn['txn_type'], [$order->payment, $ipn]); $products = $order->products; $products->map(function ($product) { // Fires an event to update the inventory. \Event::fire('paxifi.product.ordered', array($product, $product['pivot']['quantity'])); // Fires an event to notification the driver that the product is in low inventory. if (EloquentProductRepository::find($product->id)->inventory <= 5) { \Event::fire('paxifi.notifications.stock', array($product)); } }); } \DB::commit(); } }); $listener->listen(function () use($listener) { $resp = $listener->getVerifier()->getVerificationResponse(); }, function () use($listener) { // on invalid IPN (somethings not right!) $report = $listener->getReport(); $resp = $listener->getVerifier()->getVerificationResponse(); \Log::useFiles(storage_path() . '/logs/' . 'error-' . time() . '.txt'); \Log::info($report); return $this->setStatusCode(400)->respondWithError('Payment failed.'); }); } catch (\RuntimeException $e) { return $this->setStatusCode(400)->respondWithError($e->getMessage()); } catch (\Exception $e) { print_r($e->getMessage()); return $this->errorInternalError(); } }
public function run() { /** * Truncate Product Tables before seed faker data */ DB::table('products')->truncate(); DB::table('product_costs')->truncate(); $faker = Faker::create(); for ($i = 0; $i < 10; $i++) { $product = Product::create(array('name' => $faker->name, 'driver_id' => $faker->numberBetween(1, 10), 'description' => $faker->text(), 'photos' => array(array('order' => 1, 'url' => $faker->imageUrl(250, 250))), 'tax_amount' => 0.02 + $i * 0.001, 'unit_price' => $faker->randomFloat(1, 2, 10), 'category_id' => $faker->numberBetween(1, 3), 'inventory' => 0, 'average_cost' => 0)); for ($j = 0; $j < 5; $j++) { Cost::create(array('unit_cost' => (5 + $j) / 2, 'inventory' => 10, 'product_id' => $product->id)); } } }
/** * @param $payment * * @return \Illuminate\Http\JsonResponse */ public function confirm($payment) { try { \DB::beginTransaction(); if ($this->getAuthenticatedDriver()->email != $payment->order->OrderDriver()->email) { throw new PaymentNotMatchException('Payment owner not match'); } if ($payment->status == 1) { return $this->errorWrongArgs('Payment has been completed already.'); } with(new UpdatePaymentValidator())->validate(\Input::only('confirm')); $confirm = \Input::get('confirm', 1); $payment->status = $confirm; $payment->save(); $order = $payment->order; $order->status = 1; $order->save(); if ($confirm == 1) { $products = $payment->order->products; $products->map(function ($product) { // Fires an event to update the inventory. \Event::fire('paxifi.product.ordered', array($product, $product['pivot']['quantity'])); // Fires an event to notification the driver that the product is in low inventory. if (EloquentProductRepository::find($product->id)->inventory <= 5) { $product->type = "inventory"; \Event::fire('paxifi.notifications.stock', [$product]); } }); } \DB::commit(); return $this->setStatusCode(200)->respond(["success" => true]); } catch (PaymentNotMatchException $e) { return $this->errorForbidden(); } catch (ValidationException $e) { return $this->errorWrongArgs($e->getErrors()); } catch (\Exception $e) { return $this->errorInternalError(); } }
/** * Sort the products. * * @return \Illuminate\Http\JsonResponse */ public function setWeight() { try { $driver = $this->getAuthenticatedDriver(); $weights = \Input::get('weights'); foreach ($weights as $index => $weights) { if ($product = EloquentProductRepository::find($weights['id'])) { if ($product->driver_id == $driver->id) { $product->weight = $weights['weight']; $product->save(); } } } return $this->setStatusCode(200)->respondWithCollection($driver->products); } catch (\Exception $e) { return $this->errorInternalError($e->getMessage()); } }
/** * Add order item. * * @param array $item * * @throws \InvalidArgumentException * @return $this */ public function addItem(array $item) { // Product id exists? $product = EloquentProductRepository::findOrFail($item['product_id']); // Product stock available? if ((int) $item['quantity'] > $product->inventory) { throw new \InvalidArgumentException('Stock is not available.'); } // Total Items $this->total_items += $item['quantity']; // Total Costs $this->total_costs += $product->average_cost * $item['quantity']; // Total Tax $totalUnitPrice = $product->unit_price * $item['quantity']; if ($product->driver->tax_enabled) { $totalTax = Calculator::calculate($totalUnitPrice, $product->getTaxRate()); $this->total_tax += $totalTax; } else { $totalTax = 0; $this->total_tax += $totalTax; } // Total Sales $this->total_sales += $product->getTaxRate()->isIncludedInPrice() ? $totalUnitPrice : $totalUnitPrice + $totalTax; $this->products()->attach($product->id, array('quantity' => $item['quantity'])); return $this; }