Called by shop to charge order's amount.

@param Cart $cart Cart.

@return bool
760387
public function onCharge($order)
    {
        $this->statusCode = 'pending';

        // Begin paypal
        try {

            if ($order->total <= 0) {
            
                $this->statusCode = 'completed';

                $this->detail = 'Order total is 0; no PayPal transaction required.';

                $this->transactionId = uniqid();

                return true;
            }

            $this->setContext();

            $payer = new Payer();
        
            $payer->setPaymentMethod('paypal');

            $list = new ItemList();
            
            $list->setItems($this->toPayPalItems($order));
            
            $details = new Details();
            
            $details->setShipping($order->totalShipping)
                ->setTax($order->totalTax)
                ->setSubtotal($order->totalPrice);

            $amount = new Amount();

            $amount->setCurrency(Config::get('shop.currency'))
                ->setTotal($order->total)
                ->setDetails($details);

            $transaction = new Transaction();

            $transaction->setAmount($amount)
                ->setItemList($list)
                ->setDescription(sprintf(
                    '%s payment, Order #%d',
                    Config::get('shop.name'),
                    $order->id
                ))
                ->setInvoiceNumber($order->id);

            $redirectUrls = new RedirectUrls();
            
            $redirectUrls->setReturnUrl($this->callbackSuccess)
                ->setCancelUrl($this->callbackFail);

            $payment = new Payment();

            $payment->setIntent('sale')
                ->setPayer($payer)
                ->setRedirectUrls($redirectUrls)
                ->setTransactions([$transaction]);

            //$request = clone $payment;

            $payment->create($this->apiContext);

            $this->approvalUrl = $payment->getApprovalLink();

            $this->detail = sprintf('Pending approval: %s', $this->approvalUrl);

            return true;

        } catch (PayPalConnectionException $e) {

            $response = json_decode($e->getData());

            throw new GatewayException(
                sprintf(
                    '%s: %s',
                    $response->name,
                    isset($response->message) ? $response->message : 'Paypal payment Failed.'
                ),
                1001,
                $e
            );

        } catch (\Exception $e) {

            throw new GatewayException(
                $e->getMessage(),
                1000,
                $e
            );

        }

        return false;
    }
165673
protected static function boot() {
        parent::boot();

        //when an item is created
        static::created(function(CartLine $line){
            $cart = $line->getCartInstance() ?: $line->cart;
            $cart->total_price = $cart->total_price + $line->getPrice();
            $cart->item_count = $cart->item_count + $line->quantity;
            $cart->relations = [];
            $cart->save();
        });

        //when an item is updated
        static::updated(function(CartLine $line){
            $cart = $line->getCartInstance() ?: $line->cart;
            $cart->total_price = $cart->total_price - $line->getOriginalPrice() + $line->getPrice();
            $cart->item_count = $cart->item_count - $line->getOriginalQuantity() + $line->quantity;
            $cart->relations = [];
            $cart->save();
        });

        static::deleted(function(CartLine $line){
            $cart = $line->getCartInstance() ?: $line->cart;
            $cart->total_price = $cart->total_price - $line->getPrice();
            $cart->item_count = $cart->item_count - $line->quantity;
            $cart->relations = [];
            $cart->save();
        });
    }
1080761
public function loadCartPurchasablesAmount(CartInterface $cart)
    {
        $currency = $this
            ->currencyWrapper
            ->get();

        $purchasableAmount = Money::create(0, $currency);

        /**
         * Calculate Amount and PurchasableAmount.
         */
        foreach ($cart->getCartLines() as $cartLine) {

            /**
             * @var CartLineInterface $cartLine
             */
            $cartLine = $this->loadCartLinePrices($cartLine);

            /**
             * @var MoneyInterface $purchasableAmount
             * @var MoneyInterface $totalAmount
             */
            $convertedPurchasableAmount = $this
                ->currencyConverter
                ->convertMoney(
                    $cartLine->getPurchasableAmount(),
                    $currency
                );

            $purchasableAmount = $purchasableAmount
                ->add($convertedPurchasableAmount->multiply(
                    $cartLine->getQuantity()
                ));
        }

        $cart->setPurchasableAmount($purchasableAmount);
    }
97069
public function charge($amount = null)
    {
        $builder = new HpsCreditServiceChargeBuilder($this);
        return $builder
            ->withAmount($amount)
            ->withCurrency('usd');
    }
1219860
private function checkProductsAvailability(OrderInterface $cart)
    {
        // no check needed
        if ($cart->getItems()->isEmpty()) {
            return true;
        }

        $containsInvalidProducts = false;
        /** @var \Sulu\Bundle\Sales\CoreBundle\Entity\ItemInterface $item */
        foreach ($cart->getItems() as $item) {
            if (!$item->getProduct() ||
                !$item->getProduct()->isValidShopProduct()
            ) {
                $containsInvalidProducts = true;
                $cart->removeItem($item);
                $this->em->remove($item);
            }
        }
        // persist new cart
        if ($containsInvalidProducts) {
            $this->em->flush();
        }

        return !$containsInvalidProducts;
    }
460337
public function removeCoupon(
        CartInterface $cart,
        CouponInterface $coupon
    ) {
        $cartCoupons = $this
            ->cartCouponDirector
            ->findBy([
                'cart' => $cart,
                'coupon' => $coupon,
            ]);

        if (empty($cartCoupons)) {
            return false;
        }

        foreach ($cartCoupons as $cartCoupon) {
            $this
                ->cartCouponEventDispatcher
                ->dispatchCartCouponOnRemoveEvent(
                    $cartCoupon
                );
        }

        return true;
    }
506478
public function loadCartTotalAmount(CartInterface $cart)
    {
        $currency = $this
            ->currencyWrapper
            ->get();

        $finalAmount = clone $cart->getPurchasableAmount();

        /**
         * Calculates the shipping amount.
         */
        $shippingAmount = $cart->getShippingAmount();
        if ($shippingAmount instanceof MoneyInterface) {
            $convertedShippingAmount = $this
                ->currencyConverter
                ->convertMoney(
                    $shippingAmount,
                    $currency
                );
            $finalAmount = $finalAmount->add($convertedShippingAmount);
        }

        /**
         * Calculates the coupon amount.
         */
        $couponAmount = $cart->getCouponAmount();
        if ($couponAmount instanceof MoneyInterface) {
            $convertedCouponAmount = $this
                ->currencyConverter
                ->convertMoney(
                    $couponAmount,
                    $currency
                );
            $finalAmount = $finalAmount->subtract($convertedCouponAmount);
        }

        $cart->setAmount($finalAmount);
    }
602734
public function doesCartWithIdExist(string $cartId): bool
    {
        $qb = $this->connection->createQueryBuilder();
        $qb->select('COUNT(*)')
            ->from('dumplie_customer_cart')
            ->where('id = :id')
            ->setParameter('id', $cartId)
            ->getSQL();

        return (bool)$this->connection->fetchColumn($qb->getSQL(), $qb->getParameters());
    }
841309
private function isValidPayment($basket)
    {
        $paymentId = $basket->getPaymentId();
        $paymentModel = oxNew(EshopPayment::class);
        $paymentModel->load($paymentId);

        $dynamicValues = $this->getDynamicValues();
        $shopId = \OxidEsales\Eshop\Core\Registry::getConfig()->getShopId();

        return $paymentModel->isValidPayment(
            $dynamicValues,
            $shopId,
            $this->getUser(),
            $basket->getPriceForPayment(),
            $basket->getShippingId()
        );
    }
101056
public function updateTransaction()
    {        
        $cart = $this->cartProvider->getCart();

        if (0 === $cart->countItems() || $this->isTransactionUpdated($cart)) {
            return;
        }

        /** @var TransactionRepository $transactionRepository */
        $transactionRepository = $this->manager->getRepository('EcommerceBundle:Transaction');

        if ($this->session->has('transaction-id')) {
            /** @var Transaction $transaction */
            $transaction = $transactionRepository->find($this->session->get('transaction-id'));

            $transactionRepository->removeItems($transaction);
        } else {
            $transactionKey = $transactionRepository->getNextNumber();

            // create a new transaction
            $transaction = new Transaction();
            $transaction->setTransactionKey($transactionKey);
            $transaction->setStatus(Transaction::STATUS_CREATED);
            $transaction->setActor($this->securityContext->getToken()->getUser());
            $cartItem = $cart->getItems()->first();
            $product = $cartItem->getProduct();
        }

        $orderTotalPrice = 0;

        foreach ($cart->getItems() as $cartItem) {
            /** @var Product $product */
            $product = $cartItem->getProduct();

            $productPurchase = new ProductPurchase();
            $productPurchase->setProduct($product);
            $productPurchase->setBasePrice($cartItem->getUnitPrice());
            $productPurchase->setQuantity($cartItem->getQuantity());
            $productPurchase->setDiscount($product->getDiscount());
            $productPurchase->setTotalPrice($cartItem->getTotal());
            $productPurchase->setTransaction($transaction);
            $productPurchase->setReturned(false);
            //free transport
            if($cartItem->isFreeTransport()){
                $productPurchase->setDeliveryExpenses(0);
            }else{ 
                $productPurchase->setDeliveryExpenses($cartItem->getShippingCost());
            }
            $orderTotalPrice += $cartItem->getProduct()->getPrice() * $cartItem->getQuantity();

            $this->manager->persist($productPurchase);
        }

        $transaction->setTotalPrice($orderTotalPrice);

        $this->manager->persist($transaction);
        $this->manager->flush();

        $this->session->set('transaction-id', $transaction->getId());
        $this->session->save();
        
    }
944889
public function findCartCouponsByCart(CartInterface $cart)
    {
        return $this
            ->createQueryBuilder('cc')
            ->where('cc.cart = :cart')
            ->setParameter('cart', $cart)
            ->getQuery()
            ->getResult();
    }
862312
public function beforeSave($insert)
    {
        if (parent::beforeSave($insert)) {
            if ($this->isNewRecord) {
                $this->key = get_current_cart_key();
                $check = Cart::find()
                    ->where(['product' => $this->product])
                    ->andWhere(['key' => $this->key])
                    ->one();
                if (!empty($check)) {
                    $this->quantity += $check->quantity;
                    Cart::deleteAll([
                        'key' => $this->key,
                        'product' => $this->product
                    ]);
                }
            }
            return true;
        }

        return false;
    }
1073538
public function charge($amount = null)
    {
        return (new AuthorizationBuilder(TransactionType::SALE, $this))
                        ->withAmount($amount)
                        ->withOneTimePayment(true);
    }
205131
public function getWeight(array $order, array &$cart)
    {
        $result = null;
        $this->hook->attach('order.weight.get.before', $order, $cart, $result, $this);

        if (isset($result)) {
            return (float) $result;
        }

        $total = 0.0;
        foreach ($cart['items'] as &$item) {
            $this->sumTotalWeight($total, $item, $order);
        }

        $result = round($total, 2);
        $this->hook->attach('order.weight.get.after', $order, $cart, $result, $this);
        return (float) $result;
    }
1013564
public function hasAmountPrice()
    {
        if (self::$_blHasAmountPrice === null) {
            self::$_blHasAmountPrice = false;

            $oDb = \OxidEsales\Eshop\Core\DatabaseProvider::getDb();
            $sQ = "SELECT 1 FROM `oxprice2article` LIMIT 1";

            if ($oDb->getOne($sQ)) {
                self::$_blHasAmountPrice = true;
            }
        }

        return self::$_blHasAmountPrice;
    }
402358
public function set(CartInterface $cart)
    {
        if (!$this->saveInSession) {
            return $this;
        }

        $this
            ->session
            ->set(
                $this->sessionFieldName,
                $cart->getId()
            );

        return $this;
    }
589787
public function getSubTotal()
    {
        $content = $this->getContent();

        $subTotal = $content->reduce(function (Money $subTotal, CartItem $cartItem) {
            return $subTotal->add($cartItem->getSubtotal());
        }, new Money(0, Config::get('shoppingcart.currency')));

        return $subTotal;
    }