ecart 0.0.2 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +9 -3
- data/ecart.gemspec +1 -0
- data/lib/cart.rb +12 -0
- data/lib/cart_item.rb +4 -0
- data/lib/config/paypal_config.yml +9 -0
- data/lib/ecart/version.rb +1 -1
- data/lib/generators/ecart/install/install_generator.rb +13 -0
- data/lib/generators/templates/cart/show.html.erb +8 -0
- data/lib/generators/templates/cart_controller.rb +4 -0
- data/lib/generators/templates/pay_pal_helper.rb +46 -0
- data/lib/generators/templates/paypal_express/review.html.erb +15 -0
- data/lib/generators/templates/paypal_express_checkout.rb +16 -0
- data/lib/generators/templates/paypal_express_controller.rb +49 -0
- metadata +23 -6
data/README.md
CHANGED
@@ -26,7 +26,7 @@ You can run bundle from command line
|
|
26
26
|
|
27
27
|
## Before installing
|
28
28
|
|
29
|
-
I'm assuming you have a Product model on your rails app, but don't worry
|
29
|
+
I'm assuming you have a Product model and index action on your rails app, but don't worry
|
30
30
|
if that's not the case you can customize it as you like
|
31
31
|
|
32
32
|
## Installing to App (using Generators)
|
@@ -53,9 +53,16 @@ You can do so in the config/application.rb file
|
|
53
53
|
config.autoload_paths += Dir["#{config.root}/lib", "#{config.root}/lib/**/"]
|
54
54
|
```
|
55
55
|
|
56
|
+
## Example of rails app using ecart
|
57
|
+
|
58
|
+
https://github.com/kurenn/ecart-example
|
59
|
+
|
56
60
|
## Changelog
|
57
61
|
<ul>
|
58
|
-
<li>
|
62
|
+
<li>Provide views for the cart</li>
|
63
|
+
<li>Paypal express checkout integration</li>
|
64
|
+
<li>Current gem v.1.0.0</li>
|
65
|
+
<li>Released gem v.0.0.1</li>
|
59
66
|
</ul>
|
60
67
|
|
61
68
|
|
@@ -69,7 +76,6 @@ config.autoload_paths += Dir["#{config.root}/lib", "#{config.root}/lib/**/"]
|
|
69
76
|
<ul>
|
70
77
|
<li>Add dinamic association with a model</li>
|
71
78
|
<li>Update the price by fetching from DB</li>
|
72
|
-
<li>Provide views for the cart</li>
|
73
79
|
</ul>
|
74
80
|
|
75
81
|
|
data/ecart.gemspec
CHANGED
@@ -21,5 +21,6 @@ Gem::Specification.new do |s|
|
|
21
21
|
# specify any dependencies here; for example:
|
22
22
|
s.add_development_dependency 'rails', '>= 3.1'
|
23
23
|
s.add_development_dependency "rspec"
|
24
|
+
s.add_dependency "activemerchant", '>= 1.26.0'
|
24
25
|
# s.add_runtime_dependency "rest-client"
|
25
26
|
end
|
data/lib/cart.rb
CHANGED
@@ -52,4 +52,16 @@ class Cart
|
|
52
52
|
self.total_items.zero?
|
53
53
|
end
|
54
54
|
|
55
|
+
def items_for_paypal
|
56
|
+
@cart_items.collect do |cart_item|
|
57
|
+
item = cart_item.item
|
58
|
+
|
59
|
+
{
|
60
|
+
:name => item.name,
|
61
|
+
:quantity => cart_item.quantity,
|
62
|
+
:amount => cart_item.to_cents,
|
63
|
+
}
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
55
67
|
end
|
data/lib/cart_item.rb
CHANGED
@@ -0,0 +1,9 @@
|
|
1
|
+
development:
|
2
|
+
login: "seller_1340253894_biz_api1.calmecac.me"
|
3
|
+
password: "1340253926"
|
4
|
+
signature: "AiPC9BjkCyDFQXbSkoZcgqH3hpacA08uw5uvFFESX6lHI4ectk1Nt.PZ "
|
5
|
+
|
6
|
+
production:
|
7
|
+
login: "seller_1340253894_biz_api1.calmecac.me"
|
8
|
+
password: "1340253926"
|
9
|
+
signature: "AiPC9BjkCyDFQXbSkoZcgqH3hpacA08uw5uvFFESX6lHI4ectk1Nt.PZ "
|
data/lib/ecart/version.rb
CHANGED
@@ -12,6 +12,10 @@ module Ecart
|
|
12
12
|
copy_file 'cart_item.rb', 'lib/cart_item.rb'
|
13
13
|
copy_file 'generators/templates/cart_helper.rb', 'app/helpers/cart_helper.rb'
|
14
14
|
copy_file 'generators/templates/cart_controller.rb', 'app/controllers/cart_controller.rb'
|
15
|
+
copy_file 'generators/templates/paypal_express_controller.rb', 'app/controllers/paypal_express_controller.rb'
|
16
|
+
copy_file 'generators/templates/pay_pal_helper.rb', 'app/helpers/pay_pal_helper.rb'
|
17
|
+
directory 'generators/templates/cart', 'app/views/cart'
|
18
|
+
directory 'generators/templates/paypal_express', 'app/views/paypal_express'
|
15
19
|
end
|
16
20
|
|
17
21
|
def add_ecart_routes
|
@@ -27,8 +31,17 @@ module Ecart
|
|
27
31
|
match 'add_to_cart/:id' => 'cart#add_to_cart', :as => :add_to_cart
|
28
32
|
match 'remove_from_cart/:id' => 'cart#remove_from_cart', :as => :remove_from_cart
|
29
33
|
match 'empty_cart' => 'cart#empty', :as => :empty_cart
|
34
|
+
match 'cart' => 'cart#show', :as => :cart
|
35
|
+
get 'paypal_express/checkout'
|
36
|
+
get 'paypal_express/purchase'
|
37
|
+
get 'paypal_express/review'
|
30
38
|
CONTENT
|
31
39
|
end
|
40
|
+
|
41
|
+
def add_paypal_configuration
|
42
|
+
copy_file 'config/paypal_config.yml', 'config/paypal_config.yml'
|
43
|
+
copy_file 'generators/templates/paypal_express_checkout.rb', 'config/initializers/paypal_express_checkout.rb'
|
44
|
+
end
|
32
45
|
end
|
33
46
|
end
|
34
47
|
end
|
@@ -0,0 +1,8 @@
|
|
1
|
+
<% @cart.cart_items.each do |cart_item| %>
|
2
|
+
<%= cart_item.item.name %> <%= cart_item.item.price %> | <%= cart_item.quantity %><br />
|
3
|
+
<% end %>
|
4
|
+
<%= @cart.total_items %> items
|
5
|
+
<br />
|
6
|
+
Total <%= number_to_currency @cart.total %> | <%= link_to_empty_cart %>
|
7
|
+
<br />
|
8
|
+
<%= link_to image_tag("https://www.paypal.com/en_US/i/btn/btn_xpressCheckout.gif"), paypal_express_checkout_path %>
|
@@ -0,0 +1,46 @@
|
|
1
|
+
module PayPalHelper
|
2
|
+
|
3
|
+
def purchase_params(cart, ip)
|
4
|
+
subtotal = cart.total
|
5
|
+
return to_cents(subtotal), {
|
6
|
+
:ip => request.remote_ip,
|
7
|
+
:return_url => url_for(:action => 'review', :only_path => false),
|
8
|
+
:cancel_return_url => products_url,
|
9
|
+
:subtotal => to_cents(subtotal),
|
10
|
+
:handling => 0,
|
11
|
+
:tax => 0,
|
12
|
+
:allow_note => true,
|
13
|
+
:items => cart.items_for_paypal,
|
14
|
+
}
|
15
|
+
end
|
16
|
+
|
17
|
+
#Paypal gets the total with cents
|
18
|
+
def to_cents(subtotal)
|
19
|
+
(subtotal * 100).round
|
20
|
+
end
|
21
|
+
|
22
|
+
def order_info(gateway_response, cart)
|
23
|
+
total = cart.total
|
24
|
+
return {
|
25
|
+
gateway_details: {
|
26
|
+
:token => gateway_response.token,
|
27
|
+
:payer_id => gateway_response.payer_id,
|
28
|
+
},
|
29
|
+
total: total,
|
30
|
+
}
|
31
|
+
end
|
32
|
+
|
33
|
+
def get_purchase_params(cart, request, params)
|
34
|
+
subtotal = cart.total
|
35
|
+
return to_cents(subtotal), {
|
36
|
+
:ip => request.remote_ip,
|
37
|
+
:token => params[:token],
|
38
|
+
:payer_id => params[:payer_id],
|
39
|
+
:subtotal => to_cents(subtotal),
|
40
|
+
:handling => 0,
|
41
|
+
:tax => 0,
|
42
|
+
:items => cart.items_for_paypal,
|
43
|
+
}
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
@@ -0,0 +1,15 @@
|
|
1
|
+
|
2
|
+
<h4>Your Total</h4>
|
3
|
+
<p><%= "Subtotal: #{number_to_currency @order[:total]}"%></p>
|
4
|
+
|
5
|
+
<% cart.cart_items.each do |cart_item| %>
|
6
|
+
<%= cart_item.item.name %> <%= cart_item.item.price %> | <%= cart_item.quantity %><br />
|
7
|
+
<% end %>
|
8
|
+
<%= cart.total_items %> items
|
9
|
+
<br />
|
10
|
+
Total <%= number_to_currency cart.total %>
|
11
|
+
<br />
|
12
|
+
|
13
|
+
<%= link_to 'Complete Purchase', paypal_express_purchase_path(:token => @order[:gateway_details][:token], :payer_id => @order[:gateway_details][:payer_id]) %>
|
14
|
+
|
15
|
+
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
require 'active_merchant'
|
3
|
+
|
4
|
+
def paypal_options
|
5
|
+
@config = YAML::load(File.open("#{Rails.root}/config/paypal_config.yml"))
|
6
|
+
@config[Rails.env].inject({}){|paypal_option,(key,value)| paypal_option[key.to_sym] = value; paypal_option}
|
7
|
+
end
|
8
|
+
|
9
|
+
::Rails.application.class.to_s.split("::").first.constantize::Application.configure do
|
10
|
+
|
11
|
+
config.after_initialize do
|
12
|
+
ActiveMerchant::Billing::Base.mode = :test
|
13
|
+
::EXPRESS_GATEWAY = ActiveMerchant::Billing::PaypalExpressGateway.new(paypal_options)
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
class PaypalExpressController < ApplicationController
|
2
|
+
|
3
|
+
include PayPalHelper
|
4
|
+
|
5
|
+
def checkout
|
6
|
+
total, setup_purchase_params = purchase_params cart, request
|
7
|
+
setup_response = EXPRESS_GATEWAY.setup_purchase(total, setup_purchase_params)
|
8
|
+
redirect_to EXPRESS_GATEWAY.redirect_url_for(setup_response.token)
|
9
|
+
end
|
10
|
+
|
11
|
+
def review
|
12
|
+
if params[:token].nil?
|
13
|
+
redirect_to products_url, :notice => 'Something went wrong!'
|
14
|
+
return
|
15
|
+
end
|
16
|
+
|
17
|
+
response = EXPRESS_GATEWAY.details_for(params[:token])
|
18
|
+
|
19
|
+
unless response.success?
|
20
|
+
redirect_to products_url, :notice => "Something went wrong with the Paypal purchase. Paypal said: #{response.message}"
|
21
|
+
return
|
22
|
+
end
|
23
|
+
|
24
|
+
@order = order_info response, cart
|
25
|
+
end
|
26
|
+
|
27
|
+
def purchase
|
28
|
+
if params[:token].nil? or params[:payer_id].nil?
|
29
|
+
redirect_to orders_url, :notice => "Sorry but something went wrong"
|
30
|
+
return
|
31
|
+
end
|
32
|
+
|
33
|
+
total_in_cents, purchase_params = get_purchase_params cart, request, params
|
34
|
+
purchase = EXPRESS_GATEWAY.purchase total_in_cents, purchase_params
|
35
|
+
|
36
|
+
if purchase.success?
|
37
|
+
session[:cart] = nil
|
38
|
+
# you might want to destroy your cart here if you have a shopping cart
|
39
|
+
notice = "Thanks!, your purchase was successfull"
|
40
|
+
else
|
41
|
+
notice = "Something wrong happened when trying to complete the purchase with Paypal. Paypal said: #{purchase.message}"
|
42
|
+
end
|
43
|
+
|
44
|
+
redirect_to products_url, :notice => notice
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
|
49
|
+
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ecart
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 1.0.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-07-
|
12
|
+
date: 2012-07-21 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
16
|
-
requirement: &
|
16
|
+
requirement: &70290051904440 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '3.1'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70290051904440
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: rspec
|
27
|
-
requirement: &
|
27
|
+
requirement: &70290051904020 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,7 +32,18 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70290051904020
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: activemerchant
|
38
|
+
requirement: &70290051903180 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: 1.26.0
|
44
|
+
type: :runtime
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *70290051903180
|
36
47
|
description: Setup a fully functional e-commerce cart
|
37
48
|
email:
|
38
49
|
- kurenn@icalialabs.com
|
@@ -49,11 +60,17 @@ files:
|
|
49
60
|
- ecart.gemspec
|
50
61
|
- lib/cart.rb
|
51
62
|
- lib/cart_item.rb
|
63
|
+
- lib/config/paypal_config.yml
|
52
64
|
- lib/ecart.rb
|
53
65
|
- lib/ecart/version.rb
|
54
66
|
- lib/generators/ecart/install/install_generator.rb
|
67
|
+
- lib/generators/templates/cart/show.html.erb
|
55
68
|
- lib/generators/templates/cart_controller.rb
|
56
69
|
- lib/generators/templates/cart_helper.rb
|
70
|
+
- lib/generators/templates/pay_pal_helper.rb
|
71
|
+
- lib/generators/templates/paypal_express/review.html.erb
|
72
|
+
- lib/generators/templates/paypal_express_checkout.rb
|
73
|
+
- lib/generators/templates/paypal_express_controller.rb
|
57
74
|
- lib/item.rb
|
58
75
|
- spec/cart_spec.rb
|
59
76
|
- spec/spec_helper.rb
|