Download the PHP package enea/laravel-cashier without Composer
On this page you can find all versions of the php package enea/laravel-cashier. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package laravel-cashier
Deprecated - Laravel Cashier Package
This repository is marked as deprecated and has been replaced by a better and more comprehensive version.
Reason: We have released Swift Cart, which offers enhanced functionalities, a more user-friendly experience, and improved performance. As a result, this repository will no longer receive updates.
Deprecation Date: August 4st, 2023.
This package provides a functionality to manage the sale of products and abstracts all the calculations you need for a sale.
Installation
Laravel Cashier requires PHP 7.4. This version supports Laravel 7
To get the latest version, simply require the project using Composer:
And publish the configuration file.
Class reference
This table defines the implementation of the models necessary for the operation of the package, there are some models that come to help such as: $discount
and $document
, however it is recommended to replace these models with your own.
Concrete | Abstract | Description |
---|---|---|
$client |
Enea\Cashier\Contracts\BuyerContract |
person who makes the purchase |
$document |
Enea\Cashier\Contracts\DocumentContract |
type of sale document |
$product |
Enea\Cashier\Contracts\ProductContract |
product being sold |
$quote |
Enea\Cashier\Contracts\QuoteContract |
quote available for sale |
$quotedProduct |
Enea\Cashier\Contracts\QuotedProductContract |
quoted product to sell |
$discount |
Enea\Cashier\Modifiers\DiscountContract |
representation of a discount |
$tax |
Enea\Cashier\Modifiers\TaxContract |
representation of a tax |
Usage
To start a purchase you must use the ShoppingManager::initialize($client, $document)
.
When you initialize a shopping cart, a token is generated so that it can be searched from ShoppingManager::find($token)
. This function gets the shopping cart from session.
It is also possible that you want to invoice a quote, and to do so you must call the attach
function of the $shoppingCart
. Doing this creates a QuoteManager
instance inside the Shopping Cart, which can be accessed from the $shoppingCart->getQuoteManager()
function.
Now you just have to add products to the shopping cart using $shoppingCart->push($product, $quantity)
.
Or you can also pull products from the quote using $shoppingCart->pull($productID)
.
The push
and pull
methods returns an instance of ProductCartItem
, which provides a lot of useful method.
Example
For this example, we are going to simulate a simple purchase. We need a $client
and we will use a $invoice
with IVA
as a sales document.
Now we add a controller to manage shopping cart products.
And to finish we save the document in the database.
Cashier
It is responsible for centralizing the calculations to get taxes, discounts and totals for each product. you can find it in Enea\Cashier\Calculations\Cashier.
Method | Description | Return |
---|---|---|
getUnitPrice() |
unit sales price | float |
getGrossUnitPrice() |
gross price (price without tax) | float |
getNetUnitPrice() |
net price (price + taxes) | float |
getSubtotal() |
subtotal | float |
getTotalDiscounts() |
total discounts | float |
getTotalTaxes() |
total taxes | float |
getTotal() |
final total with discounts and taxes | float |
getTaxes() |
all taxes grouped by name | Taxed[] |
getTax(string $name) |
tax by name | Taxed |
getDiscounts() |
all discounts grouped by code | Discounted[] |
getDiscount(string $code) |
discount by code | Discounted |
Pricing
Cashier separates the prices into 3, getGrossUnitPrice()
, getNetUnitPrice()
and getUnitPrice()
, where the latter is the unit price after evaluating taxes, both included and excluded. $cashier->getUnitPrice()
is the function used for all calculations. You can see an example in code from Enea\Tests\Calculations\PriceTest
Method | getGrossUnitPrice() | getNetUnitPrice() | getUnitPrice() |
---|---|---|---|
Base | 100.00 $USD | 100.00 $USD | 100.00 $USD |
Included Taxes | IVA(12%), AnotherTax(11%) | IVA(12%), AnotherTax(11%) | IVA(12%), AnotherTax(11%) |
Tax to use | IVA(12%) | IVA(12%) | IVA(12%) |
Applied | - | IVA and AnotherTax | IVA |
Total | 81.30 $USD | 100 $USD | 90.24 $USD |
Configuration
There are a few things you need to know to set up taxes and discounts correctly.
-
Enea\Cashier\Modifiers\DiscountContract
Represents an applicable discount. There is quite a functional helper implementation in
Enea\Cashier\Modifiers\Discount
so it is not totally necessary to assign your own model, unless you want full control over the discount codes. -
Enea\Cashier\Contracts\DocumentContract
Represents the type of document with which the sale will be made and also defines the taxes that will be applied to the products.
-
Enea\Cashier\Modifiers\TaxContract
Represents the tax on the
product
, the package has a help implementation which can be found inEnea\Cashier\Modifiers\Tax
To use taxes it is necessary to understand that they can be configured in 2 ways, included and excluded
Type INCLUDED EXCLUDED Unit Price 100.00 $USD 100.00 $USD Tax % 10% 10% Total Tax 9.09 $USD 10.00 $USD Net Price 100.00 $USD 110.00 $USD
More documentation
You can find a lot of comments within the source code as well as the tests located in the tests
directory.