Shopping Cart and Payments Architecture - wordjelly/Auth GitHub Wiki
Shopping Cart and Payments
The following concerns and classes are provided by the engine:
Model Concerns:
-
Auth::Shopping::CartConcern
-
Auth::Shopping::CartItemConcern
-
Auth::Shopping::PaymentConcern
-
Auth::Shopping::PayUMoneyConcern
-
Auth::Shopping::ProductConcern
Controller Concerns::
-
Auth::Shopping::CartControllerConcern
-
Auth::Shopping::CartItemControllerConcern
-
Auth::Shopping::PaymentControllerConcern
How It Works::
Cart Item -> Anything that can be bought.
Cart -> Many Cart Item's grouped together.
Payment -> A given Cart can accept any number of payment objects.
For eg. if you decided to purchase 5 things from the e-shop You would combine them all into an order. Then you would pay for it.
-
When you want to add an item to your 'cart', you create a new CartItem object.
-
The user should be presented an option to 'finalize' his orders. This option basically calls #create on the CartControllerConcern. This creates a new 'Cart' with the cart items that are passed into the request.
-
Cart items can be further added or removed from a cart by calling #update on the CartControllerConcern.
-
You can see all cart_items that have not yet been added to any Cart by calling #index on the CartItemControllerConcern.
-
Once a cart item is added to a Cart, it is no longer listed in #index on the CartItemControllerConcern.
-
You can make a payment to the cart by calling #create on the payments controller.
-
Payment validates for the presence of cart_id, payment_type, amount, resource_class and resource_id. Of these only cart_id, payment_type and amount are permitted to be sent in by the controller. The other two are assigned internally in the controller create action.
-
Order of callbacks in the payment concern, all these callbacks are triggered in this order, because
- before_save : First calls a function called "set_cart".
In this callback an attr_accessor called "cart" is set on the payment. This is the cart which the payment is being made to. After setting the cart, it also calls "prepare_cart", on the cart instance.
Prepare_cart : This function is defined in the CartConcern. It does the following:
- find_cart_items : Refers to array of all items in the cart. Sets them as an array attr_accessor on the cart called "cart_items".
- set_cart_price : Refers to the total price of all items in the cart. Adds the prices of all the cart_items set above and totals them, and sets them on an attr_accessor called "cart_price".
- set_cart_payments: Refers to array of all payments made to this cart. Takes all the payments that have this cart_id on them, and sets them as an array attr_accessor called "cart_payments". This will include the current payment if this is an update action, and will not include the current payment if this is a create action.
- set_cart_paid_amount: Refers to the total of all payments successfully made to this cart. This is calculated by taking all the payments made to the cart(set above) and then adding their amounts. It simultaneously also sets the cart_credit to the same amount. Basically the credit is initially all the money paid into the cart.
- set_cart_pending_balance: Refers to how much money the customer owes the cart. It basically subtracts from the cart_price the cart_paid_amount.
So at the end of prepare_cart we have set all the above as accessors on the cart. The cart_credit and cart_total_paid amount are the same and equal to the sum of all successfull payments on the cart.
After this calls a function called payment_callback. This basically calls the right callback i.e cash, card, cheque or gateway based on the payment_type. It executes gateway only if the document is not new, i.e this is an update. It will call the other three, only if the document is_new, i.e this is a create. The payment callback yields a block -> this simply checks if the payment_status has changed and then calls update_cart_items_accepted described below.
- before_save : First calls a function called "set_cart".
In this callback an attr_accessor called "cart" is set on the payment. This is the cart which the payment is being made to. After setting the cart, it also calls "prepare_cart", on the cart instance.
Prepare_cart : This function is defined in the CartConcern. It does the following: