Skip to content

Commit

Permalink
mantendo a ideia principal da gem spree_pag_seguro para fazer funcion…
Browse files Browse the repository at this point in the history
…ar na versao 2-1 do spree
  • Loading branch information
cassioscabral committed Nov 27, 2013
1 parent 418dc83 commit 0d55bde
Show file tree
Hide file tree
Showing 26 changed files with 400 additions and 7 deletions.
Binary file added app/assets/images/pag_seguro_checkout.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
//= require admin/spree_core
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
//= require store/spree_core
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/*
*= require admin/spree_core
*/
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/*
*= require store/spree_core
*/
29 changes: 29 additions & 0 deletions app/controllers/spree/pag_seguro_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module Spree
class PagSeguroController < BaseController
protect_from_forgery :except => [:notify]
skip_before_filter :restriction_access

def notify
notification = Spree::PaymentNotification.create_from_params(params)

if notification.approved?
Order.transaction do
@order = Spree::Order.find(notification.id)

# 1. Assume that if payment notification comes, it's exactly for the amount
# sent to pagseguro (safe assumption -- cart can't be edited while on pagseguro)
# 2. Can't use Order#total, as it's intercepted by spree-multi-currency
# which might lead to lots of false "credit owed" payment states
# (when they should be "complete")
@order.payment.complete
end
end

render nothing: true, head: :ok
end

def callback
end

end
end
11 changes: 11 additions & 0 deletions app/models/spree/order_decorator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Spree::Order.class_eval do
has_many :payment_notifications

def payable_via_pag_seguro?
!!self.class.pag_seguro_payment_method
end

def self.pag_seguro_payment_method
Spree::PaymentMethod.where(type: "Spree::PaymentMethod::PagSeguroMethod").first
end
end
36 changes: 36 additions & 0 deletions app/models/spree/pag_seguro_payment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
module Spree
class PagSeguroPayment < ActiveRecord::Base
attr_accessor :order_id
belongs_to :payment

def process!(payment)
order = payment.order

redirect_url = Rails.env.development? ? nil : "#{Spree::Config.site_url}/pag_seguro/callback"

pag_seguro_payment = ::PagSeguro::Payment.new(
Order.pag_seguro_payment_method.preferred_email,
Order.pag_seguro_payment_method.preferred_token,
redirect_url: redirect_url,
extra_amount: format("%.2f", (order.total - order.item_total).round(2)),
id: order.id)

pag_seguro_payment.items = order.line_items.map do |item|
pag_seguro_item = ::PagSeguro::Item.new
pag_seguro_item.id = item.id
pag_seguro_item.description = item.product.name
pag_seguro_item.amount = format("%.2f", item.price.round(2))
pag_seguro_item.quantity = item.quantity
pag_seguro_item.weight = (item.product.weight * 1000).to_i if item.product.weight.present?
pag_seguro_item
end

pag_seguro_payment.sender = ::PagSeguro::Sender.new(name: order.name, email: order.email, phone_number: order.ship_address.phone)
pag_seguro_payment.shipping = ::PagSeguro::Shipping.new(type: ::PagSeguro::Shipping::SEDEX, state: (order.ship_address.state ? order.ship_address.state.abbr : nil), city: order.ship_address.city, postal_code: order.ship_address.zipcode, street: order.ship_address.address1, complement: order.ship_address.address2)
self.code = pag_seguro_payment.code
self.date = pag_seguro_payment.date
self.payment = payment
self.save
end
end
end
36 changes: 36 additions & 0 deletions app/models/spree/pagseguro_payment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
module Spree
class PagseguroPayment < ActiveRecord::Base
attr_accessor :order_id
belongs_to :payment

def process!(payment)
order = payment.order

redirect_url = Rails.env.development? ? nil : "#{Spree::Config.site_url}/pag_seguro/callback"

pagseguro_payment = ::PagSeguro::Payment.new(
Order.pagseguro_payment_method.preferred_email,
Order.pagseguro_payment_method.preferred_token,
redirect_url: redirect_url,
extra_amount: format("%.2f", (order.total - order.item_total).round(2)),
id: order.id)

pagseguro_payment.items = order.line_items.map do |item|
pagseguro_item = ::PagSeguro::Item.new
pagseguro_item.id = item.id
pagseguro_item.description = item.product.name
pagseguro_item.amount = format("%.2f", item.price.round(2))
pagseguro_item.quantity = item.quantity
pagseguro_item.weight = (item.product.weight * 1000).to_i if item.product.weight.present?
pagseguro_item
end

pagseguro_payment.sender = ::PagSeguro::Sender.new(name: order.name, email: order.email, phone_number: order.ship_address.phone)
pagseguro_payment.shipping = ::PagSeguro::Shipping.new(type: ::PagSeguro::Shipping::SEDEX, state: (order.ship_address.state ? order.ship_address.state.abbr : nil), city: order.ship_address.city, postal_code: order.ship_address.zipcode, street: order.ship_address.address1, complement: order.ship_address.address2)
self.code = pagseguro_payment.code
self.date = pagseguro_payment.date
self.payment = payment
self.save
end
end
end
3 changes: 3 additions & 0 deletions app/models/spree/payment_decorator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Spree::Payment.class_eval do
has_one :pag_seguro_payment
end
50 changes: 50 additions & 0 deletions app/models/spree/payment_method/pag_seguro_method.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
module Spree
class PaymentMethod::PagSeguroMethod < PaymentMethod
attr_protected
attr_accessor :order_id

preference :email, :string
preference :token, :string
has_many :payments, :as => :source

def actions
%w{capture void}
end

# Indicates whether its possible to capture the payment
def can_capture?(payment)
['checkout', 'pending'].include?(payment.state)
end

# Indicates whether its possible to void the payment.
def can_void?(payment)
payment.state != 'void'
end

def capture(*args)
ActiveMerchant::Billing::Response.new(true, "", {}, {})
end

def void(*args)
ActiveMerchant::Billing::Response.new(true, "", {}, {})
end

def payment_source_class
self.class
end

def source_required?
false
end

def code(payment)
if payment.pag_seguro_payment.present?
payment.pag_seguro_payment.code
else
pag_seguro_payment = Spree::PagSeguroPayment.new
pag_seguro_payment.process!(payment)
pag_seguro_payment.code
end
end
end
end
65 changes: 65 additions & 0 deletions app/models/spree/payment_method/pagseguro.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
module Spree
class PaymentMethod::Pagseguro < PaymentMethod
preference :email, :string
preference :token, :string


#adicionar atributos as listas de permitidos
#attr_accessible :preferred_moip_token, :preferred_moip_key, :preferred_currency, :preferred_moip_environment

def provider_class
Spree::PagseguroPayment
end

def payment_source_class
nil
end

def actions
%w{capture void}
end

# Indicates whether its possible to capture the payment
def can_capture?(payment)
['checkout', 'pending'].include?(payment.state)
end

# Indicates whether its possible to void the payment.
def can_void?(payment)
payment.state != 'void'
end

def capture(*args)
ActiveMerchant::Billing::Response.new(true, "", {}, {})
end

def void(*args)
ActiveMerchant::Billing::Response.new(true, "", {}, {})
end

def source_required?
false
end


# metodos do spree_pag_seguro, ainda n sei a utilidade

# attr_protected
# attr_accessor :order_id
# has_many :payments, :as => :source

# def payment_source_class
# self.class
# end

# def code(payment)
# if payment.pag_seguro_payment.present?
# payment.pag_seguro_payment.code
# else
# pag_seguro_payment = Spree::PagSeguroPayment.new
# pag_seguro_payment.process!(payment)
# pag_seguro_payment.code
# end
# end
end
end
24 changes: 24 additions & 0 deletions app/models/spree/payment_notification.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module Spree
class PaymentNotification < ActiveRecord::Base
belongs_to :order
serialize :params
attr_protected

def self.create_from_params(params)
email = Order.pag_seguro_payment_method.preferred_email
token = Order.pag_seguro_payment_method.preferred_token
notification_code = params[:notificationCode]
notification = ::PagSeguro::Notification.new(email, token, notification_code)

self.create!(
params: params,
order_id: notification.id,
status: notification.status,
transaction_id: notification.transaction_id,
notification_code: notification_code
)

notification
end
end
end
6 changes: 6 additions & 0 deletions app/overrides/replace_payment_info.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Deface::Override.new(
virtual_path: "spree/shared/_order_details",
name: "replace_payment_info",
insert_bottom: ".payment-info",
partial: "spree/checkout/payment/order_details"
)
23 changes: 23 additions & 0 deletions app/views/spree/admin/orders/_pag_seguro_txns.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<% if @order.pag_seguro_payment %>

<b><%= t("PagSeguro Transaction") %></b>

<table class="basic-table">
<tr>
<th><%= t("Amount") %></th>
<th><%= t("Fee") %></th>
<th><%= t("Status") %></th>
<th><%= t("Transaction Id") %></th>
<th><%= t("Received At") %></th>
</tr>
<% @order.payment.pag_seguro_payment.txns.each do |t| %>
<tr>
<td><%=number_to_currency t.amount %></td>
<td><%=number_to_currency t.fee %></td>
<td><%=t.status %></td>
<td><%=t.transaction_id %></td>
<td><%=t.received_at %></td>
</tr>
<% end %>
</table>
<% end %>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= link_to PagSeguro::Payment.checkout_payment_url(@order.payment.source.code) %>
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<fieldset data-hook="creditcard">
<legend>PagSeguro</legend>

<table class="index">
<tr>
<th>Código de Transação no Pagseguro:</th>
<th>Data de criação do pagamento:</th>
<th>URL para efetuar o pagamento</th>
</tr>
<tr>
<td>
<% if payment.order.payment_notifications.present? %>
<%= payment.order.payment_notifications.last.transaction_id %>
<% else %>
Sem Código
<% end %>
</td>
<td><%= l payment.pag_seguro_payment.date %></td>
<td><%= link_to PagSeguro::Payment.checkout_payment_url(payment.pag_seguro_payment.code) %></td>
</tr>
</table>
</fieldset>
8 changes: 8 additions & 0 deletions app/views/spree/checkout/payment/_order_details.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<% if @order.payment_method.instance_of? Spree::PaymentMethod::PagSeguroMethod %>
<% if @order.payment.completed? %>
Seu pagamento foi aprovado pelo PagSeguro através da transação de código: <%= @order.payment_notifications.last.transaction_id if @order.payment_notifications.present? %>
<% else %>
Clique na imagem abaixo para realizar o pagamento através do Pag Seguro.
<%= link_to image_tag("pag_seguro_checkout.gif"), PagSeguro::Payment.checkout_payment_url(@order.payment_method.code(@order.payment)) %>
<% end %>
<% end %>
2 changes: 2 additions & 0 deletions app/views/spree/checkout/payment/_pagseguromethod.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<%= image_tag "pag_seguro_checkout.gif" %>
<%= hidden_field "payment_source[#{payment_method.id}]", 'order_id', value: @order.id %>
3 changes: 3 additions & 0 deletions app/views/spree/pag_seguro/callback.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Aguardando liberação de pagamento pelo PagSeguro.
%br
Obrigado por comprar conosco.
10 changes: 10 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
en:
please_select_your_country: "Please select your country"
please_enter_valid_zip: "Please enter a valid zipcode."
zip_code_if_you_have_one: "Zipcode (if you have one)"
payment_failure: "Payment Failure"
payment_pending: "Payment Pending"
check_out_with_pag_seguro: "Check out with PayPal"
pag_seguro_payment_received: "Thank you.. We received the payment for your order from PayPal. Your order will be shipped shortly"
pag_seguro_order_processed_successfully: "Thank you.. Your order processed succesfully. You will be notified once we receive the payment from PayPal"
10 changes: 10 additions & 0 deletions config/locales/pt-BR.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
pt-BR:
please_select_your_country: "Por favor, escolha seu país"
please_enter_valid_zip: "Por favor, coloque seu código postal."
zip_code_if_you_have_one: "Código postal"
payment_failure: "Falha no Pagamento"
payment_pending: "Pagamento Pendente"
check_out_with_pag_seguro: "Compre com o PagSeguro"
pag_seguro_payment_received: "Obrigado. Nós recebemos seu pagamento para a órdem de compra. Sua compra será entregue em breve."
pag_seguro_order_processed_successfully: "Obrigado. Sua compra foi processada com sucesso. Você será notificado assim que recebermos o comprovante de pagamento do PagSeguro"
7 changes: 5 additions & 2 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
Rails.application.routes.draw do
end
Spree::Core::Engine.routes.prepend do
match "pag_seguro/notify", :to => "pag_seguro#notify", method: :post
match "pag_seguro/callback", :to => "pag_seguro#callback", method: :get
resources :payment_notifications, :only => [:create]
end
Loading

0 comments on commit 0d55bde

Please sign in to comment.