Commit 8d3a2c4a by hades

card: send smtp mail

parent 55625ba6
class CardsController < ApplicationController class CardsController < ApplicationController
add_breadcrumb "Cards", :cards_path add_breadcrumb "Cards", :cards_path
def index def index
@card_infos = Hash.new @card_infos = Hash.new
Please register or sign in to reply
if session[:SHOPPING_CARD_SESSION_NAME] != nil if session[:SHOPPING_CARD_SESSION_NAME] != nil
@card_infos = session[:SHOPPING_CARD_SESSION_NAME] @card_infos = session[:SHOPPING_CARD_SESSION_NAME]
end end
if params[:product_id] != nil if params[:product_id] != nil
# if remove a product from card # if remove a product from card
if params[:card_action] != nil && params[:card_action] == 'remove' if params[:card_action] != nil && params[:card_action] == 'remove'
# Nothing to delete # Nothing to delete
if @card_infos.empty? if @card_infos.empty?
redirect_to cards_path redirect_to cards_path
else else
card_items = @card_infos[:card_items] card_items = @card_infos[:card_items]
card_items.each do |card_item, key| card_items.each do |card_item, key|
# if a product exist in card # if a product exist in card
if card_item[:product_id] == params[:product_id] if card_item[:product_id] == params[:product_id]
card_items.delete(card_item) card_items.delete(card_item)
@card_infos = { card_items: card_items, customer_info: @card_infos[:customer_info] } @card_infos = { card_items: card_items, customer_info: @card_infos[:customer_info] }
session[:SHOPPING_CARD_SESSION_NAME] = @card_infos session[:SHOPPING_CARD_SESSION_NAME] = @card_infos
redirect_to cards_path redirect_to cards_path
end end
end end
end end
else else
# if add a product to card # if add a product to card
# first time add to card # first time add to card
if @card_infos.empty? if @card_infos.empty?
product = Product.find(params[:product_id]) product = Product.find(params[:product_id])
card_items = Array.new card_items = Array.new
card_items.push({ product_id: params[:product_id], quantity: 1 }) card_items.push({ product_id: params[:product_id], quantity: 1 })
customer_info = Hash.new customer_info = Hash.new
@card_infos = { card_items: card_items, customer_info: customer_info } @card_infos = { card_items: card_items, customer_info: customer_info }
session[:SHOPPING_CARD_SESSION_NAME] = @card_infos session[:SHOPPING_CARD_SESSION_NAME] = @card_infos
redirect_to cards_path redirect_to cards_path
else else
card_items = @card_infos[:card_items] card_items = @card_infos[:card_items]
card_items.each do |card_item, key| found = false
# if a product exist in card card_items.each do |card_item|
if card_item[:product_id] == params[:product_id] # if a product exist in card
card_item[:quantity] += 1 if card_item[:product_id] == params[:product_id]
card_items[key] = card_item # card_item[:quantity] += 1
@card_infos = { card_items: card_items, customer_info: @card_infos[:customer_info] } # @card_infos = { card_items: card_items, customer_info: @card_infos[:customer_info] }
session[:SHOPPING_CARD_SESSION_NAME] = @card_infos # session[:SHOPPING_CARD_SESSION_NAME] = @card_infos
redirect_to cards_path # redirect_to cards_path
end found = true
end break
# if a product not exist in card end
card_items.push({ product_id: params[:product_id], quantity: 1 }) end
customer_info = @card_infos[:customer_info] # if a product not exist in card
@card_infos = { card_items: card_items, customer_info: customer_info } if !found
session[:SHOPPING_CARD_SESSION_NAME] = @card_infos card_items.push({ product_id: params[:product_id], quantity: 1 })
redirect_to cards_path customer_info = @card_infos[:customer_info]
end @card_infos = { card_items: card_items, customer_info: customer_info }
end session[:SHOPPING_CARD_SESSION_NAME] = @card_infos
end end
redirect_to cards_path
end
end
end
end end
def checkout def checkout
add_breadcrumb "Checkout", url_for(action: 'checkout') add_breadcrumb "Checkout", url_for(action: 'checkout')
@card_infos = Hash.new @card_infos = Hash.new
if session[:SHOPPING_CARD_SESSION_NAME] != nil if session[:SHOPPING_CARD_SESSION_NAME] != nil
@card_infos = session[:SHOPPING_CARD_SESSION_NAME] @card_infos = session[:SHOPPING_CARD_SESSION_NAME]
end end
if @card_infos.empty? if @card_infos.empty?
redirect_to cards_path redirect_to cards_path
end end
if request.post? if request.post?
@errors = Array.new @errors = Array.new
customer_info = Hash.new customer_info = Hash.new
if params[:full_name].empty? if params[:full_name].empty?
@errors.push("Full name is required") @errors.push("Full name is required")
end end
if params[:email].empty? if params[:email].empty?
@errors.push("Email is required") @errors.push("Email is required")
else else
if params[:email] !~ /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i if params[:email] !~ /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
@errors.push("Invalid email address") @errors.push("Invalid email address")
end end
end end
if params[:phone].empty? if params[:phone].empty?
@errors.push("Phone is required") @errors.push("Phone is required")
end end
if params[:address].empty? if params[:address].empty?
@errors.push("Address is required") @errors.push("Address is required")
end end
if @errors.count == 0 if @errors.count == 0
customer_info[:full_name] = params[:full_name] customer_info[:full_name] = params[:full_name]
customer_info[:email] = params[:email] customer_info[:email] = params[:email]
customer_info[:phone] = params[:phone] customer_info[:phone] = params[:phone]
customer_info[:address] = params[:address] customer_info[:address] = params[:address]
customer_info[:note] = params[:note] customer_info[:note] = params[:note]
@card_infos = { card_items: @card_infos[:card_items], customer_info: customer_info } @card_infos = { card_items: @card_infos[:card_items], customer_info: customer_info }
session[:SHOPPING_CARD_SESSION_NAME] = @card_infos session[:SHOPPING_CARD_SESSION_NAME] = @card_infos
redirect_to url_for(action: 'confirm_checkout') redirect_to url_for(action: 'confirm_checkout')
end end
else else
customer_info = @card_infos[:customer_info] customer_info = @card_infos[:customer_info]
params[:full_name] = customer_info[:full_name] params[:full_name] = customer_info[:full_name]
params[:email] = customer_info[:email] params[:email] = customer_info[:email]
params[:phone] = customer_info[:phone] params[:phone] = customer_info[:phone]
params[:address] = customer_info[:address] params[:address] = customer_info[:address]
params[:note] = customer_info[:note] params[:note] = customer_info[:note]
end end
end end
def confirm_checkout def confirm_checkout
add_breadcrumb "Check out", url_for(action: 'checkout') add_breadcrumb "Check out", url_for(action: 'checkout')
add_breadcrumb "Confirm", url_for(action: 'confirm_checkout') add_breadcrumb "Confirm", url_for(action: 'confirm_checkout')
@card_infos = Hash.new @card_infos = Hash.new
if session[:SHOPPING_CARD_SESSION_NAME] != nil if session[:SHOPPING_CARD_SESSION_NAME] != nil
@card_infos = session[:SHOPPING_CARD_SESSION_NAME] @card_infos = session[:SHOPPING_CARD_SESSION_NAME]
end end
if @card_infos.empty? if @card_infos.empty?
redirect_to cards_path redirect_to cards_path
end end
if request.post? if request.post?
customer_info = @card_infos[:customer_info] customer_info = @card_infos[:customer_info]
card_items = @card_infos[:card_items] card_items = @card_infos[:card_items]
xml_data = '<?xml version="1.0"?>'; xml_data = '<?xml version="1.0"?>';
...@@ -137,13 +141,14 @@ class CardsController < ApplicationController ...@@ -137,13 +141,14 @@ class CardsController < ApplicationController
note: customer_info[:note], note: customer_info[:note],
items: xml_data items: xml_data
) )
session[:SHOPPING_CARD_SESSION_NAME] = nil UserMailer.card_send_to_consignee(customer_info[:email], @card_infos).deliver
redirect_to url_for(action: 'thankyou') redirect_to url_for(action: 'thankyou')
end end
end end
def thankyou def thankyou
add_breadcrumb "Thank out", url_for(action: 'thankyou') add_breadcrumb "Thank out", url_for(action: 'thankyou')
render :layout => "application_one_col" session[:SHOPPING_CARD_SESSION_NAME] = nil
render :layout => "application_one_col"
end end
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 @@ ...@@ -25,7 +25,7 @@
%> %>
<tr> <tr>
<td><%= $i %></td> <td><%= $i %></td>
<td><%= link_to product.name, product_path(product) %></td> <td><%= link_to product.name, product_url(product) %></td>
<% if can_edit %> <% if can_edit %>
<td><input type="text" class="form-control" value="<%= card_item[:quantity] %>" /></td> <td><input type="text" class="form-control" value="<%= card_item[:quantity] %>" /></td>
<% else %> <% else %>
......
...@@ -5,28 +5,7 @@ ...@@ -5,28 +5,7 @@
</h2> </h2>
<p><br />Please check your information:</p> <p><br />Please check your information:</p>
<% customer_info = @card_infos[:customer_info] %> <% customer_info = @card_infos[:customer_info] %>
<table class="table"> <%= render partial: 'cards/customer_info', locals: { customer_info: customer_info } %>
<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/item_list', locals: { card_infos: @card_infos, can_edit: false } %> <%= render partial: 'cards/item_list', locals: { card_infos: @card_infos, can_edit: false } %>
<%= form_tag do %> <%= form_tag do %>
<div class="text-center"> <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 ...@@ -24,5 +24,6 @@ module VenshopApp
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded. # 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.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
# config.i18n.default_locale = :de # config.i18n.default_locale = :de
config.action_mailer.default_url_options = { host: 'localhost:3000' }
end end
end end
...@@ -9,5 +9,18 @@ RecaptchaMailhide.configure do |c| ...@@ -9,5 +9,18 @@ RecaptchaMailhide.configure do |c|
c.public_key = '6LfDL-kSAAAAAG8JuwZhxLaT8X8iHZYpWu-7DAFe' c.public_key = '6LfDL-kSAAAAAG8JuwZhxLaT8X8iHZYpWu-7DAFe'
end 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_APP_ID = 'dj0zaiZpPXpIMzBsMUQyTk55dSZkPVlXazlZWGxzYjNoWU0yVW1jR285TUEtLSZzPWNvbnN1bWVyc2VjcmV0Jng9MzI-'
YAHOO_SHOPPING_DATA_SECRECT = '45b3584021d48d65984b515a2cd23623a3ec90cc' 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