Commit 8d3a2c4a by hades

card: send smtp mail

parent 55625ba6
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
class UserMailer < ActionMailer::Base
default from: "from@example.com"
def card_send_to_consignee(email, card_infos)
@card_infos = card_infos
mail(
to: email,
subject: 'Shopping information'
)
end
end
<table class="table">
<tr>
<td><b>Full name</b>:</td>
<td><%= customer_info[:full_name] %></td>
</tr>
<tr>
<td><b>Email</b>:</td>
<td><%= customer_info[:email] %></td>
</tr>
<tr>
<td><b>Phone</b>:</td>
<td><%= customer_info[:phone] %></td>
</tr>
<tr>
<td><b>Address</b>:</td>
<td><%= customer_info[:address] %></td>
</tr>
<tr>
<td><b>Note</b>:</td>
<td><%= customer_info[:note] %></td>
</tr>
</table>
\ No newline at end of file
......@@ -25,7 +25,7 @@
%>
<tr>
<td><%= $i %></td>
<td><%= link_to product.name, product_path(product) %></td>
<td><%= link_to product.name, product_url(product) %></td>
<% if can_edit %>
<td><input type="text" class="form-control" value="<%= card_item[:quantity] %>" /></td>
<% else %>
......
......@@ -5,28 +5,7 @@
</h2>
<p><br />Please check your information:</p>
<% customer_info = @card_infos[:customer_info] %>
<table class="table">
<tr>
<td><b>Full name</b>:</td>
<td><%= customer_info[:full_name] %></td>
</tr>
<tr>
<td><b>Email</b>:</td>
<td><%= customer_info[:email] %></td>
</tr>
<tr>
<td><b>Phone</b>:</td>
<td><%= customer_info[:phone] %></td>
</tr>
<tr>
<td><b>Address</b>:</td>
<td><%= customer_info[:address] %></td>
</tr>
<tr>
<td><b>Note</b>:</td>
<td><%= customer_info[:note] %></td>
</tr>
</table>
<%= render partial: 'cards/customer_info', locals: { customer_info: customer_info } %>
<%= render partial: 'cards/item_list', locals: { card_infos: @card_infos, can_edit: false } %>
<%= form_tag do %>
<div class="text-center">
......
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Venshop</title>
</head>
<body>
<% customer_info = @card_infos[:customer_info] %>
<p>
Hi <b><%= customer_info[:full_name] %></b>,<br /><br />
You or someone has been ordered on our website with the following information:
</p>
<%= render partial: 'cards/customer_info', locals: { customer_info: customer_info } %>
<%= render partial: 'cards/item_list', locals: { card_infos: @card_infos, can_edit: false } %>
</body>
</html>
\ No newline at end of file
......@@ -24,5 +24,6 @@ module VenshopApp
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
# config.i18n.default_locale = :de
config.action_mailer.default_url_options = { host: 'localhost:3000' }
end
end
......@@ -9,5 +9,18 @@ RecaptchaMailhide.configure do |c|
c.public_key = '6LfDL-kSAAAAAG8JuwZhxLaT8X8iHZYpWu-7DAFe'
end
VenshopApp::Application.configure do
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'smtp.gmail.com',
port: 587,
domain: 'alphaplus.vn',
user_name: 'coi.no.reply@gmail.com',
password: 'q5cE05OUwJ',
authentication: 'plain',
enable_starttls_auto: true
}
end
YAHOO_SHOPPING_DATA_APP_ID = 'dj0zaiZpPXpIMzBsMUQyTk55dSZkPVlXazlZWGxzYjNoWU0yVW1jR285TUEtLSZzPWNvbnN1bWVyc2VjcmV0Jng9MzI-'
YAHOO_SHOPPING_DATA_SECRECT = '45b3584021d48d65984b515a2cd23623a3ec90cc'
\ No newline at end of file
require "spec_helper"
describe UserMailer do
pending "add some examples to (or delete) #{__FILE__}"
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment