spree_pag_seguro 1.0.0.beta1

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,26 @@
1
+ Copyright (c) 2011 [name of plugin creator]
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without modification,
5
+ are permitted provided that the following conditions are met:
6
+
7
+ * Redistributions of source code must retain the above copyright notice,
8
+ this list of conditions and the following disclaimer.
9
+ * Redistributions in binary form must reproduce the above copyright notice,
10
+ this list of conditions and the following disclaimer in the documentation
11
+ and/or other materials provided with the distribution.
12
+ * Neither the name Spree nor the names of its contributors may be used to
13
+ endorse or promote products derived from this software without specific
14
+ prior written permission.
15
+
16
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17
+ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19
+ A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
20
+ CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21
+ EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22
+ PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23
+ PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24
+ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25
+ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
data/README.md ADDED
@@ -0,0 +1,30 @@
1
+ # Spree Pagseguro
2
+
3
+ Uma extensão do [Spree](http://spreecommerce.com) para permitir pagamentos utilizando o PagSeguro.
4
+
5
+ ## Instalação
6
+
7
+ Adicione spree ao gemfile da sua aplicação, e também:
8
+
9
+ gem "spree_pag_seguro", :git => "git://github.com/heavenstudio/spree-pag_seguro.git"
10
+
11
+ Rode a task de instalação:
12
+
13
+ rails generate spree_pag_seguro:install
14
+
15
+ ## Configuração
16
+
17
+ Adicione as seguintes linhas em config/initializers/spree_pag_seguro.rb, de acordo com sua conta no pag_seguro:
18
+
19
+ <SuaAplicacao>::Application.configure do
20
+ config.after_initialize do
21
+ Spree::PagSeguro::Config.email = "seu_email_cadastrad@pag_seguro.com.br"
22
+ Spree::PagSeguro::Config.token = "ALGUMTOKENMALDITO"
23
+ end
24
+ end
25
+
26
+ ## TODO
27
+
28
+ * Fazer funcionar
29
+ * README mais completo
30
+ * Testes
@@ -0,0 +1 @@
1
+ //= require admin/spree_core
@@ -0,0 +1 @@
1
+ //= require store/spree_core
@@ -0,0 +1,3 @@
1
+ /*
2
+ *= require admin/spree_core
3
+ */
@@ -0,0 +1,3 @@
1
+ /*
2
+ *= require store/spree_core
3
+ */
@@ -0,0 +1,13 @@
1
+ Spree::BaseController.class_eval do
2
+ before_filter :check_current_order
3
+ def check_current_order
4
+ if current_order or session[:order_id]
5
+ order = current_order
6
+ if (order.payment_state == "paid") or (order.payment_state == "credit_owed")
7
+ session[:order_id] = nil
8
+ order.line_items.destroy_all
9
+ flash[:notice] = t(:pp_ws_payment_received)
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,31 @@
1
+ Spree::CheckoutController.class_eval do
2
+ def edit
3
+ if ((@order.state == "payment") && @order.valid?)
4
+ puts "valid, processing"
5
+ if @order.payable_via_pag_seguro?
6
+ puts "payable via pag_seguro, adding payment"
7
+ payment = Spree::Payment.new
8
+ payment.amount = @order.total
9
+ payment.payment_method = Spree::Order.pag_seguro_payment_method
10
+ @order.payments << payment
11
+ pag_seguro_payment = PagSeguro::Payment.new(Spree::PagSeguro::Config.email, Spree::PagSeguro::Config.token)
12
+
13
+ pag_seguro_payment.items = @order.line_items.collect do |item|
14
+ PagSeguro::Item.new(
15
+ id: item.id,
16
+ description: item.product.description,
17
+ amount: format("%.2f", item.price.round(2)),
18
+ quantity: item.quantity,
19
+ weight: item.product.weight,
20
+ )
21
+ end
22
+
23
+ pag_seguro_payment.sender = PagSeguro::Sender.new(name: @order.name, email: @order.email, phone_number: @order.ship_address.phone)
24
+ pag_seguro_payment.shipping = PagSeguro::Shipping.new(type: PagSeguro::Shipping::SEDEX, state: @order.ship_address.state.abbr, city: @order.ship_address.city, postal_code: @order.ship_address.zipcode, street: @order.ship_address.address1, complement: @order.ship_address.address2)
25
+
26
+ @pag_seguro_url = pag_seguro_payment.checkout_payment_url
27
+ payment.started_processing
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,27 @@
1
+ module Spree
2
+ class PagSeguroController < Spree::CheckoutController
3
+ protect_from_forgery :except => [:confirm]
4
+ skip_before_filter :persist_gender
5
+
6
+ def confirm
7
+ unless current_order
8
+ redirect_to root_path
9
+ else
10
+ order = current_order
11
+ if (order.payment_state == "paid") or (order.payment_state == "credit_owed")
12
+ flash[:notice] = t(:pag_seguro_payment_received)
13
+ state_callback(:after)
14
+ else
15
+ while order.state != "complete"
16
+ order.next
17
+ state_callback(:after)
18
+ end
19
+ flash[:notice] = t(:pag_seguro_order_processed_successfully)
20
+ flash[:commerce_tracking] = "nothing special"
21
+ end
22
+ redirect_to order_path(current_order)
23
+ end
24
+ end
25
+
26
+ end
27
+ end
@@ -0,0 +1,79 @@
1
+ module Spree
2
+ class PaymentNotificationsController < BaseController
3
+ protect_from_forgery :except => [:create]
4
+ skip_before_filter :restriction_access
5
+
6
+ def create
7
+ @order = Spree::Order.find_by_number(params[:invoice])
8
+ Spree::PaymentNotification.create!(:params => params,
9
+ :order_id => @order.id,
10
+ :status => params[:payment_status],
11
+ :transaction_id => params[:txn_id])
12
+
13
+ logger.info "PagSeguro: processing payment notification for invoice #{params["invoice"]}, amount is #{params["mc_gross"]} #{params["mc_currency"]}"
14
+ # this logging stuff won't live here for long...
15
+
16
+ Order.transaction do
17
+ # main part of hacks
18
+ order = @order
19
+
20
+ #create payment for this order
21
+ payment = Spree::Payment.new
22
+
23
+ # 1. Assume that if payment notification comes, it's exactly for the amount
24
+ # sent to paypal (safe assumption -- cart can't be edited while on paypal)
25
+ # 2. Can't use Order#total, as it's intercepted by spree-multi-currency
26
+ # which might lead to lots of false "credit owed" payment states
27
+ # (when they should be "complete")
28
+ payment.amount = order.read_attribute(:total)
29
+ logger.info "PagSeguro: set payment.amount to #{payment.amount} based on order's total #{order.read_attribute(:total)}"
30
+
31
+ payment.payment_method = Spree::Order.pag_seguro_payment_method
32
+ order.payments << payment
33
+ payment.started_processing
34
+
35
+ order.payment.complete
36
+ logger.info("PagSeguro: order #{order.number} (#{order.id}) -- completed payment")
37
+
38
+ until @order.state == "complete"
39
+ if @order.next!
40
+ @order.update!
41
+ state_callback(:after)
42
+ end
43
+ end
44
+
45
+ logger.info("PagSeguro: Order #{order.number} (#{order.id}) updated successfully")
46
+ end
47
+
48
+ render :nothing => true
49
+ end
50
+
51
+ private
52
+
53
+ # those methods are copy-pasted from CheckoutController
54
+ # we cannot inherit from that class unless we want to skip_before_filter
55
+ # half of calls in SpreeBase module
56
+ def state_callback(before_or_after = :before)
57
+ method_name = :"#{before_or_after}_#{@order.state}"
58
+ send(method_name) if respond_to?(method_name, true)
59
+ end
60
+
61
+ def before_address
62
+ @order.bill_address ||= Address.new
63
+ @order.ship_address ||= Address.new
64
+ end
65
+
66
+ def before_delivery
67
+ @order.shipping_method ||= (@order.rate_hash.first && @order.rate_hash.first[:shipping_method])
68
+ end
69
+
70
+ def before_payment
71
+ current_order.payments.destroy_all if request.put?
72
+ end
73
+
74
+ #This isn't working here in payment_nofitications_controller since IPN will run on a different session
75
+ def after_complete
76
+ session[:order_id] = nil
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,16 @@
1
+ Spree::Order.class_eval do
2
+ has_many :payment_notifications
3
+
4
+ def shipment_cost
5
+ adjustment_total - credit_total
6
+ end
7
+
8
+ def payable_via_pag_seguro?
9
+ # !!self.class.pag_seguro_payment_method
10
+ true
11
+ end
12
+
13
+ def self.pag_seguro_payment_method
14
+ Spree::PaymentMethod.select{ |pm| pm.name.downcase =~ /pag_seguro/}.first
15
+ end
16
+ end
@@ -0,0 +1,16 @@
1
+ module Spree
2
+ class PaymentNotification < ActiveRecord::Base
3
+ belongs_to :order
4
+ serialize :params
5
+ after_create :mark_order_as_paid
6
+
7
+ private
8
+
9
+ def mark_order_as_paid
10
+ if(status == "Completed")
11
+ logger.info "Order #{order.number} should be marked as paid now -- IPN status 'Completed'"
12
+ #order.update_attributes({:paid_on => Date.today, :status_id => Status.PAID.id})
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,23 @@
1
+ <% if @order.pag_seguro_payment %>
2
+
3
+ <b><%= t("PagSeguro Transaction") %></b>
4
+
5
+ <table class="basic-table">
6
+ <tr>
7
+ <th><%= t("Amount") %></th>
8
+ <th><%= t("Fee") %></th>
9
+ <th><%= t("Status") %></th>
10
+ <th><%= t("Transaction Id") %></th>
11
+ <th><%= t("Received At") %></th>
12
+ </tr>
13
+ <% @order.pag_seguro_payment.txns.each do |t| %>
14
+ <tr>
15
+ <td><%=number_to_currency t.amount %></td>
16
+ <td><%=number_to_currency t.fee %></td>
17
+ <td><%=t.status %></td>
18
+ <td><%=t.transaction_id %></td>
19
+ <td><%=t.received_at %></td>
20
+ </tr>
21
+ <% end %>
22
+ </table>
23
+ <% end %>
@@ -0,0 +1,3 @@
1
+ <%= link_to @pag_seguro_url do %>
2
+ <%= image_tag "pag_seguro_checkout.gif" %>
3
+ <% end %>
@@ -0,0 +1,28 @@
1
+ <% content_for :head do %>
2
+ <%= javascript_include_tag '/states' %>
3
+ <% end %>
4
+ <div id="checkout">
5
+ <h1><%= t("checkout")%></h1>
6
+ <%= checkout_progress %>
7
+ <br clear="left" />
8
+ <%= render "spree/shared/error_messages", :target => @order %>
9
+ <%= hook :checkout_summary_box do %>
10
+ <div id="checkout-summary">
11
+ <%= render 'summary', :order => @order %>
12
+ </div>
13
+ <% end %>
14
+
15
+
16
+ <% if(@order.state == "payment") %>
17
+ <%= render('spree/checkout/pag_seguro_checkout') if @order.payable_via_pag_seguro? %>
18
+ <% else %>
19
+ <%= form_for @order, :url => update_checkout_path(@order.state), :html => { :id => "checkout_form_#{@order.state}" } do |form| %>
20
+ <%= render @order.state, :form => form %>
21
+ <input id="post-final" type="submit" style="display:none"/>
22
+ <% end %>
23
+ <% end %>
24
+ </div>
25
+
26
+ <div id="payment_step">
27
+
28
+ </div>
@@ -0,0 +1,18 @@
1
+ module SpreePaypalWebsiteStandard
2
+ module Generators
3
+ class InstallGenerator < Rails::Generators::Base
4
+ def add_migrations
5
+ run 'bundle exec rake railties:install:migrations FROM=spree_pag_seguro'
6
+ end
7
+
8
+ def run_migrations
9
+ res = ask "Would you like to run the migrations now? [Y/n]"
10
+ if res == "" || res.downcase == "y"
11
+ run 'bundle exec rake db:migrate'
12
+ else
13
+ puts "Skiping rake db:migrate, don't forget to run it!"
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,32 @@
1
+ module Spree
2
+ module PagSeguro
3
+ class Engine < Rails::Engine
4
+ engine_name 'spree_pag_seguro'
5
+
6
+ initializer "spree.active_shipping.configuration", :after => "spree.environment" do |app|
7
+ Dir.glob(File.join(File.dirname(__FILE__), "../../lib/spree_pag_seguro_configuration.rb")) do |c|
8
+ Rails.configuration.cache_classes ? require(c) : load(c)
9
+ end
10
+ end
11
+
12
+ initializer "spree.pag_seguro.preferences", :after => "spree.active_shipping.configuration" do |app|
13
+ Spree::PagSeguro::Config = Spree::PagSeguroConfiguration.new
14
+ end
15
+
16
+ config.autoload_paths += %W(#{config.root}/lib)
17
+
18
+ # use rspec for tests
19
+ config.generators do |g|
20
+ g.test_framework :rspec
21
+ end
22
+
23
+ def self.activate
24
+ Dir.glob(File.join(File.dirname(__FILE__), "../../app/**/*_decorator*.rb")) do |c|
25
+ Rails.configuration.cache_classes ? require(c) : load(c)
26
+ end
27
+ end
28
+
29
+ config.to_prepare &method(:activate).to_proc
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,3 @@
1
+ require 'spree_core'
2
+ require 'pag_seguro'
3
+ require 'spree_pag_seguro/engine'
@@ -0,0 +1,6 @@
1
+ module Spree
2
+ class PagSeguroConfiguration < Spree::Preferences::Configuration
3
+ preference :email, default: "seu_email_cadastrad@pag_seguro.com.br"
4
+ preference :token, default: "SEUTOKENNOPASEGURO"
5
+ end
6
+ end
metadata ADDED
@@ -0,0 +1,144 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: spree_pag_seguro
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0.beta1
5
+ prerelease: 6
6
+ platform: ruby
7
+ authors:
8
+ - Gregg Pollack, Sean Schofield, Tomasz Stachewicz, Buddhi DeSilva
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-01-26 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: spree_core
16
+ requirement: &70357017472480 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 1.0.0.rc1
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70357017472480
25
+ - !ruby/object:Gem::Dependency
26
+ name: pag_seguro
27
+ requirement: &70357017472000 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - =
31
+ - !ruby/object:Gem::Version
32
+ version: 0.1.1
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70357017472000
36
+ - !ruby/object:Gem::Dependency
37
+ name: capybara
38
+ requirement: &70357017471500 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - =
42
+ - !ruby/object:Gem::Version
43
+ version: 1.0.1
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70357017471500
47
+ - !ruby/object:Gem::Dependency
48
+ name: factory_girl
49
+ requirement: &70357017471100 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *70357017471100
58
+ - !ruby/object:Gem::Dependency
59
+ name: ffaker
60
+ requirement: &70357017470620 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *70357017470620
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec-rails
71
+ requirement: &70357017470120 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ~>
75
+ - !ruby/object:Gem::Version
76
+ version: '2.7'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: *70357017470120
80
+ - !ruby/object:Gem::Dependency
81
+ name: sqlite3
82
+ requirement: &70357017469560 !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ! '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ type: :development
89
+ prerelease: false
90
+ version_requirements: *70357017469560
91
+ description: Spree extension for integration with PagSeguro payment. Based on spree_pp_website_standart
92
+ gem
93
+ email: tomekrs@o2.pl
94
+ executables: []
95
+ extensions: []
96
+ extra_rdoc_files: []
97
+ files:
98
+ - README.md
99
+ - LICENSE
100
+ - lib/generators/spree_pag_seguro/install/install_generator.rb
101
+ - lib/spree_pag_seguro/engine.rb
102
+ - lib/spree_pag_seguro.rb
103
+ - lib/spree_pag_seguro_configuration.rb
104
+ - app/assets/images/pag_seguro_checkout.gif
105
+ - app/assets/javascripts/admin/spree_paypal_website_standard.js
106
+ - app/assets/javascripts/store/spree_paypal_website_standard.js
107
+ - app/assets/stylesheets/admin/spree_paypal_website_standard.css
108
+ - app/assets/stylesheets/store/spree_paypal_website_standard.css
109
+ - app/controllers/spree/base_controller_decorator.rb.rb
110
+ - app/controllers/spree/checkout_controller_decorator.rb
111
+ - app/controllers/spree/pag_seguro_controller.rb
112
+ - app/controllers/spree/payment_notifications_controller.rb
113
+ - app/models/spree/order_decorator.rb
114
+ - app/models/spree/payment_notification.rb
115
+ - app/views/spree/admin/orders/_pag_seguro_txns.html.erb
116
+ - app/views/spree/checkout/_pag_seguro_checkout.html.erb
117
+ - app/views/spree/checkout/edit.html.erb
118
+ - app/views/spree/pag_seguro_payments/successful.html.erb
119
+ homepage: http://github.com/heavenstudio/spree-pag_seguro
120
+ licenses: []
121
+ post_install_message:
122
+ rdoc_options: []
123
+ require_paths:
124
+ - lib
125
+ required_ruby_version: !ruby/object:Gem::Requirement
126
+ none: false
127
+ requirements:
128
+ - - ! '>='
129
+ - !ruby/object:Gem::Version
130
+ version: 1.9.2
131
+ required_rubygems_version: !ruby/object:Gem::Requirement
132
+ none: false
133
+ requirements:
134
+ - - ! '>'
135
+ - !ruby/object:Gem::Version
136
+ version: 1.3.1
137
+ requirements:
138
+ - none
139
+ rubyforge_project:
140
+ rubygems_version: 1.8.10
141
+ signing_key:
142
+ specification_version: 3
143
+ summary: Spree extension for integration with PagSeguro payment
144
+ test_files: []