Skip to content

Commit

Permalink
Created checkout using Pagseguro lightbox.
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigoulisses committed Jun 28, 2020
1 parent 99ee5aa commit e0fb171
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 19 deletions.
20 changes: 20 additions & 0 deletions app/assets/javascripts/spree/frontend/pagseguro.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//Insira o código de checkout gerado no Passo 1
var pagsegurCodeSelector = document.querySelector('div[data-pagseguro-code]')
if (pagsegurCodeSelector) {
var code = pagsegurCodeSelector.dataset.pagseguroCode;
var callback = {
success : function(transactionCode) {
var checkoutPaymentForm = document.querySelector('form.checkout_form_payment');
var submit = checkout_form_payment.querySelector("input[type='submit']")
$(submit).trigger('click');
},
abort : function() {
console.log("abortado");
}
};
var isOpenLightbox = PagSeguroLightbox(code, callback);
if (!isOpenLightbox){
location.href="https://pagseguro.uol.com.br/v2/checkout/payment.html?code=" + code;
console.log("Redirecionamento")
}
}
14 changes: 14 additions & 0 deletions app/controllers/spree/checkout_controller_decorator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module Spree::CheckoutControllerDecorator
def self.prepended(base)
base.before_action :create_pag_seguro_transaction, only: :edit
end

def create_pag_seguro_transaction
return unless params[:state] == "payment"
payment = @order.payments.build(payment_method: Spree::PaymentMethod.where(type: 'Spree::PaymentMethod::Pagseguro').last)
SpreePagseguroSimple::Gateway.new(payment)
@pag_seguro_payment = Spree::PagSeguroTransaction.find_by_order_id @order.number
end
end

::Spree::CheckoutController.prepend Spree::CheckoutControllerDecorator
27 changes: 19 additions & 8 deletions app/controllers/spree/pagseguro_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,31 @@ def notify
payment_method_id: payment_method.id,
amount: notification.gross_amount).last

if payment
if notification.approved?
payment.complete!
end
pag_seguro_transaction = Spree::PagSeguroTransaction.find_by_order_id @order.number

if notification.cancelled? || notification.returned?
payment.void!
if payment
ActiveRecord::Base.transaction do
if notification.approved?
payment.complete!

if pag_seguro_transaction.present?
pag_seguro_transaction.update!(state: 'approved')
end
end

if notification.cancelled? || notification.returned?
payment.void!

if pag_seguro_transaction.present?
pag_seguro_transaction.update!(state: 'cancelled')
end
end
end
else
raise StandardError
end

render nothing: true, head: :ok
render body: :ok
end

end
end

This file was deleted.

7 changes: 6 additions & 1 deletion app/views/spree/checkout/payment/_pag_seguro.html.erb
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
<p>Finalize seu pedido para efetuar o pagamento no PagSeguro</p>
<p>Aguarde, enquanto carregamos a página para que realize a o pagamento.</p>
<%= content_for :head do %>
<script type="text/javascript" src="https://stc.sandbox.pagseguro.uol.com.br/pagseguro/api/v2/checkout/pagseguro.lightbox.js"></script>
<% end %>

<div data-pagseguro-code="<%= @pag_seguro_payment.code %>">
5 changes: 5 additions & 0 deletions db/migrate/20200318233804_add_code_to_spree_payments.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddCodeToSpreePayments < ActiveRecord::Migration[6.0]
def change
add_column :spree_payments, :code, :string
end
end
5 changes: 5 additions & 0 deletions db/migrate/20200410203710_remove_code_to_spree_payments.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class RemoveCodeToSpreePayments < ActiveRecord::Migration[6.0]
def change
remove_column :spree_payments, :code
end
end
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
module SpreePagseguroSimple
module Generators
class InstallGenerator < Rails::Generators::Base
source_root File.expand_path("../../../templates", __FILE__)

class_option :auto_run_migrations, :type => :boolean, :default => false

def add_javascripts
append_file 'vendor/assets/javascripts/spree/frontend/all.js', "//= require spree/frontend/pagseguro\n"
end

def copy_intializer
template "pagseguro.rb", "config/initializers/pagseguro.rb"
end

def add_migrations
run 'bundle exec rake railties:install:migrations FROM=spree_pagseguro_simple'
end
Expand Down
1 change: 1 addition & 0 deletions lib/generators/templates/pagseguro.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PagSeguro::Url.environment = ENV['PAGSEGURO_ENV'].to_sym
17 changes: 9 additions & 8 deletions lib/spree_pagseguro_simple/gateway.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,31 @@ def initialize(payment)
process
end

def code
@pag_seguro_transaction.code
end

def is_not_completed?
!@payment.completed? && transaction_exists_and_is_loaded && @pag_seguro_transaction.state == 'pending'
end

def payment_url
subdomain = @env == :sandbox ? 'sandbox.pagseguro' : 'pagseguro'
"https://#{subdomain}.uol.com.br/v2/checkout/payment.html?code=#{@pag_seguro_transaction.code}"
end

private
def process
set_environment

def subdomain
@subdomain ||= (ENV['PAGSEGURO_ENV'] == 'sandbox') ? 'sandbox.pagseguro' : 'pagseguro'
end

def process
unless transaction_exists_and_is_loaded
build_pagseguro
create_transaction
end
end

def set_environment
@env = ENV['PAGSEGURO_ENV'] ? :production : :sandbox
PagSeguro::Url.environment = @env
end

def transaction_exists_and_is_loaded
@pag_seguro_transaction ||= Spree::PagSeguroTransaction.find_by!(order_id: @order.number)
rescue
Expand Down

0 comments on commit e0fb171

Please sign in to comment.