erp_commerce 4.0.0 → 4.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (49) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +0 -1
  3. data/app/models/accepted_credit_card.rb +10 -0
  4. data/app/models/bank_account.rb +13 -0
  5. data/app/models/bank_account_type.rb +9 -0
  6. data/app/models/credit_card.rb +54 -19
  7. data/app/models/credit_card_account.rb +31 -9
  8. data/app/models/credit_card_account_party_role.rb +17 -0
  9. data/app/models/credit_card_account_purpose.rb +24 -0
  10. data/app/models/extensions/financial_txn.rb +62 -1
  11. data/app/models/extensions/order_txn.rb +48 -0
  12. data/app/models/extensions/party.rb +26 -13
  13. data/app/models/extensions/simple_product_offer.rb +3 -0
  14. data/app/models/payment.rb +21 -0
  15. data/app/models/payment_gateway.rb +12 -0
  16. data/app/models/payment_gateway_action.rb +12 -0
  17. data/app/models/pricing_plan.rb +29 -1
  18. data/db/data_migrations/20150125202814_add_payment_transaction_types.rb +14 -0
  19. data/db/migrate/20100823174238_erp_commerce_base.rb +196 -192
  20. data/db/migrate/20160310163057_add_created_by_updated_by_to_erp_commerce.rb +35 -0
  21. data/lib/erp_commerce.rb +1 -2
  22. data/lib/erp_commerce/config.rb +2 -3
  23. data/lib/erp_commerce/engine.rb +1 -5
  24. data/lib/erp_commerce/extensions/active_record/acts_as_priceable.rb +1 -2
  25. data/lib/erp_commerce/version.rb +1 -1
  26. metadata +12 -32
  27. data/app/mailers/checkout_mailer.rb +0 -9
  28. data/app/views/checkout_mailer/email_confirmation.html.erb +0 -15
  29. data/app/widgets/orders/base.rb +0 -36
  30. data/app/widgets/orders/javascript/orders.js +0 -12
  31. data/app/widgets/orders/views/index.html.erb +0 -63
  32. data/app/widgets/product_catalog/base.rb +0 -52
  33. data/app/widgets/product_catalog/javascript/product_catalog.js +0 -64
  34. data/app/widgets/product_catalog/views/add_to_cart.html.erb +0 -10
  35. data/app/widgets/product_catalog/views/index.html.erb +0 -69
  36. data/app/widgets/product_catalog/views/show.html.erb +0 -23
  37. data/app/widgets/shopping_cart/base.rb +0 -175
  38. data/app/widgets/shopping_cart/javascript/shopping_cart.js +0 -110
  39. data/app/widgets/shopping_cart/views/cart_items.html.erb +0 -83
  40. data/app/widgets/shopping_cart/views/confirmation.html.erb +0 -11
  41. data/app/widgets/shopping_cart/views/demographics.html.erb +0 -142
  42. data/app/widgets/shopping_cart/views/login.html.erb +0 -1
  43. data/app/widgets/shopping_cart/views/payment.html.erb +0 -81
  44. data/app/widgets/shopping_cart/views/price_summary.html.erb +0 -4
  45. data/lib/erp_commerce/active_merchant_wrappers.rb +0 -3
  46. data/lib/erp_commerce/active_merchant_wrappers/authorize_net_wrapper.rb +0 -66
  47. data/lib/erp_commerce/active_merchant_wrappers/brain_tree_gateway_wrapper.rb +0 -68
  48. data/lib/erp_commerce/active_merchant_wrappers/credit_card_validation.rb +0 -34
  49. data/lib/erp_commerce/order_helper.rb +0 -245
@@ -1,11 +0,0 @@
1
- <div id="<%=widget_result_id%>">
2
- <p>Your order is complete.
3
- <br/>Your should receive a confirmation email.
4
- <br/>Confirmation Number <%=@payment.authorization_code%>
5
- <br/>Your order number is <%=@order.order_number%>
6
- </p>
7
-
8
- </div>
9
- <script type="text/javascript">
10
- Compass.ErpApp.Widgets.refreshWidget('shopping_cart', 'price_summary');
11
- </script>
@@ -1,142 +0,0 @@
1
- <div id="<%=widget_result_id%>">
2
- <h2>What you're buying...</h2>
3
- <table id="products-view">
4
- <thead>
5
- <tr>
6
- <th>Product</th>
7
- <th>Description</th>
8
- <th>Quantity</th>
9
- <th>Total</th>
10
- <th>Remove</th>
11
- </tr>
12
- </thead>
13
- <tbody>
14
- <% @order.order_line_items.each do |order_line_item| %>
15
- <tr class="product-wrap">
16
- <td>
17
- <div><img src="<%=order_line_item.product_type.images.empty? ? '/images/img_blank.png' : order_line_item.product_type.images.first.data.url%>" alt="Product Image" /></div>
18
- <div>
19
- <span><%= raw order_line_item.product_type.description %></span><br/>
20
- </div>
21
- </td>
22
- <td><%= raw order_line_item.product_type.descriptions.find_by_internal_identifier('long_description').description%></td>
23
- <td>1</td>
24
- <td><%=link_to 'Remove', build_widget_url(:remove_from_cart, order_line_item.id), :remote => true, :class => 'ajax_replace'%></td>
25
- <td><span><%=order_line_item.product_type.get_current_simple_amount_with_currency.nil? ? 'no price set' : order_line_item.product_type.get_current_simple_amount_with_currency%></span></td>
26
- </tr>
27
- <%end%>
28
- </tbody>
29
- </table>
30
- <hr/>
31
- <div style="text-align:right;"><h2>Total <%=@price%></h2></div>
32
- <hr/>
33
-
34
- <div>
35
- <h2>Step 1 of 3 Billing Information</h2>
36
- <div style="color:red">
37
- <%= raw @billing_error_message%>
38
- </div>
39
- <%= form_remote_tag build_widget_url(:checkout_payment), :id => 'demographics_form' do %>
40
- <div class="form_settings">
41
- <p>
42
- <span>First Name</span>
43
- <%= text_field_tag :bill_to_first_name, params[:bill_to_first_name] %>
44
- </p>
45
- <p>
46
- <span>Last Name</span>
47
- <%= text_field_tag :bill_to_last_name, params[:bill_to_last_name] %>
48
- </p>
49
- <p>
50
- <span>Address Line 1</span>
51
- <%= text_field_tag :bill_to_address_line_1, params[:bill_to_address_line_1] %>
52
- </p>
53
- <p>
54
- <span>Address Line 2</span>
55
- <%= text_field_tag :bill_to_address_line_2, params[:bill_to_address_line_2] %>
56
- </p>
57
- <p>
58
- <span>City</span>
59
- <%= text_field_tag :bill_to_city, params[:bill_to_city] %>
60
- </p>
61
- <p>
62
- <span>State</span>
63
- <%= select_tag(:bill_to_state, options_for_select(GeoZone.all.collect{|item| [item.zone_name,item.zone_code]}, params[:bill_to_state])) %>
64
- </p>
65
- <p>
66
- <span>Country</span>
67
- <%= select_tag(:bill_to_country, options_for_select(GeoCountry.find_all_by_display(true).collect{|item| [item.name,item.iso_code_2]}, params[:bill_to_country])) %>
68
- </p>
69
- <p>
70
- <span>Postal/Zip Code</span>
71
- <%= text_field_tag :bill_to_postal_code, params[:bill_to_postal_code] %>
72
- </p>
73
- <p>
74
- <span>Email</span>
75
- <%= text_field_tag :bill_to_email, params[:bill_to_email] %>
76
- </p>
77
- <p>
78
- <span>Phone Number</span>
79
- <%= text_field_tag :bill_to_phone, params[:bill_to_phone] %>
80
- </p>
81
- <br/>
82
- <input type="checkbox" name="ship_to_billing" style="width:10px;" <%= if params[:ship_to_billing] == 'on' then raw('checked="checked"') end%> onclick="
83
- if(this.checked){
84
- Ext.get('shippingToBillingMessage').setStyle('display','');
85
- Ext.get('shippingInfoDiv').setStyle('display','none');
86
- Ext.get('shipToErrorMessages').setStyle('display','none');
87
- }
88
- else{
89
- Ext.get('shippingInfoDiv').setStyle('display','');
90
- Ext.get('shipToErrorMessages').setStyle('display','');
91
- Ext.get('shippingToBillingMessage').setStyle('display','none');
92
- }"/>&nbsp;Ship items to the above billing address
93
- <br/>
94
- <div class="form_settings">
95
- <h2>Step 2 of 3 Shipping Information</h2>
96
- <div style="color:red" id="shipToErrorMessages">
97
- <%= raw @shipping_error_message%>
98
- </div>
99
- <p id="shippingToBillingMessage" <%= if params[:ship_to_billing].blank? then raw('style="display:none;"') end%>>Item(s) will be shipped to your billing address.</p>
100
- <div id="shippingInfoDiv" <%= if params[:ship_to_billing] == 'on' then raw('style="display:none;"') end%>>
101
- <p>
102
- <span>First Name</span>
103
- <%= text_field_tag :ship_to_first_name, params[:ship_to_first_name] %>
104
- </p>
105
- <p>
106
- <span>Last Name</span>
107
- <%= text_field_tag :ship_to_last_name, params[:ship_to_last_name] %>
108
- </p>
109
- <p>
110
- <span>Address Line 1</span>
111
- <%= text_field_tag :ship_to_address_line_1, params[:ship_to_address_line_1] %>
112
- </p>
113
- <p>
114
- <span>Address Line 2</span>
115
- <%= text_field_tag :ship_to_address_line_2, params[:ship_to_address_line_2] %>
116
- </p>
117
- <p>
118
- <span>City</span>
119
- <%= text_field_tag :ship_to_city, params[:ship_to_city] %>
120
- </p>
121
- <p>
122
- <span>State</span>
123
- <%= select_tag(:ship_to_state, options_for_select(GeoZone.all.collect{|item| [item.zone_name,item.zone_code]}, params[:ship_to_state])) %>
124
- </p>
125
- <p>
126
- <span>Country</span>
127
- <%= select_tag(:ship_to_country, options_for_select(GeoCountry.find_all_by_display(true).collect{|item| [item.name,item.iso_code_2]}, params[:ship_to_country])) %>
128
- </p>
129
- <p>
130
- <span>Postal/Zip Code</span>
131
- <%= text_field_tag :ship_to_postal_code, params[:ship_to_postal_code] %>
132
- </p>
133
- </div>
134
- </div>
135
- <hr/>
136
- <p style="padding-top: 15px">
137
- <%= submit_tag "Continue", :disable_with => "Please wait...", :class => "submit", :style => 'margin:0px;' %> | <a href="<%=@products_url%>">Back to store</a>
138
- </p>
139
- </div>
140
- <%end%>
141
- </div>
142
- </div>
@@ -1 +0,0 @@
1
- <script type="text/javascript">window.location.href = '/login'</script>
@@ -1,81 +0,0 @@
1
- <div id="<%=widget_result_id%>">
2
- <h2>What you're buying...</h2>
3
- <table id="products-view">
4
- <thead>
5
- <tr>
6
- <th>Product</th>
7
- <th>Description</th>
8
- <th>Quantity</th>
9
- <th>Total</th>
10
- <th>Remove</th>
11
- </tr>
12
- </thead>
13
- <tbody>
14
- <% @order.order_line_items.each do |order_line_item| %>
15
- <tr class="product-wrap">
16
- <td>
17
- <div><img src="<%=order_line_item.product_type.images.empty? ? '/images/img_blank.png' : order_line_item.product_type.images.first.data.url%>" alt="Product Image" /></div>
18
- <div>
19
- <span><%= raw order_line_item.product_type.description %></span><br/>
20
- </div>
21
- </td>
22
- <td><%= raw order_line_item.product_type.descriptions.find_by_internal_identifier('long_description').description%></td>
23
- <td>1</td>
24
- <td><%=link_to_remote 'Remove', build_widget_url(:remove_from_cart, order_line_item.id)%></td>
25
- <td><span><%=order_line_item.product_type.get_current_simple_amount_with_currency.nil? ? 'no price set' : order_line_item.product_type.get_current_simple_amount_with_currency%></span></td>
26
- </tr>
27
- <%end%>
28
- </tbody>
29
- </table>
30
- <hr/>
31
- <div style="text-align:right;"><h2>Total <%=@price%></h2></div>
32
- <hr/>
33
- <%= form_remote_tag build_widget_url(:checkout_finalize), :id => 'checkout_finalize_form' do %>
34
- <div class="form_settings">
35
- <h2>Step 3 of 3 Pay for it...</h2>
36
- <div id="errors" style="color:red">
37
- <%=raw @message%>
38
- </div>
39
- <br/>
40
- <p>
41
- <span>First Name</span>
42
- <%= text_field_tag :first_name, params[:first_name] %>
43
- </p>
44
- <p>
45
- <span>Last Name</span>
46
- <%= text_field_tag :last_name, params[:last_name] %>
47
- </p>
48
- <p>
49
- <span>Credit Card Number</span>
50
- <%= text_field_tag :card_number, params[:card_number] %>
51
- </p>
52
- <p>
53
- <span>Expiration Month</span>
54
- <%= select_tag(:exp_month,
55
- options_for_select([["1 - January", 1],
56
- ["2 - Febuary", 2],
57
- ["3 - March",3],
58
- ["4 - April",4],
59
- ["5 - May",5],
60
- ["6 - June",6],
61
- ["7 - July",7],
62
- ["8 - August",8],
63
- ["9 - September",9],
64
- ["10 - October",10],
65
- ["11 - November",11],
66
- ["12 - December",12]], params[:exp_month])) %>
67
- </p>
68
- <p>
69
- <span>Expiration Year</span>
70
- <%= select_tag(:exp_year, options_for_select((0..30).collect{|addition| Date.today.year + addition}, params[:exp_year])) %>
71
- </p>
72
- <p>
73
- <span>Card Security Code</span>
74
- <%= text_field_tag :cvvs, params[:cvvs] %>
75
- </p>
76
- <p style="padding-top: 15px">
77
- <%= submit_tag "Purchase", :disable_with => "Please wait...", :class => "submit", :style => 'margin:0px;' %> | <a href="<%=@products_url%>">Back to store</a>
78
- </p>
79
- </div>
80
- <%end%>
81
- </div>
@@ -1,4 +0,0 @@
1
- <a style="float:right;width:75px;" href="<%=@cart_items_url%>" id="shopping-cart-link">
2
- <span><%=@item_count%> items<br/><%=@price%></span>
3
- <img style="float:right;" src="/images/icons/shoppingcart_full/shoppingcart_full_24x24.png" alt="Check Out" />
4
- </a>
@@ -1,3 +0,0 @@
1
- Dir.glob("#{File.dirname(__FILE__)}/active_merchant_wrappers/**/*.rb").each do |file|
2
- require "erp_commerce/active_merchant_wrappers/#{File.basename(file)}"
3
- end
@@ -1,66 +0,0 @@
1
- require 'active_merchant'
2
- require 'yaml'
3
-
4
- module ErpCommerce
5
- module ActiveMerchantWrappers
6
- class AuthorizeNetWrapper
7
-
8
- def self.purchase(credit_card, amount, cvv, gateway_options={})
9
- result = {}
10
-
11
- login_information = YAML.load_file(File.join(Rails.root, 'config/authorize_net.yml')).symbolize_keys
12
-
13
- #setup gateway
14
- gateway = ActiveMerchant::Billing::AuthorizeNetGateway.new(login_information)
15
-
16
- #set credit card info
17
- credit_card_result = ActiveMerchant::Billing::CreditCard.new({
18
- :first_name => credit_card.first_name_on_card,
19
- :last_name => credit_card.last_name_on_card,
20
- :number => credit_card.private_card_number,
21
- :month => credit_card.expiration_month,
22
- :year => credit_card.expiration_year,
23
- :verification_value => cvv,
24
- :brand => ErpCommerce::ActiveMerchantWrappers::CreditCardValidation.get_card_type(credit_card.private_card_number)
25
- })
26
-
27
- if credit_card_result.valid?
28
- cents = (amount.to_d * 100)
29
- response = gateway.purchase(cents, credit_card_result, gateway_options)
30
-
31
- if response.success?
32
- result[:message] = response.message
33
- result[:payment] = Payment.new
34
- result[:payment].authorization_code = response.authorization
35
- result[:payment].success = true
36
- result[:payment].purchase
37
- else
38
- result[:message] = response.message
39
- result[:payment] = Payment.new
40
- result[:payment].success = false
41
- result[:payment].decline
42
- end
43
-
44
- gateway = PaymentGateway.create(
45
- :response => response.message,
46
- :payment_gateway_action => PaymentGatewayAction.find_by_internal_identifier('authorize')
47
- )
48
-
49
- result[:payment].payment_gateways << gateway
50
- result[:payment].save
51
- else
52
- result[:message] = "<ul>"
53
- credit_card_result.errors.full_messages.each do |current_notice_msg|
54
- result[:message] << "<li>"
55
- result[:message] << current_notice_msg
56
- result[:message] << "</li>"
57
- end
58
- result[:message] << "<ul>"
59
- end
60
-
61
- result
62
- end
63
-
64
- end #AuthorizeNetWrapper
65
- end #ActiveMerchantWrapper
66
- end #ErpCommerce
@@ -1,68 +0,0 @@
1
- require 'active_merchant'
2
-
3
- module ErpCommerce
4
- module ActiveMerchantWrappers
5
- class BrainTreeGatewayWrapper
6
-
7
- def self.purchase(credit_card, amount, cvv, gateway_options={})
8
- result = {}
9
-
10
- #this needs config options
11
- gateway_options.merge!({:login => 'demo', :password => 'password'})
12
-
13
- ActiveMerchant::Billing::Base.mode = :test
14
-
15
- #setup gateway
16
- gateway = ActiveMerchant::Billing::BraintreeGateway.new(gateway_options)
17
-
18
- #set credit card info
19
- credit_card_result = ActiveMerchant::Billing::CreditCard.new({
20
- :first_name => credit_card.first_name_on_card,
21
- :last_name => credit_card.last_name_on_card,
22
- :number => credit_card.private_card_number,
23
- :month => credit_card.expiration_month,
24
- :year => credit_card.expiration_year,
25
- :verification_value => cvv,
26
- :type => ErpCommerce::ActiveMerchantWrappers::CreditCardValidation.get_card_type(credit_card.private_card_number)
27
- })
28
-
29
- if credit_card_result.valid?
30
- cents = (amount.to_d * 100)
31
- response = gateway.purchase(cents, credit_card_result)
32
-
33
- if response.success?
34
- result[:message] = response.message
35
- result[:payment] = Payment.new
36
- result[:payment].authorization_code = response.authorization
37
- result[:payment].success = true
38
- result[:payment].purchase
39
- else
40
- result[:message] = response.message
41
- result[:payment] = Payment.new
42
- result[:payment].success = false
43
- result[:payment].decline
44
- end
45
-
46
- gateway = PaymentGateway.create(
47
- :response => response.message,
48
- :payment_gateway_action => PaymentGatewayAction.find_by_internal_identifier('authorize')
49
- )
50
-
51
- result[:payment].payment_gateways << gateway
52
- result[:payment].save
53
- else
54
- result[:message] = "<ul>"
55
- credit_card_result.errors.full_messages.each do |current_notice_msg|
56
- result[:message] << "<li>"
57
- result[:message] << current_notice_msg
58
- result[:message] << "</li>"
59
- end
60
- result[:message] << "<ul>"
61
- end
62
-
63
- result
64
- end
65
-
66
- end#BrainTreeGatewayWrapper
67
- end#ActiveMerchantWrappers
68
- end#ErpCommerce
@@ -1,34 +0,0 @@
1
- # To change this template, choose Tools | Templates
2
- # and open the template in the editor.
3
-
4
- module ErpCommerce
5
- module ActiveMerchantWrappers
6
- module CreditCardValidation
7
- VISA_PATTERN = /^4[0-9]{12}(?:[0-9]{3})?$/
8
- MASTER_CARD_PATTERN = /^5[1-5][0-9]{14}$/
9
- AMEX_PATTERN = /^3[47][0-9]{13}$'/
10
- DINERS_CLUB_PATTERN = /^3(?:0[0-5]|[68][0-9])[0-9]{11}$/
11
- DISCOVER_PATTERN = /^6(?:011|5[0-9]{2})[0-9]{12}$/
12
-
13
-
14
- def self.get_card_type(number)
15
- card_type = nil
16
-
17
- if number =~ VISA_PATTERN
18
- card_type = 'visa'
19
- elsif number =~ MASTER_CARD_PATTERN
20
- card_type = 'master'
21
- elsif number =~ AMEX_PATTERN
22
- card_type = 'american_express'
23
- elsif number =~ DINERS_CLUB_PATTERN
24
- card_type = 'diners_club'
25
- elsif number =~ DISCOVER_PATTERN
26
- card_type = 'discover'
27
- end
28
-
29
- card_type
30
- end
31
-
32
- end
33
- end
34
- end
@@ -1,245 +0,0 @@
1
- module ErpCommerce
2
- class OrderHelper
3
- attr_accessor :widget
4
- delegate :params, :session, :request, :logger, :current_user, :to => :widget
5
-
6
- def initialize(widget)
7
- self.widget = widget
8
- end
9
-
10
- def get_order(create=nil)
11
- order = nil
12
- unless session['order_txn_id'].nil?
13
- order = OrderTxn.where('id = ?',session['order_txn_id']).first
14
- order = create_order if order.nil?
15
- else
16
- order = create_order if create
17
- end
18
- order
19
- end
20
-
21
- #add a product type to cart
22
- def add_to_cart(product_type)
23
- order = get_order(true)
24
-
25
- #see if we need to update quantity or create new line item
26
- order_line_item = get_line_item_for_product_type(product_type)
27
- if order_line_item.nil? || order_line_item.empty?
28
- #create order line item
29
- order_line_item = order.add_line_item(product_type)
30
-
31
- #get pricing plan and create charge lines
32
- pricing_plan = product_type.get_current_simple_plan
33
- money = Money.create(
34
- :description => pricing_plan.description,
35
- :amount => pricing_plan.money_amount,
36
- :currency => pricing_plan.currency)
37
-
38
- charge_line = ChargeLine.create(
39
- :charged_item => order_line_item,
40
- :money => money,
41
- :description => pricing_plan.description)
42
- charge_line.save
43
- order_line_item.charge_lines << charge_line
44
- order.status = 'Items Added'
45
- order.save
46
- else
47
- #update quantity
48
- end
49
- order
50
- end
51
-
52
- #set billing information
53
- def set_demographic_info(params)
54
- order = get_order
55
-
56
- #if biz txn party roles have not been setup set them up
57
- if order.root_txn.biz_txn_party_roles.nil? || order.root_txn.biz_txn_party_roles.empty?
58
- setup_biz_txn_party_roles(order)
59
- end
60
-
61
- party = self.current_user.party
62
-
63
- #set billing information on party
64
- #get geo codes
65
- geo_country = GeoCountry.find_by_iso_code_2(params[:bill_to_country])
66
- geo_zone = GeoZone.find_by_zone_code(params[:bill_to_state])
67
- party.update_or_add_contact_with_purpose(PostalAddress, ContactPurpose.billing,
68
- {
69
- :address_line_1 => params[:bill_to_address_line_1],
70
- :address_line_2 => params[:bill_to_address_line_2],
71
- :city => params[:bill_to_city],
72
- :state => geo_zone.zone_name,
73
- :zip => params[:bill_to_postal_code],
74
- :country => geo_country.name
75
- })
76
- billing_postal_address = party.find_contact_mechanism_with_purpose(PostalAddress, ContactPurpose.billing)
77
- billing_postal_address.geo_country = geo_country
78
- billing_postal_address.geo_zone = geo_zone
79
- billing_postal_address.save
80
-
81
- #set shipping information on party
82
- #same as billing us billing info
83
- if params[:ship_to_billing] == 'on'
84
- #get geo codes
85
- geo_country = GeoCountry.find_by_iso_code_2(params[:bill_to_country])
86
- geo_zone = GeoZone.find_by_zone_code(params[:bill_to_state])
87
- party.update_or_add_contact_with_purpose(PostalAddress, ContactPurpose.shipping,
88
- {
89
- :address_line_1 => params[:bill_to_address_line_1],
90
- :address_line_2 => params[:bill_to_address_line_2],
91
- :city => params[:bill_to_city],
92
- :state => geo_zone.zone_name,
93
- :zip => params[:bill_to_postal_code],
94
- :country => geo_country.name
95
- })
96
- shipping_postal_address = party.find_contact_mechanism_with_purpose(PostalAddress, ContactPurpose.shipping)
97
- shipping_postal_address.geo_country = geo_country
98
- shipping_postal_address.geo_zone = geo_zone
99
- shipping_postal_address.save
100
- else
101
- #get geo codes
102
- geo_country = GeoCountry.find_by_iso_code_2(params[:ship_to_country])
103
- geo_zone = GeoZone.find_by_zone_code(params[:ship_to_state])
104
- party.update_or_add_contact_with_purpose(PostalAddress, ContactPurpose.shipping,
105
- {
106
- :address_line_1 => params[:ship_to_address_line_1],
107
- :address_line_2 => params[:ship_to_address_line_2],
108
- :city => params[:ship_to_city],
109
- :state => geo_zone.zone_name,
110
- :zip => params[:ship_to_postal_code],
111
- :country => geo_country.name
112
- })
113
- shipping_postal_address = party.find_contact_mechanism_with_purpose(PostalAddress, ContactPurpose.shipping)
114
- shipping_postal_address.geo_country = geo_country
115
- shipping_postal_address.geo_zone = geo_zone
116
- shipping_postal_address.save
117
- end
118
-
119
- #set phone and email
120
- party.update_or_add_contact_with_purpose(PhoneNumber, ContactPurpose.billing, {:phone_number => params[:bill_to_phone]})
121
- party.update_or_add_contact_with_purpose(EmailAddress, ContactPurpose.billing, {:email_address => params[:bill_to_email]})
122
-
123
- #set billing and shipping info on order
124
- order.set_shipping_info(party)
125
- order.set_billing_info(party)
126
-
127
- #update status
128
- order.status = 'Demographics Gathered'
129
- order.save
130
-
131
- order
132
- end
133
-
134
- #complete the order
135
- def complete_order(params, charge_credit_card=true)
136
- success = true
137
- message = nil
138
- order = get_order
139
-
140
- if charge_credit_card
141
- #make credit financial txns and payment txns
142
- #create financial txn for order
143
- financial_txn = create_financial_txns(order)
144
-
145
- credit_card = CreditCard.new(
146
- :first_name_on_card => params[:first_name],
147
- :last_name_on_card => params[:last_name],
148
- :expiration_month => params[:exp_month],
149
- :expiration_year => params[:exp_year]
150
- )
151
- credit_card.card_number = params[:card_number]
152
- result = CreditCardAccount.new.purchase(financial_txn, params[:cvvs], ErpCommerce::Config.active_merchant_gateway_wrapper, {}, credit_card)
153
-
154
- #make sure cedit card payment was successful
155
- if result[:payment].nil? or !result[:payment].success
156
- success = false
157
- message = result[:message]
158
- order.status = 'Credit Card Failed'
159
- end
160
- end
161
-
162
- if success
163
- order.status = 'Pending Shipment'
164
- #update inventory counts
165
- #should be moved to model somewhere
166
- order.order_line_items.each do |oli|
167
- inventory_entry = oli.product_type.inventory_entries.first
168
- inventory_entry.number_available -= 1
169
- inventory_entry.number_sold += 1
170
- inventory_entry.save
171
- end
172
- #clear order from session
173
- clear_order
174
- end
175
-
176
- order.save
177
- return success, message, order, result[:payment]
178
- end
179
-
180
- #remove line item
181
- def remove_from_cart(order_line_item_id)
182
- order = get_order
183
-
184
- order.line_items.find(order_line_item_id).destroy
185
-
186
- order
187
- end
188
-
189
- private
190
-
191
- def get_line_item_for_product_type(product_type)
192
- order = get_order(true)
193
- order.line_items.select{|oli| oli.product_type == product_type.id}
194
- end
195
-
196
- def clear_order
197
- session['order_txn_id'] = nil
198
- end
199
-
200
- def create_order
201
- order = OrderTxn.create
202
- order.status = 'Initialized'
203
- order.order_number = (Time.now.to_i / (rand(100)+2)).round
204
- order.save
205
- session['order_txn_id'] = order.id
206
- setup_biz_txn_party_roles(order) unless self.current_user.nil?
207
- order
208
- end
209
-
210
- #sets up payor party role
211
- def setup_biz_txn_party_roles(order)
212
- payor_role = BizTxnPartyRoleType.find_by_internal_identifier('payor')
213
- biz_txn_event = order.root_txn
214
- tpr = BizTxnPartyRole.new
215
- tpr.biz_txn_event = biz_txn_event
216
- tpr.party = self.current_user.party
217
- tpr.biz_txn_party_role_type = payor_role
218
- tpr.save
219
- end
220
-
221
- #create financial_txn for charge lines
222
- def create_financial_txns(order)
223
- #total up all order line items charge lines
224
- total_payment = 0
225
- currency = nil
226
- order.get_total_charges.each do |money|
227
- total_payment += money.amount
228
- currency = money.currency
229
- end
230
-
231
- financial_txn = FinancialTxn.create(:money => Money.create(:description => 'Order Payment', :amount => total_payment, :currency => currency))
232
- financial_txn.description = 'Order Payment'
233
- financial_txn.txn_type = BizTxnType.iid('payment_txn')
234
- financial_txn.save
235
-
236
- order.get_all_charge_lines.each do |charge_line|
237
- charge_line.add_payment_txn(financial_txn)
238
- charge_line.save
239
- end
240
-
241
- financial_txn
242
- end
243
-
244
- end#ErpCommerce
245
- end#OrderHelper