card: send smtp mail
Showing
venshop_app/app/mailers/user_mailer.rb
0 → 100644
venshop_app/spec/mailers/user_mailer_spec.rb
0 → 100644
| class CardsController < ApplicationController | ||
| add_breadcrumb "Cards", :cards_path | ||
| add_breadcrumb "Cards", :cards_path | ||
| def index | ||
| @card_infos = Hash.new | ||
| if session[:SHOPPING_CARD_SESSION_NAME] != nil | ||
| @card_infos = session[:SHOPPING_CARD_SESSION_NAME] | ||
| end | ||
| if params[:product_id] != nil | ||
| # if remove a product from card | ||
| if params[:card_action] != nil && params[:card_action] == 'remove' | ||
| # Nothing to delete | ||
| if @card_infos.empty? | ||
| redirect_to cards_path | ||
| else | ||
| card_items = @card_infos[:card_items] | ||
| card_items.each do |card_item, key| | ||
| # if a product exist in card | ||
| if card_item[:product_id] == params[:product_id] | ||
| card_items.delete(card_item) | ||
| @card_infos = { card_items: card_items, customer_info: @card_infos[:customer_info] } | ||
| session[:SHOPPING_CARD_SESSION_NAME] = @card_infos | ||
| redirect_to cards_path | ||
| end | ||
| end | ||
| end | ||
| else | ||
| # if add a product to card | ||
| # first time add to card | ||
| if @card_infos.empty? | ||
| product = Product.find(params[:product_id]) | ||
| card_items = Array.new | ||
| card_items.push({ product_id: params[:product_id], quantity: 1 }) | ||
| customer_info = Hash.new | ||
| @card_infos = { card_items: card_items, customer_info: customer_info } | ||
| session[:SHOPPING_CARD_SESSION_NAME] = @card_infos | ||
| redirect_to cards_path | ||
| else | ||
| card_items = @card_infos[:card_items] | ||
| card_items.each do |card_item, key| | ||
| # if a product exist in card | ||
| if card_item[:product_id] == params[:product_id] | ||
| card_item[:quantity] += 1 | ||
| card_items[key] = card_item | ||
| @card_infos = { card_items: card_items, customer_info: @card_infos[:customer_info] } | ||
| session[:SHOPPING_CARD_SESSION_NAME] = @card_infos | ||
| redirect_to cards_path | ||
| end | ||
| end | ||
| # if a product not exist in card | ||
| card_items.push({ product_id: params[:product_id], quantity: 1 }) | ||
| customer_info = @card_infos[:customer_info] | ||
| @card_infos = { card_items: card_items, customer_info: customer_info } | ||
| session[:SHOPPING_CARD_SESSION_NAME] = @card_infos | ||
| redirect_to cards_path | ||
| end | ||
| end | ||
| end | ||
| @card_infos = Hash.new | ||
Please
register
or
sign in
to reply
|
||
| if session[:SHOPPING_CARD_SESSION_NAME] != nil | ||
| @card_infos = session[:SHOPPING_CARD_SESSION_NAME] | ||
| end | ||
| if params[:product_id] != nil | ||
| # if remove a product from card | ||
| if params[:card_action] != nil && params[:card_action] == 'remove' | ||
| # Nothing to delete | ||
| if @card_infos.empty? | ||
| redirect_to cards_path | ||
| else | ||
| card_items = @card_infos[:card_items] | ||
| card_items.each do |card_item, key| | ||
| # if a product exist in card | ||
| if card_item[:product_id] == params[:product_id] | ||
| card_items.delete(card_item) | ||
| @card_infos = { card_items: card_items, customer_info: @card_infos[:customer_info] } | ||
| session[:SHOPPING_CARD_SESSION_NAME] = @card_infos | ||
| redirect_to cards_path | ||
| end | ||
| end | ||
| end | ||
| else | ||
| # if add a product to card | ||
| # first time add to card | ||
| if @card_infos.empty? | ||
| product = Product.find(params[:product_id]) | ||
| card_items = Array.new | ||
| card_items.push({ product_id: params[:product_id], quantity: 1 }) | ||
| customer_info = Hash.new | ||
| @card_infos = { card_items: card_items, customer_info: customer_info } | ||
| session[:SHOPPING_CARD_SESSION_NAME] = @card_infos | ||
| redirect_to cards_path | ||
| else | ||
| card_items = @card_infos[:card_items] | ||
| found = false | ||
| card_items.each do |card_item| | ||
| # if a product exist in card | ||
| if card_item[:product_id] == params[:product_id] | ||
| # card_item[:quantity] += 1 | ||
| # @card_infos = { card_items: card_items, customer_info: @card_infos[:customer_info] } | ||
| # session[:SHOPPING_CARD_SESSION_NAME] = @card_infos | ||
| # redirect_to cards_path | ||
| found = true | ||
| break | ||
| end | ||
| end | ||
| # if a product not exist in card | ||
| if !found | ||
| card_items.push({ product_id: params[:product_id], quantity: 1 }) | ||
| customer_info = @card_infos[:customer_info] | ||
| @card_infos = { card_items: card_items, customer_info: customer_info } | ||
| session[:SHOPPING_CARD_SESSION_NAME] = @card_infos | ||
| end | ||
| redirect_to cards_path | ||
| end | ||
| end | ||
| end | ||
| end | ||
| def checkout | ||
| add_breadcrumb "Checkout", url_for(action: 'checkout') | ||
| @card_infos = Hash.new | ||
| if session[:SHOPPING_CARD_SESSION_NAME] != nil | ||
| @card_infos = session[:SHOPPING_CARD_SESSION_NAME] | ||
| end | ||
| if @card_infos.empty? | ||
| redirect_to cards_path | ||
| end | ||
| if request.post? | ||
| @errors = Array.new | ||
| customer_info = Hash.new | ||
| if params[:full_name].empty? | ||
| @errors.push("Full name is required") | ||
| end | ||
| if params[:email].empty? | ||
| @errors.push("Email is required") | ||
| else | ||
| if params[:email] !~ /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i | ||
| @errors.push("Invalid email address") | ||
| end | ||
| end | ||
| if params[:phone].empty? | ||
| @errors.push("Phone is required") | ||
| end | ||
| if params[:address].empty? | ||
| @errors.push("Address is required") | ||
| end | ||
| if @errors.count == 0 | ||
| customer_info[:full_name] = params[:full_name] | ||
| customer_info[:email] = params[:email] | ||
| customer_info[:phone] = params[:phone] | ||
| customer_info[:address] = params[:address] | ||
| customer_info[:note] = params[:note] | ||
| @card_infos = { card_items: @card_infos[:card_items], customer_info: customer_info } | ||
| session[:SHOPPING_CARD_SESSION_NAME] = @card_infos | ||
| redirect_to url_for(action: 'confirm_checkout') | ||
| end | ||
| else | ||
| customer_info = @card_infos[:customer_info] | ||
| params[:full_name] = customer_info[:full_name] | ||
| params[:email] = customer_info[:email] | ||
| params[:phone] = customer_info[:phone] | ||
| params[:address] = customer_info[:address] | ||
| params[:note] = customer_info[:note] | ||
| end | ||
| add_breadcrumb "Checkout", url_for(action: 'checkout') | ||
| @card_infos = Hash.new | ||
| if session[:SHOPPING_CARD_SESSION_NAME] != nil | ||
| @card_infos = session[:SHOPPING_CARD_SESSION_NAME] | ||
| end | ||
| if @card_infos.empty? | ||
| redirect_to cards_path | ||
| end | ||
| if request.post? | ||
| @errors = Array.new | ||
| customer_info = Hash.new | ||
| if params[:full_name].empty? | ||
| @errors.push("Full name is required") | ||
| end | ||
| if params[:email].empty? | ||
| @errors.push("Email is required") | ||
| else | ||
| if params[:email] !~ /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i | ||
| @errors.push("Invalid email address") | ||
| end | ||
| end | ||
| if params[:phone].empty? | ||
| @errors.push("Phone is required") | ||
| end | ||
| if params[:address].empty? | ||
| @errors.push("Address is required") | ||
| end | ||
| if @errors.count == 0 | ||
| customer_info[:full_name] = params[:full_name] | ||
| customer_info[:email] = params[:email] | ||
| customer_info[:phone] = params[:phone] | ||
| customer_info[:address] = params[:address] | ||
| customer_info[:note] = params[:note] | ||
| @card_infos = { card_items: @card_infos[:card_items], customer_info: customer_info } | ||
| session[:SHOPPING_CARD_SESSION_NAME] = @card_infos | ||
| redirect_to url_for(action: 'confirm_checkout') | ||
| end | ||
| else | ||
| customer_info = @card_infos[:customer_info] | ||
| params[:full_name] = customer_info[:full_name] | ||
| params[:email] = customer_info[:email] | ||
| params[:phone] = customer_info[:phone] | ||
| params[:address] = customer_info[:address] | ||
| params[:note] = customer_info[:note] | ||
| end | ||
| end | ||
| def confirm_checkout | ||
| add_breadcrumb "Check out", url_for(action: 'checkout') | ||
| add_breadcrumb "Confirm", url_for(action: 'confirm_checkout') | ||
| @card_infos = Hash.new | ||
| if session[:SHOPPING_CARD_SESSION_NAME] != nil | ||
| @card_infos = session[:SHOPPING_CARD_SESSION_NAME] | ||
| end | ||
| if @card_infos.empty? | ||
| redirect_to cards_path | ||
| end | ||
| if request.post? | ||
| add_breadcrumb "Check out", url_for(action: 'checkout') | ||
| add_breadcrumb "Confirm", url_for(action: 'confirm_checkout') | ||
| @card_infos = Hash.new | ||
| if session[:SHOPPING_CARD_SESSION_NAME] != nil | ||
| @card_infos = session[:SHOPPING_CARD_SESSION_NAME] | ||
| end | ||
| if @card_infos.empty? | ||
| redirect_to cards_path | ||
| end | ||
| if request.post? | ||
| customer_info = @card_infos[:customer_info] | ||
| card_items = @card_infos[:card_items] | ||
| xml_data = '<?xml version="1.0"?>'; | ||
| ... | ... | @@ -137,13 +141,14 @@ class CardsController < ApplicationController |
| note: customer_info[:note], | ||
| items: xml_data | ||
| ) | ||
| session[:SHOPPING_CARD_SESSION_NAME] = nil | ||
| redirect_to url_for(action: 'thankyou') | ||
| end | ||
| UserMailer.card_send_to_consignee(customer_info[:email], @card_infos).deliver | ||
| redirect_to url_for(action: 'thankyou') | ||
| end | ||
| end | ||
| def thankyou | ||
| add_breadcrumb "Thank out", url_for(action: 'thankyou') | ||
| render :layout => "application_one_col" | ||
| add_breadcrumb "Thank out", url_for(action: 'thankyou') | ||
| session[:SHOPPING_CARD_SESSION_NAME] = nil | ||
| render :layout => "application_one_col" | ||
| end | ||
| end | ||