Exemple #1
0
 /**
  * Create order record.
  *
  * @return \Illuminate\Http\JsonResponse
  */
 public function store()
 {
     try {
         \DB::beginTransaction();
         $items = Collection::make(\Input::get('items'));
         $order = new Order();
         // create the order
         $order->save();
         // Attach items to order
         $items->each(function ($item) use($order) {
             $order->addItem($item);
         });
         // Calculate commission & profit
         /** @var \Paxifi\Support\Commission\CalculatorInterface $calculator */
         $calculator = \App::make('Paxifi\\Support\\Commission\\CalculatorInterface');
         $calculator->setCommissionRate($order->OrderDriver()->getCommissionRate());
         $order->setCommission($calculator->calculateCommission($order));
         $order->setProfit($calculator->calculateProfit($order));
         // save order
         $order->save();
         \DB::commit();
         return $this->setStatusCode(201)->respondWithItem(Order::find($order->id));
     } catch (ModelNotFoundException $e) {
         return $this->errorWrongArgs('Invalid product id');
     } catch (\InvalidArgumentException $e) {
         return $this->errorWrongArgs($e->getMessage());
     } catch (\Exception $e) {
         return $this->errorInternalError();
     }
 }
Exemple #2
0
 function __construct(array $salesIds = array())
 {
     foreach ($salesIds as $sale) {
         $this->push(new SaleRepository(EloquentOrderRepository::find($sale->id)));
     }
     $this->calculateTotals();
 }
 public function run()
 {
     DB::table('orders')->truncate();
     $faker = Faker::create();
     for ($i = 0; $i < 10; $i++) {
         EloquentOrderRepository::create(array('total_costs' => 50.5, 'total_sales' => 70, 'total_items' => $faker->randomNumber(null), 'buyer_email' => $faker->email, 'feedback' => 1, 'comment' => $faker->text()));
     }
 }
 /**
  * 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();
     }
 }
 /**
  * Get all the images file path and url path for generate the invoice pdf and invoice email.
  *
  * @return array
  */
 public function getInvoiceData()
 {
     $invoice = [];
     $invoice['order'] = $this->order->toArray();
     $invoice['products'] = $this->order->products->toArray();
     $invoice['driver'] = $this->getOrderDriver()->toArray();
     $invoice['content'] = $this->translation;
     $invoice['template'] = ["email_logo" => $this->paxifiLogoUrlPath, "pdf_logo" => $this->paxifiLogoFilePath, "email_driver_logo" => $this->getDriverLogoImageUrl(), "pdf_driver_logo" => $this->getDriverLogoImagePath()];
     return $invoice;
 }
 /**
  * Register a saved model event with the dispatcher.
  *
  * @param EloquentOrderRepository $order
  *
  * @return void
  */
 public function created(EloquentOrderRepository $order)
 {
     $order->setAttribute('commission', $this->commissionCalculator->calculateCommission($order));
     $order->setAttribute('profit', $this->commissionCalculator->calculateProfit($order));
     $order->save();
 }