payable 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +20 -0
- data/README.rdoc +3 -0
- data/Rakefile +40 -0
- data/app/assets/javascripts/payable/application.js +15 -0
- data/app/assets/stylesheets/payable/application.css +13 -0
- data/app/controllers/payable/application_controller.rb +4 -0
- data/app/controllers/payable/welcome_controller.rb +25 -0
- data/app/helpers/payable/application_helper.rb +4 -0
- data/app/models/payable/gateway_payment.rb +115 -0
- data/app/views/layouts/payable/application.html.erb +14 -0
- data/app/views/payable/welcome/index.html.erb +10 -0
- data/app/views/payable/welcome/json_form.html.erb +44 -0
- data/config/routes.rb +7 -0
- data/db/migrate/20120607044142_create_payable_gateway_payments.rb +20 -0
- data/lib/model_extensions.rb +97 -0
- data/lib/payable/engine.rb +5 -0
- data/lib/payable/version.rb +3 -0
- data/lib/payable.rb +6 -0
- data/lib/tasks/payable_tasks.rake +4 -0
- data/test/dummy/README.rdoc +261 -0
- data/test/dummy/Rakefile +7 -0
- data/test/dummy/app/assets/javascripts/application.js +15 -0
- data/test/dummy/app/assets/javascripts/welcome.js +2 -0
- data/test/dummy/app/assets/stylesheets/application.css +13 -0
- data/test/dummy/app/assets/stylesheets/welcome.css +4 -0
- data/test/dummy/app/controllers/application_controller.rb +3 -0
- data/test/dummy/app/helpers/application_helper.rb +2 -0
- data/test/dummy/app/helpers/welcome_helper.rb +2 -0
- data/test/dummy/app/models/order.rb +16 -0
- data/test/dummy/app/views/layouts/application.html.erb +18 -0
- data/test/dummy/config/application.rb +59 -0
- data/test/dummy/config/boot.rb +10 -0
- data/test/dummy/config/database.yml +25 -0
- data/test/dummy/config/environment.rb +5 -0
- data/test/dummy/config/environments/development.rb +37 -0
- data/test/dummy/config/environments/production.rb +67 -0
- data/test/dummy/config/environments/test.rb +37 -0
- data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/test/dummy/config/initializers/inflections.rb +15 -0
- data/test/dummy/config/initializers/mime_types.rb +5 -0
- data/test/dummy/config/initializers/secret_token.rb +7 -0
- data/test/dummy/config/initializers/session_store.rb +8 -0
- data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/test/dummy/config/locales/en.yml +5 -0
- data/test/dummy/config/routes.rb +4 -0
- data/test/dummy/config.ru +4 -0
- data/test/dummy/db/development.sqlite3 +0 -0
- data/test/dummy/db/migrate/20120608040630_create_orders.rb +10 -0
- data/test/dummy/db/schema.rb +33 -0
- data/test/dummy/log/development.log +3476 -0
- data/test/dummy/public/404.html +26 -0
- data/test/dummy/public/422.html +26 -0
- data/test/dummy/public/500.html +25 -0
- data/test/dummy/public/favicon.ico +0 -0
- data/test/dummy/script/rails +6 -0
- data/test/dummy/test/fixtures/orders.yml +7 -0
- data/test/dummy/test/functional/welcome_controller_test.rb +9 -0
- data/test/dummy/test/unit/helpers/welcome_helper_test.rb +4 -0
- data/test/dummy/test/unit/order_test.rb +7 -0
- data/test/dummy/teste.rb +11 -0
- data/test/dummy/tmp/cache/assets/C7B/190/sprockets%2F37b103f4623089af1456b90830fe941c +0 -0
- data/test/dummy/tmp/cache/assets/CD5/140/sprockets%2Fa21a126d87fb83916bd0862280727aca +0 -0
- data/test/dummy/tmp/cache/assets/CD8/370/sprockets%2F357970feca3ac29060c1e3861e2c0953 +0 -0
- data/test/dummy/tmp/cache/assets/D02/7C0/sprockets%2F5a91dd113f57ac4a330b9800376ed1e6 +0 -0
- data/test/dummy/tmp/cache/assets/D0D/4C0/sprockets%2F3e934303399d3f2cc7a8cf4c450ad254 +0 -0
- data/test/dummy/tmp/cache/assets/D13/C60/sprockets%2F2dedb8177c20286c4259c1d58c5646cc +0 -0
- data/test/dummy/tmp/cache/assets/D21/5D0/sprockets%2Fe2c4f946939f2d7d0b42d86383755cae +0 -0
- data/test/dummy/tmp/cache/assets/D32/A10/sprockets%2F13fe41fee1fe35b49d145bcc06610705 +0 -0
- data/test/dummy/tmp/cache/assets/D35/E90/sprockets%2F7447bd893b001d61304c5ac3fe2bb88d +0 -0
- data/test/dummy/tmp/cache/assets/D3E/F40/sprockets%2F25a167c7563d6fe8ec6b13ec1ac09274 +0 -0
- data/test/dummy/tmp/cache/assets/D4E/1B0/sprockets%2Ff7cbd26ba1d28d48de824f0e94586655 +0 -0
- data/test/dummy/tmp/cache/assets/D5A/EA0/sprockets%2Fd771ace226fc8215a3572e0aa35bb0d6 +0 -0
- data/test/dummy/tmp/cache/assets/D9B/F30/sprockets%2F661d4c52d1a8ccb7b4788bdc8a42ad63 +0 -0
- data/test/dummy/tmp/cache/assets/DAF/2C0/sprockets%2Fcab45e548fe8052c62e467fde4dfe368 +0 -0
- data/test/dummy/tmp/cache/assets/DBB/8C0/sprockets%2F32852d4d3d7bffa878dba0ac1e311b1b +0 -0
- data/test/dummy/tmp/cache/assets/DDC/400/sprockets%2Fcffd775d018f68ce5dba1ee0d951a994 +0 -0
- data/test/dummy/tmp/cache/assets/E01/970/sprockets%2F299518aedad3aebe0edf5660d5ee57c2 +0 -0
- data/test/dummy/tmp/cache/assets/E03/DD0/sprockets%2Fc0b56c43d65acbf25696aced895cde6a +0 -0
- data/test/dummy/tmp/cache/assets/E04/890/sprockets%2F2f5173deea6c795b8fdde723bb4b63af +0 -0
- data/test/dummy/tmp/cache/assets/E54/0A0/sprockets%2F0c12aa3768b3dcccdeec7f29ed0b15ff +0 -0
- data/test/fixtures/payable/gateway_payments.yml +11 -0
- data/test/integration/navigation_test.rb +10 -0
- data/test/payable_test.rb +7 -0
- data/test/test_helper.rb +15 -0
- data/test/unit/payable/gateway_payment_test.rb +9 -0
- metadata +227 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2012 YOURNAME
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
begin
|
3
|
+
require 'bundler/setup'
|
4
|
+
rescue LoadError
|
5
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
6
|
+
end
|
7
|
+
begin
|
8
|
+
require 'rdoc/task'
|
9
|
+
rescue LoadError
|
10
|
+
require 'rdoc/rdoc'
|
11
|
+
require 'rake/rdoctask'
|
12
|
+
RDoc::Task = Rake::RDocTask
|
13
|
+
end
|
14
|
+
|
15
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
16
|
+
rdoc.rdoc_dir = 'rdoc'
|
17
|
+
rdoc.title = 'Payable'
|
18
|
+
rdoc.options << '--line-numbers'
|
19
|
+
rdoc.rdoc_files.include('README.rdoc')
|
20
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
21
|
+
end
|
22
|
+
|
23
|
+
APP_RAKEFILE = File.expand_path("../test/dummy/Rakefile", __FILE__)
|
24
|
+
load 'rails/tasks/engine.rake'
|
25
|
+
|
26
|
+
|
27
|
+
|
28
|
+
Bundler::GemHelper.install_tasks
|
29
|
+
|
30
|
+
require 'rake/testtask'
|
31
|
+
|
32
|
+
Rake::TestTask.new(:test) do |t|
|
33
|
+
t.libs << 'lib'
|
34
|
+
t.libs << 'test'
|
35
|
+
t.pattern = 'test/**/*_test.rb'
|
36
|
+
t.verbose = false
|
37
|
+
end
|
38
|
+
|
39
|
+
|
40
|
+
task :default => :test
|
@@ -0,0 +1,15 @@
|
|
1
|
+
// This is a manifest file that'll be compiled into application.js, which will include all the files
|
2
|
+
// listed below.
|
3
|
+
//
|
4
|
+
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
|
5
|
+
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
|
6
|
+
//
|
7
|
+
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
|
8
|
+
// the compiled file.
|
9
|
+
//
|
10
|
+
// WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
|
11
|
+
// GO AFTER THE REQUIRES BELOW.
|
12
|
+
//
|
13
|
+
//= require jquery
|
14
|
+
//= require jquery_ujs
|
15
|
+
//= require_tree .
|
@@ -0,0 +1,13 @@
|
|
1
|
+
/*
|
2
|
+
* This is a manifest file that'll be compiled into application.css, which will include all the files
|
3
|
+
* listed below.
|
4
|
+
*
|
5
|
+
* Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
|
6
|
+
* or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
|
7
|
+
*
|
8
|
+
* You're free to add application-wide styles to this file and they'll appear at the top of the
|
9
|
+
* compiled file, but it's generally better to create a new file per style scope.
|
10
|
+
*
|
11
|
+
*= require_self
|
12
|
+
*= require_tree .
|
13
|
+
*/
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Payable
|
2
|
+
class WelcomeController < ApplicationController
|
3
|
+
def index
|
4
|
+
Order.create(:value => rand(100) * 1.234, :bill_status => 'open') if Order.open.size.zero?
|
5
|
+
@orders = Order.order('id DESC').all
|
6
|
+
end
|
7
|
+
|
8
|
+
def checkout
|
9
|
+
@order = Order.find(params[:id])
|
10
|
+
|
11
|
+
begin
|
12
|
+
@order.checkout_bill
|
13
|
+
redirect_to :controller => "welcome", :action => "json_form", :id => @order.id
|
14
|
+
rescue
|
15
|
+
flash[:error] = $!.message
|
16
|
+
redirect_to root_url
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def json_form
|
21
|
+
@order = Order.find(params[:id])
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,115 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
module Payable
|
3
|
+
class GatewayPayment < ActiveRecord::Base
|
4
|
+
attr_accessible :payable_type, :payable_id, :config
|
5
|
+
serialize :config
|
6
|
+
|
7
|
+
state_machine :status, :initial => :open do
|
8
|
+
state :open
|
9
|
+
state :waiting
|
10
|
+
state :payed
|
11
|
+
state :canceled
|
12
|
+
|
13
|
+
after_transition :on => :pay do |gate, transition|
|
14
|
+
return unless gate.payable.present?
|
15
|
+
gate.payable.pay_bill
|
16
|
+
gate.payable.try(:payable_callback)
|
17
|
+
end
|
18
|
+
|
19
|
+
after_transition :on => :cancel do |gate, transition|
|
20
|
+
gateway_payment = gate.payable.try(:verify_gateway_payment)
|
21
|
+
gateway_payment.save if gateway_payment.present?
|
22
|
+
end
|
23
|
+
|
24
|
+
before_transition :on => :checkout do |gate, transition|
|
25
|
+
gate.payable_checkout
|
26
|
+
|
27
|
+
if gate.config[:response]["Status"] == "Sucesso"
|
28
|
+
gate.config.delete(:response).each { |k,v| gate.config[k.to_s.downcase] = v }
|
29
|
+
gate.config.symbolize_keys!
|
30
|
+
else
|
31
|
+
#TODO: Como tratar esta exception handling
|
32
|
+
raise "Erro no envio da requisição: #{gate.config[:response]['Erro']['__content__']}"
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
after_transition :on => :checkout do |gate, transition|
|
37
|
+
gate.payable.try(:checkout_bill)
|
38
|
+
gate.update_attribute :expires_at, 15.minutes.from_now
|
39
|
+
end
|
40
|
+
|
41
|
+
event :cancel do
|
42
|
+
transition :open => :canceled
|
43
|
+
end
|
44
|
+
|
45
|
+
event :checkout do
|
46
|
+
transition :open => :waiting
|
47
|
+
end
|
48
|
+
|
49
|
+
event :pay do
|
50
|
+
transition [:waiting, :open] => :payed
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
scope :active, :conditions => "status <> 'canceled'", :limit => 1
|
55
|
+
|
56
|
+
belongs_to :payable, :polymorphic => true
|
57
|
+
|
58
|
+
before_validation :ensure_config_is_a_hash
|
59
|
+
def ensure_config_is_a_hash
|
60
|
+
self.config = {} unless self.config.is_a?(Hash)
|
61
|
+
end
|
62
|
+
|
63
|
+
def payable_checkout
|
64
|
+
builder = Nokogiri::XML::Builder.new(:encoding => "UTF-8") do |xml|
|
65
|
+
# Identificador do tipo de instrução
|
66
|
+
xml.EnviarInstrucao {
|
67
|
+
xml.InstrucaoUnica(:TipoValidacao => "Transparente") {
|
68
|
+
xml.Razao{xml.text "Pagamento #{payable.payable_name} ##{self.id}"}
|
69
|
+
xml.Valores{
|
70
|
+
xml.Valor(:moeda => "BRL"){ xml.text payable.payable_value.to_f }
|
71
|
+
}
|
72
|
+
xml.IdProprio{ xml.text "#{('aa'..'zz').to_a.sample}-#{self.id}-#{rand(1000)}" }
|
73
|
+
|
74
|
+
#dados do pagador
|
75
|
+
xml.Pagador{
|
76
|
+
xml.IdPagador { xml.text payable.payable_user.id }
|
77
|
+
xml.Nome { xml.text payable.payable_user.name }
|
78
|
+
xml.Email { xml.text payable.payable_user.email }
|
79
|
+
xml.EnderecoCobranca {
|
80
|
+
addr = payable.payable_user.billing_address
|
81
|
+
xml.Logradouro { xml.text addr.address}
|
82
|
+
xml.Numero { xml.text addr.number}
|
83
|
+
xml.Complemento { xml.text addr.complement}
|
84
|
+
xml.Bairro { xml.text addr.district}
|
85
|
+
xml.Cidade { xml.text addr.city_name}
|
86
|
+
xml.Estado { xml.text addr.state_name}
|
87
|
+
xml.Pais { xml.text addr.country_name}
|
88
|
+
xml.CEP { xml.text addr.zip}
|
89
|
+
xml.TelefoneFixo{ xml.text addr.phone}
|
90
|
+
}
|
91
|
+
}
|
92
|
+
|
93
|
+
#dados do parcelamento
|
94
|
+
xml.Parcelamentos{
|
95
|
+
xml.Parcelamento{
|
96
|
+
xml.MinimoParcelas{ xml.text 2}
|
97
|
+
xml.MaximoParcelas{ xml.text 99}
|
98
|
+
xml.Repassar{ xml.text 'true' }
|
99
|
+
}
|
100
|
+
}
|
101
|
+
|
102
|
+
#dados de comissões
|
103
|
+
}
|
104
|
+
}
|
105
|
+
end
|
106
|
+
|
107
|
+
response = HTTParty.post('https://desenvolvedor.moip.com.br/sandbox/ws/alpha/EnviarInstrucao/Unica',
|
108
|
+
:body => builder.to_xml,
|
109
|
+
:basic_auth => {:username => 'E6T1EBOPOMQ62CXJI4PGKPETAPROIEJD',
|
110
|
+
:password => 'FELWDIWMPL7QPFABGZHH0OFF24BGL2ZGDSDYCS3A'}).parsed_response
|
111
|
+
self.config ||= {}
|
112
|
+
self.config[:response] = response["EnviarInstrucaoUnicaResponse"]["Resposta"]
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
<!DOCTYPE html>
|
2
|
+
<html>
|
3
|
+
<head>
|
4
|
+
<title>Payable</title>
|
5
|
+
<%= stylesheet_link_tag "payable/application", :media => "all" %>
|
6
|
+
<%= javascript_include_tag "payable/application" %>
|
7
|
+
<%= csrf_meta_tags %>
|
8
|
+
</head>
|
9
|
+
<body>
|
10
|
+
|
11
|
+
<%= yield %>
|
12
|
+
|
13
|
+
</body>
|
14
|
+
</html>
|
@@ -0,0 +1,10 @@
|
|
1
|
+
<h1>Welcome</h1>
|
2
|
+
<% @orders.each do |order| %>
|
3
|
+
<p>
|
4
|
+
<% if order.bill_canceled? || order.bill_payed? %>
|
5
|
+
<%= "Pedido ##{order.id}, R$ #{order.value} - #{order.bill_status}" %>
|
6
|
+
<% else %>
|
7
|
+
<%= button_to("Pagar Pedido ##{order.id}, R$ #{order.value} - #{order.bill_status}", :action => :checkout, :id => order.id) %>
|
8
|
+
<% end %>
|
9
|
+
</p>
|
10
|
+
<% end %>
|
@@ -0,0 +1,44 @@
|
|
1
|
+
|
2
|
+
<% @order.gateway_payment.config.each do |k, v| %>
|
3
|
+
<p>
|
4
|
+
<%= k %>: <%= v %>
|
5
|
+
</p>
|
6
|
+
<% end %>
|
7
|
+
|
8
|
+
|
9
|
+
<fieldset>
|
10
|
+
<legend>Campos Crédito</legend>
|
11
|
+
|
12
|
+
<p class="operadora">
|
13
|
+
<span><label for="operadora_AmericanExpress"><%= radio_button_tag 'operadora', 'AmericanExpress' %> AmericanExpress</label></span>
|
14
|
+
<span><label for="operadora_Diners"><%= radio_button_tag 'operadora', 'Diners' %> Diners</label></span>
|
15
|
+
<span><label for="operadora_Hipercard"><%= radio_button_tag 'operadora', 'Hipercard' %> Hipercard</label></span>
|
16
|
+
<span><label for="operadora_Mastercard"><%= radio_button_tag 'operadora', 'Mastercard' %> Mastercard</label></span>
|
17
|
+
<span><label for="operadora_Visa"><%= radio_button_tag 'operadora', 'Visa' %> Visa</label></span>
|
18
|
+
</p>
|
19
|
+
|
20
|
+
<p>
|
21
|
+
<label for="credit_number">Número</label>: <%= text_field_tag "credit_number" %>
|
22
|
+
</p>
|
23
|
+
|
24
|
+
<p>
|
25
|
+
<label for="expires_date">Validade</label>: <%= text_field_tag "expires_date" %> MM/AA
|
26
|
+
</p>
|
27
|
+
|
28
|
+
<p>
|
29
|
+
<label for="security_code">Cód. Segurança</label>: <%= text_field_tag "security_code" %>
|
30
|
+
</p>
|
31
|
+
|
32
|
+
<%= button_to_function "Pagar", "alert('pagando')" %>
|
33
|
+
</fieldset>
|
34
|
+
|
35
|
+
|
36
|
+
|
37
|
+
|
38
|
+
<%= content_tag(:div, nil, 'id' => 'MoipWidget',
|
39
|
+
'data-token' => @order.payable_token,
|
40
|
+
'callback-method-success' => 'func1',
|
41
|
+
'callback-method-error' => 'func2' ) %>
|
42
|
+
|
43
|
+
|
44
|
+
<%= link_to "Voltar", root_url %>
|
data/config/routes.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
class CreatePayableGatewayPayments < ActiveRecord::Migration
|
2
|
+
def up
|
3
|
+
create_table :payable_gateway_payments do |t|
|
4
|
+
t.string :payable_type
|
5
|
+
t.integer :payable_id
|
6
|
+
t.datetime :expires_at
|
7
|
+
t.string :status
|
8
|
+
t.string :config
|
9
|
+
t.timestamps
|
10
|
+
end
|
11
|
+
|
12
|
+
begin
|
13
|
+
execute "CREATE UNIQUE INDEX un_index_payable_gateway_payments ON payable_gateway_payments USING btree (payable_id, payable_type) WHERE status <> 'canceled';"
|
14
|
+
rescue; end
|
15
|
+
end
|
16
|
+
|
17
|
+
def down
|
18
|
+
drop_table :payable_gateway_payments
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,97 @@
|
|
1
|
+
module Payable
|
2
|
+
module ModelExtensions
|
3
|
+
|
4
|
+
# Eager loading (applies to all models)
|
5
|
+
def self.included(base)
|
6
|
+
base.extend ClassMethods
|
7
|
+
base.send(:include, InstanceMethods)
|
8
|
+
end
|
9
|
+
|
10
|
+
module ClassMethods
|
11
|
+
def payable?; false end
|
12
|
+
|
13
|
+
def payable(*args)
|
14
|
+
|
15
|
+
class_eval "def self.payable?; true end"
|
16
|
+
|
17
|
+
config = {:name => nil,
|
18
|
+
:user => :user,
|
19
|
+
:value => :value,
|
20
|
+
:callback => nil,
|
21
|
+
:bill_status => :bill_status}
|
22
|
+
config.update(args.extract_options!)
|
23
|
+
|
24
|
+
has_many :gateway_payments, :as => :payable, :class_name => "Payable::GatewayPayment"
|
25
|
+
has_one :gateway_payment, :as => :payable, :class_name => "Payable::GatewayPayment", :conditions => "status <> 'canceled'"
|
26
|
+
|
27
|
+
state_machine config[:bill_status], :namespace => "bill", :initial => :open do
|
28
|
+
state :open
|
29
|
+
state :waiting
|
30
|
+
state :payed
|
31
|
+
state :canceled
|
32
|
+
|
33
|
+
after_transition :on => :cancel do |payable, transition|
|
34
|
+
payable.gateway_payment.cancel
|
35
|
+
end
|
36
|
+
|
37
|
+
after_transition :on => :checkout do |payable, transition|
|
38
|
+
payable.gateway_payment.checkout
|
39
|
+
end
|
40
|
+
|
41
|
+
after_transition :on => :pay do |payable, transition|
|
42
|
+
payable.gateway_payment.pay
|
43
|
+
end
|
44
|
+
|
45
|
+
event :cancel do
|
46
|
+
transition :open => :canceled
|
47
|
+
end
|
48
|
+
|
49
|
+
event :checkout do
|
50
|
+
transition :open => :waiting
|
51
|
+
end
|
52
|
+
|
53
|
+
event :pay do
|
54
|
+
transition [:waiting, :open] => :payed
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
before_save :verify_gateway_payment
|
59
|
+
define_method :verify_gateway_payment do
|
60
|
+
self.gateway_payments.build if !self.bill_canceled? && (self.gateway_payments.size.zero? || self.gateway_payments.all?(&:canceled?))
|
61
|
+
end
|
62
|
+
|
63
|
+
define_method :payable_value do
|
64
|
+
get_config_param(config[:value])
|
65
|
+
end
|
66
|
+
|
67
|
+
define_method :payable_user do
|
68
|
+
get_config_param(config[:user])
|
69
|
+
end
|
70
|
+
|
71
|
+
define_method :payable_name do
|
72
|
+
get_config_param(config[:user]) || self.class.name
|
73
|
+
end
|
74
|
+
|
75
|
+
define_method :payable_callback do
|
76
|
+
get_config_param(config[:callback])
|
77
|
+
end
|
78
|
+
|
79
|
+
define_method :payable_token do
|
80
|
+
return unless self.gateway_payment.present?
|
81
|
+
|
82
|
+
self.gateway_payment.config ||= {}
|
83
|
+
self.gateway_payment.config[:token]
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
module InstanceMethods
|
89
|
+
def get_config_param(proc); return if proc.blank?; proc.is_a?(Proc) ? (proc.arity.zero? ? proc.call : proc.call(self)) : self.send(proc) end
|
90
|
+
def payable?; self.class.payable? end
|
91
|
+
end
|
92
|
+
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
ActiveRecord::Base.send(:include, Payable::ModelExtensions)
|
97
|
+
|
data/lib/payable.rb
ADDED