/** * 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(); } }
function __construct(array $salesIds = array()) { foreach ($salesIds as $sale) { $this->push(new SaleRepository(EloquentOrderRepository::find($sale->id))); } $this->calculateTotals(); }
/** * 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(); } }