Example #1
0
 /**
  * @param Basket $basket
  *
  * @throws \Exception
  *
  * @return \ChingShop\Modules\Sales\Domain\Order\Order
  */
 private function basketToOrder(Basket $basket) : Order
 {
     $order = Order::create();
     $basket->order()->associate($order);
     foreach ($basket->basketItems as $basketItem) {
         if ($basketItem instanceof BasketItemPresenter) {
             $basketItem = $basketItem->getWrappedObject();
         }
         $this->basketItemToOrderItem($basketItem, $order);
     }
     $order->address()->associate($basket->address);
     $order->save();
     $basket->delete();
     return $order;
 }
Example #2
0
 /**
  * @param ProductOption $productOption
  *
  * @throws \ChingShop\Modules\Sales\Domain\Payment\StockAllocationException
  */
 private function checkStock(ProductOption $productOption)
 {
     $inBasketCount = $this->basket->itemsForOption($productOption)->count();
     $availableCount = $productOption->availableStock()->count();
     if ($inBasketCount + 1 > $availableCount) {
         throw new StockAllocationException(sprintf('Only %d %s are available, and there %s.', $availableCount, $productOption->name(), "are already {$inBasketCount} in the basket"));
     }
 }
 /**
  * @return Basket|Model
  */
 private function basket() : Basket
 {
     $basket = Basket::where('user_id', '=', $this->customerUser()->id)->first();
     if ($basket) {
         return $basket->load('basketItems.productOption');
     }
     $basket = new Basket();
     $this->customerUser()->basket()->save($basket);
     return $basket;
 }
 /**
  * Seed an order.
  */
 private function seedOrder()
 {
     $basket = Basket::create();
     $address = Address::create(['name' => $this->faker()->name, 'line_one' => $this->faker()->streetAddress, 'line_two' => $this->faker()->streetName, 'city' => $this->faker()->city, 'post_code' => $this->faker()->postcode, 'country_code' => $this->faker()->countryCode]);
     $basket->address()->associate($address);
     $basket->save();
     $basketItems = [];
     $this->repeat(function () use($basket, &$basketItems) {
         $basketItems[] = BasketItem::create(['basket_id' => $basket->id, 'product_option_id' => ProductOption::inRandomOrder()->first()->id]);
     }, random_int(1, 4));
     $order = Order::create();
     $order->address()->associate($address);
     $order->save();
     /** @var BasketItem $basketItem */
     foreach ($basketItems as $basketItem) {
         $stockItem = new StockItem();
         $basketItem->productOption->stockItems()->save($stockItem);
         $orderItem = OrderItem::create(['basket_item_id' => $basketItem->id, 'order_id' => $order->id, 'price' => $basketItem->priceAsFloat()]);
         $orderItem->stockItem()->save($stockItem);
     }
     Payment::create(['order_id' => $order->id, 'settlement_id' => PayPalSettlement::create(['payment_id' => $this->faker()->uuid, 'payer_id' => $this->faker()->uuid])->id, 'settlement_type' => PayPalSettlement::class]);
 }
Example #5
0
 /**
  * @throws \InvalidArgumentException
  *
  * @return Amount
  */
 private function amount() : Amount
 {
     return (new Amount())->setCurrency(self::DEFAULT_CURRENCY)->setTotal($this->basket->totalPrice()->asFloat())->setDetails($this->details());
 }
Example #6
0
 /**
  * @throws \InvalidArgumentException
  *
  * @return string
  */
 public function totalPrice() : string
 {
     return $this->wrappedObject->totalPrice()->formatted();
 }
 /**
  * @param Basket $basket
  *
  * @return PayPalCheckout
  */
 public function makeCheckout(Basket $basket) : PayPalCheckout
 {
     $this->log->info("Starting PayPal checkout for basket {$basket->id}");
     $basket->load(['basketItems.productOption.product.prices', 'address']);
     return new PayPalCheckout($basket, $this->apiContext);
 }