/**
  * @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;
 }
 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());
 }
示例#3
0
 /**
  * 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 testUpdateProductInventoryByMultiple()
 {
     Cost::create(array('unit_cost' => 10.1, 'inventory' => 10, 'product_id' => 1));
     Cost::create(array('unit_cost' => 10.0, 'inventory' => 10, 'product_id' => 1));
     $this->assertEquals(20, Product::find(1)->inventory);
     Product::find(1)->updateInventory(3);
     $this->assertEquals(17, Product::find(1)->inventory);
     Cost::create(array('unit_cost' => 12.1, 'inventory' => 10, 'product_id' => 1));
     $this->assertEquals(27, Product::find(1)->inventory);
 }
 public function testCorrectlyUpdateProductInventoryAfterCostDeletion()
 {
     $this->assertEquals(0, Product::find(1)->inventory);
     Cost::create(array('unit_cost' => 10.1, 'inventory' => 10, 'product_id' => 1));
     $this->assertEquals(10, Product::find(1)->inventory);
     Cost::find(1)->delete();
     $this->assertEquals(0, Product::find(1)->inventory);
     Cost::create(array('unit_cost' => 10.1, 'inventory' => 10, 'product_id' => 1));
     Cost::create(array('unit_cost' => 7.33, 'inventory' => 10, 'product_id' => 1));
     Cost::find(2)->delete();
     $this->assertEquals(10, Product::find(1)->inventory);
 }
示例#6
0
 /**
  * @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();
     }
 }
示例#7
0
 /**
  * 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());
     }
 }