Displaying unique transactions in Ruby on Rails - ruby-on-rails

Currently there are multiple transactions with the same txnid. But for each transaction the payment mode is credit card, debit card or net banking.
If the customer has to pay $3000 he might pay that $3000 like $1000 with Credit card, $1000 with Debit Card, $1000 with Net banking.
Currently am displaying all those 3 transactions in 3 rows but with the same txnid. But I want to display those 3 transactions in a single row by mentioning in the payment mode column ($1000- Credit Card, $1000 Debit Card, $1000 Net bank)
Thanks for your help .
transactions.html.erb
<tr>
<th>Merchant Name</th>
<th>Transaction ID</th>
<th>Payment Mode</th>
<th>Amount</th>
<th>Status</th>
<th>Transaction Fee</th>
<th>Discount</th>
<th>Additional Charges</th>
<th>Added On</th>
</tr>
</thead>
<tbody id="details">
<% #all_settlement_details.map(&:txnid).uniq.each do |sd| %>
<tr>
<td><%= sd.merchantname %></td>
<td><%= sd.txnid %></td>
<td><%= get_mode_ui.value(sd.mode) %></td>
<td><%= sd.amount %></td>
<td><%= get_status(sd.status) %></td>
<td><%= sd.mer_service_fee %></td>
<td><%= sd.discount %></td>
<td><%= sd.additional_charges%></td>
<td><%= get_added_on_date sd.addedon %></td>
</tr>
<% end %>
</tbody>
trasactions.helper.rb
INDEX_OPTIONS = {'dashboard' => 'get_dashboard_data', 'merchants' => 'get_merchants', 'transactions' => 'get_transactions',
'admin' => 'get_admin_data', 'profile' => 'get_merchant_data'}
INDEX_PARTIAL = {'dashboard' => 'dashboard', 'merchants' => 'merchant_list', 'transactions' => 'transactions',
'admin' => 'admin', 'profile' => 'merchant_profile'}
AJAX_OPTIONS = {'stat-board' => 'get_stats', 'transactions' => 'get_graph_transactions', 'payment-methods' => 'get_payment_method_graph_data',
'merchants' => 'get_merchant_graph_data', 'merchant_profile' => 'get_merchant_profile_data', 'load_more' => 'load_more',
'merchant_list' => 'get_merchants'}
AJAX_PARTIAL = {'stat-board' => 'stat_board', 'transactions' => 'transactions_graph', 'payment-methods' => 'payment_methods_graph',
'merchants' => 'merchant_graphs', 'load_more' => 'transaction_details', 'merchant_profile' => 'merchant_profile_detail',
'merchant_list' => 'filtered_merchants',}
STATUS={"dropped"=>0,"bounced"=>1,"captured"=>2,"auth"=>3,"failed"=>4,"usercancelled"=>5,"pending"=>6}
MODE={'CC'=>0,'DC'=>1,'NB'=>2,'EMI'=>3,'CASH'=>4,'IVR'=>5,'COD'=>6}
MODE_UI = {'CC'=>'Credit Card','DC'=>'Debit Card','NB'=>'Net Banking','EMI'=>'EMI','CASH'=>'Cash','IVR'=>'IVR','COD'=>'Cash on Delivery'}
PG_MODE = {'Credit Card'=>0,'Debit Card'=>1,'Net Banking'=>2,'EMI'=>3,'Cash'=>4,'IVR'=>5,'Cash on Delivery'=>6,'Others'=>nil}
STATUS_UI={"Dropped"=>0,"Bounced"=>1,"Captured"=>2,"Authenticated"=>3,"Failed"=>4,"User Cancelled"=>5,"Pending"=>6}
CARDTYPE={'domestic'=>1,'international'=>2}
TRANSACTIONS = ["Merchant Name", "Transaction ID", "Payment Mode","Amount","Status","Transaction Fee","Discount","Additional Charges","Added On"]
TXN_SUCCESS_STATUS = [2,3]
TXN_FAILURE_STATUS = [0,1,4,5]
TXN_PENDING_STATUS = 6
PARTNER_PANEL_REDMINE_STATUS_MAPPING = {
16 => "In Process",
3 => "In Process",
69 => "In Process",
11 => "Line of Business Rejected",
14 => "Live",
42 => "In Process",
51 => "In Process",
15 => "In Process",
17 => "In Process",
64 => "In Process",
46 => "In Process",
62 => "Line of Business Rejected",
65 => "In Process",
18 => "Assigned to Sales",
79 => "In Process",
61 => "In Process",
5 => "In Process",
70 => "In Process",
71 => "In Process",
50 => "Line of Business Rejected",
32 => "Contacted",
48 => "In Process",
59 => "In Process",
49 => "In Process",
47 => "In Process",
60 => "In Process",
29 => "Line of Business Rejected",
78 => "Assigned to Sales"
}
PARTNER_PANEL_REDMINE_STATUS_MAPPING_UI = {
"In Process" => "16,3,69,42,51,15,17,64,46,65,79,61,5,70,71,48,59,49,47,60",
"Line of Business Rejected" => "11,62,50,29",
"Assigned to Sales" => "78,18",
"Live" => "14",
"Contacted" => "32"
}
def get_status _status_flag
STATUS.key(_status_flag)
end
def get_mode _mode_flag
MODE.key(_mode_flag)
end
def get_mode_ui
PG_MODE
end
def get_transactions_ui
TRANSACTIONS
end
def get_status_ui
STATUS_UI
end
def get_merchant_status_ui
PARTNER_PANEL_REDMINE_STATUS_MAPPING_UI
end
def status_merchant
PARTNER_PANEL_REDMINE_STATUS_MAPPING
end
def get_added_on_date _date
_date.strftime("%A, %d %B\'%y %H:%M")
end
def link_to_add_issues(name, f, association)
new_object = f.object.class.reflect_on_association(association).klass.new
fields = f.fields_for(association, new_object, :child_index => "new_#{association}") do |builder|
render(association.to_s.singularize + "_fields", :f => builder)
end
link_to_function(name, "add_issues(this, \"#{association}\", \"#{escape_javascript(fields)}\")")
end
def link_to_remove_issues(name, f)
f.hidden_field(:_destroy) + link_to_function(name, "remove_issues(this)")
end
def get_issue_object _partner
_partner.issues.blank? ? _partner.class.reflect_on_association(:issues).klass.new : _partner.issues
end
def get_contact_detail_object issue
issue.contact_detail.blank? ? issue.class.reflect_on_association(:contact_detail).klass.new : issue.contact_detail
end
def issue_contact_detail issue, contact_column_name
issue.contact_detail ? issue.contact_detail.send(contact_column_name) : ""
end
def options_for_state(issue)
options_for_select(Setting[:states].split(/\r\n/).sort, issue.state).html_safe
end
# First Landing page for a Partner
def get_dashboard_data
get_stats
get_merchants
end
# Merchant list for a Partner
def get_merchants
_merchant_filter_scope = {'merchant_status' => ["merchant_status", (params[:merchant_status].split(',') rescue nil)],
'merchant_email' => ["merchant_mail", params[:merchant_email]],
'merchant_url' => ["merchant_url", params[:merchant_url]],
'merchant_mid' => ['payu_mid', params[:merchant_mid]],
'merchant_name' => ['merchant_name', params[:merchant_name]],
'order' => ['order_issue', params[:order]]
}
method_array = [['issues'],["page", params[:page]],["per", per_page_option],["date_filter", #start_date, #end_date]]
#partner_merchants = call_scope_methods_dynamically(method_array, _merchant_filter_scope)
end
# Transactions for a Partner
def get_transactions
#get more records to load
load_more
end
# Merchant Profile page for a Partner
def get_merchant_data
get_stats
load_more
end
# Admin page for Partners
def get_admin_data
#partners = Partner.where(partner_admin: false)
end
# Stat Board tab on Dashboard
def get_stats
method_array = [['settlement_details'],['stat_board_details'],['count_as_graph_data']]
method_array << ['merchant_mid', #merchant_mid] if #merchant_mid
method_array = add_date_or_hour_filter method_array
data1 = call_methods(method_array)
method_array << ['group_by_settlements_date']
data2 = call_methods(method_array)
#graph_data = [data1, data2]
end
# Merchant tab on Dashboard
def get_merchant_graph_data
method_array = [['issues']]
method_array = add_date_or_hour_filter method_array
method_array << ['group_by_merchant_date']
data1 = #bank_partner.issues.date_filter(#start_date, #end_date).group_by(&:status_id)
data2 = call_methods(method_array)
#merchant_status_hash = {}
data1.each{|key, value| get_partner_status_hash(get_merchant_status(key), value.count)}
[#merchant_status_hash, data2]
end
def get_merchant_status redmine_status
PARTNER_PANEL_REDMINE_STATUS_MAPPING[redmine_status] || 'In Process'
end
def get_partner_status_hash key, value
#merchant_status_hash.update({key => value}){|k, ov, nv| ov + nv}
end
# Transaction tab on Dashboard
def get_graph_transactions
method_array = [['settlement_details'],['count_as_graph_data']]
method_array << ['merchant_mid', #merchant_mid] if #merchant_mid
method_array = add_date_or_hour_filter method_array
method_array << ['group_by_settlements_date']
#data1 = call_methods(method_array << ["status", TXN_SUCCESS_STATUS])
#data2 = call_methods(method_array << ["status", TXN_FAILURE_STATUS])
#data3 = call_methods(method_array << ["status", TXN_PENDING_STATUS])
[#data1, #data2, #data3]
end
# Payment Method tab on Dashboard
def get_payment_method_graph_data
method_array = [['settlement_details'],["date_filter", #start_date, #end_date],['payment_mode']]
method_array << ['merchant_mid', #merchant_mid] if #merchant_mid
method_array << ['count']
call_methods(method_array)
end
def get_transaction_graph_hash successful, failure, pending
data_array= []
successful_hash = {}
failure_hash = {}
pending_hash = {}
successful.each{|d| successful_hash.merge!({(d.hour rescue d.date) => d.graph_data})}
failure.each{|d| failure_hash.merge!({(d.hour rescue d.date) => d.graph_data})}
pending.each{|d| pending_hash.merge!({(d.hour rescue d.date) => d.graph_data})}
(successful_hash.keys + failure_hash.keys + pending_hash.keys).uniq.each do |datetime|
data_array << {'datetime'=> "#{datetime}",'successful'=> (successful_hash[datetime] || 0) ,'unsuccessful'=> (failure_hash[datetime] || 0) ,'pending'=> (pending_hash[datetime] || 0)}
end
data_array
end
private
def load_more
#all_settlement_details = get_all_settlement_details || []
end
def get_all_settlement_details
# Offset increments by 10 and Limit remains 11
_txn_filter_scope = {'transaction_status' => ["status", (params[:transaction_status].split(',') rescue nil)],
'payment_options' => ["payment_options", (params[:payment_options].split(',') rescue nil)],
'transaction_id' => ['transaction_id', params[:transaction_id]],
'transaction_amount' => ['amount_range', ((params[:transaction_amount].split(',')[0] || 0) rescue nil), ((params[:transaction_amount].split(',')[1] || 100000000) rescue nil)],
'merchant_mid' => ['merchant_mid',params[:merchant_mid]],
'merchant_name' => ['merchant_name', params[:merchant_name]],
'clicks' => ['txn_offset', ((params[:clicks].to_i)*10 rescue 0)]
}
method_array = [['settlement_details'],["date_filter", #start_date, #end_date]]
method_array << ["txn_limit", 11] unless #csv_format
method_array << ['merchant_mid', #merchant_mid] if #merchant_mid
records = call_scope_methods_dynamically(method_array, _txn_filter_scope)
records.empty? ? nil : records
end
# extend Object to introduce the call_methods method
def call_methods(methods)
methods.inject(#bank_partner) do |obj, method|
case method.count
when 3
obj.send(method.first, method[1], method[2])
when 2
obj.send(method.first, method.last)
when 1
obj.send(method.first)
else
obj
end
end
end
def call_scope_methods_dynamically(_method_array, _method_hash)
method_array = _method_array
_dynamic_method_hash = _method_hash
# Add scopes in the methods array to call dynamically
params.each{|key, value| method_array << _dynamic_method_hash[key]}
call_methods(method_array.compact)
end
def add_date_or_hour_filter _method_array
if #start_date == #end_date
#end_date = (Date.parse(#end_date) + 1.day).strftime("%Y/%m/%d")
_method_array << ['hour_filter']
end
_method_array << ["date_filter", #start_date, #end_date]
_method_array
end
My final output should be look like.
Txnid payment mode Amount
23234XS24XS $1000Credit Card, $3000
$1000 Debit Card,
$1000 Net bank
Some one said I need to create a helper method for that. But I am not sure how!
Thanks for your help!

I don't know what all those files you posted are for. You say that you want the following in the db:
"$1000- Credit Card, $1000 Debit Card, $1000 Net bank"
First, you would never write that format to the db. Instead, you would try to use a consistent format, like:
Credit Card:1000,Debit Card:1000,Net Bank:1000
Then you can retrieve that column and store it in a variable:
payment_mode = "Credit Card: 1000, Debit Card: 1000, Net Bank: 1000"
Then you can do:
payments = Hash[
payment_mode
.split(',')
.map{|mode| mode.split(':')}
]
p payments
total = payment_by.values.sum
#total = payments.values.map(&:to_i).inject(:+)
taxnid = 123
first_line = true
puts "Taxnid\tPayment mode\t\tTotal"
payments.each_pair do |type, amount|
if first_line
puts "#{taxnid}\t#{type} #{amount}\t#{total}"
first_line = false
else
puts "\t#{type} #{amount}"
end
end
--output:--
Taxnid Payment mode Total
123 Credit Card 1000 3000
Debit Card 1000
Net Bank 1000
If you actually want to create some html that displays that format, then substitute <td>'s and <tr>'sin the appropriate places:

Related

PayPal Express Checkout with multiple items with active merchant in rails

I am trying to use PayPal ExpressCheckout button with multiple items but with no success.I am using NetBeans IDE, rails 4 and MySQL db.Here is what I did so far:
In my production.rb file I have:
Rails.application.configure do
config/application.rb.
config.after_initialize do
ActiveMerchant::Billing::Base.mode = :test
paypal_options = {
:login => "xxxx",
:password => "xxxx ",
:signature => "xxxx "
}
::STANDARD_GATEWAY = ActiveMerchant::Billing::PaypalGateway.new(paypal_options)
::EXPRESS_GATEWAY = ActiveMerchant::Billing::PaypalExpressGateway.new(paypal_options)
end
In my transaction.rb model I have:
def valid_purchase
if express_token.blank?
standard_purchase
else
express_token
end
def express_purchase
# price_in_cents = total
response = EXPRESS_GATEWAY.purchase(total, express_purchase_options)
if response.success?
self.status = "processed"
else
errors.add(:transactions, "---- #{response.message}.")
end
end
def express_token=(token)
self[:express_token] = token
if new_record? && !token.blank?
details = EXPRESS_GATEWAY.details_for(token)
self.express_payer_id = details.payer_id
self.ship_to_first_name = details.params["first_name"]
self.ship_to_last_name = details.params["last_name"]
end
end
private
def express_purchase_options
{
:ip => customer_ip,
:token => express_token,
:payer_id => express_payer_id
}
end
And in my transaction_controller.rb I have:
def express_checkout
order_items =[]
postage_rate=nil
item = Hash.new
#order = Order.find(session[:order_id])
#receipts = Receipt.where(:order_id=>#order.id)
#receipts.each do |r|
postage_rate = r.postage_rate * 100
end
#cart = Cart.find(#order.cart_id)
#cart.cart_items.each do |i|
#product = Product.find(i.product_id)
item = {
name: #product.product_name,
quantity: i.amount,
description: "ORDER_ID: #{#order.id}",
amount: #product.selling_price * 100 ,
shipping: postage_rate/#cart.cart_items.size
}
order_items << item
end
price_in_cents = (#order.total_p_pr * 100).round(2)
options = {
:ip => request.remote_ip,
:return_url => url_for(:action=>:new, :only_path => false),
:cancel_return_url => catalogs_traders_url,
:currency => "USD",
:allow_guest_checkout=> true,
:items => order_items # this line outputs: [{:name=>"owl potty", :quantity=>1, :description=>"ORDER_ID: 249", :amount=>2808.0, :shipping=>332.0}, {:name=>"a bag", :quantity=>1, :description=>"ORDER_ID: 249", :amount=>1260.0, :shipping=>332.0}, {:name=>"bracelet", :quantity=>1, :description=>"ORDER_ID: 249", :amount=>120.0, :shipping=>332.0}, {:name=>"beautiful woman", :quantity=>1, :description=>"ORDER_ID: 249", :amount=>74352.0, :shipping=>332.0}]
}
#passing the cost of the order
response = EXPRESS_GATEWAY.setup_purchase(price_in_cents,options )
redirect_to EXPRESS_GATEWAY.redirect_url_for(response.token)
end
def new
#transaction = Transaction.new(:express_token => params[:token])
end
I get:
Any help will be more than welcome. Thank you!
I red this post very,very carefully
setting tax amount in Active Merchant / PayPal Express Checkout
and I understood my mistakes. Here is my corrected transaction_controller:
# to redirect to PayPay site
def express_checkout
pr = nil
tp = nil
items =[]
postage_r=[]
total_p = []
order_items =[]
postage_rate=nil
item = Hash.new
#order = Order.find(session[:order_id])
#receipts = Receipt.where(:order_id=>#order.id)
#receipts.each do |r|
total_p << r.total_price
postage_r << r.postage_rate
end
tp = total_p.inject{|sum,x| sum + x }
pr = postage_r.inject{|sum,x| sum + x }
#cart = Cart.find(#order.cart_id)
#cart.cart_items.each do |i|
#product = Product.find(i.product_id)
item = {
name: #product.product_name,
quantity: i.amount,
description: "ORDER_ID: #{#order.id}",
amount: #product.selling_price * 100 ,
}
order_items << item
end
price_in_cents = (#order.total_p_pr * 100).round(2)
options = {
:subtotal => tp * 100,
:shipping => pr * 100,
:handling => 0,
:tax => 0,
:ip => request.remote_ip,
:return_url => url_for(:action=>:new, :only_path => false),
:cancel_return_url => catalogs_traders_url,
:currency => "USD",
:allow_guest_checkout=> true,
:items => order_items
}
#passing the cost of the order
response = EXPRESS_GATEWAY.setup_purchase(price_in_cents,options )
redirect_to EXPRESS_GATEWAY.redirect_url_for(response.token)
end
It worked. I hope my post will be useful for someone who want to integrate Express Checkout button. Thank you for all your help!

How to consolidate these many rake tasks into one task

The application I'm working on runs import tasks each night to get information from an external database and processes and writes it to the local PostgreSQL database. This is so it can be viewed in our application.
We do the following:
Create a connection to the external database
Perform some currency conversion
Do some calculations on the data based on the average
Check if the item already exists and update if it's changed
Also check to see how it is classified and change the category to fit our categorisations
Create the item if it does not exist
All of this data is on different tables in the external database, so I've created a task for each table (about 30), but I am repeating myself excessively so looking for some guidance on how to DRY this bad boy up.
Is it possible to create some kind of loop on a collection of the external database table names? I've tried this a couple of times but haven't had any success. Please find the code below of examples of two different import scripts.
desc 'Imports ABC Data'
task :abcdeal_import => :environment do
# Connect to legacy db
class OldDb < ActiveRecord::Base
establish_connection :cleardb
self.table_name = 'abcdeal'
end
class Import < OldDb
self.table_name = 'abcdeal'
end
# Currency Conversion
require 'money'
require 'money/bank/google_currency'
# set the seconds after than the current rates are automatically expired
Money::Bank::GoogleCurrency.ttl_in_seconds = 86400
# set default bank to instance of GoogleCurrency
bank = Money::Bank::GoogleCurrency.new
#gbp = bank.get_rate(:USD, :GBP).to_f
#eur = bank.get_rate(:USD, :EUR).to_f
#aud = bank.get_rate(:USD, :AUD).to_f
#cad = bank.get_rate(:USD, :CAD).to_f
#nzd = bank.get_rate(:USD, :NZD).to_f
Import.all.find_each do |u|
# Define Variables
#price = u.UnitPrice
#interest = u.UnitSold
##interest_changes = u.unitsold_changes
#company = u.Company
#legacy_id = u.id
#end_date = u.EndDate
#category = u.Category
#revenue = u.Revenue
# Calculate average
#company_average = company_average(#company)
if #company_average == 0
#company_average = 1
end
# Does item exist?
if Dailydeal.exists?(:legacy_id => #legacy_id, :company => #company)
# It exists.
#historical_interest = Product.where(:legacy_id => #legacy_id).pluck(:interest)
# Another statement to check that the Interest != UnitSold
if #interest != #historical_interest
Product.where(:legacy_id => #legacy_id, :company => #company).update_all(:interest => #interest, :end_date => #end_date, :interest_changes => #interest_changes, :status => average_change_interest, :revenue => #revenue, :revenue_eur => eur_revenue_currency_conversion, :revenue_aud => aud_revenue_currency_conversion, :revenue_gbp => gbp_revenue_currency_conversion, :revenue_cad => cad_revenue_currency_conversion, :revenue_nzd => nzd_revenue_currency_conversion)
end
else
# Doesn't exist
# Set Category
case #category
when 'Apparel'
#category = 'Apparel & Footwear'
when 'Gadgets'
#category = 'Electronics & Accessories'
when 'Gifts'
#category = 'Miscellaneous'
when 'Health & Cosmetics'
#category = 'Health & Beauty'
when 'Home'
#category = 'Home & Furniture'
when 'Other'
#category = 'Miscellaneous'
when 'Toys'
#category = 'Toys & Kids'
else
#category
end
Product.create(
:name => u.ProductName,
:link => u.ProductLink,
:image_url => u.ImageUrl,
:price => u.UnitPrice,
:interest => u.UnitSold,
:start_date => u.StartDate,
:end_date => u.EndDate,
:revenue => u.Revenue,
:company => u.Company,
:category => #category,
:country => u.Country,
:price_eur => eur_currency_conversion,
:price_aud => aud_currency_conversion,
:price_gbp => gbp_currency_conversion,
:price_cad => cad_currency_conversion,
:price_nzd => nzd_currency_conversion,
:revenue_eur => eur_revenue_currency_conversion,
:revenue_aud => aud_revenue_currency_conversion,
:revenue_gbp => gbp_revenue_currency_conversion,
:revenue_cad => cad_revenue_currency_conversion,
:revenue_nzd => nzd_revenue_currency_conversion,
:status => average_change_interest,
:legacy_id => u.id)
end
end
puts "Successfully imported data from abc"
end
desc 'Imports def Data'
task :def_import => :environment do
# Connect to legacy db
class OldDb < ActiveRecord::Base
establish_connection :cleardb
self.table_name = 'def'
end
class Import < OldDb
self.table_name = 'def'
end
# Currency Conversion
require 'money'
require 'money/bank/google_currency'
# set the seconds after than the current rates are automatically expired
Money::Bank::GoogleCurrency.ttl_in_seconds = 86400
# set default bank to instance of GoogleCurrency
bank = Money::Bank::GoogleCurrency.new
#gbp = bank.get_rate(:USD, :GBP).to_f
#eur = bank.get_rate(:USD, :EUR).to_f
#aud = bank.get_rate(:USD, :AUD).to_f
#cad = bank.get_rate(:USD, :CAD).to_f
#nzd = bank.get_rate(:USD, :NZD).to_f
Import.all.find_each do |u|
# Define Variables
#price = u.UnitPrice
#interest = u.UnitSold
##interest_changes = u.unitsold_changes
#company = u.Company
#legacy_id = u.id
#end_date = u.EndDate
#category = u.Category
#revenue = u.Revenue
# Calculate average
#company_average = company_average(#company)
if #company_average == 0
#company_average = 1
end
# Does item exist?
if Product.exists?(:legacy_id => #legacy_id, :company => #company)
# It exists.
#historical_interest = Product.where(:legacy_id => #legacy_id).pluck(:interest)
# Another statement to check that the Interest != UnitSold
if #interest != #historical_interest
Product.where(:legacy_id => #legacy_id, :company => #company).update_all(:interest => #interest, :end_date => #end_date, :interest_changes => #interest_changes, :status => average_change_interest, :revenue => #revenue, :revenue_eur => eur_revenue_currency_conversion, :revenue_aud => aud_revenue_currency_conversion, :revenue_gbp => gbp_revenue_currency_conversion, :revenue_cad => cad_revenue_currency_conversion, :revenue_nzd => nzd_revenue_currency_conversion)
end
else
# Doesn't exist
# Set Categories
case #category
when 'Adult Products'
#category
when 'Arts & Crafts', 'Furniture', 'Garden', 'Large Appliances', 'Lighting', 'Outdoor & Patio', 'Stationary', 'Storage & Organization'
#category = 'Home & Furniture'
when 'Audio & Audio Accessories', 'Electronics Accessories', 'Gadgets', 'GPS & Car Accessories', 'Tools & Hardware', 'Tablets'
#category = 'Electronics & Accessories'
when 'Automotive Services'
#category = 'Automotive'
when 'Bath', 'Bedding'
#category = 'Bed & Bath'
when 'Books & Media', 'Canada', 'Gifts', 'Miscellaneous', 'Recreational'
#category = 'Miscellaneous'
when 'Cafe, Bakery & Treats', 'Kitchen'
#category = 'Kitchen'
when 'Casual Restaurants', 'Cleaning Services', 'Dance Classes', 'Fast Food', 'Fine Dining', 'Home Services', 'Local Exhibits & Shows', 'Other Services', 'Other Workshops & Classes', 'Outdoor Adventures', 'Other Activities', 'Tours & Sightseeing', 'Fitness Classes', 'Massage', 'Med Spa', 'Salon & Hair Care Services', 'Spa Services', 'Yoga, Pilates & Aerobics'
#category = 'Experiences'
when 'Cats', 'Dogs', 'Pets', 'Pets Accessories'
#category = 'Pets Accessories'
when 'Clothing', 'Clothing, Fashion & Accessories', 'Fashion & Accessories', 'Footwear', "Men's", 'Unisex', "Women's"
#category = 'Apparel & Footwear'
when 'Earrings', 'Fashion Accessories'
#category = 'Jewellery & Accessories'
when 'Fitness', 'Hair Care', 'Hair Removal', 'Health Care', 'Manicure & Pedicure', 'Other Beauty & Spa', 'Other Health & Fitness', 'Personal Care', 'Teeth Whitening', 'Wellness & Nutrition', 'Facial'
#category = 'Health & Beauty'
when 'Golf', 'Sports', 'Football', 'Baseball', 'Hockey', 'Ice Hockey'
#category = 'Sporting Accessories'
else
#category
end
Product.create(
:name => u.ProductName,
:link => u.ProductLink,
:image_url => u.ImageUrl,
:price => u.UnitPrice,
:interest => u.UnitSold,
:start_date => u.StartDate,
:end_date => u.EndDate,
:revenue => u.Revenue,
:company => u.Company,
:category => #category,
:country => u.Country,
:price_eur => eur_currency_conversion,
:price_aud => aud_currency_conversion,
:price_gbp => gbp_currency_conversion,
:price_cad => cad_currency_conversion,
:price_nzd => nzd_currency_conversion,
:revenue_eur => eur_revenue_currency_conversion,
:revenue_aud => aud_revenue_currency_conversion,
:revenue_gbp => gbp_revenue_currency_conversion,
:revenue_cad => cad_revenue_currency_conversion,
:revenue_nzd => nzd_revenue_currency_conversion,
:status => average_change_interest,
:legacy_id => u.id)
end
end
puts "Successfully imported data from def"
end
This indeed look repetitive, why can't you come up with a Structure like this and not put everything in below format
.
├── Rakefile
└── models
├── import.rb
└── old_db.rb
I did similar kind of thing, for one of my project where i am extracting data from a DB, you can also refer to it here https://github.com/ankit8898/sql-server-mapper

Error "No Method" when using delayed job

I'm trying to import 90k lines of xml into my ruby app. herokus timeout limit is 30s so i'm trying to use delayed job.
The import class works wonderfully in around 48-hippopotomuses locally. When i add the line
handle_asynchronously :save_races
I get the error "undefined method save_races' for classXmltube'"
What am i doing wrong with DJ and how can i get this to work?
Full class code below
require "rexml/document"
class Xmltube
def self.convert_save(xml_data)
doc = REXML::Document.new xml_data.read
doc.elements.each("Meeting") do |meeting_element|
meeting = save_meeting(meeting_element)
save_races(meeting_element, meeting)
Rails.logger.info "all done"
end
end
def self.save_races(meeting_element, meeting)
meeting_element.elements.each("Races/Race") do |race_element|
race = save_race(race_element, meeting)
save_race_entrants(race_element, race)
end
end
def self.save_race_entrants(race_element, race)
race_element.elements.each("RaceEntries/RaceEntry") do |entry_element|
horse = save_horse(entry_element)
jockey = save_jockey(entry_element)
start = save_start(entry_element, horse, jockey, race)
save_sumaries(entry_element, start)
end
end
def self.save_track(meeting_element)
# there is only one track, but still, each? wtf.
t = {}
meeting_element.elements.each("Track") do |track|
t = {
:name => track.attributes["VenueName"],
:track_code => track.attributes["VenueCode"],
:condition => track.elements['TrackRating'].text,
:club_id => save_club(meeting_element.elements["Club"]).id
}
end
track = Track.where(:track_code => t[:track_code] ).first
if track
Track.update(track.id, t)
else
Track.create(t)
end
end
def self.save_meeting meeting_element
t = {
:meet_code => meeting_element.attributes['MeetCode'],
:stage => meeting_element.elements["MeetingStage"].text,
:phase => meeting_element.elements["MeetingPhase"].text,
:nominations_close_at => meeting_element.elements["NominationsClose"].text,
:acceptance_close_at => meeting_element.elements["AcceptanceClose"].text,
:riders_close_at => meeting_element.elements["RidersClose"].text,
:weights_published_at => meeting_element.elements["WeightsPublishing"].text,
:club_id => save_club(meeting_element.elements["Club"]).id ,
:track_id => save_track(meeting_element).id,
:tab_status => meeting_element.elements["TabStatus"].text,
:state => meeting_element.elements["StateDesc"].text,
:day_night => meeting_element.elements["DayNight"].text,
:number_of_races => meeting_element.elements["NumOfRaces"].text,
:meet_date => meeting_element.elements["MeetDate"].text,
}
meeting = Meeting.where(:meet_code => t[:meet_code] ).first
if meeting
Meeting.update(meeting.id, t)
else
Meeting.create(t)
end
end
############################################################
def self.save_sumaries entry_element, start
entry_element.elements.each('Form/ResultsSummaries/ResultsSummary') do | element |
s = {
:name => element.attributes['Name'],
:start_id => start.id,
:starts => element.attributes['Starts'],
:wins => element.attributes['Wins'],
:seconds => element.attributes['Seconds'],
:thirds => element.attributes['Thirds'],
:prize_money => element.attributes['PrizeMoney'],
}
sum = Summary.where(:name => s[:name] ).where(:start_id => s[:start_id]).first
if sum
Summary.update(sum.id, s)
else
Summary.create(s)
end
end
end
def self.save_start entry_element, horse, jockey, race
s = {
:horse_id => horse.id,
:jockey_id => jockey.id,
:race_id => race.id,
:silk => entry_element.elements["JockeySilksImage"].attributes["FileName_NoExt"],
:start_code => entry_element.attributes['RaceEntryCode'],
:handicap_weight => entry_element.elements['HandicapWeight'].text,
}
# Rails.logger.info entry_element['HandicapWeight'].text
start = Start.where(:start_code => s[:start_code] ).first
if start
Start.update(start.id, s)
else
Start.create(s)
end
end
def self.save_jockey entry_element
j={
:name => entry_element.elements['JockeyRaceEntry/Name'].text,
:jockey_code => entry_element.elements['JockeyRaceEntry'].attributes["JockeyCode"],
}
jockey = Jockey.where(:jockey_code => j[:jockey_code] ).first
if jockey
Jockey.update(jockey.id, j)
else
Jockey.create(j)
end
end
def self.save_horse entry_element
trainer = save_trainer entry_element
h= {
:name => entry_element.elements['Horse'].attributes["HorseName"],
:color => entry_element.elements['Horse'].attributes["Colour"],
:dob => entry_element.elements['Horse'].attributes["FoalDate"],
:sex => entry_element.elements['Horse'].attributes["Sex"],
:trainer_id => trainer.id,
:horse_code => entry_element.elements['Horse'].attributes["HorseCode"],
}
horse = Horse.where(:horse_code => h[:horse_code] ).first
if horse
Horse.update(horse.id, h)
else
Horse.create(h)
end
end
def self.save_trainer entry_element
t= {
:name => entry_element.elements['Trainer/Name'].text,
:trainer_code => entry_element.elements['Trainer'].attributes["TrainerCode"]
}
trainer = Trainer.where(:trainer_code => t[:trainer_code] ).first
if trainer
Trainer.update(trainer.id, t)
else
Trainer.create(t)
end
end
def self.save_club element
t = {}
t = {
:club_code => element.attributes['ClubCode'],
:title => element.attributes["Title"],
}
club = Club.where(:club_code => t[:club_code] ).first
if club
Club.update(club.id, t)
else
Club.create(t)
end
end
def self.save_race element, meeting
r = {
:name => element.elements['NameRaceFull'].text,
:occur => element.elements['RaceStartTime'].attributes["TimeAtVenue"],
:distance => element.elements['RaceDistance'].text,
:race_type => element.elements['RaceType'].text,
:track_id => meeting.track_id,
:race_code => element.attributes["RaceCode"],
:meeting_id => meeting.id
}
race = Race.where(:race_code => r[:race_code] ).first
if race
Race.update(race.id, r)
else
Race.create(r)
end
end
handle_asynchronously :save_races
end
Since your save_races is a class method, you should call handle_asynchronously on Xmltube's singleton class:
class << self
handle_asynchronously :save_races
end
This just worked as I would expect
class Foo
def self.bar(s)
Rails.logger.info "From Foo.bar('#{s}')"
end
end
# then ...
Foo.delay.bar('hello')
I was running 4.0.4 of DJ with ruby 2.1

Ruby On Rails Mail Routine Not sending

I Have a ROR IF statement that is supposed to look up a record and then send emails based on the login can anyone tell me what is wrong in the syntax?
this is now working but there is two test cases that return ActiveRecord::RecordNotFound (Couldn't find SignOn with userw1 = xxx#example.com):
app/models/order.rb:219:in `submit_order'
class Order < ActiveRecord::Base
# NOTE: ensure that you always setup methods here to include and filter on
# query_account_number as a security measure.
establish_connection "web_#{RAILS_ENV}"
has_many :order_details, :dependent => :destroy, :order => 'id DESC'
def self.delete_item query_account_number, login, id
order = Order.first(:conditions => {:account_number => query_account_number, :login => login})
detail = order.order_details.first(:conditions => {:id => id})
detail.destroy if detail
# Here we would delete the order, but have chosen to leave it so they do not have
# questionable gaps in IDs.
end
def self.verify_upc1 upc_rule, upc1
"Invalid UPC Prefix" unless Item.first(:conditions => ["rul31c = ? AND CONCAT(TRIM(comi1c), 'A') = ?", upc_rule, upc1 + "A"],
:select => "style")
end
def self.verify_upc2 upc_rule, upc1, upc2
"Invalid UPC Prefix or Subnumber" unless Item.first(:conditions => ["rul31c = ? AND CONCAT(TRIM(comi1c), CONCAT(TRIM(arsb1c), 'A')) = ?", upc_rule, upc1 + upc2 + "A"],
:select => "style")
end
def self.verify_style upc_rule, style
"Invalid Style" unless Item.first(:conditions => ["rul31c = ? AND CONCAT(TRIM(style), 'A') = ?", upc_rule, style + "A"],
:select => "style")
end
def self.update_quantity query_account_number, login, id, quantity
if quantity.to_i > 0
order = Order.first(:conditions => {:account_number => query_account_number, :login => login})
detail = order.order_details.first(:conditions => {:id => id})
if detail
detail.quantity = quantity.to_i
detail.save
end
end
end
def self.update_order_note query_account_number, login, order_note
order = Order.first(:conditions => {:account_number => query_account_number, :login => login})
order.note = order_note
order.save
end
def self.update_order_item_note query_account_number, login, id, note
order = Order.first(:conditions => {:account_number => query_account_number, :login => login})
detail = order.order_details.first(:conditions => {:id => id})
if detail
detail.note = note
detail.save
end
end
def self.add_item query_account_number, login, quantity, upc1, upc2, price_code, upc_rule
if quantity.to_i > 0
order = Order.first(:conditions => {:account_number => query_account_number, :login => login})
order = Order.add_order_header(query_account_number, login) if order.blank?
if order
item = Item.first(:conditions => ["rul31c = ? AND CONCAT(TRIM(comi1c), CONCAT(TRIM(arsb1c), 'A')) = ?", upc_rule, upc1 + upc2 + "A"],
:select => "style, color, size, pnum35, cono35, comi1c, arsb1c, ptyp35")
price = Price.first(:conditions => {:catnwp => item.pnum35, :conowp => item.cono35, :listwp => price_code}, :select => "prcewp") unless item.blank?
if item and price
order_detail = OrderDetail.new(:style => item.style,
:order_id => order.id,
:quantity => quantity,
:color => item.color,
:size => item.size)
order_detail.save
# Return a cart item hash
cart_contents = []
cart_contents << {:style => item.style,
:color => item.color,
:size => item.size,
:quantity => quantity,
:name => ProItem.short_description(item.style),
:price => price.prcewp,
:type => item.ptyp35,
:upc => item.comi1c + item.arsb1c,
:id => order_detail.id}
end
end
end
end
def self.order_info_update query_account_number, login, info
order = Order.first(:conditions => {:account_number => query_account_number, :login => login})
if order
if info.has_key?("name")
order.name = info[:name]
order.address1 = info[:address1]
order.address2 = info[:address2]
order.address3 = info[:address3]
order.city = info[:city]
order.state = info[:state]
order.zip = info[:zip]
end
if info.has_key?("po_number")
order.po_number = info[:po_number]
end
order.save
# where does po number go???
end
end
def self.mailing_address query_account_number, login
o = Order.first(:conditions => {:account_number => query_account_number, :login => login})
{:name => o.name, :address1 => o.address1, :address2 => o.address2, :address3 => o.address3, :city => o.city, :state => o.state, :zip => o.zip} if o
end
def self.order_note query_account_number, login
o = Order.first(:conditions => {:account_number => query_account_number, :login => login}, :select => "note")
o.note if o
end
def self.cart_contents query_account_number, login, price_code, upc_rule
# TODO: what to do if item changed since they put it in cart?
# Perhaps we expire a cart after a while, but, that still is not a perfect solution.
# Ideally here we go through each style, make sure it is still valid for the customer, and
# check color/size as well.
order_items = Order.first(:conditions => {:account_number => query_account_number, :login => login} )
if order_items
cart_contents = []
order_items.order_details.each do |oi|
as400_item = Item.first(:conditions => {:rul31c => upc_rule, :style => oi.style.ljust(9), :color => oi.color, :size => oi.size },
:select => "pnum35, cono35, ptyp35, comi1c, arsb1c")
price = Price.first(:conditions => {:catnwp => as400_item.pnum35, :conowp => as400_item.cono35, :listwp => price_code}, :select => "prcewp") unless as400_item.blank?
cart_contents << {:style => oi.style,
:color => oi.color,
:size => oi.size,
:quantity => oi.quantity,
:name => ProItem.short_description(oi.style),
:price => price.prcewp,
:type => as400_item.ptyp35,
:note => oi.note,
:upc => as400_item.comi1c + as400_item.arsb1c,
:id => oi.id} if as400_item and price
end unless order_items.blank?
cart_contents
end
end
def self.cart_contents_count query_account_number, login
order_items = Order.first(:conditions => {:account_number => query_account_number, :login => login} )
count = 0
order_items.order_details.each {|oi| count += oi.quantity } if order_items
count
end
def self.cart_contents_total query_account_number, login, price_code, upc_rule
order_items = Order.first(:conditions => {:account_number => query_account_number, :login => login} )
total = 0
order_items.order_details.each do |oi|
as400_item = Item.first(:conditions => {:rul31c => upc_rule, :style => oi.style.ljust(9), :color => oi.color, :size => oi.size },
:select => "pnum35, cono35, ptyp35")
price = 0
if !as400_item.blank?
price = Price.first(:conditions => {:catnwp => as400_item.pnum35, :conowp => as400_item.cono35, :listwp => price_code}, :select => "prcewp")
total += (oi.quantity * price.prcewp.to_f)
end
end if order_items
total
end
def self.add_order_details query_account_number, login, items, notes = Hash.new
result = false
if items
# If there is not an order record, create one. Basically we only
# allow one open order per login at a time, and if there is
# one already, we continue to append order_detail records to it.
order = Order.first(:conditions => {:account_number => query_account_number, :login => login.upcase})
order = Order.add_order_header(query_account_number, login) if order.blank?
items.each do |key, value|
if value.to_i > 0
result = true
style = key[0..8]
color = key[9..11]
size = key[12..14]
order_detail = OrderDetail.new(:style => style,
:order_id => order.id,
:quantity => value,
:color => color,
:size => size,
:note => (notes ? notes[key] : ''))
order_detail.save
end
end if order
end
result
end
def self.submit_order query_account_number, login, price_code, distributor_number
o = Order.first(:conditions => {:account_number => query_account_number, :login => login})
# TODO: transaction or some error handling
if o
# Find all associated records we need.
customer = Customer.first(:conditions => {:cusn05 => query_account_number}, :select => "cono05, cgp405, slmn05, list20" )
sign_on = SignOn.first(:conditions => {:userw1 => login.upcase},
:select => "idnow1, bsnamw1, accntw1, prfdstw1, cntacw1, bsadd1w1, bsadd2w1, bsadd3w1, bscityw1, bsstcdw1, bszipw1, acctypw1")
distributor = Distributor.first(:conditions => {:cusnp1 => distributor_number}, :select => "cnam05, emailp1")
if sign_on.acctypw1.strip == "DS"
rec = SignOn.find_by_userw1!(login)
webid = rec.prfdstw1
elsif sign_on.acctypw1.strip == "DSD"
rec = SignOn.find_by_userw1!(login)
webid = rec.prfdstw1
end
#cnt = Weboel23.count(:all,:conditions => {:act223 => mp})
weboel23 = Weboel23.first(:conditions => {:act223 => webid}, :select => "emal23")
#cnt=weboel23.count
approval0=Weboel23.get_email_by_account0(webid)
approval1=Weboel23.get_email_by_account1(webid)
approval2=Weboel23.get_email_by_account2(webid)
approval3=Weboel23.get_email_by_account3(webid)
approval4=Weboel23.get_email_by_account4(webid)
# weboel23 = Weboel23.each(:conditions => {:act223 => mp}, :select => "emal23")
#approval = weboel23.emal23
#f=File.open("/var/www/onlineordering.coastalpet.com/log/debug3.txt")
#establish_connection "as400_#{RAILS_ENV}"
#f.puts "prfdstw1"
#f.puts rec = SignOn.find_by_userw1!(login)
#f.puts "type="+elephant
#f.puts "email="+approval0
#f.close
# s = find_by_sql ["SELECT EMAL23 emal23 FROM WEBOEL23 WHERE ACT223 = ?", distributor_number]
# d << { :id => s[0].emal23 } unless s.blank?
if customer and sign_on
as400_order_id = "WB" + o.id.to_s.rjust(8, "0")
total = 0
count = 0
line = 1 # Start at 1 per Ron Hoopes 2/14/2011
o.order_details.each do |oi|
item = Item.first(:conditions => {:rul31c => customer.cgp405, :style => oi.style.ljust(9), :color => oi.color, :size => oi.size },
:select => "comi1c, arsb1c, chdt1c, pnum35, cono35, pdes35")
price = Price.first(:conditions => {:catnwp => item.pnum35, :conowp => item.cono35, :listwp => price_code}, :select => "prcewp") unless item.blank?
if item and price
WebOrderDetail.create(:conowd => customer.cono05,
:uniqwd => as400_order_id,
:linewd => line,
:itemwd => oi.style.ljust(9) + oi.color + oi.size,
:barcwd => item.comi1c.strip + item.arsb1c.strip + item.chdt1c.strip,
:altiwd => "",
:itmdwd => item.pdes35,
:qtywd => oi.quantity,
:uprcwd => price.prcewp.to_f.round_to(2),
:listwd => customer.list20,
:dtdrwd => "0",
:dcd1wd => "",
:dsc1wd => "0",
:dcd2wd => "",
:dsc2wd => "0")
# Create any order ITEM notes.
unless oi.note.blank?
oi.note.scan(/.{1,70}/).each_with_index do |note, index|
WebOrderNote.create(:conown => customer.cono05,
:textwn => note,
:uniqwn => as400_order_id,
:linewn => line,
:seqwn => index)
end
end
count += oi.quantity.to_i
total += oi.quantity.to_i * price.prcewp.to_f.round_to(2)
line += 1
end
end
# Create any order notes.
unless o.note.blank?
o.note.scan(/.{1,70}/).each_with_index do |note, index|
WebOrderNote.create(:conown => customer.cono05,
:textwn => note,
:uniqwn => as400_order_id,
:linewn => 0,
:seqwn => index)
end
end
# Coastal's definition a current century is off by one, hence the math for the date.
as400_order_header = WebOrder.new(:conowc => customer.cono05,
:uniqwc => as400_order_id,
:idnowc => sign_on.idnow1,
:namewc => o.name,
:addr1wc => o.address1,
:addr2wc => o.address2,
:addr3wc => o.address3,
:citywc => o.city,
:statewc => o.state,
:zipcdwc => o.zip,
:cusnwc => query_account_number,
:dtordwc => Utilities.to_db2_date(Date.today),
:shpdtwc => Utilities.to_db2_date(Date.today),
:cusowc => o.po_number.blank? ? as400_order_id : o.po_number,
:slmnwc => customer.slmn05,
:rulewc => customer.cgp405,
:listwc => customer.list20,
:valuewc => total.to_f.round_to(2),
:statwc => "")
if as400_order_header.save
# Sent confirmation email, if required. Only send it for users that chose a distributor,
# whether they had to choose one or not. We determine this by looking at the account number
# in the sign_on and comparing it with the order number we are using for the order. If
# that is different, it indicates that they chose a distributor.
# These are used in the email body, key for the hash is the replacement tags, capitalized, &
# prepended and appended with '##', e.g. ##NAME## - and any underlines are replaced with space
email_details = {"name" => sign_on.bsnamw1,
"order_number" => as400_order_id,
"order_total" => format('$%.2f', total),
"order_count" => count.to_s,
"order_date" => Date.today.to_s,
"distributor_name" => (distributor ? distributor.cnam05 : ""),
"distributor_contact" => "UNKNOWN",
"customer_name" => sign_on.bsnamw1,
"customer_address" => sign_on.bsadd1w1.strip + (!sign_on.bsadd2w1.blank? ? "<br>" + sign_on.bsadd2w1.strip : "") + (!sign_on.bsadd3w1.blank? ? "<br>" + sign_on.bsadd3w1.strip : ""),
"customer_city" => sign_on.bscityw1,
"customer_state" => sign_on.bsstcdw1,
"customer_zip" => sign_on.bszipw1,
"customer_contact" => sign_on.cntacw1}
Mailer.deliver_order_coastal_notify_email("Coastal Pet Online Ordering<noreply#coastalpet.com>", "Order Confirmation", email_details)
# Per Dana 6/30/2012, JUST DS
if sign_on.acctypw1.strip == "DS" or sign_on.acctypw1.strip == "DSD"
# If there is no distributor email address, the mailer model will substitute in the admin's email from their settings
Mailer.deliver_order_distributor_approval_email(distributor ? distributor.emailp1 : "", "Coastal Pet Online Ordering<noreply#coastalpet.com>", "Order Confirmation #{approval0} #{approval1}", email_details)
if approval0!=""
Mailer.deliver_order_confirmation_email(login, "Coastal Pet Online Ordering<noreply#coastalpet.com>", "Order Confirmation", email_details)
Mailer.deliver_order_distributor_approval_email(approval0, "Coastal Pet Online Ordering<noreply#coastalpet.com>", "Order Confirmation ", email_details)
Mailer.deliver_order_coastal_notify_email("Coastal Pet Online Ordering<noreply#coastalpet.com>", "Order Confirmation", email_details)
end
if approval1!=""
Mailer.deliver_order_confirmation_email(login, "Coastal Pet Online Ordering<noreply#coastalpet.com>", "Order Confirmation", email_details)
Mailer.deliver_order_distributor_approval_email(approval1, "Coastal Pet Online Ordering<noreply#coastalpet.com>", "Order Confirmation ", email_details)
Mailer.deliver_order_coastal_notify_email("Coastal Pet Online Ordering<noreply#coastalpet.com>", "Order Confirmation", email_details)
end
if approval2!=""
Mailer.deliver_order_confirmation_email(login, "Coastal Pet Online Ordering<noreply#coastalpet.com>", "Order Confirmation", email_details)
Mailer.deliver_order_distributor_approval_email(approval2, "Coastal Pet Online Ordering<noreply#coastalpet.com>", "Order Confirmation ", email_details)
Mailer.deliver_order_coastal_notify_email("Coastal Pet Online Ordering<noreply#coastalpet.com>", "Order Confirmation", email_details)
end
if approval3!=""
Mailer.deliver_order_confirmation_email(login, "Coastal Pet Online Ordering<noreply#coastalpet.com>", "Order Confirmation", email_details)
Mailer.deliver_order_distributor_approval_email(approval3, "Coastal Pet Online Ordering<noreply#coastalpet.com>", "Order Confirmation ", email_details)
Mailer.deliver_order_coastal_notify_email("Coastal Pet Online Ordering<noreply#coastalpet.com>", "Order Confirmation", email_details)
end
if approval4!=""
Mailer.deliver_order_confirmation_email(login, "Coastal Pet Online Ordering<noreply#coastalpet.com>", "Order Confirmation", email_details)
Mailer.deliver_order_distributor_approval_email(approval4, "Coastal Pet Online Ordering<noreply#coastalpet.com>", "Order Confirmation ", email_details)
Mailer.deliver_order_coastal_notify_email("Coastal Pet Online Ordering<noreply#coastalpet.com>", "Order Confirmation", email_details)
end
Mailer.deliver_order_distributor_approval_email(weboel23 ? weboel23.emal23: "", "Coastal Pet Online Ordering<noreply#coastalpet.com>", "*Order Confirmation", email_details)
Mailer.deliver_order_distributor_approval_email("robert.kendall#coastalpet.com", "Coastal Pet Online Ordering<noreply#coastalpet.com>", "*Order Confirmation", email_details)
end
# Al ways notify Coastal staff
# Update the last ordered date (used for reminders)
User.update_last_order_date(login)
o.destroy
as400_order_id
end
end
end
end
def self.count_user_orders query_account_number, login
count = Order.first(:conditions => {:account_number => query_account_number, :login => login})
count.order_details.count unless count.blank? or count == 0
end
def self.reorder(order_number, login, query_account_number, upc_rule, price_code, mode)
warnings = ""
# If we are not in check mode, then blank out cart
self.empty_cart(query_account_number, login) if mode != "check"
# Get the previous order
oo = WebOrder.get_previous_order(order_number, login)
# Go through each line item
oo.each do |ooi|
# Verify it still exists in the AS400 and is same price
# Note that this reports back a -1 if it cannot find the
# same style+color+size option, otherwise just reports
# price if it finds it.
current_price = Item.check_item_exists(query_account_number, ooi[:style], ooi[:color], ooi[:size], upc_rule, price_code)
# If item was not found, we can't add it.
# We could spend more time to determine if it was the color, or size, or that
# the style itself is not available, but it seems reasonable enough to
# present it this way. Really should not be doing this "presentation" in
# the model, but we don't have views setup for Ajax so we have to give the
# controller the text we want rendered.
if current_price == -1
warnings += "SKU "+ ooi[:style].to_s + ooi[:color].to_s + ooi[:size].to_s + " is no longer available<br />"
else
# If price did not match, append to list of warnings
if current_price.to_f.round_to(2) != ooi[:price].to_f.round_to(2)
warnings += "SKU "+ ooi[:style].to_s + ooi[:color].to_s + ooi[:size].to_s + " price is " +
ActionController::Base.helpers.number_to_currency(current_price) + " and it was " +
ActionController::Base.helpers.number_to_currency(ooi[:price]) + "<br />"
end
# If not in check mode, add item to cart
# In this case, we don't care that the price did
# not match, we still add it.
populate_cart(query_account_number, login, ooi[:style], ooi[:color], ooi[:size], ooi[:quantity], ooi[:line_item_note], ooi[:order_note]) if mode != "check"
end
end
mode == "check" ? warnings : ""
end
def self.empty_cart query_account_number, login
order = Order.first(:conditions => {:account_number => query_account_number, :login => login})
order.destroy if order
end
def self.populate_cart query_account_number, login, style, color, size, quantity, note, order_note
# If there is not an order record, create one. Basically we only
# allow one open order per login at a time, and if there is
# one already, we continue to append order_detail records to it.
order = Order.first(:conditions => {:account_number => query_account_number, :login => login.upcase})
order = Order.add_order_header(query_account_number, login) if order.blank?
# These can be null for kits, and we don't like nulls in the database for this
color = "" if color.blank?
size = "" if size.blank?
if order
order.note = order_note # yeah we do this each time but while inefficient it works
order.save
order_detail = OrderDetail.new(:style => style,
:order_id => order.id,
:quantity => quantity,
:color => color,
:size => size,
:note => note)
order_detail.save
end
end
private
def self.add_order_header query_account_number, login
# Default address info to sign_on
sign_on = SignOn.first(:conditions => {:userw1 => login.upcase}, :select => "bsnamw1, bsadd1w1, bsadd2w1, bsadd3w1, bscityw1, bsstcdw1, bszipw1")
if sign_on
order = Order.new(:login => login,
:account_number => query_account_number,
:name => sign_on.bsnamw1,
:address1 => sign_on.bsadd1w1,
:address2 => sign_on.bsadd2w1,
:address3 => sign_on.bsadd3w1,
:city => sign_on.bscityw1,
:state => sign_on.bsstcdw1,
:zip => sign_on.bszipw1)
order.save
order
end
end
end

How to get gateway response from model into controller - Ruby on Rails

My application uses activemerchant to process payments. I'm using Eway as my payment gateway. I'm storing credit card details with Eway to keep them out of my application database.
I'm using a method store which returns a response with a customer billing id that I can use at a later time to process the order.
http://rdoc.info/github/Shopify/active_merchant/master/ActiveMerchant/Billing/EwayManagedGateway
My main issue is how do I get the response value into my controller so I can save it to the member model.
I've created a simple ruby file to test this all works and it does. I just need to convert this code to work inside my rails app.
require "rubygems"
gem 'activemerchant', '1.15.0'
require 'activemerchant'
ActiveMerchant::Billing::Base.mode = :production
gateway = ActiveMerchant::Billing::EwayManagedGateway.new(
:login => '12345678',
:username => 'mylogin#example.com',
:password => 'mypassword'
)
credit_card = ActiveMerchant::Billing::CreditCard.new(
:type => "visa",
:number => "4444333322221111",
:verification_value => "123",
:month => "11",
:year => "2011",
:first_name => "John",
:last_name => "Citizen"
)
options = {
:order_id => '1230123',
:ip => "127.0.0.1",
:email => 'john.citizen#example.com',
:billing_address => { :title => "Mr.",
:address1 => '123 Sample Street',
:city => 'Sampleville',
:state => 'NSW',
:country => 'AU',
:zip => '2000'
},
:description => 'purchased items'
}
if credit_card.valid?
response = gateway.store(credit_card, options)
if response.success?
puts "Credit Card Stored. #{response.message}"
customer = response.params['CreateCustomerResult']
puts "Customer Id: #{customer}"
else
puts "Error: #{response.message}"
end
else
puts "Error, credit card is not valid. #{credit_card.errors.full_messages.join('. ')}"
end
Here is the relevant code in my order model.
serialize :params
cattr_accessor :gateway
def response=(response)
self.success = response.success?
self.message = response.message
self.params = response.params
self.billing_id = response.params['CreateCustomerResult']
rescue ActiveMerchant::ActiveMerchantError => e
self.success = false
self.message = e.message
self.params = {}
self.billing_id = nil
end
def store
response = Order.gateway.store(credit_card, options)
end
Here is my order controller create code.
def create
#member = current_member
#order_deal = Deal.find(params[:deal_id])
#order = #order_deal.orders.build(params[:order])
#order.member_id = current_member.id
#order.ip_address = request.remote_ip
#deal = #order_deal
if #order.save
if #order.store
render :action => "success"
flash[:notice] = "Successfully processed your order."
else
render :action => "new"
end
else
render :action => 'new'
end
end
So essentially I want to get the
response.params['CreateCustomerResult']
and add it to my member model under
member.billing_id = response.params['CreateCustomerResult]
How can I do this?

Resources