# ----------------------------------------------------------------------- # Copyright © 2012 ShepHertz Technologies Pvt Ltd. All rights reserved. # ----------------------------------------------------------------------- require 'rubygems' require 'connection/RESTConnection' require 'util/util' require 'json/pure' require 'App42_Ruby_API/App42Response' require 'shopping/CartResponseBuilder' require 'shopping/Cart' require 'shopping/PaymentStatus' require 'shopping/ItemData' module App42 module Shopping # # This is Cloud Persistent Shopping Cart Service. App Developers can use this to # create a Shopping Cart. Add Items and Check Out items. It also maintains the transactions # and the corresponding Payment Status. # # The Payment Gateway interface is not provided by the Platform. It is left to the App developer # how he wants to do the Payment Integration. This can be used along with Catalogue or used independently # # @see Catalgoue # @see Cart # @see App42Response # @see ItemData # @see PaymentStatus # class CartService # # this is a constructor that takes # # @param apiKey # @param secretKey # @param baseURL # def initialize(api_key, secret_key, base_url) puts "Shopping ->initialize" @api_key = api_key @secret_key = secret_key @base_url = base_url @resource = "cart" @version = "1.0" end # # Creates a Cart Session for the specified User # # @param user # - User for whom Cart Session has to be created # # @return Cart Object containing Cart Id with Creation Time. The id has to be used in subsequent calls for # adding and checking out # # @raise App42Exception # def create_cart(user) puts "Create Cart Called " puts "Base url #{@base_url}" response = nil; cartObj = nil; cartObj = Cart.new util = Util.new util.throwExceptionIfNullOrBlank(user, "User"); begin connection = App42::Connection::RESTConnection.new(@base_url) body = {'app42' => {"cart"=> { "userName" => user, }}}.to_json puts "Body #{body}" query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } query_params = params.clone params.store("body", body) puts params puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}" response = connection.post(signature, resource_url, query_params, body) cart = CartResponseBuilder.new cartObj = cart.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return cartObj end # # Fetch Cart details. Can be used by the App developer to display Cart Details i.e. Items in a Cart. # # @param cartId # - The Cart Id that has to be fetched # # @return Cart object containing cart details with all the items which are in it. It also tells the state of # the Cart # # @raise App42Exception # def get_cart_details(cartId) puts "Get Cart Details Called " puts "Base url #{@base_url}" response = nil; cartObj = nil; cartObj = Cart.new util = Util.new util.throwExceptionIfNullOrBlank(cartId, "CartId"); begin connection = App42::Connection::RESTConnection.new(@base_url) query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } query_params = params.clone puts params params.store("cartId", cartId) puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/#{cartId}/details" response = connection.get(signature, resource_url, query_params) puts "Response is #{response}" cart = CartResponseBuilder.new cartObj = cart.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return cartObj end # # Adds an Item in the Cart with quantity and price. This method does not take currency. Its the # bonus of the App developer to maintain the currency. It takes only the price. # # @param cartID # - The Cart Id into which item has to be added # @param itemID # - The Item id which has to be added in the cart. If the Catalogue Service is used along # with the Cart Service then the Item ids should be same. # @param itemQuantity # - Quantity of the Item to be purchased # @param price # - Price of the item # # @return Cart object containing added item. # # @raise App42Exception # def add_item(cartID, itemID, itemQuantity, price) puts "Add Item Called " puts "Base url #{@base_url}" response = nil; cartObj = nil; cartObj = Cart.new util = Util.new util.throwExceptionIfNullOrBlank(cartID, "CartID"); util.throwExceptionIfNullOrBlank(itemID, "ItemID"); util.throwExceptionIfNullOrBlank(itemQuantity, "ItemQuantity"); util.throwExceptionIfNullOrBlank(price, "Price"); begin connection = App42::Connection::RESTConnection.new(@base_url) body = {'app42'=>{ "cart"=>{ "cartId"=>cartID, "item"=>{ "quantity"=>itemQuantity, "amount"=>price } } }}.to_json puts "Body #{body}" query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } query_params = params.clone puts params params.store("body", body) params.store("itemId", itemID) puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/item/#{itemID}" response = connection.post(signature, resource_url, query_params, body) cart = CartResponseBuilder.new cartObj = cart.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return cartObj end # # Fetches the Items from the specified Cart # # @param cartId # - The cart id from which items have to be fetched # # @return Cart object which contains all items in the cart # # @raise App42Exception # def get_items(cartId) puts "Get Items Called " puts "Base url #{@base_url}" response = nil; cartObj = nil; cartObj = Cart.new util = Util.new util.throwExceptionIfNullOrBlank(cartId, "CartId"); begin connection = App42::Connection::RESTConnection.new(@base_url) query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } query_params = params.clone params.store("cartId", cartId) puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/#{cartId}" response = connection.get(signature, resource_url, query_params) cart = CartResponseBuilder.new cartObj = cart.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return cartObj end # # Fetches the specified Item from the specified Cart # # @param cartId # - The cart id from which item has to be fetched # @param itemId # - The item for which the information has to be fetched # # @return Cart Object # # @raise App42Exception # def get_item(cartId, itemId) puts "Get Item Called " puts "Base url #{@base_url}" response = nil; cartObj = nil; cartObj = Cart.new util = Util.new util.throwExceptionIfNullOrBlank(cartId, "CartId"); util.throwExceptionIfNullOrBlank(itemId, "ItemId"); begin connection = App42::Connection::RESTConnection.new(@base_url) query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } query_params = params.clone params.store("cartId", cartId) params.store("itemId", itemId) puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/#{cartId}/#{itemId}" response = connection.get(signature, resource_url, query_params) cart = CartResponseBuilder.new cartObj = cart.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return cartObj end # # Removes the specified item from the specified Cart # # @param cartId # - The cart id from which the item has to be removed # @param itemId # - Id of the Item which has to be removed # # @return App42Response if removed successfully # # @raise App42Exception # def remove_item(cartId, itemId) puts "Remove Item Called " puts "Base url #{@base_url}" response = nil; responseObj = App42Response.new(); util = Util.new util.throwExceptionIfNullOrBlank(cartId, "CartId"); util.throwExceptionIfNullOrBlank(itemId, "ItemId"); begin connection = App42::Connection::RESTConnection.new(@base_url) query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } query_params = params.clone params.store("cartId", cartId) params.store("itemId", itemId) puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/#{cartId}/#{itemId}" response = connection.delete(signature, resource_url, query_params) responseObj.strResponse=(response) responseObj.isResponseSuccess=(true) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return responseObj end # # Removes all Items from the specified Cart # # @param cartId # - The cart id from which items have to be removed # # @return App42Response if removed successfully # # @raise App42Exception # def remove_all_items(cartId) puts "Remove All Item Called " puts "Base url #{@base_url}" response = nil; responseObj = App42Response.new(); util = Util.new util.throwExceptionIfNullOrBlank(cartId, "CartId"); begin connection = App42::Connection::RESTConnection.new(@base_url) query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } query_params = params.clone params.store("cartId", cartId) puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/#{cartId}" response = connection.delete(signature, resource_url, query_params) responseObj.strResponse=(response) responseObj.isResponseSuccess=(true) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return responseObj end # # Checks whether the Cart is Empty or not # # @param cartId # - The cart id to check for empty # # @return Cart object (isEmpty method on Cart object can be used to check status) # # @raise App42Exception # def is_empty(cartId) puts "Is Empty Called " puts "Base url #{@base_url}" response = nil; cartObj = nil; cartObj = Cart.new util = Util.new cartStatus = false; util.throwExceptionIfNullOrBlank(cartId, "cartId"); begin connection = App42::Connection::RESTConnection.new(@base_url) query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } query_params = params.clone params.store("cartId", cartId) puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/#{cartId}/isEmpty" response = connection.get(signature, resource_url, query_params) puts "Response is #{response}" cart = CartResponseBuilder.new cartObj = cart.buildResponse(response) cartStatus = cartObj.isEmpty() rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return cartStatus end # # Checks out the Cart and put it in CheckOut Stage and returns the Transaction Id # The transaction id has to be used in future to update the Payment Status. # # @param cartID # - The cart id that has to be checkedout # # @return Cart object containing Checked Out Cart Information with the Transaction Id # # @raise App42Exception # def check_out(cartId) puts "Check Out Called " puts "Base url #{@base_url}" response = nil; cartObj = nil; cartObj = Cart.new util = Util.new util.throwExceptionIfNullOrBlank(cartId, "CartId"); begin connection = App42::Connection::RESTConnection.new(@base_url) body = {'app42' => {"cart"=> { "cartId" => cartId, }}}.to_json puts "Body #{body}" query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } query_params = params.clone params.store("body", body) signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/checkOut" response = connection.put(signature, resource_url, query_params, body) cart = CartResponseBuilder.new cartObj = cart.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return cartObj end # # Update Payment Status of the Cart. When a Cart is checkout, it is in Checkout state. The payment # status has to be updated based on the Payment Gateway interaction # # @param cartID # - The cart id for which the payment status has to be updated # @param transactionID # - Transaction id for which the payment status has to be updated # @param paymentStatus # - Payment Status to be updated. The probable values are PaymentStatus.DECLINED, PaymentStatus.AUTHORIZED, PaymentStatus.PENDING # # @return Cart object which contains Payment Status # # @raise App42Exception # def payment(cartId, transactionID, paymentStatus) puts "Is Empty Called " puts "Base url #{@base_url}" response = nil; cartObj = nil; cartObj = Cart.new util = Util.new util.throwExceptionIfNullOrBlank(cartId, "CartId"); util.throwExceptionIfNullOrBlank(transactionID, "TransactionID"); util.throwExceptionIfNullOrBlank(paymentStatus, "paymentStatus"); begin if (PaymentStatus.new.isAvailable(paymentStatus) == nil) raise App42NotFoundException.new("Payment Status #{paymentStatus} does not Exist "); end connection = App42::Connection::RESTConnection.new(@base_url) body = {'app42' => {"cart"=> { "cartId" => cartId, "transactionId" => transactionID, "status" => paymentStatus, }}}.to_json puts "Body #{body}" query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } query_params = params.clone params.store("body", body) puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/payment" response = connection.put(signature, resource_url, query_params, body) cart = CartResponseBuilder.new cartObj = cart.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return cartObj end # # Fetches Payment information for a User. This can be used to display Order and Payment History # # @param userId # - User Id for whom payment information has to be fetched # # @return ArrayList containing Cart objects. Payment history can be retrieved from individual Cart object. # # @raise App42Exception # def get_payments_by_user(userId) puts "Is Empty Called " puts "Base url #{@base_url}" response = nil; cartList = nil; cartList = Array.new util = Util.new util.throwExceptionIfNullOrBlank(userId, "UserId"); begin connection = App42::Connection::RESTConnection.new(@base_url) query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, 'userId' => userId } query_params = params.clone params.store("userId", userId) puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/payments/user/#{userId}" response = connection.get(signature, resource_url, query_params) cart = CartResponseBuilder.new cartList = cart.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return cartList end # # Fetches Payment information for the specified Cart Id # # @param cartID # - Cart Id for which the payment information has to be fetched # # @return Cart object which contains Payment History for the specified Cart # # @raise App42Exception # def get_payment_by_cart(cartId) puts "Get Payments By Cart Called " puts "Base url #{@base_url}" response = nil; cartObj = nil; cartObj = Cart.new util = Util.new util.throwExceptionIfNullOrBlank(cartId, "CartId"); begin connection = App42::Connection::RESTConnection.new(@base_url) query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } query_params = params.clone params.store("cartId", cartId) puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/payments/cart/#{cartId}" response = connection.get(signature, resource_url, query_params) cart = CartResponseBuilder.new cartObj = cart.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return cartObj end # # Fetches Payment information based on User Id and Status # # @param userId # - User Id for whom payment information has to be fetched # # @return ArrayList containing Cart objects. Payment history can be retrieved from individual Cart object. # @return Payment History # # @raise App42Exception # def get_payments_by_user_and_status(userId, paymentStatus) puts "Get Payments By User and Status Called " puts "Base url #{@base_url}" response = nil; cartList = nil; cartList = Array.new util = Util.new util.throwExceptionIfNullOrBlank(userId, "UserId"); util.throwExceptionIfNullOrBlank(paymentStatus, "paymentStatus"); begin if (PaymentStatus.new.isAvailable(paymentStatus) == nil) raise App42NotFoundException.new("Payment Status #{paymentStatus} does not Exist "); end connection = App42::Connection::RESTConnection.new(@base_url) query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } query_params = params.clone params.store("userId", userId) params.store("status", paymentStatus) puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/payments/user/#{userId}/#{paymentStatus}" response = connection.get(signature, resource_url, query_params) cart = CartResponseBuilder.new cartList = cart.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return cartList end # # Fetches Payment information based on Status # # @param paymentStatus # - Status of type which payment information has to be fetched # # @return ArrayList containing Cart objects. Payment history can be retrieved from individual Cart object. # # @raise App42Exception # def get_payments_by_status(paymentStatus) puts "Get Payments By Status Called " puts "Base url #{@base_url}" response = nil; cartList = nil; cartList = Array.new util = Util.new util.throwExceptionIfNullOrBlank(paymentStatus, "paymentStatus"); begin if (PaymentStatus.new.isAvailable(paymentStatus) == nil) raise App42NotFoundException.new("Payment Status #{paymentStatus} does not Exist "); end connection = App42::Connection::RESTConnection.new(@base_url) query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } query_params = params.clone params.store("status", paymentStatus) puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/payments/status/#{paymentStatus}" response = connection.get(signature, resource_url, query_params) cart = CartResponseBuilder.new cartList = cart.buildArrayResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return cartList end # # History of Carts and Payments for a User. It gives all the carts which are in AUTHORIZED, DECLINED, PENDING state. # # @param userId # - User Id for whom payment history has to be fetched # # @return ArrayList containing Cart objects. Payment history can be retrieved from individual Cart object. # # @raise App42Exception # def get_payment_history_by_user(userId) puts "Get Payment History By User Called " puts "Base url #{@base_url}" response = nil; cartList = nil; cartList = Array.new util = Util.new util.throwExceptionIfNullOrBlank(userId, "UserId"); begin connection = App42::Connection::RESTConnection.new(@base_url) query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } query_params = params.clone params.store("userId", userId) puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/payment/history/#{userId}" response = connection.get(signature, resource_url, query_params) cart = CartResponseBuilder.new cartList = cart.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return cartList end # # History of all carts. It gives all the carts which are in AUTHORIZED, DECLINED, PENDING state. # # @return ArrayList containing Cart objects. Payment history can be retrieved from individual Cart object. # # @raise App42Exception # def get_payment_history_all() puts "Get Payment History By User Called " puts "Base url #{@base_url}" response = nil; cartList = nil; cartList = Array.new util = Util.new begin connection = App42::Connection::RESTConnection.new(@base_url) query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } query_params = params.clone puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/payment/history" response = connection.get(signature, resource_url, query_params) cart = CartResponseBuilder.new cartList = cart.buildArrayResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return cartList end # # To increase quantity of existing item in the cart. # # @return Cart object containing updated item. # # @raise App42Exception # def increase_quantity(cartID, itemID, itemQuantity) puts "Increase Quantity Called " puts "Base url #{@base_url}" response = nil; cartObj = nil; cartObj = Cart.new util = Util.new util.throwExceptionIfNullOrBlank(cartID, "Cart Id"); util.throwExceptionIfNullOrBlank(itemID, "Item Id"); util.throwExceptionIfNullOrBlank(itemQuantity, "Quantity"); begin connection = App42::Connection::RESTConnection.new(@base_url) body = {'app42' => {"cart"=> { "cartId" => cartID, "itemId" => itemID, "quantity" => itemQuantity }}}.to_json puts "Body #{body}" query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } query_params = params.clone params.store("body", body) puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/increaseQuantity" response = connection.put(signature, resource_url, query_params, body) cart = CartResponseBuilder.new cartObj = cart.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return cartObj end # # To decrease quantity of existing item in the cart. # # @return Cart object containing updated item. # # @raise App42Exception # def decrease_quantity(cartId, itemId, itemQuantity) puts "Decrease Quantity Called " puts "Base url #{@base_url}" response = nil; cartObj = nil; cartObj = Cart.new util = Util.new util.throwExceptionIfNullOrBlank(cartId, "CartId"); util.throwExceptionIfNullOrBlank(itemId, "ItemId"); util.throwExceptionIfNullOrBlank(itemQuantity, "ItemQuantity"); begin connection = App42::Connection::RESTConnection.new(@base_url) body = {'app42' => {"cart"=> { "cartId" => cartId, "itemId" => itemId, "quantity" => itemQuantity }}}.to_json puts "Body #{body}" query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } query_params = params.clone puts params params.store("body", body) puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/decreaseQuantity" response = connection.put(signature, resource_url, query_params,body) cart = CartResponseBuilder.new cartObj = cart.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return cartObj end end end end